Skip to content

Commit 1a97dbb

Browse files
lizhefengzjshen14
authored andcommitted
Commit contract states in a deterministic way (#790)
1 parent 6c29aa6 commit 1a97dbb

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

action/protocol/execution/evm/evmstatedbadapter.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ package evm
88

99
import (
1010
"bytes"
11+
"encoding/hex"
1112
"fmt"
1213
"math/big"
14+
"sort"
1315

1416
"github.com/ethereum/go-ethereum/common"
1517
"github.com/ethereum/go-ethereum/core/types"
@@ -571,11 +573,24 @@ func (stateDB *StateDBAdapter) setContractState(addr hash.Hash160, key, value ha
571573

572574
// CommitContracts commits contract code to db and update pending contract account changes to trie
573575
func (stateDB *StateDBAdapter) CommitContracts() error {
574-
for addr, contract := range stateDB.cachedContract {
576+
addrStrs := make([]string, 0)
577+
for addr := range stateDB.cachedContract {
578+
addrStrs = append(addrStrs, hex.EncodeToString(addr[:]))
579+
}
580+
sort.Strings(addrStrs)
581+
582+
for _, addrStr := range addrStrs {
583+
var addr hash.Hash160
584+
addrBytes, err := hex.DecodeString(addrStr)
585+
if err != nil {
586+
return errors.Wrap(err, "failed to decode address hash")
587+
}
588+
copy(addr[:], addrBytes)
575589
if _, ok := stateDB.suicided[addr]; ok {
576590
// no need to update a suicide account/contract
577591
continue
578592
}
593+
contract := stateDB.cachedContract[addr]
579594
if err := contract.Commit(); err != nil {
580595
stateDB.logError(err)
581596
return errors.Wrap(err, "failed to commit contract")
@@ -588,14 +603,39 @@ func (stateDB *StateDBAdapter) CommitContracts() error {
588603
}
589604
}
590605
// delete suicided accounts/contract
606+
addrStrs = make([]string, 0)
591607
for addr := range stateDB.suicided {
608+
addrStrs = append(addrStrs, hex.EncodeToString(addr[:]))
609+
}
610+
sort.Strings(addrStrs)
611+
612+
for _, addrStr := range addrStrs {
613+
var addr hash.Hash160
614+
addrBytes, err := hex.DecodeString(addrStr)
615+
if err != nil {
616+
return errors.Wrap(err, "failed to decode address hash")
617+
}
618+
copy(addr[:], addrBytes)
592619
if err := stateDB.sm.DelState(addr); err != nil {
593620
stateDB.logError(err)
594621
return errors.Wrapf(err, "failed to delete suicide account/contract %x", addr[:])
595622
}
596623
}
597624
// write preimages to DB
598-
for k, v := range stateDB.preimages {
625+
addrStrs = make([]string, 0)
626+
for addr := range stateDB.preimages {
627+
addrStrs = append(addrStrs, hex.EncodeToString(addr[:]))
628+
}
629+
sort.Strings(addrStrs)
630+
631+
for _, addrStr := range addrStrs {
632+
var k common.Hash
633+
addrBytes, err := hex.DecodeString(addrStr)
634+
if err != nil {
635+
return errors.Wrap(err, "failed to decode address hash")
636+
}
637+
copy(k[:], addrBytes)
638+
v := stateDB.preimages[k]
599639
h := make([]byte, len(k))
600640
copy(h, k[:])
601641
stateDB.cb.Put(PreimageKVNameSpace, h, v, "failed to put hash %x preimage %x", k, v)

tools/executiontester/executiontester.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func main() {
4949
cfg.Plugins[config.GatewayPlugin] = true
5050
cfg.Chain.EnableAsyncIndexWrite = false
5151
cfg.Genesis.ActionGasLimit = 10000000
52-
cfg.Genesis.BlockInterval = 2
52+
cfg.Genesis.BlockInterval = 2 * time.Second
5353
itxsvr, err := itx.NewServer(cfg)
5454
if err != nil {
5555
log.L().Fatal("Failed to start itxServer.", zap.Error(err))

0 commit comments

Comments
 (0)