diff --git a/packages/trie/src/db/checkpoint.ts b/packages/trie/src/db/checkpoint.ts index 8112f6b274..66d10b92d9 100644 --- a/packages/trie/src/db/checkpoint.ts +++ b/packages/trie/src/db/checkpoint.ts @@ -20,7 +20,7 @@ export class CheckpointDB implements DB { /** * Is the DB during a checkpoint phase? */ - get isCheckpoint() { + hasCheckpoints() { return this.checkpoints.length > 0 } @@ -37,7 +37,7 @@ export class CheckpointDB implements DB { */ async commit() { const { keyValueMap } = this.checkpoints.pop()! - if (!this.isCheckpoint) { + if (!this.hasCheckpoints()) { // This was the final checkpoint, we should now commit and flush everything to disk const batchOp: BatchDBOp[] = [] for (const [key, value] of keyValueMap.entries()) { @@ -86,7 +86,7 @@ export class CheckpointDB implements DB { // Nothing has been found in cache, look up from disk const value = await this.db.get(key) - if (this.isCheckpoint) { + if (this.hasCheckpoints()) { // Since we are a checkpoint, put this value in cache, so future `get` calls will not look the key up again from disk. this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('binary'), value) } @@ -98,7 +98,7 @@ export class CheckpointDB implements DB { * @inheritDoc */ async put(key: Buffer, val: Buffer): Promise { - if (this.isCheckpoint) { + if (this.hasCheckpoints()) { // put value in cache this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('binary'), val) } else { @@ -110,7 +110,7 @@ export class CheckpointDB implements DB { * @inheritDoc */ async del(key: Buffer): Promise { - if (this.isCheckpoint) { + if (this.hasCheckpoints()) { // delete the value in the current cache this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('binary'), null) } else { @@ -123,7 +123,7 @@ export class CheckpointDB implements DB { * @inheritDoc */ async batch(opStack: BatchDBOp[]): Promise { - if (this.isCheckpoint) { + if (this.hasCheckpoints()) { for (const op of opStack) { if (op.type === 'put') { await this.put(op.key, op.value) diff --git a/packages/trie/src/trie/trie.ts b/packages/trie/src/trie/trie.ts index c4f80fe0b4..b21250cf0e 100644 --- a/packages/trie/src/trie/trie.ts +++ b/packages/trie/src/trie/trie.ts @@ -750,7 +750,7 @@ export class Trie { useHashedKeys: this._useHashedKeys, useHashedKeysFunction: this._useHashedKeysFunction, }) - if (includeCheckpoints && this.isCheckpoint) { + if (includeCheckpoints && this.hasCheckpoints()) { trie.db.checkpoints = [...this.db.checkpoints] } return trie @@ -803,8 +803,8 @@ export class Trie { /** * Is the trie during a checkpoint phase? */ - get isCheckpoint() { - return this.db.isCheckpoint + hasCheckpoints() { + return this.db.hasCheckpoints() } /** @@ -821,7 +821,7 @@ export class Trie { * @throws If not during a checkpoint phase */ async commit(): Promise { - if (!this.isCheckpoint) { + if (!this.hasCheckpoints()) { throw new Error('trying to commit when not checkpointed') } @@ -837,7 +837,7 @@ export class Trie { * parent checkpoint as current. */ async revert(): Promise { - if (!this.isCheckpoint) { + if (!this.hasCheckpoints()) { throw new Error('trying to revert when not checkpointed') } diff --git a/packages/trie/test/trie/checkpoint.spec.ts b/packages/trie/test/trie/checkpoint.spec.ts index 657a3e6895..8e51290d31 100644 --- a/packages/trie/test/trie/checkpoint.spec.ts +++ b/packages/trie/test/trie/checkpoint.spec.ts @@ -31,7 +31,7 @@ tape('testing checkpoints', function (tester) { it('should create a checkpoint', function (t) { trie.checkpoint() - t.ok(trie.isCheckpoint) + t.ok(trie.hasCheckpoints()) t.end() }) @@ -58,7 +58,7 @@ tape('testing checkpoints', function (tester) { trieCopy = trie.copy() t.equal(trieCopy.root.toString('hex'), postRoot) t.equal(trieCopy.db.checkpoints.length, 1) - t.ok(trieCopy.isCheckpoint) + t.ok(trieCopy.hasCheckpoints()) const res = await trieCopy.get(Buffer.from('do')) t.ok(Buffer.from('verb').equals(Buffer.from(res!))) const res2 = await trieCopy.get(Buffer.from('love')) @@ -83,10 +83,10 @@ tape('testing checkpoints', function (tester) { }) it('should revert to the orginal root', async function (t) { - t.ok(trie.isCheckpoint) + t.ok(trie.hasCheckpoints()) await trie.revert() t.equal(trie.root.toString('hex'), preRoot) - t.notOk(trie.isCheckpoint) + t.notOk(trie.hasCheckpoints()) t.end() }) @@ -101,7 +101,7 @@ tape('testing checkpoints', function (tester) { await trie.put(Buffer.from('test'), Buffer.from('something')) await trie.put(Buffer.from('love'), Buffer.from('emotion')) await trie.commit() - t.equal(trie.isCheckpoint, false) + t.equal(trie.hasCheckpoints(), false) t.equal(trie.root.toString('hex'), postRoot) t.end() }) @@ -120,7 +120,7 @@ tape('testing checkpoints', function (tester) { await trie.put(Buffer.from('the feels'), Buffer.from('emotion')) await trie.revert() await trie.commit() - t.equal(trie.isCheckpoint, false) + t.equal(trie.hasCheckpoints(), false) t.equal(trie.root.toString('hex'), root.toString('hex')) t.end() })