/
CheckpointTrie.ts
163 lines (125 loc) · 4.18 KB
/
CheckpointTrie.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2015-2018 authors & contributors of https://github.com/ethereumjs/merkle-patricia-tree
// Copyright 2017-2018 @polkadot/trie-db authors & contributors
// This software may be modified and distributed under the terms
// of the MPL-2.0 license. See the LICENSE file for details.
import { HashFn } from './types';
// @ts-ignore FIXME, we need to properly check the full file
import levelws from 'level-ws';
// @ts-ignore FIXME, we need to properly check the full file
import levelup from 'levelup';
import memdown from 'memdown';
import assert from '@polkadot/util/assert';
// import logger from '@polkadot/util/logger';
// import u8aToHex from '@polkadot/util/u8a/toHex';
import BaseTrie from './BaseTrie';
import encoder from './encoder';
import ScratchReadStream from './streams/ScratchRead';
// const l = logger('trie/checkpt');
function putRaw (key: Uint8Array, value: Uint8Array) {
return Promise.all(
// @ts-ignore FIXME, we need to properly check the full file
this.__putDBs.map((db) => {
return db.put(key, value);
})
);
}
export default class CheckpointTrie extends BaseTrie {
_checkpoints: Array<Uint8Array>;
_scratch: any; // FIXME
// @ts-ignore FIXME, we need to properly check the full file
__putDBs: any[]; // tslint:disable-line
// @ts-ignore FIXME, we need to properly check the full file
constructor (db: any, root?: Uint8Array, hashing?: HashFn) {
super(db, root, hashing);
this._scratch = null;
this._checkpoints = [];
// l.debug(() => 'Created CheckpointTrie');
}
get isCheckpoint () {
return !!this._checkpoints.length;
}
copy () {
// l.debug(() => ['Copying CheckpointTrie', typeof this.dbDown, u8aToHex(this.root)]);
return new CheckpointTrie(this.dbDown, this.root, this.hashing);
}
async checkpoint () {
// l.debug(() => 'Marking checkpoint');
const wasCheckpoint = this.isCheckpoint;
this._checkpoints.push(this.root);
if (!wasCheckpoint && this.isCheckpoint) {
this._enterCpMode();
}
}
commit () {
// l.debug(() => 'Committing checkpoint');
return this.semaphore(async () => {
assert(this.isCheckpoint, 'trying to commit when not checkpointed');
this._checkpoints.pop();
if (this.isCheckpoint) {
return;
}
return this._exitCpMode(true);
});
}
revert () {
// l.debug(() => 'Reverting checkpoint');
return this.semaphore(async () => {
if (!this.isCheckpoint) {
return;
}
this.root = this._checkpoints.pop() as Uint8Array;
if (this.isCheckpoint) {
return;
}
return this._exitCpMode(false);
});
}
// @ts-ignore FIXME, we need to properly check the full file
createScratchReadStream (scratch) {
const trie = this.copy();
scratch = scratch || this._scratch;
// only read from the scratch
trie._getDBs = [scratch];
trie._scratch = scratch;
return new ScratchReadStream(trie);
}
_enterCpMode () {
// l.debug(() => 'Entering checkpoint mode');
this._scratch = levelup(encoder(memdown()));
this._getDBs = [this._scratch].concat(this._getDBs);
this.__putDBs = this._putDBs;
this._putDBs = [this._scratch];
this._putRaw = this.putRaw;
this.putRaw = putRaw;
}
// @ts-ignore FIXME, we need to properly check the full file
_exitCpMode (commitState) {
// l.debug(() => 'Exiting checkpoint mode');
const scratch = this._scratch;
this._scratch = null;
this._getDBs = this._getDBs.slice(1);
this._putDBs = this.__putDBs;
this.putRaw = this._putRaw;
if (!commitState) {
return;
}
return Promise.all(
this._putDBs.map((db) => {
return new Promise((resolve) => {
if (!db.createWriteStream) {
db = levelws(db);
}
this
// @ts-ignore FIXME, we need to properly check the full file
.createScratchReadStream(scratch)
// @ts-ignore FIXME, we need to properly check the full file
.pipe(
// @ts-ignore FIXME, we need to properly check the full file
db.createWriteStream(encoder.options)
)
.on('close', resolve);
});
})
);
}
}