Skip to content
This repository has been archived by the owner on Aug 19, 2024. It is now read-only.

Commit

Permalink
Use ExtHash in triedb.obj and uncache
Browse files Browse the repository at this point in the history
  • Loading branch information
blukat29 committed Jun 12, 2023
1 parent 0e31915 commit 45b7da1
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions storage/statedb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (n *cachedNode) rlp() []byte {

// obj returns the decoded and expanded trie node, either directly from the cache,
// or by regenerating it from the rlp encoded blob.
func (n *cachedNode) obj(hash common.Hash) node {
func (n *cachedNode) obj(hash common.ExtHash) node {
if node, ok := n.node.(rawNode); ok {
return mustDecodeNode(hash[:], node)
}
Expand Down Expand Up @@ -506,7 +506,7 @@ func (db *Database) node(_hash common.ExtHash) (n node, fromDB bool) {
node := db.nodes[hash]
db.lock.RUnlock()
if node != nil {
return node.obj(hash), false
return node.obj(_hash), false
}

// Content unavailable in memory, attempt to retrieve from disk
Expand Down Expand Up @@ -912,7 +912,9 @@ func (db *Database) concurrentCommit(hash common.ExtHash, resultCh chan<- commit
// node must be a state root.
//
// As a side effect, all pre-images accumulated up to this point are also written.
func (db *Database) CommitRoot(node common.Hash, report bool, blockNum uint64) error {
func (db *Database) CommitRoot(root common.Hash, report bool, blockNum uint64) error {
// TODO-Klaytn-Pruning: Use ExtendRoot
hash := root.ExtendLegacy()
// Create a database batch to flush persistent data out. It is important that
// outside code doesn't see an inconsistent state (referenced data removed from
// memory cache during commit but not yet in persistent database). This is ensured
Expand All @@ -927,7 +929,7 @@ func (db *Database) CommitRoot(node common.Hash, report bool, blockNum uint64) e

// Move the trie itself into the batch, flushing if enough data is accumulated
numNodes, nodesSize := len(db.nodes), db.nodesSize
if err := db.writeBatchNodes(node.ExtendLegacy()); err != nil {
if err := db.writeBatchNodes(hash); err != nil {
db.lock.RUnlock()
return err
}
Expand All @@ -942,7 +944,7 @@ func (db *Database) CommitRoot(node common.Hash, report bool, blockNum uint64) e
db.preimagesSize = 0

uncacheStart := time.Now()
db.uncache(node)
db.uncache(hash)
commitEnd := time.Now()

memcacheUncacheTimeGauge.Update(int64(commitEnd.Sub(uncacheStart)))
Expand Down Expand Up @@ -984,7 +986,8 @@ func (db *Database) commit(hash common.ExtHash, resultCh chan<- commitResult) {
// persisted trie is removed from the cache. The reason behind the two-phase
// commit is to ensure consistent data availability while moving from memory
// to disk.
func (db *Database) uncache(hash common.Hash) {
func (db *Database) uncache(_hash common.ExtHash) {
hash := _hash.Unextend()
// If the node does not exists, we're done on this path
node, ok := db.nodes[hash]
if !ok {
Expand All @@ -994,7 +997,7 @@ func (db *Database) uncache(hash common.Hash) {
db.removeNodeInFlushList(hash)
// Uncache the node's subtries and remove the node itself too
for _, child := range node.childs() {
db.uncache(child.Unextend())
db.uncache(child)
}
delete(db.nodes, hash)
db.nodesSize -= common.StorageSize(common.HashLength + int(node.size))
Expand Down

0 comments on commit 45b7da1

Please sign in to comment.