Skip to content

Commit fecbbd7

Browse files
committed
trie -> new CP mechanism: removed all scratchDB related logic from CheckpointTrie, basic follow-up VM test fixes
1 parent e949bb1 commit fecbbd7

File tree

6 files changed

+16
-53
lines changed

6 files changed

+16
-53
lines changed

packages/trie/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"tsc": "ethereumjs-config-tsc",
2222
"test": "npm run test:node && npm run test:browser",
2323
"test:browser": "npm run build && karma start karma.conf.js",
24-
"test:node": "npm run build && tape -r ts-node/register test/*.ts"
24+
"test:node": "tape -r ts-node/register test/*.ts"
2525
},
2626
"author": {
2727
"name": "mjbecze",

packages/trie/src/checkpointTrie.ts

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
import { Trie as BaseTrie } from './baseTrie'
2-
import { ScratchReadStream } from './scratchReadStream'
3-
import { ScratchDB } from './scratch'
4-
import { DB } from './db'
5-
const WriteStream = require('level-ws')
62

73
/**
84
* Adds checkpointing to the {@link BaseTrie}
95
*/
106
export class CheckpointTrie extends BaseTrie {
11-
_mainDB: DB
12-
_scratch: ScratchDB | null
137
_checkpoints: Buffer[]
148

159
constructor(...args: any) {
1610
super(...args)
17-
// Reference to main DB instance
18-
this._mainDB = this.db
19-
// DB instance used for checkpoints
20-
this._scratch = null
2111
// Roots of trie at the moment of checkpoint
2212
this._checkpoints = []
2313
}
@@ -31,8 +21,7 @@ export class CheckpointTrie extends BaseTrie {
3121

3222
/**
3323
* Creates a checkpoint that can later be reverted to or committed.
34-
* After this is called, no changes to the trie will be permanently saved until `commit` is called.
35-
* To override the checkpointing mechanism use `_maindb.put` to write directly write to db.
24+
* After this is called, all changes can be reverted until `commit` is called.
3625
*/
3726
checkpoint() {
3827
const wasCheckpoint = this.isCheckpoint
@@ -88,12 +77,10 @@ export class CheckpointTrie extends BaseTrie {
8877
* @param includeCheckpoints - If true and during a checkpoint, the copy will contain the checkpointing metadata and will use the same scratch as underlying db.
8978
*/
9079
copy(includeCheckpoints = true): CheckpointTrie {
91-
const db = this._mainDB.copy()
80+
const db = this.db.copy()
9281
const trie = new CheckpointTrie(db._leveldb, this.root)
9382
if (includeCheckpoints && this.isCheckpoint) {
9483
trie._checkpoints = this._checkpoints.slice()
95-
trie._scratch = this._scratch!.copy()
96-
trie.db = trie._scratch
9784
}
9885
return trie
9986
}
@@ -102,43 +89,19 @@ export class CheckpointTrie extends BaseTrie {
10289
* Enter into checkpoint mode.
10390
* @private
10491
*/
105-
_enterCpMode() {
106-
this._scratch = new ScratchDB(this._mainDB)
107-
this.db = this._scratch
108-
}
92+
_enterCpMode() {}
10993

11094
/**
11195
* Exit from checkpoint mode.
11296
* @private
11397
*/
11498
async _exitCpMode(commitState: boolean): Promise<void> {
11599
return new Promise((resolve) => {
116-
const scratch = this._scratch as ScratchDB
117-
this._scratch = null
118-
this.db = this._mainDB
119-
120-
if (commitState) {
121-
this._createScratchReadStream(scratch)
122-
.pipe(WriteStream(this.db._leveldb))
123-
.on('close', resolve)
100+
/*if (commitState) {
124101
} else {
125-
process.nextTick(resolve)
126-
}
102+
}*/
103+
commitState = !!commitState // Temporary dummy line for linting
104+
resolve()
127105
})
128106
}
129-
130-
/**
131-
* Returns a `ScratchReadStream` based on the state updates
132-
* since checkpoint.
133-
* @private
134-
*/
135-
_createScratchReadStream(scratchDb?: ScratchDB) {
136-
const scratch = scratchDb || this._scratch
137-
if (!scratch) {
138-
throw new Error('No scratch found to use')
139-
}
140-
const trie = new BaseTrie(scratch._leveldb, this.root)
141-
trie.db = scratch
142-
return new ScratchReadStream(trie)
143-
}
144107
}

packages/trie/test/checkpoint.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tape('testing checkpoints', function (tester) {
1717
t.end()
1818
})
1919

20-
it('should copy trie and get value before checkpoint', async function (t) {
20+
it('should copy trie and get value added to original trie', async function (t) {
2121
trieCopy = trie.copy()
2222
t.equal(trieCopy.root.toString('hex'), preRoot)
2323
const res = await trieCopy.get(Buffer.from('do'))

packages/trie/test/stream.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,21 @@ tape('db stream test', function (tester) {
156156
},
157157
] as BatchDBOp[]
158158

159-
const expectedNodes = {
159+
/*const expectedNodes = {
160160
'3c38d9aa6ad288c8e27da701e17fe99a5b67c8b12fd0469651c80494d36bc4c1': true,
161161
d5f61e1ff2b918d1c2a2c4b1732a3c68bd7e3fd64f35019f2f084896d4546298: true,
162162
e64329dadee2fb8a113b4c88cfe973aeaa9b523d4dc8510b84ca23f9d5bfbd90: true,
163163
c916d458bfb5f27603c5bd93b00f022266712514a59cde749f19220daffc743f: true,
164164
'2386bfb0de9cf93902a110f5ab07b917ffc0b9ea599cb7f4f8bb6fd1123c866c': true,
165-
} as any
165+
} as any*/
166166

167167
it('should populate trie', async function (t) {
168168
trie.checkpoint()
169169
await trie.batch(ops)
170170
t.end()
171171
})
172172

173-
it('should only fetch nodes in the current trie', function (t) {
173+
/*it('should only fetch nodes in the current trie', function (t) {
174174
const stream = trie._createScratchReadStream()
175175
stream.on('data', (d: any) => {
176176
const key = d.key.toString('hex')
@@ -182,5 +182,5 @@ tape('db stream test', function (tester) {
182182
t.equal(keys.length, 0)
183183
t.end()
184184
})
185-
})
185+
})*/
186186
})

packages/vm/lib/state/stateManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export default class DefaultStateManager implements StateManager {
129129
return
130130
}
131131

132-
await this._trie._mainDB.put(codeHash, value)
132+
await this._trie.db.put(codeHash, value)
133133

134134
const account = await this.getAccount(address)
135135
account.codeHash = codeHash
@@ -147,7 +147,7 @@ export default class DefaultStateManager implements StateManager {
147147
if (!account.isContract()) {
148148
return Buffer.alloc(0)
149149
}
150-
const code = await this._trie._mainDB.get(account.codeHash)
150+
const code = await this._trie.db.get(account.codeHash)
151151
return code || Buffer.alloc(0)
152152
}
153153

packages/vm/tests/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export async function setupPreConditions(state: any, testData: any) {
326326
}
327327

328328
// Put contract code
329-
await state._mainDB.put(codeHash, codeBuf)
329+
await state.db.put(codeHash, codeBuf)
330330

331331
// Put account data
332332
const account = Account.fromAccountData({ nonce, balance, codeHash, stateRoot })

0 commit comments

Comments
 (0)