Skip to content
This repository has been archived by the owner on Nov 30, 2021. It is now read-only.

Commit

Permalink
evm: implement vm.GetHashFn
Browse files Browse the repository at this point in the history
  • Loading branch information
fedekunze committed Nov 25, 2020
1 parent 4eccac8 commit 6c31ea2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
19 changes: 19 additions & 0 deletions x/evm/types/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,30 @@ type ExecutionResult struct {
GasInfo GasInfo
}

// GetHashFn implements vm.GetHashFunc for Ethermint. It casts the current
// tendermint header hash to an ethereum Hash format. If the context block height
// doesn't match the requested height, it will return an empty hash.
func GetHashFn(ctx sdk.Context) vm.GetHashFunc {
return func(height uint64) common.Hash {
if ctx.BlockHeight() != int64(height) {
return common.Hash{}
}

// cast the ABCI header to tendermint Header type
tmHeader := AbciHeaderToTendermint(ctx.BlockHeader())

// get the Tendermint block hash from the current header
tmBlockHash := tmHeader.Hash()
return common.BytesToHash(tmBlockHash.Bytes())
}
}

func (st StateTransition) newEVM(ctx sdk.Context, csdb *CommitStateDB, gasLimit uint64, gasPrice *big.Int, config ChainConfig) *vm.EVM {
// Create context for evm
context := vm.Context{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
GetHash: GetHashFn(ctx),
Origin: st.Sender,
Coinbase: common.Address{}, // there's no benefitiary since we're not mining
BlockNumber: big.NewInt(ctx.BlockHeight()),
Expand Down
37 changes: 37 additions & 0 deletions x/evm/types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/pkg/errors"
"golang.org/x/crypto/sha3"

abci "github.com/tendermint/tendermint/abci/types"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/tendermint/tendermint/version"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand Down Expand Up @@ -152,3 +156,36 @@ func recoverEthSig(R, S, Vb *big.Int, sigHash ethcmn.Hash) (ethcmn.Address, erro

return addr, nil
}

// AbciHeaderToTendermint is a util function to parse a tendermint ABCI Header to
// tendermint types Header.
func AbciHeaderToTendermint(header abci.Header) tmtypes.Header {
return tmtypes.Header{
Version: version.Consensus{
Block: version.Protocol(header.Version.Block),
App: version.Protocol(header.Version.App),
},
ChainID: header.ChainID,
Height: header.Height,
Time: header.Time,

LastBlockID: tmtypes.BlockID{
Hash: header.LastBlockId.Hash,
PartsHeader: tmtypes.PartSetHeader{
Total: int(header.LastBlockId.PartsHeader.Total),
Hash: header.LastBlockId.PartsHeader.Hash,
},
},
LastCommitHash: header.LastCommitHash,
DataHash: header.DataHash,

ValidatorsHash: header.ValidatorsHash,
NextValidatorsHash: header.NextValidatorsHash,
ConsensusHash: header.ConsensusHash,
AppHash: header.AppHash,
LastResultsHash: header.LastResultsHash,

EvidenceHash: header.EvidenceHash,
ProposerAddress: header.ProposerAddress,
}
}

0 comments on commit 6c31ea2

Please sign in to comment.