Skip to content

Commit

Permalink
Merge pull request #5524 from onflow/ramtin/5512-update-to-evm-fork
Browse files Browse the repository at this point in the history
[Flow EVM] update EVM version to the one supporting configurable precompiles
  • Loading branch information
ramtinms authored Mar 21, 2024
2 parents 022f21f + 27a7771 commit 7af17df
Show file tree
Hide file tree
Showing 49 changed files with 146 additions and 166 deletions.
22 changes: 13 additions & 9 deletions fvm/evm/emulator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"math"
"math/big"

gethCommon "github.com/ethereum/go-ethereum/common"
gethCore "github.com/ethereum/go-ethereum/core"
gethVM "github.com/ethereum/go-ethereum/core/vm"
gethParams "github.com/ethereum/go-ethereum/params"
gethCommon "github.com/onflow/go-ethereum/common"
gethCore "github.com/onflow/go-ethereum/core"
gethVM "github.com/onflow/go-ethereum/core/vm"
gethParams "github.com/onflow/go-ethereum/params"

"github.com/onflow/flow-go/fvm/evm/types"
)
Expand All @@ -31,8 +31,6 @@ type Config struct {
TxContext *gethVM.TxContext
// base unit of gas for direct calls
DirectCallBaseGasUsage uint64
// a set of extra precompiles to be injected
ExtraPrecompiles map[gethCommon.Address]gethVM.PrecompiledContract
}

func (c *Config) ChainRules() gethParams.Rules {
Expand Down Expand Up @@ -96,6 +94,7 @@ func defaultConfig() *Config {
GetHash: func(n uint64) gethCommon.Hash {
return gethCommon.Hash{}
},
GetPrecompile: gethCore.GetPrecompile,
},
}
}
Expand Down Expand Up @@ -186,11 +185,16 @@ func WithDirectCallBaseGasUsage(gas uint64) Option {
// WithExtraPrecompiles appends precompile list with extra precompiles
func WithExtraPrecompiles(precompiles []types.Precompile) Option {
return func(c *Config) *Config {
extraPreCompMap := make(map[gethCommon.Address]gethVM.PrecompiledContract)
for _, pc := range precompiles {
if c.ExtraPrecompiles == nil {
c.ExtraPrecompiles = make(map[gethCommon.Address]gethVM.PrecompiledContract)
extraPreCompMap[pc.Address().ToCommon()] = pc
}
c.BlockContext.GetPrecompile = func(rules gethParams.Rules, addr gethCommon.Address) (gethVM.PrecompiledContract, bool) {
prec, found := extraPreCompMap[addr]
if found {
return prec, true
}
c.ExtraPrecompiles[pc.Address().ToCommon()] = pc
return gethCore.GetPrecompile(rules, addr)
}
return c
}
Expand Down
47 changes: 7 additions & 40 deletions fvm/evm/emulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ package emulator

import (
"math/big"
"sync"

gethCommon "github.com/ethereum/go-ethereum/common"
gethCore "github.com/ethereum/go-ethereum/core"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethVM "github.com/ethereum/go-ethereum/core/vm"
gethCrypto "github.com/ethereum/go-ethereum/crypto"
gethParams "github.com/ethereum/go-ethereum/params"

"github.com/onflow/atree"
gethCommon "github.com/onflow/go-ethereum/common"
gethCore "github.com/onflow/go-ethereum/core"
gethTypes "github.com/onflow/go-ethereum/core/types"
gethVM "github.com/onflow/go-ethereum/core/vm"
gethCrypto "github.com/onflow/go-ethereum/crypto"
gethParams "github.com/onflow/go-ethereum/params"

"github.com/onflow/flow-go/fvm/evm/emulator/state"
"github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/flow-go/model/flow"
)

var pcUpdater = &precompileUpdater{}

// Emulator handles operations against evm runtime
type Emulator struct {
rootAddr flow.Address
Expand Down Expand Up @@ -61,7 +58,6 @@ func (em *Emulator) NewReadOnlyBlockView(ctx types.BlockContext) (types.ReadOnly
// NewBlockView constructs a new block view (mutable)
func (em *Emulator) NewBlockView(ctx types.BlockContext) (types.BlockView, error) {
cfg := newConfig(ctx)
pcUpdater.SetupPrecompile(cfg)
return &BlockView{
config: cfg,
rootAddr: em.rootAddr,
Expand Down Expand Up @@ -437,32 +433,3 @@ func (proc *procedure) run(
}
return &res, nil
}

type precompileUpdater struct {
updateLock sync.Mutex
}

func (pu *precompileUpdater) SetupPrecompile(cfg *Config) {
pu.updateLock.Lock()
defer pu.updateLock.Unlock()

rules := cfg.ChainRules()
// captures the pointer to the map that has to be augmented
var precompiles map[gethCommon.Address]gethVM.PrecompiledContract
switch {
case rules.IsCancun:
precompiles = gethVM.PrecompiledContractsCancun
case rules.IsBerlin:
precompiles = gethVM.PrecompiledContractsBerlin
case rules.IsIstanbul:
precompiles = gethVM.PrecompiledContractsIstanbul
case rules.IsByzantium:
precompiles = gethVM.PrecompiledContractsByzantium
default:
precompiles = gethVM.PrecompiledContractsHomestead
}
for addr, contract := range cfg.ExtraPrecompiles {
// we override if exist since we call this method on every block
precompiles[addr] = contract
}
}
8 changes: 4 additions & 4 deletions fvm/evm/emulator/emulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"math/big"
"testing"

gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethVM "github.com/ethereum/go-ethereum/core/vm"
gethParams "github.com/ethereum/go-ethereum/params"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
gethVM "github.com/onflow/go-ethereum/core/vm"
gethParams "github.com/onflow/go-ethereum/params"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/emulator"
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/emulator/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package emulator
import (
"math/big"

"github.com/ethereum/go-ethereum/core/types"
"github.com/onflow/go-ethereum/core/types"
)

var defaultBlockNumberForEVMRules = big.NewInt(1) // anything bigger than 0
Expand Down
6 changes: 3 additions & 3 deletions fvm/evm/emulator/state/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package state
import (
"math/big"

gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
"github.com/onflow/go-ethereum/rlp"
)

// Account holds the metadata of an address and provides (de)serialization functionality
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/emulator/state/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package state_test
import (
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/onflow/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/emulator/state"
Expand Down
4 changes: 2 additions & 2 deletions fvm/evm/emulator/state/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"math/big"

gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/onflow/atree"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"

"github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/flow-go/model/flow"
Expand Down
6 changes: 3 additions & 3 deletions fvm/evm/emulator/state/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"math/big"
"testing"

gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethCrypto "github.com/ethereum/go-ethereum/crypto"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
gethCrypto "github.com/onflow/go-ethereum/crypto"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/emulator/state"
Expand Down
6 changes: 3 additions & 3 deletions fvm/evm/emulator/state/delta.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"math/big"

gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethCrypto "github.com/ethereum/go-ethereum/crypto"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
gethCrypto "github.com/onflow/go-ethereum/crypto"

"github.com/onflow/flow-go/fvm/evm/types"
)
Expand Down
6 changes: 3 additions & 3 deletions fvm/evm/emulator/state/delta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"math/big"
"testing"

gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethCrypto "github.com/ethereum/go-ethereum/crypto"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
gethCrypto "github.com/onflow/go-ethereum/crypto"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/emulator/state"
Expand Down
6 changes: 3 additions & 3 deletions fvm/evm/emulator/state/stateDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"math/big"
"sort"

gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethParams "github.com/ethereum/go-ethereum/params"
"github.com/onflow/atree"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
gethParams "github.com/onflow/go-ethereum/params"

"github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/flow-go/model/flow"
Expand Down
6 changes: 3 additions & 3 deletions fvm/evm/emulator/state/stateDB_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"math/big"
"testing"

gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethParams "github.com/ethereum/go-ethereum/params"
"github.com/onflow/atree"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
gethParams "github.com/onflow/go-ethereum/params"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/emulator/state"
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/emulator/state/state_growth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/onflow/flow-go/utils/io"

"github.com/ethereum/go-ethereum/common"
"github.com/onflow/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/emulator/state"
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/handler/addressAllocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package handler_test
import (
"testing"

gethCommon "github.com/ethereum/go-ethereum/common"
gethCommon "github.com/onflow/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/handler"
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/handler/blockstore.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package handler

import (
gethCommon "github.com/ethereum/go-ethereum/common"
gethCommon "github.com/onflow/go-ethereum/common"

"github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/flow-go/model/flow"
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/handler/blockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"math/big"
"testing"

gethCommon "github.com/ethereum/go-ethereum/common"
gethCommon "github.com/onflow/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/handler"
Expand Down
6 changes: 3 additions & 3 deletions fvm/evm/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"math/big"

gethCommon "github.com/ethereum/go-ethereum/common"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/onflow/cadence/runtime/common"
gethCommon "github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
"github.com/onflow/go-ethereum/rlp"

"github.com/onflow/flow-go/fvm/environment"
fvmErrors "github.com/onflow/flow-go/fvm/errors"
Expand Down
12 changes: 6 additions & 6 deletions fvm/evm/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"strings"
"testing"

gethCommon "github.com/ethereum/go-ethereum/common"
gethCore "github.com/ethereum/go-ethereum/core"
gethTypes "github.com/ethereum/go-ethereum/core/types"
gethVM "github.com/ethereum/go-ethereum/core/vm"
gethParams "github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rlp"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
gethCommon "github.com/onflow/go-ethereum/common"
gethCore "github.com/onflow/go-ethereum/core"
gethTypes "github.com/onflow/go-ethereum/core/types"
gethVM "github.com/onflow/go-ethereum/core/vm"
gethParams "github.com/onflow/go-ethereum/params"
"github.com/onflow/go-ethereum/rlp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/precompiles/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"math/big"

gethCommon "github.com/ethereum/go-ethereum/common"
gethCommon "github.com/onflow/go-ethereum/common"
)

// This package provides fast and efficient
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/precompiles/abi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"math/big"
"testing"

gethCommon "github.com/ethereum/go-ethereum/common"
gethCommon "github.com/onflow/go-ethereum/common"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/precompiles"
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/precompiles/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

gethCrypto "github.com/ethereum/go-ethereum/crypto"
gethCrypto "github.com/onflow/go-ethereum/crypto"
)

const FunctionSelectorLength = 4
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/precompiles/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package precompiles_test
import (
"testing"

gethCrypto "github.com/ethereum/go-ethereum/crypto"
gethCrypto "github.com/onflow/go-ethereum/crypto"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/fvm/evm/precompiles"
Expand Down
4 changes: 2 additions & 2 deletions fvm/evm/stdlib/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"regexp"
"strings"

gethABI "github.com/ethereum/go-ethereum/accounts/abi"
gethCommon "github.com/ethereum/go-ethereum/common"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/sema"
"github.com/onflow/cadence/runtime/stdlib"
gethABI "github.com/onflow/go-ethereum/accounts/abi"
gethCommon "github.com/onflow/go-ethereum/common"

"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/fvm/evm/types"
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/stdlib/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/binary"
"testing"

"github.com/ethereum/go-ethereum/crypto"
"github.com/onflow/cadence"
"github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime"
Expand All @@ -13,6 +12,7 @@ import (
cadenceStdlib "github.com/onflow/cadence/runtime/stdlib"
"github.com/onflow/cadence/runtime/tests/utils"
coreContracts "github.com/onflow/flow-core-contracts/lib/go/contracts"
"github.com/onflow/go-ethereum/crypto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down
Loading

0 comments on commit 7af17df

Please sign in to comment.