Skip to content
This repository was archived by the owner on Jan 19, 2021. It is now read-only.

Commit 3de44d1

Browse files
authored
Merge pull request #119 from ethereumjs/include-checkpoints-in-secure-copy
Include checkpoints by default in SecureTrie.copy
2 parents c349e93 + 5f61cb2 commit 3de44d1

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ See the [docs](https://github.com/ethereumjs/merkle-patricia-tree/tree/master/do
3030

3131
`getRaw`, `putRaw` and `delRaw` were deprecated in `v3.0.0` and have been removed from this release. Instead, please use `trie.db.get`, `trie.db.put`, and `trie.db.del`. If using a `SecureTrie` or `CheckpointTrie`, use `trie._maindb` to override the checkpointing mechanism and interact directly with the db.
3232

33+
#### SecureTrie.copy
34+
35+
`SecureTrie.copy` now includes checkpoint metadata by default. To maintain original behavior of _not_ copying checkpoint state, pass `false` to param `includeCheckpoints`.
36+
3337
### Changed
3438

3539
- Convert trieNode to ES6 class ([#71](https://github.com/ethereumjs/merkle-patricia-tree/pull/71))
@@ -42,6 +46,7 @@ See the [docs](https://github.com/ethereumjs/merkle-patricia-tree/tree/master/do
4246
- Use `Nibbles` type for `number[]` ([#115](https://github.com/ethereumjs/merkle-patricia-tree/pull/115))
4347
- Upgrade ethereumjs-util to 7.0.0 / Upgrade level-mem to 5.0.1 ([#116](https://github.com/ethereumjs/merkle-patricia-tree/pull/116))
4448
- Create dual ES5 and ES2017 builds ([#117](https://github.com/ethereumjs/merkle-patricia-tree/pull/117))
49+
- Include checkpoints by default in SecureTrie.copy ([#119](https://github.com/ethereumjs/merkle-patricia-tree/pull/119))
4550

4651
### Added
4752

src/checkpointTrie.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,10 @@ export class CheckpointTrie extends BaseTrie {
8181
}
8282

8383
/**
84-
* Returns a copy of the underlying trie with the interface
85-
* of CheckpointTrie. If during a checkpoint, the copy will
86-
* contain the checkpointing metadata (incl. reference to the same scratch).
87-
* @param {boolean} includeCheckpoints - If true and during a checkpoint, the copy will
88-
* contain the checkpointing metadata and will use the same scratch as underlying db.
84+
* Returns a copy of the underlying trie with the interface of CheckpointTrie.
85+
* @param {boolean} includeCheckpoints - If true and during a checkpoint, the copy will contain the checkpointing metadata and will use the same scratch as underlying db.
8986
*/
90-
copy(includeCheckpoints: boolean = true): CheckpointTrie {
87+
copy(includeCheckpoints = true): CheckpointTrie {
9188
const db = this._mainDB.copy()
9289
const trie = new CheckpointTrie(db._leveldb, this.root)
9390
if (includeCheckpoints && this.isCheckpoint) {

src/secure.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ export class SecureTrie extends CheckpointTrie {
2424
return super.verifyProof(rootHash, hash, proof)
2525
}
2626

27-
copy(): SecureTrie {
28-
const trie = super.copy(false)
27+
/**
28+
* Returns a copy of the underlying trie with the interface of SecureTrie.
29+
* @param {boolean} includeCheckpoints - If true and during a checkpoint, the copy will contain the checkpointing metadata and will use the same scratch as underlying db.
30+
*/
31+
copy(includeCheckpoints = true): SecureTrie {
32+
const trie = super.copy(includeCheckpoints)
2933
const db = trie.db.copy()
3034
return new SecureTrie(db._leveldb, this.root)
3135
}

test/secure.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,29 @@ tape('secure tests should not crash', async function (t) {
124124
await trie.put(gk, g)
125125
t.end()
126126
})
127+
128+
tape('SecureTrie.copy', function (it) {
129+
it.test('created copy includes values added after checkpoint', async function (t) {
130+
const trie = new SecureTrie()
131+
132+
await trie.put(Buffer.from('key1'), Buffer.from('value1'))
133+
trie.checkpoint()
134+
await trie.put(Buffer.from('key2'), Buffer.from('value2'))
135+
const trieCopy = trie.copy()
136+
const value = await trieCopy.get(Buffer.from('key2'))
137+
t.equal(value.toString(), 'value2')
138+
t.end()
139+
})
140+
141+
it.test('created copy includes values added before checkpoint', async function (t) {
142+
const trie = new SecureTrie()
143+
144+
await trie.put(Buffer.from('key1'), Buffer.from('value1'))
145+
trie.checkpoint()
146+
await trie.put(Buffer.from('key2'), Buffer.from('value2'))
147+
const trieCopy = trie.copy()
148+
const value = await trieCopy.get(Buffer.from('key1'))
149+
t.equal(value.toString(), 'value1')
150+
t.end()
151+
})
152+
})

0 commit comments

Comments
 (0)