Skip to content

Commit 84d4f8c

Browse files
committed
trie -> new CP mechanism: expanded checkpoints data structure, moved to DB
1 parent fecbbd7 commit 84d4f8c

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

packages/trie/src/checkpointTrie.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,15 @@ import { Trie as BaseTrie } from './baseTrie'
44
* Adds checkpointing to the {@link BaseTrie}
55
*/
66
export class CheckpointTrie extends BaseTrie {
7-
_checkpoints: Buffer[]
8-
97
constructor(...args: any) {
108
super(...args)
11-
// Roots of trie at the moment of checkpoint
12-
this._checkpoints = []
139
}
1410

1511
/**
1612
* Is the trie during a checkpoint phase?
1713
*/
1814
get isCheckpoint() {
19-
return this._checkpoints.length > 0
15+
return this.db.checkpoints.length > 0
2016
}
2117

2218
/**
@@ -25,7 +21,7 @@ export class CheckpointTrie extends BaseTrie {
2521
*/
2622
checkpoint() {
2723
const wasCheckpoint = this.isCheckpoint
28-
this._checkpoints.push(this.root)
24+
this.db.checkpoints.push({ root: this.root, operations: [] })
2925

3026
// Entering checkpoint mode is not necessary for nested checkpoints
3127
if (!wasCheckpoint && this.isCheckpoint) {
@@ -45,7 +41,7 @@ export class CheckpointTrie extends BaseTrie {
4541

4642
await this.lock.wait()
4743

48-
this._checkpoints.pop()
44+
this.db.checkpoints.pop()
4945
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
5046
if (!this.isCheckpoint) {
5147
await this._exitCpMode(true)
@@ -63,7 +59,8 @@ export class CheckpointTrie extends BaseTrie {
6359
await this.lock.wait()
6460
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
6561
if (this.isCheckpoint) {
66-
this.root = this._checkpoints.pop()!
62+
const { root } = this.db.checkpoints.pop()!
63+
this.root = root
6764
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
6865
if (!this.isCheckpoint) {
6966
await this._exitCpMode(false)
@@ -80,7 +77,7 @@ export class CheckpointTrie extends BaseTrie {
8077
const db = this.db.copy()
8178
const trie = new CheckpointTrie(db._leveldb, this.root)
8279
if (includeCheckpoints && this.isCheckpoint) {
83-
trie._checkpoints = this._checkpoints.slice()
80+
trie.db.checkpoints = this.db.checkpoints.slice()
8481
}
8582
return trie
8683
}

packages/trie/src/db.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ const level = require('level-mem')
33

44
export const ENCODING_OPTS = { keyEncoding: 'binary', valueEncoding: 'binary' }
55

6+
export type Checkpoint = {
7+
root: Buffer
8+
operations: any[]
9+
}
10+
611
export type BatchDBOp = PutBatch | DelBatch
712
export interface PutBatch {
813
type: 'put'
@@ -19,6 +24,8 @@ export interface DelBatch {
1924
* which validates inputs and sets encoding type.
2025
*/
2126
export class DB {
27+
public checkpoints: Checkpoint[]
28+
2229
_leveldb: LevelUp
2330

2431
/**
@@ -27,6 +34,9 @@ export class DB {
2734
* @param leveldb - An abstract-leveldown compliant store
2835
*/
2936
constructor(leveldb?: LevelUp) {
37+
// Roots of trie at the moment of checkpoint
38+
this.checkpoints = []
39+
3040
this._leveldb = leveldb || level()
3141
}
3242

packages/trie/test/checkpoint.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ tape('testing checkpoints', function (tester) {
5353
it('should copy trie and get upstream and cache values after checkpoint', async function (t) {
5454
trieCopy = trie.copy()
5555
t.equal(trieCopy.root.toString('hex'), postRoot)
56-
t.equal(trieCopy._checkpoints.length, 1)
56+
t.equal(trieCopy.db.checkpoints.length, 1)
5757
t.ok(trieCopy.isCheckpoint)
5858
const res = await trieCopy.get(Buffer.from('do'))
5959
t.ok(Buffer.from('verb').equals(Buffer.from(res!)))

0 commit comments

Comments
 (0)