Skip to content

Commit 4da59ab

Browse files
envestccCoderZhidustinxie
authored
[evm] Refactor evm statedb adapter (#4550)
Co-authored-by: CoderZhi <[email protected]> Co-authored-by: dustinxie <[email protected]>
1 parent 36b3467 commit 4da59ab

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

action/protocol/execution/evm/evm.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,22 @@ type (
9595
actionCtx protocol.ActionCtx
9696
helperCtx HelperContext
9797
}
98+
99+
stateDB interface {
100+
vm.StateDB
101+
102+
CommitContracts() error
103+
Logs() []*action.Log
104+
TransactionLogs() []*action.TransactionLog
105+
clear()
106+
Error() error
107+
}
98108
)
99109

100110
// newParams creates a new context for use in the EVM.
101111
func newParams(
102112
ctx context.Context,
103113
execution action.TxData,
104-
stateDB *StateDBAdapter,
105114
) (*Params, error) {
106115
var (
107116
actionCtx = protocol.MustGetActionCtx(ctx)
@@ -134,15 +143,15 @@ func newParams(
134143
}
135144
case featureCtx.FixGetHashFnHeight:
136145
getHashFn = func(n uint64) common.Hash {
137-
hash, err := getBlockHash(stateDB.blockHeight - (n + 1))
146+
hash, err := getBlockHash(blkCtx.BlockHeight - (n + 1))
138147
if err == nil {
139148
return common.BytesToHash(hash[:])
140149
}
141150
return common.Hash{}
142151
}
143152
default:
144153
getHashFn = func(n uint64) common.Hash {
145-
hash, err := getBlockHash(stateDB.blockHeight - n)
154+
hash, err := getBlockHash(blkCtx.BlockHeight - n)
146155
if err != nil {
147156
// initial implementation did wrong, should return common.Hash{} in case of error
148157
return common.BytesToHash(hash[:])
@@ -241,7 +250,7 @@ func ExecuteContract(
241250
if err != nil {
242251
return nil, nil, err
243252
}
244-
ps, err := newParams(ctx, execution, stateDB)
253+
ps, err := newParams(ctx, execution)
245254
if err != nil {
246255
return nil, nil, err
247256
}
@@ -428,7 +437,7 @@ func getChainConfig(g genesis.Blockchain, height uint64, id uint32, getBlockTime
428437
}
429438

430439
// Error in executeInEVM is a consensus issue
431-
func executeInEVM(ctx context.Context, evmParams *Params, stateDB *StateDBAdapter) ([]byte, uint64, uint64, string, iotextypes.ReceiptStatus, error) {
440+
func executeInEVM(ctx context.Context, evmParams *Params, stateDB stateDB) ([]byte, uint64, uint64, string, iotextypes.ReceiptStatus, error) {
432441
var (
433442
gasLimit = evmParams.blkCtx.GasLimit
434443
blockHeight = evmParams.blkCtx.BlockHeight

action/protocol/execution/evm/evm_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ func TestConstantinople(t *testing.T) {
297297
})
298298
stateDB, err := prepareStateDB(fCtx, sm)
299299
require.NoError(err)
300-
ps, err := newParams(fCtx, elp, stateDB)
300+
ps, err := newParams(fCtx, elp)
301301
require.NoError(err)
302302

303303
evm := vm.NewEVM(ps.context, ps.txCtx, stateDB, ps.chainConfig, ps.evmConfig)

action/protocol/execution/evm/evmstatedbadapter.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type (
7272
createdAccountSnapshot map[int]createdAccount
7373
logsSnapshot map[int]int // logs is an array, save len(logs) at time of snapshot suffices
7474
txLogsSnapshot map[int]int
75+
newContract func(addr hash.Hash160, account *state.Account) (Contract, error)
7576
notFixTopicCopyBug bool
7677
asyncContractTrie bool
7778
disableSortCachedContracts bool
@@ -247,6 +248,9 @@ func NewStateDBAdapter(
247248
s.createdAccount = make(createdAccount)
248249
s.createdAccountSnapshot = make(map[int]createdAccount)
249250
}
251+
s.newContract = func(addr hash.Hash160, account *state.Account) (Contract, error) {
252+
return newContract(addr, account, s.sm, s.asyncContractTrie)
253+
}
250254
return s, nil
251255
}
252256

@@ -1129,7 +1133,7 @@ func (stateDB *StateDBAdapter) getNewContract(evmAddr common.Address) (Contract,
11291133
if err != nil {
11301134
return nil, errors.Wrapf(err, "failed to load account state for address %x", addr)
11311135
}
1132-
contract, err := newContract(addr, account, stateDB.sm, stateDB.asyncContractTrie)
1136+
contract, err := stateDB.newContract(addr, account)
11331137
if err != nil {
11341138
return nil, errors.Wrapf(err, "failed to create storage trie for new contract %x", addr)
11351139
}

0 commit comments

Comments
 (0)