Skip to content

Commit 827a184

Browse files
trie: binary encode checkpointTrie keys
1 parent a569e6d commit 827a184

File tree

3 files changed

+10
-14
lines changed

3 files changed

+10
-14
lines changed

packages/trie/src/checkpointDb.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { LevelUp } from 'levelup'
2-
import { DB, BatchDBOp } from './db'
3-
4-
export const ENCODING_OPTS = { keyEncoding: 'binary', valueEncoding: 'binary' }
2+
import { DB, BatchDBOp, ENCODING_OPTS } from './db'
53

64
export type Checkpoint = {
75
// We cannot use a Buffer => Buffer map directly. If you create two Buffers with the same internal value,
@@ -55,12 +53,12 @@ export class CheckpointDB extends DB {
5553
if (value === null) {
5654
batchOp.push({
5755
type: 'del',
58-
key: Buffer.from(key, 'hex'),
56+
key: Buffer.from(key, 'binary'),
5957
})
6058
} else {
6159
batchOp.push({
6260
type: 'put',
63-
key: Buffer.from(key, 'hex'),
61+
key: Buffer.from(key, 'binary'),
6462
value,
6563
})
6664
}
@@ -69,9 +67,7 @@ export class CheckpointDB extends DB {
6967
} else {
7068
// dump everything into the current (higher level) cache
7169
const currentKeyValueMap = this.checkpoints[this.checkpoints.length - 1].keyValueMap
72-
keyValueMap.forEach(function (value, key) {
73-
currentKeyValueMap.set(key, value)
74-
})
70+
keyValueMap.forEach((value, key) => currentKeyValueMap.set(key, value))
7571
}
7672
}
7773

@@ -91,7 +87,7 @@ export class CheckpointDB extends DB {
9187
async get(key: Buffer): Promise<Buffer | null> {
9288
// Lookup the value in our cache. We return the latest checkpointed value (which should be the value on disk)
9389
for (let index = this.checkpoints.length - 1; index >= 0; index--) {
94-
const value = this.checkpoints[index].keyValueMap.get(key.toString('hex'))
90+
const value = this.checkpoints[index].keyValueMap.get(key.toString('binary'))
9591
if (value !== undefined) {
9692
return value
9793
}
@@ -101,7 +97,7 @@ export class CheckpointDB extends DB {
10197
const value = await super.get(key)
10298
if (this.isCheckpoint) {
10399
// Since we are a checkpoint, put this value in cache, so future `get` calls will not look the key up again from disk.
104-
this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('hex'), value)
100+
this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('binary'), value)
105101
}
106102

107103
return value
@@ -115,7 +111,7 @@ export class CheckpointDB extends DB {
115111
async put(key: Buffer, val: Buffer): Promise<void> {
116112
if (this.isCheckpoint) {
117113
// put value in cache
118-
this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('hex'), val)
114+
this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('binary'), val)
119115
} else {
120116
await super.put(key, val)
121117
}
@@ -128,7 +124,7 @@ export class CheckpointDB extends DB {
128124
async del(key: Buffer): Promise<void> {
129125
if (this.isCheckpoint) {
130126
// delete the value in the current cache
131-
this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('hex'), null)
127+
this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('binary'), null)
132128
} else {
133129
// delete the value on disk
134130
await this._leveldb.del(key, ENCODING_OPTS)

packages/trie/src/checkpointTrie.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class CheckpointTrie extends BaseTrie {
6565
const db = this.db.copy()
6666
const trie = new CheckpointTrie(db._leveldb, this.root)
6767
if (includeCheckpoints && this.isCheckpoint) {
68-
trie.db.checkpoints = this.db.checkpoints.slice()
68+
trie.db.checkpoints = [...this.db.checkpoints]
6969
}
7070
return trie
7171
}

packages/trie/src/secure.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class SecureTrie extends CheckpointTrie {
9292
const db = this.db.copy()
9393
const secureTrie = new SecureTrie(db._leveldb, this.root)
9494
if (includeCheckpoints && this.isCheckpoint) {
95-
secureTrie.db.checkpoints = this.db.checkpoints.slice()
95+
secureTrie.db.checkpoints = [...this.db.checkpoints]
9696
}
9797
return secureTrie
9898
}

0 commit comments

Comments
 (0)