2020package firewood
2121
2222import (
23- "bytes"
2423 "context"
2524 "errors"
2625 "fmt"
@@ -159,7 +158,7 @@ func New(config Config) (*Database, error) {
159158 return nil , err
160159 }
161160
162- currentRoot , err := fw .Root ()
161+ intialRoot , err := fw .Root ()
163162 if err != nil {
164163 if closeErr := fw .Close (context .Background ()); closeErr != nil {
165164 return nil , fmt .Errorf ("%w: error while closing: %w" , err , closeErr )
@@ -173,7 +172,7 @@ func New(config Config) (*Database, error) {
173172 byStateRoot : make (map [common.Hash ][]* proposal ),
174173 tree : & proposal {
175174 proposalMeta : & proposalMeta {
176- root : common .Hash (currentRoot ),
175+ root : common .Hash (intialRoot ),
177176 blockHashes : map [common.Hash ]struct {}{
178177 {}: {}, // genesis block has empty hash
179178 },
@@ -225,7 +224,7 @@ func (db *Database) Initialized(common.Hash) bool {
225224
226225 // If the current root isn't empty, then unless the genesis block is empty,
227226 // the database is initialized.
228- return ! bytes . Equal (root , types .EmptyRootHash [:])
227+ return common . Hash (root ) != types .EmptyRootHash
229228}
230229
231230// Size returns the storage size of diff layer nodes above the persistent disk
@@ -437,7 +436,7 @@ func (db *Database) Commit(root common.Hash, report bool) error {
437436 if err != nil {
438437 return fmt .Errorf ("firewood: error getting current root after commit: %w" , err )
439438 }
440- if ! bytes . Equal (newRoot , root [:]) {
439+ if common . Hash (newRoot ) != root {
441440 return fmt .Errorf ("firewood: root after commit (%#x) does not match expected root %#x" , newRoot , root )
442441 }
443442
@@ -501,11 +500,11 @@ func (db *Database) createProposal(parent *proposal, keys, values [][]byte) (*pr
501500 },
502501 }
503502
504- currentRootBytes , err := handle .Root ()
503+ root , err := handle .Root ()
505504 if err != nil {
506505 return nil , fmt .Errorf ("firewood: error getting root of proposals: %w" , err )
507506 }
508- p .root = common .BytesToHash ( currentRootBytes )
507+ p .root = common .Hash ( root )
509508
510509 return p , nil
511510}
@@ -612,7 +611,7 @@ func (db *Database) getProposalHash(parentRoot common.Hash, keys, values [][]byt
612611// Reader retrieves a node reader belonging to the given state root.
613612// An error will be returned if the requested state is not available.
614613func (db * Database ) Reader (root common.Hash ) (database.Reader , error ) {
615- if _ , err := db .Firewood .GetFromRoot (root . Bytes ( ), []byte {}); err != nil {
614+ if _ , err := db .Firewood .GetFromRoot (ffi . Hash ( root ), []byte {}); err != nil {
616615 return nil , fmt .Errorf ("firewood: unable to retrieve from root %s: %w" , root .Hex (), err )
617616 }
618617 return & reader {db : db , root : root }, nil
@@ -629,5 +628,5 @@ type reader struct {
629628func (reader * reader ) Node (_ common.Hash , path []byte , _ common.Hash ) ([]byte , error ) {
630629 // This function relies on Firewood's internal locking to ensure concurrent reads are safe.
631630 // This is safe even if a proposal is being committed concurrently.
632- return reader .db .Firewood .GetFromRoot (reader .root . Bytes ( ), path )
631+ return reader .db .Firewood .GetFromRoot (ffi . Hash ( reader .root ), path )
633632}
0 commit comments