Skip to content

Commit

Permalink
Commit contract states in a deterministic way (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhefeng authored and zjshen14 committed Mar 21, 2019
1 parent 6c29aa6 commit 1a97dbb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
44 changes: 42 additions & 2 deletions action/protocol/execution/evm/evmstatedbadapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ package evm

import (
"bytes"
"encoding/hex"
"fmt"
"math/big"
"sort"

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

// CommitContracts commits contract code to db and update pending contract account changes to trie
func (stateDB *StateDBAdapter) CommitContracts() error {
for addr, contract := range stateDB.cachedContract {
addrStrs := make([]string, 0)
for addr := range stateDB.cachedContract {
addrStrs = append(addrStrs, hex.EncodeToString(addr[:]))
}
sort.Strings(addrStrs)

for _, addrStr := range addrStrs {
var addr hash.Hash160
addrBytes, err := hex.DecodeString(addrStr)
if err != nil {
return errors.Wrap(err, "failed to decode address hash")
}
copy(addr[:], addrBytes)
if _, ok := stateDB.suicided[addr]; ok {
// no need to update a suicide account/contract
continue
}
contract := stateDB.cachedContract[addr]
if err := contract.Commit(); err != nil {
stateDB.logError(err)
return errors.Wrap(err, "failed to commit contract")
Expand All @@ -588,14 +603,39 @@ func (stateDB *StateDBAdapter) CommitContracts() error {
}
}
// delete suicided accounts/contract
addrStrs = make([]string, 0)
for addr := range stateDB.suicided {
addrStrs = append(addrStrs, hex.EncodeToString(addr[:]))
}
sort.Strings(addrStrs)

for _, addrStr := range addrStrs {
var addr hash.Hash160
addrBytes, err := hex.DecodeString(addrStr)
if err != nil {
return errors.Wrap(err, "failed to decode address hash")
}
copy(addr[:], addrBytes)
if err := stateDB.sm.DelState(addr); err != nil {
stateDB.logError(err)
return errors.Wrapf(err, "failed to delete suicide account/contract %x", addr[:])
}
}
// write preimages to DB
for k, v := range stateDB.preimages {
addrStrs = make([]string, 0)
for addr := range stateDB.preimages {
addrStrs = append(addrStrs, hex.EncodeToString(addr[:]))
}
sort.Strings(addrStrs)

for _, addrStr := range addrStrs {
var k common.Hash
addrBytes, err := hex.DecodeString(addrStr)
if err != nil {
return errors.Wrap(err, "failed to decode address hash")
}
copy(k[:], addrBytes)
v := stateDB.preimages[k]
h := make([]byte, len(k))
copy(h, k[:])
stateDB.cb.Put(PreimageKVNameSpace, h, v, "failed to put hash %x preimage %x", k, v)
Expand Down
2 changes: 1 addition & 1 deletion tools/executiontester/executiontester.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
cfg.Plugins[config.GatewayPlugin] = true
cfg.Chain.EnableAsyncIndexWrite = false
cfg.Genesis.ActionGasLimit = 10000000
cfg.Genesis.BlockInterval = 2
cfg.Genesis.BlockInterval = 2 * time.Second
itxsvr, err := itx.NewServer(cfg)
if err != nil {
log.L().Fatal("Failed to start itxServer.", zap.Error(err))
Expand Down

0 comments on commit 1a97dbb

Please sign in to comment.