-
Notifications
You must be signed in to change notification settings - Fork 756
/
stateManager.js
328 lines (291 loc) · 7.65 KB
/
stateManager.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
const Buffer = require('safe-buffer').Buffer
const Trie = require('merkle-patricia-tree/secure.js')
const common = require('ethereum-common')
const async = require('async')
const Account = require('ethereumjs-account')
const fakeBlockchain = require('./fakeBlockChain.js')
const Cache = require('./cache.js')
const utils = require('ethereumjs-util')
const BN = utils.BN
const rlp = utils.rlp
module.exports = StateManager
function StateManager (opts) {
var self = this
var trie = opts.trie
if (!trie) {
trie = new Trie(trie)
}
var blockchain = opts.blockchain
if (!blockchain) {
blockchain = fakeBlockchain
}
self.blockchain = blockchain
self.trie = trie
self._storageTries = {} // the storage trie cache
self.cache = new Cache(trie)
self.touched = []
}
var proto = StateManager.prototype
// gets the account from the cache, or triggers a lookup and stores
// the result in the cache
proto.getAccount = function (address, cb) {
this.cache.getOrLoad(address, cb)
}
// checks if an account exists
proto.exists = function (address, cb) {
this.cache.getOrLoad(address, function (err, account) {
cb(err, account.exists)
})
}
// saves the account
proto._putAccount = function (address, account, cb) {
var self = this
var addressHex = Buffer.from(address, 'hex')
// TODO: dont save newly created accounts that have no balance
// if (toAccount.balance.toString('hex') === '00') {
// if they have money or a non-zero nonce or code, then write to tree
self.cache.put(addressHex, account)
self.touched.push(address)
// self.trie.put(addressHex, account.serialize(), cb)
cb()
}
proto.getAccountBalance = function (address, cb) {
var self = this
self.getAccount(address, function (err, account) {
if (err) {
return cb(err)
}
cb(null, account.balance)
})
}
proto.putAccountBalance = function (address, balance, cb) {
var self = this
self.getAccount(address, function (err, account) {
if (err) {
return cb(err)
}
if ((new BN(balance)).isZero() && !account.exists) {
return cb(null)
}
account.balance = balance
self._putAccount(address, account, cb)
})
}
// sets the contract code on the account
proto.putContractCode = function (address, value, cb) {
var self = this
self.getAccount(address, function (err, account) {
if (err) {
return cb(err)
}
// TODO: setCode use trie.setRaw which creates a storage leak
account.setCode(self.trie, value, function (err) {
if (err) {
return cb(err)
}
self._putAccount(address, account, cb)
})
})
}
// given an account object, returns the code
proto.getContractCode = function (address, cb) {
var self = this
self.getAccount(address, function (err, account) {
if (err) {
return cb(err)
}
account.getCode(self.trie, cb)
})
}
// creates a storage trie from the primary storage trie
proto._lookupStorageTrie = function (address, cb) {
var self = this
// from state trie
self.getAccount(address, function (err, account) {
if (err) {
return cb(err)
}
var storageTrie = self.trie.copy()
storageTrie.root = account.stateRoot
self.touched.push(address)
storageTrie._checkpoints = []
cb(null, storageTrie)
})
}
// gets the storage trie from the storage cache or does lookup
proto._getStorageTrie = function (address, cb) {
var self = this
var storageTrie = self._storageTries[address.toString('hex')]
// from storage cache
if (storageTrie) {
return cb(null, storageTrie)
}
// lookup from state
self._lookupStorageTrie(address, cb)
}
proto.getContractStorage = function (address, key, cb) {
var self = this
self._getStorageTrie(address, function (err, trie) {
if (err) {
return cb(err)
}
trie.get(key, function (err, value) {
if (err) {
return cb(err)
}
var decoded = rlp.decode(value)
cb(null, decoded)
})
})
}
proto.putContractStorage = function (address, key, value, cb) {
var self = this
self._getStorageTrie(address, function (err, storageTrie) {
if (err) {
return cb(err)
}
if (value && value.length) {
// format input
var encodedValue = rlp.encode(value)
storageTrie.put(key, encodedValue, finalize)
} else {
// deleting a value
storageTrie.del(key, finalize)
}
function finalize (err) {
if (err) return cb(err)
// update storage cache
self._storageTries[address.toString('hex')] = storageTrie
// update contract stateRoot
var contract = self.cache.get(address)
contract.stateRoot = storageTrie.root
self._putAccount(address, contract, cb)
self.touched.push(address)
}
})
}
proto.commitContracts = function (cb) {
var self = this
async.each(Object.keys(self._storageTries), function (address, cb) {
var trie = self._storageTries[address]
delete self._storageTries[address]
// TODO: this is broken on the block level; all the contracts get written to
// disk redardless of whether or not the block is valid
if (trie.isCheckpoint) {
trie.commit(cb)
} else {
cb()
}
}, cb)
}
proto.revertContracts = function () {
var self = this
self._storageTries = {}
}
//
// blockchain
//
proto.getBlockHash = function (number, cb) {
var self = this
self.blockchain.getBlock(number, function (err, block) {
if (err) {
return cb(err)
}
var blockHash = block.hash()
cb(null, blockHash)
})
}
//
// revision history
//
proto.checkpoint = function () {
var self = this
self.trie.checkpoint()
self.cache.checkpoint()
}
proto.commit = function (cb) {
var self = this
// setup trie checkpointing
self.trie.commit(function () {
// setup cache checkpointing
self.cache.commit()
cb()
})
}
proto.revert = function (cb) {
var self = this
// setup trie checkpointing
self.trie.revert()
// setup cache checkpointing
self.cache.revert()
cb()
}
//
// cache stuff
//
proto.getStateRoot = function (cb) {
var self = this
self.cacheFlush(function (err) {
if (err) {
return cb(err)
}
var stateRoot = self.trie.root
cb(null, stateRoot)
})
}
/**
* @param {Set} address
* @param {cb} function
*/
proto.warmCache = function (addresses, cb) {
this.cache.warm(addresses, cb)
}
proto.dumpStorage = function (address, cb) {
var self = this
self._getStorageTrie(address, function (err, trie) {
if (err) {
return cb(err)
}
var storage = {}
var stream = trie.createReadStream()
stream.on('data', function (val) {
storage[val.key.toString('hex')] = val.value.toString('hex')
})
stream.on('end', function () {
cb(storage)
})
})
}
proto.hasGenesisState = function (cb) {
const root = common.genesisStateRoot.v
this.trie.checkRoot(root, cb)
}
proto.generateCanonicalGenesis = function (cb) {
var self = this
this.hasGenesisState(function (err, genesis) {
if (!genesis && !err) {
self.generateGenesis(common.genesisState, cb)
} else {
cb(err)
}
})
}
proto.generateGenesis = function (initState, cb) {
var self = this
var addresses = Object.keys(initState)
async.eachSeries(addresses, function (address, done) {
var account = new Account()
account.balance = new BN(initState[address]).toArrayLike(Buffer)
address = Buffer.from(address, 'hex')
self.trie.put(address, account.serialize(), done)
}, cb)
}
proto.accountIsEmpty = function (address, cb) {
var self = this
self.getAccount(address, function (err, account) {
if (err) {
return cb(err)
}
cb(null, account.nonce.toString('hex') === '' && account.balance.toString('hex') === '' && account.codeHash.toString('hex') === utils.SHA3_NULL_S)
})
}