Skip to content

Commit

Permalink
go-ethereum at d4bd9aed839e
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie committed Oct 9, 2023
1 parent 304f804 commit 65833f7
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 131 deletions.
20 changes: 14 additions & 6 deletions action/protocol/execution/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,18 @@ func getChainConfig(g genesis.Blockchain, height uint64, id uint32) *params.Chai
if g.IsIceland(height) {
chainConfig.ChainID = new(big.Int).SetUint64(uint64(id))
}
// enable Berlin and London
// enable Berlin and London at Okhotsk
chainConfig.BerlinBlock = new(big.Int).SetUint64(g.OkhotskBlockHeight)
chainConfig.LondonBlock = new(big.Int).SetUint64(g.OkhotskBlockHeight)
// enable ArrowGlacier, GrayGlacier, MergeNetsplit at Redsea
chainConfig.ArrowGlacierBlock = new(big.Int).SetUint64(g.RedseaBlockHeight)
chainConfig.GrayGlacierBlock = new(big.Int).SetUint64(g.RedseaBlockHeight)
chainConfig.MergeNetsplitBlock = new(big.Int).SetUint64(g.RedseaBlockHeight)
// Starting Shanghai, fork scheduling on Ethereum was switched from blocks to timestamps
// However we don't need the time-based switch as Ethereum, so continue to use block-based
// Hence, config.ShanghaiTime (and time of later forks) was set to the corresponding forking
// block height, so that the fork checking code IsShanghai() still works w/o modification
chainConfig.ShanghaiTime = new(big.Int).SetUint64(g.RedseaBlockHeight)

This comment has been minimized.

Copy link
@dustinxie

dustinxie Oct 9, 2023

Author Member

to run the test

cd action/protocol/execution
go test -run TestProtocol_Handle

you will see the panic failure

and if you comment out L407, the test will pass
so setting chainConfig.ShanghaiTime caused some diff behavior inside the go-ethereum

return &chainConfig
}

Expand Down Expand Up @@ -428,13 +437,12 @@ func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapte
remainingGas -= intriGas

// Set up the initial access list
if rules := chainConfig.Rules(evm.Context.BlockNumber, false); rules.IsBerlin {
stateDB.PrepareAccessList(evmParams.txCtx.Origin, evmParams.contract, vm.ActivePrecompiles(rules), evmParams.accessList)
}
rules := chainConfig.Rules(evm.Context.BlockNumber, false, new(big.Int).SetUint64(blockHeight))
stateDB.Prepare(rules, evmParams.txCtx.Origin, evmParams.context.Coinbase, evmParams.contract, vm.ActivePrecompiles(rules), evmParams.accessList)

var (
contractRawAddress = action.EmptyAddress
executor = vm.AccountRef(evmParams.txCtx.Origin)
london = evm.ChainConfig().IsLondon(evm.Context.BlockNumber)
ret []byte
evmErr error
refund uint64
Expand Down Expand Up @@ -466,7 +474,7 @@ func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapte
if stateDB.Error() != nil {
log.L().Debug("statedb error", zap.Error(stateDB.Error()))
}
if !london {
if !rules.IsLondon {
// Before EIP-3529: refunds were capped to gasUsed / 2
refund = (evmParams.gas - remainingGas) / params.RefundQuotient
} else {
Expand Down
17 changes: 15 additions & 2 deletions action/protocol/execution/evm/evm_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 IoTeX Foundation
// Copyright (c) 2023 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
Expand Down Expand Up @@ -219,11 +219,16 @@ func TestConstantinople(t *testing.T) {
"io1pcg2ja9krrhujpazswgz77ss46xgt88afqlk6y",
24838200,
},
// after Quebec
// Quebec - Redsea
{
action.EmptyAddress,
24838201,
},
{
"io1pcg2ja9krrhujpazswgz77ss46xgt88afqlk6y",
34838200,
},
// after Redsea
{
"io1pcg2ja9krrhujpazswgz77ss46xgt88afqlk6y",
math.MaxUint64,
Expand Down Expand Up @@ -304,6 +309,14 @@ func TestConstantinople(t *testing.T) {
require.Equal(isOkhotsk, chainRules.IsLondon)
require.False(chainRules.IsMerge)

// Redsea = enable ArrowGlacier, GrayGlacier, MergeNetsplit
isRedsea := g.IsRedsea(e.height)
require.Equal(big.NewInt(int64(g.RedseaBlockHeight)), evmChainConfig.ArrowGlacierBlock)
require.Equal(big.NewInt(int64(g.RedseaBlockHeight)), evmChainConfig.GrayGlacierBlock)
require.Equal(big.NewInt(int64(g.RedseaBlockHeight)), evmChainConfig.MergeNetsplitBlock)
require.Equal(isRedsea, evmChainConfig.IsArrowGlacier(evm.Context.BlockNumber))
require.Equal(isRedsea, evmChainConfig.IsGrayGlacier(evm.Context.BlockNumber))

// test basefee
require.Equal(new(big.Int), evm.Context.BaseFee)
}
Expand Down
62 changes: 45 additions & 17 deletions action/protocol/execution/evm/evmstatedbadapter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019 IoTeX Foundation
// Copyright (c) 2023 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
Expand All @@ -14,6 +14,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/pkg/errors"
"go.uber.org/zap"

Expand Down Expand Up @@ -57,6 +58,8 @@ type (
preimageSnapshot map[int]preimageMap
accessList *accessList // per-transaction access list
accessListSnapshot map[int]*accessList
transientStorage transientStorage // Transient storage
transientStorageSnapshot map[int]transientStorage
logsSnapshot map[int]int // logs is an array, save len(logs) at time of snapshot suffices
txLogsSnapshot map[int]int
notFixTopicCopyBug bool
Expand Down Expand Up @@ -446,6 +449,16 @@ func (stateDB *StateDBAdapter) HasSuicided(evmAddr common.Address) bool {
return ok
}

// SetTransientState sets transient storage for a given account
func (stateDB *StateDBAdapter) SetTransientState(addr common.Address, key, value common.Hash) {
stateDB.transientStorage.Set(addr, key, value)
}

// GetTransientState gets transient storage for a given account.
func (stateDB *StateDBAdapter) GetTransientState(addr common.Address, key common.Hash) common.Hash {
return stateDB.transientStorage.Get(addr, key)
}

// Exist checks the existence of an address
func (stateDB *StateDBAdapter) Exist(evmAddr common.Address) bool {
addr, err := address.FromBytes(evmAddr.Bytes())
Expand All @@ -466,30 +479,43 @@ func (stateDB *StateDBAdapter) Exist(evmAddr common.Address) bool {
return true
}

// PrepareAccessList handles the preparatory steps for executing a state transition with
// regards to both EIP-2929 and EIP-2930:
// Prepare handles the preparatory steps for executing a state transition with
// This method must be invoked before state transition
//
// Berlin fork:
// - Add sender to access list (2929)
// - Add destination to access list (2929)
// - Add precompiles to access list (2929)
// - Add the contents of the optional tx access list (2930)
//
// This method should only be called if Berlin/2929+2930 is applicable at the current number.
func (stateDB *StateDBAdapter) PrepareAccessList(sender common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) {
stateDB.AddAddressToAccessList(sender)
if dst != nil {
stateDB.AddAddressToAccessList(*dst)
// If it's a create-tx, the destination will be added inside evm.create
}
for _, addr := range precompiles {
stateDB.AddAddressToAccessList(addr)
}
for _, el := range list {
stateDB.AddAddressToAccessList(el.Address)
for _, key := range el.StorageKeys {
stateDB.AddSlotToAccessList(el.Address, key)
// Potential EIPs:
// - Reset access list (Berlin)
// - Add coinbase to access list (EIP-3651)
// - Reset transient storage(1153)
func (stateDB *StateDBAdapter) Prepare(rules params.Rules, sender, coinbase common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) {
if rules.IsBerlin {
al := stateDB.accessList
al.AddAddress(sender)
if dst != nil {
al.AddAddress(*dst)
// If it's a create-tx, the destination will be added inside evm.create
}
for _, addr := range precompiles {
al.AddAddress(addr)
}
for _, el := range list {
al.AddAddress(el.Address)
for _, key := range el.StorageKeys {
al.AddSlot(el.Address, key)
}
}
if rules.IsShanghai { // EIP-3651: warm coinbase
al.AddAddress(coinbase)
}
}
// Reset transient storage at the beginning of transaction execution
stateDB.transientStorage = newTransientStorage()
stateDB.transientStorageSnapshot = make(map[int]transientStorage)
}

// AddressInAccessList returns true if the given address is in the access list
Expand Down Expand Up @@ -1039,6 +1065,8 @@ func (stateDB *StateDBAdapter) clear() {
stateDB.preimageSnapshot = make(map[int]preimageMap)
stateDB.accessList = newAccessList()
stateDB.accessListSnapshot = make(map[int]*accessList)
stateDB.transientStorage = newTransientStorage()
stateDB.transientStorageSnapshot = make(map[int]transientStorage)
stateDB.logsSnapshot = make(map[int]int)
stateDB.txLogsSnapshot = make(map[int]int)
stateDB.logs = []*action.Log{}
Expand Down
45 changes: 45 additions & 0 deletions action/protocol/execution/evm/transient_storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) 2023 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.

package evm

import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
)

// transientStorage is a representation of EIP-1153 "Transient Storage".
type transientStorage map[common.Address]state.Storage

// newTransientStorage creates a new instance of a transientStorage.
func newTransientStorage() transientStorage {
return make(transientStorage)
}

// Set sets the transient-storage `value` for `key` at the given `addr`.
func (t transientStorage) Set(addr common.Address, key, value common.Hash) {
if _, ok := t[addr]; !ok {
t[addr] = make(state.Storage)
}
t[addr][key] = value
}

// Get gets the transient storage for `key` at the given `addr`.
func (t transientStorage) Get(addr common.Address, key common.Hash) common.Hash {
val, ok := t[addr]
if !ok {
return common.Hash{}
}
return val[key]
}

// Copy does a deep copy of the transientStorage
func (t transientStorage) Copy() transientStorage {
storage := make(transientStorage)
for key, value := range t {
storage[key] = value.Copy()
}
return storage
}
51 changes: 31 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ go 1.18

require (
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/ethereum/go-ethereum v1.10.21
github.com/ethereum/go-ethereum v1.10.26
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a
github.com/go-redis/redis/v8 v8.11.4
github.com/go-redis/redis/v8 v8.11.5
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.2
github.com/golang/snappy v0.0.4
github.com/gorilla/websocket v1.4.2
github.com/gorilla/websocket v1.5.0
github.com/grpc-ecosystem/go-grpc-middleware v1.2.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/hashicorp/vault/api v1.1.0
Expand All @@ -27,23 +27,23 @@ require (
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1
github.com/multiformats/go-multiaddr v0.3.3
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.11.1
github.com/prometheus/client_golang v1.14.0
github.com/rodaine/table v1.0.1
github.com/schollz/progressbar/v2 v2.15.0
github.com/spf13/cobra v1.1.1
github.com/spf13/cobra v1.5.0
github.com/stretchr/testify v1.8.1
github.com/tidwall/gjson v1.11.0
github.com/tyler-smith/go-bip39 v1.0.2
github.com/tyler-smith/go-bip39 v1.1.0
go.elastic.co/ecszap v1.0.0
go.etcd.io/bbolt v1.3.5
go.etcd.io/bbolt v1.3.6
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.25.0
go.opentelemetry.io/otel v1.9.0
go.opentelemetry.io/otel/exporters/jaeger v1.0.1
go.opentelemetry.io/otel/sdk v1.0.1
go.opentelemetry.io/otel/trace v1.9.0
go.uber.org/automaxprocs v1.2.0
go.uber.org/config v1.3.1
go.uber.org/zap v1.16.0
go.uber.org/zap v1.17.0
golang.org/x/crypto v0.1.0
golang.org/x/net v0.7.0
golang.org/x/sync v0.1.0
Expand All @@ -58,31 +58,33 @@ require (
github.com/cespare/xxhash/v2 v2.2.0
github.com/holiman/uint256 v1.2.0
github.com/mackerelio/go-osstat v0.2.4
github.com/prometheus/client_model v0.2.0
github.com/shirou/gopsutil/v3 v3.22.2
github.com/prometheus/client_model v0.3.0
github.com/shirou/gopsutil/v3 v3.22.8
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.34.0
golang.org/x/exp v0.0.0-20230206171751-46f607a40771
golang.org/x/text v0.7.0
)

require (
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6 // indirect
github.com/VictoriaMetrics/fastcache v1.6.0 // indirect
github.com/benbjohnson/clock v1.0.3 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcd v0.21.0-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.1.2 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustinxie/gmsm v1.4.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/google/gopacket v1.1.19 // indirect
Expand All @@ -96,6 +98,8 @@ require (
github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/vault/sdk v0.1.14-0.20200519221838-e0cfd64bc267 // indirect
github.com/holiman/big v0.0.0-20221017200358-a027dc42d04e // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/huin/goupnp v1.0.3 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/ipfs/go-cid v0.0.7 // indirect
Expand All @@ -109,6 +113,7 @@ require (
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/klauspost/cpuid/v2 v2.0.4 // indirect
github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/libp2p/go-addr-util v0.0.2 // indirect
github.com/libp2p/go-buffer-pool v0.0.2 // indirect
github.com/libp2p/go-cidranger v1.1.0 // indirect
Expand Down Expand Up @@ -152,7 +157,10 @@ require (
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magefile/mage v1.9.0 // indirect
github.com/marten-seemann/tcp v0.0.0-20210406111302-dfbc87cc63fd // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/miekg/dns v1.1.41 // indirect
github.com/mikioh/tcpinfo v0.0.0-20190314235526-30a79bb1804b // indirect
github.com/mikioh/tcpopt v0.0.0-20190314235656-172688c1accc // indirect
Expand All @@ -170,21 +178,23 @@ require (
github.com/multiformats/go-multihash v0.0.15 // indirect
github.com/multiformats/go-multistream v0.2.2 // indirect
github.com/multiformats/go-varint v0.0.6 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pierrec/lz4 v2.0.5+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/common v0.26.0 // indirect
github.com/prometheus/procfs v0.6.0 // indirect
github.com/rjeczalik/notify v0.9.2 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/prometheus/tsdb v0.10.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.9 // indirect
github.com/tklauser/numcpus v0.3.0 // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 // indirect
github.com/whyrusleeping/timecache v0.0.0-20160911033111-cfcb2f1abfee // indirect
Expand All @@ -193,6 +203,7 @@ require (
go.opentelemetry.io/otel/metric v0.31.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/time v0.1.0 // indirect
Expand All @@ -201,6 +212,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/ethereum/go-ethereum => github.com/iotexproject/go-ethereum v0.4.2
replace github.com/ethereum/go-ethereum => github.com/iotexproject/go-ethereum v1.7.4-0.20231007233235-d4bd9aed839e

replace golang.org/x/xerrors => golang.org/x/xerrors v0.0.0-20190212162355-a5947ffaace3
Loading

0 comments on commit 65833f7

Please sign in to comment.