Skip to content

Commit

Permalink
revert trie
Browse files Browse the repository at this point in the history
update trie prefix later to prevent fork
  • Loading branch information
rabbitprincess committed Oct 12, 2023
1 parent b459b30 commit 7bf35ef
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
7 changes: 1 addition & 6 deletions pkg/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"sync"

"github.com/aergoio/aergo-lib/db"
"github.com/aergoio/aergo/v2/internal/schema"
)

// Trie is a modified sparse Merkle tree.
Expand Down Expand Up @@ -483,7 +482,7 @@ func (s *Trie) loadBatch(root []byte) ([][]byte, error) {
s.loadDbMux.Unlock()
}
s.db.lock.Lock()
dbval := s.db.Store.Get(trieKey(root[:HashLength]))
dbval := s.db.Store.Get(root[:HashLength])
s.db.lock.Unlock()
nodeSize := len(dbval)
if nodeSize != 0 {
Expand Down Expand Up @@ -584,7 +583,3 @@ func (s *Trie) updatePastTries() {
s.pastTries = append(s.pastTries, s.Root)
}
}

func trieKey(key []byte) []byte {
return append([]byte(schema.TriePrefix), key...)
}
3 changes: 2 additions & 1 deletion pkg/trie/trie_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func (c *CacheDB) commit(txn *DbTx) {
c.updatedMux.Lock()
defer c.updatedMux.Unlock()
for key, batch := range c.updatedNodes {
(*txn).Set(trieKey(key[:]), c.serializeBatch(batch))
var node []byte
(*txn).Set(append(node, key[:]...), c.serializeBatch(batch))
}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/trie/trie_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (s *Trie) Revert(toOldRoot []byte) error {
// NOTE The tx interface doesnt handle ErrTxnTooBig
txn := s.db.Store.NewTx()
for _, key := range s.db.nodesToRevert {
txn.Delete(trieKey(key[:HashLength]))
txn.Delete(key[:HashLength])
}
txn.Commit()

Expand All @@ -62,7 +62,7 @@ func (s *Trie) Revert(toOldRoot []byte) error {
// If toOldRoot is a shortcut batch, it is possible that
// revert has deleted it if the key was ever stored at height0
// because in leafHash byte(0) = byte(256)
s.db.Store.Set(trieKey(toOldRoot), s.db.serializeBatch(batch))
s.db.Store.Set(toOldRoot, s.db.serializeBatch(batch))
}
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,10 @@ func TestTrieRevert(t *testing.T) {
root2, _ := smt.Update([][]byte{key1}, [][]byte{values[1]})
smt.Commit()
smt.Revert(root)
if len(smt.db.Store.Get(trieKey(root))) == 0 {
if len(smt.db.Store.Get(root)) == 0 {
t.Fatal("shortcut node shouldnt be deleted by revert")
}
if len(smt.db.Store.Get(trieKey(root2))) != 0 {
if len(smt.db.Store.Get(root2)) != 0 {
t.Fatal("reverted root should have been deleted")
}
key1 = make([]byte, 32, 32)
Expand All @@ -370,7 +370,7 @@ func TestTrieRevert(t *testing.T) {
smt.Update([][]byte{key1}, [][]byte{values[1]})
smt.Commit()
smt.Revert(root)
if len(smt.db.Store.Get(trieKey(root))) == 0 {
if len(smt.db.Store.Get(root)) == 0 {
t.Fatal("shortcut node shouldnt be deleted by revert")
}

Expand Down Expand Up @@ -412,12 +412,12 @@ func TestTrieRevert(t *testing.T) {
}
// Check all reverted nodes have been deleted
for node := range updatedNodes2 {
if len(smt.db.Store.Get(trieKey(node[:]))) != 0 {
if len(smt.db.Store.Get(node[:])) != 0 {
t.Fatal("nodes not deleted from database", node)
}
}
for node := range updatedNodes1 {
if len(smt.db.Store.Get(trieKey(node[:]))) != 0 {
if len(smt.db.Store.Get(node[:])) != 0 {
t.Fatal("nodes not deleted from database", node)
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/trie/trie_tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *Trie) loadCache(root []byte, batch [][]byte, iBatch, height int, ch cha
if height%4 == 0 {
// Load the node from db
s.db.lock.Lock()
dbval := s.db.Store.Get(trieKey(root[:HashLength]))
dbval := s.db.Store.Get(root[:HashLength])
s.db.lock.Unlock()
if len(dbval) == 0 {
ch <- fmt.Errorf("the trie node %x is unavailable in the disk db, db may be corrupted", root)
Expand Down Expand Up @@ -113,7 +113,7 @@ func (s *Trie) get(root, key []byte, batch [][]byte, iBatch, height int) ([]byte
// TrieRootExists returns true if the root exists in Database.
func (s *Trie) TrieRootExists(root []byte) bool {
s.db.lock.RLock()
dbval := s.db.Store.Get(trieKey(root))
dbval := s.db.Store.Get(root)
s.db.lock.RUnlock()
if len(dbval) != 0 {
return true
Expand Down

0 comments on commit 7bf35ef

Please sign in to comment.