Skip to content

Commit

Permalink
trie: polish commit function (ethereum#21692)
Browse files Browse the repository at this point in the history
* trie: polish commit function

* trie: fix typo
  • Loading branch information
rjl493456442 authored Oct 12, 2020
1 parent 706f5e3 commit 86dd005
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
23 changes: 15 additions & 8 deletions trie/stacktrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package trie

import (
"errors"
"fmt"
"sync"

Expand All @@ -26,6 +27,8 @@ import (
"github.com/ethereum/go-ethereum/rlp"
)

var ErrCommitDisabled = errors.New("no database for committing")

var stPool = sync.Pool{
New: func() interface{} {
return NewStackTrie(nil)
Expand Down Expand Up @@ -391,14 +394,18 @@ func (st *StackTrie) Hash() (h common.Hash) {
return common.BytesToHash(st.val)
}

// Commit will commit the current node to database db
func (st *StackTrie) Commit(db ethdb.KeyValueStore) common.Hash {
oldDb := st.db
st.db = db
defer func() {
st.db = oldDb
}()
// Commit will firstly hash the entrie trie if it's still not hashed
// and then commit all nodes to the associated database. Actually most
// of the trie nodes MAY have been committed already. The main purpose
// here is to commit the root node.
//
// The associated database is expected, otherwise the whole commit
// functionality should be disabled.
func (st *StackTrie) Commit() (common.Hash, error) {
if st.db == nil {
return common.Hash{}, ErrCommitDisabled
}
st.hash()
h := common.BytesToHash(st.val)
return h
return h, nil
}
5 changes: 4 additions & 1 deletion trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,10 @@ func TestCommitSequenceStackTrie(t *testing.T) {
// Flush memdb -> disk (sponge)
db.Commit(root, false, nil)
// And flush stacktrie -> disk
stRoot := stTrie.Commit(stTrie.db)
stRoot, err := stTrie.Commit()
if err != nil {
t.Fatalf("Failed to commit stack trie %v", err)
}
if stRoot != root {
t.Fatalf("root wrong, got %x exp %x", stRoot, root)
}
Expand Down

0 comments on commit 86dd005

Please sign in to comment.