Skip to content

Commit

Permalink
test: Enable 7702 tests using execution-spec-tests (#149)
Browse files Browse the repository at this point in the history
* Enable 7702 tests

* Fix SelfDestruct6780

* Set rules

* Fix setting createFlag

* Remove unused condition
  • Loading branch information
ulbqb authored Dec 9, 2024
1 parent 0581fac commit e99c409
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 23 deletions.
5 changes: 1 addition & 4 deletions blockchain/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,6 @@ func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject)
s.journal.append(resetObjectChange{prev: prev, prevdestruct: prevdestruct})
}

newobj.created = true

s.setStateObject(newobj)
if prev != nil && !prev.deleted {
return newobj, prev
Expand Down Expand Up @@ -811,8 +809,6 @@ func (s *StateDB) createObjectWithMap(addr common.Address, accountType account.A
s.journal.append(resetObjectChange{prev: prev, prevdestruct: prevdestruct})
}

newobj.created = true

s.setStateObject(newobj)
if prev != nil && !prev.deleted {
return newobj, prev
Expand Down Expand Up @@ -856,6 +852,7 @@ func (s *StateDB) CreateSmartContractAccountWithKey(addr common.Address, humanRe
account.AccountValueKeyCodeInfo: params.NewCodeInfoWithRules(format, r),
}
new, prev := s.createObjectWithMap(addr, account.SmartContractAccountType, values)
new.created = true
if prev != nil {
new.setBalance(prev.account.GetBalance())
}
Expand Down
7 changes: 5 additions & 2 deletions blockchain/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,13 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
// - reset transient storage(eip 1153)
st.state.Prepare(rules, msg.ValidatedSender(), msg.ValidatedFeePayer(), st.evm.Context.Coinbase, msg.To(), vm.ActivePrecompiles(rules), msg.AccessList())

// SetCode sender nonce increment should be done before set code process.
if msg.Type() == types.TxTypeEthereumSetCode {
st.state.IncNonce(msg.ValidatedSender())
}

// Check authorization list validity and set code.
if msg.AuthorizationList() != nil {
// SetCode sender nonce increment should be done before set code process.
st.state.IncNonce(msg.ValidatedSender())
st.processAuthorizationList(msg.AuthorizationList(), *msg.To(), rules)
}

Expand Down
2 changes: 1 addition & 1 deletion node/cn/tracers/tracers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func runTracer(t *testing.T, tc *tracerTestdata, tracer vm.Tracer) (*types.Trans
signer = types.MakeSigner(config, header.Number)
blockContext = blockchain.NewEVMBlockContext(header, nil, &common.Address{}) // stub author (COINBASE) to 0x0
txContext = blockchain.NewEVMTxContext(tx, header, config)
statedb = tests.MakePreState(database.NewMemoryDBManager(), alloc, false)
statedb = tests.MakePreState(database.NewMemoryDBManager(), alloc, false, config.Rules(new(big.Int).SetUint64(uint64(tc.Context.Number))))
evm = vm.NewEVM(blockContext, txContext, statedb, config, &vm.Config{Debug: true, Tracer: tracer})
)

Expand Down
7 changes: 6 additions & 1 deletion tests/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,16 @@ func (suite *ExecutionSpecStateTestSuite) TestExecutionSpecState() {
}
st := new(testMatcher)

// TODO-Kaia: should remove these skip
// json format error
st.skipLoad(`^prague\/eip7702_set_code_tx\/set_code_txs\/invalid_tx_invalid_auth_signature.json`)
st.skipLoad(`^prague\/eip7702_set_code_tx\/set_code_txs\/tx_validity_chain_id.json`)
st.skipLoad(`^prague\/eip7702_set_code_tx\/set_code_txs\/tx_validity_nonce.json`)

// tests to skip
// unsupported EIPs
st.skipLoad(`^cancun\/eip4788_beacon_root\/`)
st.skipLoad(`^cancun\/eip4844_blobs\/`)
st.skipLoad(`^prague\/eip7702_set_code_tx\/`)
// calculate the different consumed gas because 0x0a and 0x0b contract is set to access list by ActivePrecompiles in Cancun
st.skipLoad(`^prague\/eip2537_bls_12_381_precompiles\/bls12_precompiles_before_fork\/precompile_before_fork.json\/tests\/prague\/eip2537_bls_12_381_precompiles\/test_bls12_precompiles_before_fork.py::test_precompile_before_fork`)

Expand Down
33 changes: 19 additions & 14 deletions tests/state_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,10 +255,10 @@ func (t *StateTest) RunNoVerify(subtest StateSubtest, vmconfig vm.Config, isTest
blockchain.InitDeriveSha(config)
block := t.genesis(config).ToBlock(common.Hash{}, nil)
memDBManager := database.NewMemoryDBManager()
st = MakePreState(memDBManager, t.json.Pre, isTestExecutionSpecState)
rules := config.Rules(block.Number())
st = MakePreState(memDBManager, t.json.Pre, isTestExecutionSpecState, rules)

post := t.json.Post[subtest.Fork][subtest.Index]
rules := config.Rules(block.Number())
msg, err := t.json.Tx.toMessage(post, rules, isTestExecutionSpecState)
if err != nil {
return st, common.Hash{}, err
Expand Down Expand Up @@ -331,15 +331,19 @@ func (t *StateTest) gasLimit(subtest StateSubtest) uint64 {
return t.json.Tx.GasLimit[t.json.Post[subtest.Fork][subtest.Index].Indexes.Gas]
}

func MakePreState(db database.DBManager, accounts blockchain.GenesisAlloc, isTestExecutionSpecState bool) *state.StateDB {
func MakePreState(db database.DBManager, accounts blockchain.GenesisAlloc, isTestExecutionSpecState bool, rules params.Rules) *state.StateDB {
sdb := state.NewDatabase(db)
statedb, _ := state.New(common.Hash{}, sdb, nil, nil)
for addr, a := range accounts {
if len(a.Code) != 0 {
if isTestExecutionSpecState {
statedb.CreateSmartContractAccount(addr, params.CodeFormatEVM, params.Rules{IsIstanbul: true})
if _, ok := types.ParseDelegation(a.Code); ok {
statedb.SetCodeToEOA(addr, a.Code, rules)
} else {
if isTestExecutionSpecState {
statedb.CreateSmartContractAccount(addr, params.CodeFormatEVM, rules)
}
statedb.SetCode(addr, a.Code)
}
statedb.SetCode(addr, a.Code)
}
for k, v := range a.Storage {
statedb.SetState(addr, k, v)
Expand Down Expand Up @@ -414,12 +418,6 @@ func (tx *stTransaction) toMessage(ps stPostState, r params.Rules, isTestExecuti
accessList = *tx.AccessLists[ps.Indexes.Data]
}

var intrinsicGas uint64
if isTestExecutionSpecState {
intrinsicGas, err = useEthIntrinsicGas(data, accessList, to == nil, r)
} else {
intrinsicGas, err = types.IntrinsicGas(data, nil, nil, to == nil, r)
}
var authorizationList types.AuthorizationList
if tx.AuthorizationList != nil {
authorizationList = make(types.AuthorizationList, 0)
Expand All @@ -435,6 +433,13 @@ func (tx *stTransaction) toMessage(ps stPostState, r params.Rules, isTestExecuti
}
}

var intrinsicGas uint64
if isTestExecutionSpecState {
intrinsicGas, err = useEthIntrinsicGas(data, accessList, authorizationList, to == nil, r)
} else {
intrinsicGas, err = types.IntrinsicGas(data, nil, nil, to == nil, r)
}

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -491,11 +496,11 @@ func useEthOpCodeGas(r params.Rules, evm *vm.EVM) {
}
}

func useEthIntrinsicGas(data []byte, accessList types.AccessList, contractCreation bool, r params.Rules) (uint64, error) {
func useEthIntrinsicGas(data []byte, accessList types.AccessList, authorizationList types.AuthorizationList, contractCreation bool, r params.Rules) (uint64, error) {
if r.IsIstanbul {
r.IsPrague = true
}
return types.IntrinsicGas(data, accessList, nil, contractCreation, r)
return types.IntrinsicGas(data, accessList, authorizationList, contractCreation, r)
}

func useEthMiningReward(statedb *state.StateDB, evm *vm.EVM, tx *stTransaction, envBaseFee *big.Int, usedGas uint64, gasPrice *big.Int) {
Expand Down
3 changes: 2 additions & 1 deletion tests/vm_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ type vmExecMarshaling struct {

func (t *VMTest) Run(vmconfig vm.Config) error {
memDBManager := database.NewMemoryDBManager()
statedb := MakePreState(memDBManager, t.json.Pre, false)
rules := params.MainnetChainConfig.Rules(new(big.Int).SetUint64(t.json.Env.Number))
statedb := MakePreState(memDBManager, t.json.Pre, false, rules)
ret, gasRemaining, err := t.exec(statedb, vmconfig)

if t.json.GasRemaining == nil {
Expand Down

0 comments on commit e99c409

Please sign in to comment.