Skip to content

Commit

Permalink
Merge branch 'master' into combine-node-classes
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 authored Aug 24, 2022
2 parents 041cd3a + 7c0b0ad commit 25543dd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
12 changes: 6 additions & 6 deletions packages/trie/src/db/checkpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export class CheckpointDB implements DB {
/**
* Is the DB during a checkpoint phase?
*/
get isCheckpoint() {
hasCheckpoints() {
return this.checkpoints.length > 0
}

Expand All @@ -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()) {
Expand Down Expand Up @@ -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)
}
Expand All @@ -98,7 +98,7 @@ export class CheckpointDB implements DB {
* @inheritDoc
*/
async put(key: Buffer, val: Buffer): Promise<void> {
if (this.isCheckpoint) {
if (this.hasCheckpoints()) {
// put value in cache
this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('binary'), val)
} else {
Expand All @@ -110,7 +110,7 @@ export class CheckpointDB implements DB {
* @inheritDoc
*/
async del(key: Buffer): Promise<void> {
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 {
Expand All @@ -123,7 +123,7 @@ export class CheckpointDB implements DB {
* @inheritDoc
*/
async batch(opStack: BatchDBOp[]): Promise<void> {
if (this.isCheckpoint) {
if (this.hasCheckpoints()) {
for (const op of opStack) {
if (op.type === 'put') {
await this.put(op.key, op.value)
Expand Down
10 changes: 5 additions & 5 deletions packages/trie/src/trie/trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

/**
Expand All @@ -821,7 +821,7 @@ export class Trie {
* @throws If not during a checkpoint phase
*/
async commit(): Promise<void> {
if (!this.isCheckpoint) {
if (!this.hasCheckpoints()) {
throw new Error('trying to commit when not checkpointed')
}

Expand All @@ -837,7 +837,7 @@ export class Trie {
* parent checkpoint as current.
*/
async revert(): Promise<void> {
if (!this.isCheckpoint) {
if (!this.hasCheckpoints()) {
throw new Error('trying to revert when not checkpointed')
}

Expand Down
12 changes: 6 additions & 6 deletions packages/trie/test/trie/checkpoint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand All @@ -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'))
Expand All @@ -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()
})

Expand All @@ -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()
})
Expand All @@ -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()
})
Expand Down

0 comments on commit 25543dd

Please sign in to comment.