Skip to content

Commit

Permalink
newLegacyNodeDB and support for pure-preimage mode
Browse files Browse the repository at this point in the history
  • Loading branch information
i-norden committed Jun 12, 2024
1 parent 4cd8107 commit 429a806
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ type nodeDB struct {
legacyLatestVersion int64 // Latest version of nodeDB in legacy format.
nodeCache cache.Cache // Cache for nodes in the regular tree that consists of key-value pairs at any version.
fastNodeCache cache.Cache // Cache for nodes in the fast index that represents only key-value pairs at the latest version.
useLegacyFormat bool
}

func newNodeDB(db dbm.DB, cacheSize int, opts Options, lg log.Logger) *nodeDB {
storeVersion, err := db.Get(metadataKeyFormat.Key([]byte(storageVersionKey)))
storeVersion, err := db.Get(metadataKeyFormat.Key(ibytes.UnsafeStrToBytes(storageVersionKey)))

if err != nil || storeVersion == nil {
storeVersion = []byte(defaultStorageVersionValue)
Expand All @@ -114,6 +115,34 @@ func newNodeDB(db dbm.DB, cacheSize int, opts Options, lg log.Logger) *nodeDB {
}
}

func newLegacyNodeDB(db dbm.DB, cacheSize int, opts Options, noStoreVersion bool, lg log.Logger) *nodeDB {
var storeVersion []byte
if noStoreVersion {
storeVersion = []byte(defaultStorageVersionValue)
} else {
var err error
storeVersion, err = db.Get(metadataKeyFormat.Key(ibytes.UnsafeStrToBytes(storageVersionKey)))
if err != nil || storeVersion == nil {
storeVersion = []byte(defaultStorageVersionValue)
}
}

return &nodeDB{
logger: lg,
db: db,
batch: NewBatchWithFlusher(db, opts.FlushThreshold),
opts: opts,
firstVersion: 0,
latestVersion: 0, // initially invalid
legacyLatestVersion: 0,
nodeCache: cache.New(cacheSize),
fastNodeCache: cache.New(fastNodeCacheSize),
versionReaders: make(map[int64]uint32, 8),
storageVersion: string(storeVersion),
useLegacyFormat: true,
}
}

// GetNode gets a node from memory or disk. If it is an inner node, it does not
// load its children.
// It is used for both formats of nodes: legacy and new.
Expand Down Expand Up @@ -209,7 +238,7 @@ func (ndb *nodeDB) SaveNode(node *Node) error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()

if node.nodeKey == nil {
if (node.nodeKey == nil && !node.isLegacy) || (node.hash == nil && node.isLegacy) {
return ErrNodeMissingNodeKey
}

Expand Down

0 comments on commit 429a806

Please sign in to comment.