Skip to content

Commit

Permalink
commit return error
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcb9ff9 committed Dec 14, 2022
1 parent 3713caf commit 2c9d7fd
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 48 deletions.
5 changes: 4 additions & 1 deletion blockchain/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,10 @@ func (b *Blockchain) executeBlockTransactions(block *types.Block) (*BlockResult,
return nil, ErrClosed
}

_, root := txn.Commit()
_, root, err := txn.Commit()
if err != nil {
return nil, err
}

// Append the receipts to the receipts cache
b.receiptsCache.Add(header.Hash, txn.Receipts())
Expand Down
5 changes: 4 additions & 1 deletion consensus/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,10 @@ func (d *Dev) writeNewBlock(parent *types.Header) error {
txns := d.writeTransactions(gasLimit, transition)

// Commit the changes
_, root := transition.Commit()
_, root, err := transition.Commit()
if err != nil {
return err
}

// Update the header
header.StateRoot = root
Expand Down
6 changes: 5 additions & 1 deletion consensus/ibft/ibft.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,11 @@ func (i *Ibft) buildBlock(snap *Snapshot, parent *types.Header) (*types.Block, e
return nil, err
}

_, root := transition.Commit()
_, root, err := transition.Commit()
if err != nil {
return nil, err
}

header.StateRoot = root
header.GasUsed = transition.TotalGas()

Expand Down
6 changes: 5 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,11 @@ func NewServer(config *Config) (*Server, error) {
m.executor.SetRuntime(evm.NewEVM())

// compute the genesis root state
genesisRoot := m.executor.WriteGenesis(config.Chain.Genesis.Alloc)
genesisRoot, err := m.executor.WriteGenesis(config.Chain.Genesis.Alloc)
if err != nil {
return nil, err
}

config.Chain.Genesis.StateRoot = genesisRoot

// create leveldb storageBuilder
Expand Down
24 changes: 17 additions & 7 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewExecutor(config *chain.Params, s State, logger hclog.Logger) *Executor {
}
}

func (e *Executor) WriteGenesis(alloc map[types.Address]*chain.GenesisAccount) types.Hash {
func (e *Executor) WriteGenesis(alloc map[types.Address]*chain.GenesisAccount) (types.Hash, error) {
snap := e.state.NewSnapshot()
txn := NewTxn(e.state, snap)

Expand All @@ -77,9 +77,12 @@ func (e *Executor) WriteGenesis(alloc map[types.Address]*chain.GenesisAccount) t
}

objs := txn.Commit(false)
_, root := snap.Commit(objs)
_, root, err := snap.Commit(objs)
if err != nil {
return types.ZeroHash, nil
}

return types.BytesToHash(root)
return types.BytesToHash(root), nil
}

// SetRuntime adds a runtime to the runtime set
Expand Down Expand Up @@ -360,7 +363,11 @@ func (t *Transition) Write(txn *types.Transaction) error {
}
} else {
objs := t.txn.Commit(t.config.EIP155)
ss, aux := t.txn.snapshot.Commit(objs)
ss, aux, err := t.txn.snapshot.Commit(objs)
if err != nil {
return err
}

t.txn = NewTxn(t.auxState, ss)
root = aux
receipt.Root = types.BytesToHash(root)
Expand Down Expand Up @@ -437,11 +444,14 @@ func (t *Transition) handleBridgeLogs(msg *types.Transaction, logs []*types.Log)
}

// Commit commits the final result
func (t *Transition) Commit() (Snapshot, types.Hash) {
func (t *Transition) Commit() (Snapshot, types.Hash, error) {
objs := t.txn.Commit(t.config.EIP155)
s2, root := t.txn.snapshot.Commit(objs)
s2, root, err := t.txn.snapshot.Commit(objs)
if err != nil {
return nil, types.ZeroHash, err
}

return s2, types.BytesToHash(root)
return s2, types.BytesToHash(root), nil
}

func (t *Transition) subGasPool(amount uint64) error {
Expand Down
14 changes: 9 additions & 5 deletions state/immutable-trie/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type StateDBReader interface {
type StateDB interface {
StateDBReader

Transaction(execute func(st StateDBTransaction))
Transaction(execute func(st StateDBTransaction) error) error
}

type stateDBImpl struct {
Expand Down Expand Up @@ -137,7 +137,7 @@ var stateTxnPool = sync.Pool{
},
}

func (db *stateDBImpl) Transaction(execute func(StateDBTransaction)) {
func (db *stateDBImpl) Transaction(execute func(StateDBTransaction) error) error {
db.txnMux.Lock()
defer db.txnMux.Unlock()

Expand All @@ -154,10 +154,14 @@ func (db *stateDBImpl) Transaction(execute func(StateDBTransaction)) {
defer stateDBTxnRef.Reset()

// execute transaction
execute(stateDBTxnRef)
err := execute(stateDBTxnRef)

// update cache
for _, pair := range stateDBTxnRef.db {
db.cached.Set(pair.key, pair.value)
if err == nil {
for _, pair := range stateDBTxnRef.db {
db.cached.Set(pair.key, pair.value)
}
}

return err
}
26 changes: 14 additions & 12 deletions state/immutable-trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package itrie

import (
"bytes"
"errors"

"github.com/dogechain-lab/dogechain/state"
"github.com/dogechain-lab/dogechain/types"
Expand Down Expand Up @@ -38,13 +39,13 @@ var accountArenaPool fastrlp.ArenaPool

var stateArenaPool fastrlp.ArenaPool // TODO, Remove once we do update in fastrlp

func (t *Trie) Commit(objs []*state.Object) (state.Snapshot, []byte) {
func (t *Trie) Commit(objs []*state.Object) (state.Snapshot, []byte, error) {
var root []byte = nil

var nTrie *Trie = nil

// Create an insertion batch for all the entries
t.stateDB.Transaction(func(st StateDBTransaction) {
err := t.stateDB.Transaction(func(st StateDBTransaction) error {
defer st.Rollback()

tt := t.Txn()
Expand Down Expand Up @@ -75,7 +76,7 @@ func (t *Trie) Commit(objs []*state.Object) (state.Snapshot, []byte) {

trie, ok := localSnapshot.(*Trie)
if !ok {
panic("invalid type assertion")
return errors.New("invalid type assertion")
}

localTxn := trie.Txn()
Expand All @@ -96,10 +97,10 @@ func (t *Trie) Commit(objs []*state.Object) (state.Snapshot, []byte) {

if obj.DirtyCode {
// write code to memory object, never failed
// if failed, memory can't alloc, it will panic
// if failed, can't alloc memory, it will panic
err := st.SetCode(obj.CodeHash, obj.Code)
if err != nil {
panic(err)
return err
}
}

Expand All @@ -111,7 +112,12 @@ func (t *Trie) Commit(objs []*state.Object) (state.Snapshot, []byte) {
}
}

root, _ = tt.Hash(st)
var err error

root, err = tt.Hash(st)
if err != nil {
return err
}

// dont use st here, we need to use the original stateDB
nTrie = &Trie{
Expand All @@ -122,14 +128,10 @@ func (t *Trie) Commit(objs []*state.Object) (state.Snapshot, []byte) {
nTrie.stateDB = t.stateDB

// Commit all the entries to db
// TODO, need to handle error
err := st.Commit()
if err != nil {
panic(err)
}
return st.Commit()
})

return nTrie, root
return nTrie, root, err
}

// Hash returns the root hash of the trie. It does not write to the
Expand Down
2 changes: 1 addition & 1 deletion state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type State interface {

type Snapshot interface {
Get(k []byte) ([]byte, bool)
Commit(objs []*Object) (Snapshot, []byte)
Commit(objs []*Object) (Snapshot, []byte, error)
}

// account trie
Expand Down
37 changes: 25 additions & 12 deletions state/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,16 @@ func testDeleteCommonStateRoot(t *testing.T, buildPreState buildPreState) {
txn.SetState(addr2, hash1, hash1)
txn.SetState(addr2, hash2, hash1)

snap2, _ := snap.Commit(txn.Commit(false))
snap2, _, err := snap.Commit(txn.Commit(false))
assert.NoError(t, err)

txn2 := newTxn(state, snap2)

txn2.SetState(addr1, hash0, hash0)
txn2.SetState(addr1, hash1, hash0)

snap3, _ := snap2.Commit(txn2.Commit(false))
snap3, _, err := snap2.Commit(txn2.Commit(false))
assert.NoError(t, err)

txn3 := newTxn(state, snap3)
assert.Equal(t, hash1, txn3.GetState(addr1, hash2))
Expand All @@ -121,7 +124,8 @@ func testWriteState(t *testing.T, buildPreState buildPreState) {
assert.Equal(t, hash1, txn.GetState(addr1, hash1))
assert.Equal(t, hash2, txn.GetState(addr1, hash2))

snap, _ = snap.Commit(txn.Commit(false))
snap, _, err := snap.Commit(txn.Commit(false))
assert.NoError(t, err)

txn = newTxn(state, snap)
assert.Equal(t, hash1, txn.GetState(addr1, hash1))
Expand All @@ -136,7 +140,8 @@ func testWriteEmptyState(t *testing.T, buildPreState buildPreState) {

// Without EIP150 the data is added
txn.SetState(addr1, hash1, hash0)
snap, _ = snap.Commit(txn.Commit(false))
snap, _, err := snap.Commit(txn.Commit(false))
assert.NoError(t, err)

txn = newTxn(state, snap)
assert.True(t, txn.Exist(addr1))
Expand All @@ -146,7 +151,8 @@ func testWriteEmptyState(t *testing.T, buildPreState buildPreState) {

// With EIP150 the empty data is removed
txn.SetState(addr1, hash1, hash0)
snap, _ = snap.Commit(txn.Commit(true))
snap, _, err = snap.Commit(txn.Commit(true))
assert.NoError(t, err)

txn = newTxn(state, snap)
assert.False(t, txn.Exist(addr1))
Expand All @@ -163,7 +169,8 @@ func testUpdateStateWithEmpty(t *testing.T, buildPreState buildPreState) {

// TODO, test with false (should not be deleted)
// TODO, test with balance on the account and nonce
snap, _ = snap.Commit(txn.Commit(true))
snap, _, err := snap.Commit(txn.Commit(true))
assert.NoError(t, err)

txn = newTxn(state, snap)
assert.False(t, txn.Exist(addr1))
Expand All @@ -177,7 +184,8 @@ func testSuicideAccountInPreState(t *testing.T, buildPreState buildPreState) {

txn := newTxn(state, snap)
txn.Suicide(addr1)
snap, _ = snap.Commit(txn.Commit(true))
snap, _, err := snap.Commit(txn.Commit(true))
assert.NoError(t, err)

txn = newTxn(state, snap)
assert.False(t, txn.Exist(addr1))
Expand All @@ -195,7 +203,8 @@ func testSuicideAccount(t *testing.T, buildPreState buildPreState) {
// Note, even if has commit suicide it still exists in the current txn
assert.True(t, txn.Exist(addr1))

snap, _ = snap.Commit(txn.Commit(true))
snap, _, err := snap.Commit(txn.Commit(true))
assert.NoError(t, err)

txn = newTxn(state, snap)
assert.False(t, txn.Exist(addr1))
Expand All @@ -216,7 +225,8 @@ func testSuicideAccountWithData(t *testing.T, buildPreState buildPreState) {
txn.SetState(addr1, hash1, hash1)

txn.Suicide(addr1)
snap, _ = snap.Commit(txn.Commit(true))
snap, _, err := snap.Commit(txn.Commit(true))
assert.NoError(t, err)

txn = newTxn(state, snap)

Expand All @@ -239,7 +249,8 @@ func testSuicideCoinbase(t *testing.T, buildPreState buildPreState) {
txn := newTxn(state, snap)
txn.Suicide(addr1)
txn.AddSealingReward(addr1, big.NewInt(10))
snap, _ = snap.Commit(txn.Commit(true))
snap, _, err := snap.Commit(txn.Commit(true))
assert.NoError(t, err)

txn = newTxn(state, snap)
assert.Equal(t, big.NewInt(10), txn.GetBalance(addr1))
Expand Down Expand Up @@ -293,7 +304,8 @@ func testChangePrestateAccountBalanceToZero(t *testing.T, buildPreState buildPre

txn := newTxn(state, snap)
txn.SetBalance(addr1, big.NewInt(0))
snap, _ = snap.Commit(txn.Commit(true))
snap, _, err := snap.Commit(txn.Commit(true))
assert.NoError(t, err)

txn = newTxn(state, snap)
assert.False(t, txn.Exist(addr1))
Expand All @@ -307,7 +319,8 @@ func testChangeAccountBalanceToZero(t *testing.T, buildPreState buildPreState) {
txn := newTxn(state, snap)
txn.SetBalance(addr1, big.NewInt(10))
txn.SetBalance(addr1, big.NewInt(0))
snap, _ = snap.Commit(txn.Commit(true))
snap, _, err := snap.Commit(txn.Commit(true))
assert.NoError(t, err)

txn = newTxn(state, snap)
assert.False(t, txn.Exist(addr1))
Expand Down
2 changes: 1 addition & 1 deletion state/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (m *mockSnapshot) Get(k []byte) ([]byte, bool) {
return v, ok
}

func (m *mockSnapshot) Commit(objs []*Object) (Snapshot, []byte) {
func (m *mockSnapshot) Commit(objs []*Object) (Snapshot, []byte, error) {
panic("Not implemented in tests")
}

Expand Down
4 changes: 3 additions & 1 deletion tests/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/dogechain-lab/dogechain/types"
"github.com/dogechain-lab/fastrlp"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
)

var mainnetChainConfig = chain.Params{
Expand Down Expand Up @@ -50,7 +51,8 @@ func testVMCase(t *testing.T, name string, c *VMCase) {
env.GasPrice = types.BytesToHash(c.Exec.GasPrice.Bytes())
env.Origin = c.Exec.Origin

s, _, root := buildState(c.Pre)
s, _, root, err := buildState(c.Pre)
assert.NoError(t, err)

config := mainnetChainConfig.Forks.At(uint64(env.Number))

Expand Down
Loading

0 comments on commit 2c9d7fd

Please sign in to comment.