This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy pathblockchain_double.js
1217 lines (1017 loc) · 35.2 KB
/
blockchain_double.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var to = require("./utils/to.js");
var Account = require("ethereumjs-account");
var Block = require("ethereumjs-block");
var Log = require("./utils/log");
var Receipt = require("./utils/receipt");
var VM = require("ethereumjs-vm");
var RuntimeError = require("./utils/runtimeerror");
var Trie = require("merkle-patricia-tree");
var utils = require("ethereumjs-util");
var async = require("async");
var Heap = require("heap");
var Database = require("./database");
var EventEmitter = require("events");
var estimateGas = require("./utils/gasEstimation");
var _ = require("lodash");
var promisify = require("util").promisify;
const BN = require("bn.js");
function BlockchainDouble(options) {
var self = this;
EventEmitter.apply(self);
this.options = options = this._applyDefaultOptions(options || {});
this.logger = options.logger || console;
this.data = new Database(options);
if (options.trie != null && options.db_path != null) {
throw new Error("Can't initialize a TestRPC with a db and a custom trie.");
}
this.pending_transactions = [];
// updated periodically to keep up with the times
this.blockGasLimit = options.gasLimit;
this.defaultTransactionGasLimit = options.defaultTransactionGasLimit;
this.timeAdjustment = 0;
}
const defaultOptions = {
gasLimit: "0x6691b7",
defaultTransactionGasLimit: "0x15f90",
time: null,
debug: false,
hardfork: "petersburg",
allowUnlimitedContractSize: false
};
// inheritence w/ prototype chaining
BlockchainDouble.prototype = Object.create(EventEmitter.prototype);
BlockchainDouble.prototype.constructor = BlockchainDouble;
BlockchainDouble.prototype._applyDefaultOptions = function(options) {
// We want this function to mutate the options object so that we can report
// our settings back to our consumer application (e.g., ganache)
return _.merge(options, defaultOptions, Object.assign({}, options));
};
BlockchainDouble.prototype.initialize = function(accounts, callback) {
var self = this;
this.data.initialize(function(err) {
if (err) {
return callback(err);
}
self.latestBlock(function(err, block) {
if (err) {
return callback(err);
}
var options = self.options;
var root = null;
if (block) {
root = block.header.stateRoot;
}
// I haven't yet found a good way to do this. Getting the trie from the
// forked blockchain without going through the other setup is a little gross.
self.stateTrie = self.createStateTrie(self.data.trie_db, root);
self.vm = options.vm || self.createVMFromStateTrie(self.stateTrie, true);
if (options.time) {
self.setTime(options.time);
}
// If we already have a block, then that means there's an existing chain.
// Don't create a genesis block.
if (block) {
self.emit("block", block);
return callback();
}
self.createGenesisBlock(function(err, block) {
if (err) {
return callback(err);
}
accounts = accounts || [];
self.vm.stateManager.checkpoint(() => {
async.eachSeries(
accounts,
function(accountData, finished) {
self.vm.stateManager.putAccount(utils.toBuffer(accountData.address), accountData.account, finished);
},
function(err) {
if (err) {
return callback(err);
}
self.vm.stateManager.commit(() => {
// Create first block
self.putBlock(block, [], [], callback);
});
}
);
});
});
});
});
};
BlockchainDouble.prototype.createVMFromStateTrie = function(state, activatePrecompiles) {
const self = this;
const vm = new VM({
state: state,
blockchain: {
// EthereumJS VM needs a blockchain object in order to get block information.
// When calling getBlock() it will pass a number that's of a Buffer type.
// Unfortunately, it uses a 64-character buffer (when converted to hex) to
// represent block numbers as well as block hashes. Since it's very unlikely
// any block number will get higher than the maximum safe Javascript integer,
// we can convert this buffer to a number ahead of time before calling our
// own getBlock(). If the conversion succeeds, we have a block number.
// If it doesn't, we have a block hash. (Note: Our implementation accepts both.)
getBlock: function(number, done) {
try {
number = to.number(number);
} catch (e) {
// Do nothing; must be a block hash.
}
self.getBlock(number, done);
}
},
activatePrecompiles: activatePrecompiles || false,
hardfork: self.options.hardfork,
allowUnlimitedContractSize: self.options.allowUnlimitedContractSize
});
if (self.options.debug === true) {
// log executed opcodes, including args as hex
vm.on("step", function(info) {
var name = info.opcode.name;
var argsNum = info.opcode.in;
if (argsNum) {
var args = info.stack
.slice(-argsNum)
.map((arg) => to.hex(arg))
.join(" ");
self.logger.log(`${name} ${args}`);
} else {
self.logger.log(name);
}
});
}
return vm;
};
BlockchainDouble.prototype.createStateTrie = function(db, root) {
return new Trie(db, root);
};
// Overrideable so other implementations (forking) can edit it.
BlockchainDouble.prototype.createGenesisBlock = function(callback) {
this.createBlock(callback);
};
BlockchainDouble.prototype.latestBlock = function(callback) {
this.data.blocks.last(function(err, last) {
if (err) {
return callback(err);
}
callback(null, last);
});
};
// number accepts number (integer, hex) or tag (e.g., "latest")
BlockchainDouble.prototype.getEffectiveBlockNumber = function(number, callback) {
if (typeof number !== "string") {
number = to.hex(number);
}
// If we have a hex number
if (number.indexOf("0x") >= 0) {
return callback(null, to.number(number));
} else {
if (number === "latest" || number === "pending") {
return this.getHeight(callback);
} else if (number === "earliest") {
return callback(null, 0);
}
}
};
// number accepts number (integer, hex), tag (e.g., "latest") or block hash
// This function is used by ethereumjs-vm
BlockchainDouble.prototype.getBlock = function(number, callback) {
var self = this;
if (typeof number !== "string") {
number = to.hex(number);
}
// If we have a hex number or a block hash
if (number.indexOf("0x") >= 0) {
var hash = number;
// block hash
if (hash.length > 40) {
this.data.blockHashes.get(to.hex(hash), function(err, blockIndex) {
if (err) {
return callback(err);
}
return self.data.blocks.get(blockIndex, callback);
});
} else {
// Block number
return this.data.blocks.get(to.number(hash), callback);
}
} else {
if (number === "latest" || number === "pending") {
return this.latestBlock(callback);
} else if (number === "earliest") {
return this.data.blocks.first(callback);
} else {
process.nextTick(callback, new Error("Invalid `blockNumber`: \"" + number + "\""));
}
}
};
BlockchainDouble.prototype.putBlock = function(block, logs, receipts, callback) {
var self = this;
// Lock in the state root for this block.
block.header.stateRoot = this.stateTrie.root;
this.data.blocks.length(function(err, length) {
if (err) {
return callback(err);
}
var requests = [
self.data.blocks.push.bind(self.data.blocks, block),
self.data.blockLogs.push.bind(self.data.blockLogs, logs),
self.data.blockHashes.set.bind(self.data.blockHashes, to.hex(block.hash()), length)
];
block.transactions.forEach(function(tx, index) {
var txHash = to.hex(tx.hash());
requests.push(
self.data.transactions.set.bind(self.data.transactions, txHash, tx),
self.data.transactionReceipts.set.bind(self.data.transactionReceipts, txHash, receipts[index])
);
});
async.parallel(requests, (err, result) => {
if (!err) {
self.emit("block", block);
}
callback(err, result);
});
});
};
BlockchainDouble.prototype.popBlock = function(callback) {
var self = this;
this.data.blocks.last(function(err, block) {
if (err) {
return callback(err);
}
if (block == null) {
return callback(null, null);
}
var requests = [];
var blockHash = to.hex(block.hash());
block.transactions.forEach(function(tx) {
var txHash = to.hex(tx.hash());
requests.push(
self.data.transactions.del.bind(self.data.transactions, txHash),
self.data.transactionReceipts.del.bind(self.data.transactionReceipts, txHash)
);
});
requests.push(
self.data.blockLogs.pop.bind(self.data.blockLogs),
self.data.blockHashes.del.bind(self.data.blockHashes, blockHash),
self.data.blocks.pop.bind(self.data.blocks) // Do this one last in case anything relies on it.
);
async.series(requests, function(err) {
if (err) {
return callback(err);
}
// Set the root to the last available, which will "roll back" to the previous
// moment in time. Note that all the old data is still in the db, but it's now just junk data.
self.data.blocks.last(function(err, newLastBlock) {
if (err) {
return callback(err);
}
// using setStateRoot because in the future it will automatically take care
// of clearing the cache for us.
// note setStateRoot checks for checkpoints, and if there are any, it will fail.
// At time of writing this comment, the only time there could be a checkpoint
// is in the middle of a vm.runBlock call. Once asyncRequestProcessing is reenabled
// this will likely cause problems.
self.vm.stateManager.setStateRoot(newLastBlock.header.stateRoot, function(err) {
// remove this `._storageTries = {}` statement once https://github.com/ethereumjs/ethereumjs-vm/pull/445
// is in a release, probably in ejs-vm 2.6.1
if (!err) {
self.vm.stateManager._storageTries = {};
}
// Remember: Return block we popped off.
callback(err, block);
});
});
});
});
};
BlockchainDouble.prototype.clearPendingTransactions = function() {
this.pending_transactions = [];
};
/**
* createBlock
*
* Create a new block, where the parent's block is either the latest block
* on the chain or the parent block passed in.
*
* @param {Block} parent The block meant to be the parent block (optional)
* @param {Function} callback Callback function called after block is created
* @return Block The block created.
*/
BlockchainDouble.prototype.createBlock = function(parent, callback) {
var self = this;
if (typeof parent === "function") {
callback = parent;
parent = null;
}
var block = new Block();
function getParent(callback) {
if (parent) {
return callback(null, parent);
} else {
self.latestBlock(callback);
}
}
getParent(function(err, parent) {
if (err) {
return callback(err);
}
var parentNumber = parent != null ? to.number(parent.header.number) : -1;
block.header.gasLimit = self.blockGasLimit;
// Ensure we have the right block number for the VM.
block.header.number = to.hex(parentNumber + 1);
// Set the timestamp before processing txs
block.header.timestamp = to.hex(self.currentTime());
if (parent != null) {
block.header.parentHash = to.hex(parent.hash());
}
callback(null, block);
});
};
BlockchainDouble.prototype.getQueuedNonce = function(address, callback) {
var nonce = null;
var addressBuffer = to.buffer(address);
this.pending_transactions.forEach(function(tx) {
if (!tx.from.equals(addressBuffer)) {
return;
}
var pendingNonce = new BN(tx.nonce);
// If this is the first queued nonce for this address we found,
// or it's higher than the previous highest, note it.
if (nonce === null || pendingNonce.gt(nonce)) {
nonce = pendingNonce;
}
});
// If we found a queued transaction nonce, return one higher
// than the highest we found
if (nonce != null) {
return callback(null, nonce.iaddn(1).toArrayLike(Buffer));
}
this.stateTrie.get(addressBuffer, function(err, val) {
if (err) {
return callback(err);
}
var account = new Account(val);
// nonces are initiallized as an empty buffer, which isn't what we want.
callback(null, account.nonce.length === 0 ? Buffer.from([0]) : account.nonce);
});
};
BlockchainDouble.prototype.queueTransaction = function(tx) {
this.pending_transactions.push(tx);
};
BlockchainDouble.prototype.sortByPriceAndNonce = function() {
// Sorts transactions like I believe geth does.
// See the description of 'SortByPriceAndNonce' at
// https://github.com/ethereum/go-ethereum/blob/290e851f57f5d27a1d5f0f7ad784c836e017c337/core/types/transaction.go
var self = this;
var sortedByNonce = {};
self.pending_transactions.forEach((tx) => {
const from = tx.from.toString("hex");
const arr = sortedByNonce[from];
if (arr) {
arr.push(tx);
} else {
sortedByNonce[from] = [tx];
}
});
var priceSort = function(a, b) {
return parseInt(to.hex(b.gasPrice), 16) - parseInt(to.hex(a.gasPrice), 16);
};
var nonceSort = function(a, b) {
return parseInt(to.hex(a.nonce), 16) - parseInt(to.hex(b.nonce), 16);
};
// Now sort each address by nonce
Object.keys(sortedByNonce).forEach((address) => {
sortedByNonce[address].sort(nonceSort);
});
// Initialise a heap, sorted by price, for the head transaction from each account.
var heap = new Heap(priceSort);
Object.keys(sortedByNonce).forEach((address) => {
heap.push(sortedByNonce[address].shift());
});
// Now reorder our transactions. Compare the next transactions from each account, and choose
// the one with the highest gas price.
const sortedTransactions = [];
while (heap.size() > 0) {
const best = heap.pop();
let address = best.from.toString("hex");
if (sortedByNonce[address].length > 0) {
// Push on the next transaction from this account
heap.push(sortedByNonce[address].shift());
}
sortedTransactions.push(best);
}
self.pending_transactions = sortedTransactions;
};
BlockchainDouble.prototype.processCall = function(tx, blockNumber, callback) {
var self = this;
var runCall = function(tx, err, parentBlock) {
if (err) {
return callback(err);
}
// create a fake block with this fake transaction
self.createBlock(parentBlock, function(err, newBlock) {
if (err) {
return callback(err);
}
newBlock.transactions.push(tx);
var runArgs = {
tx: tx,
block: newBlock,
skipBalance: true,
skipNonce: true
};
var stateTrie = self.createStateTrie(self.data.trie_db, parentBlock.header.stateRoot);
var vm = self.createVMFromStateTrie(stateTrie);
vm.runTx(runArgs, function(vmerr, result) {
// This is a check that has been in there for awhile. I'm unsure if it's required, but it can't hurt.
if (vmerr && vmerr instanceof Error === false) {
vmerr = new Error("VM error: " + vmerr);
}
// If we're given an error back directly, it's worse than a runtime error. Expose it and get out.
if (vmerr) {
return callback(vmerr, err);
}
// If no error, check for a runtime error. This can return null if no runtime error.
vmerr = RuntimeError.fromResults([tx], { results: [result] });
callback(vmerr, result);
});
});
};
// Delegate block selection
if (blockNumber === "latest") {
self.latestBlock(runCall.bind(null, tx));
} else {
self.getBlock(blockNumber, runCall.bind(null, tx));
}
};
BlockchainDouble.prototype.estimateGas = function(tx, blockNumber, callback) {
var self = this;
var runCall = function(tx, err, parentBlock) {
if (err) {
return callback(err);
}
// create a fake block with this fake transaction
self.createBlock(parentBlock, function(err, newBlock) {
if (err) {
return callback(err);
}
newBlock.transactions.push(tx);
var runArgs = {
tx: tx,
block: newBlock,
skipBalance: true,
skipNonce: true
};
var stateTrie = self.createStateTrie(self.data.trie_db, parentBlock.header.stateRoot);
var vm = self.createVMFromStateTrie(stateTrie);
estimateGas(vm, runArgs, err, callback);
});
};
// Delegate block selection
if (blockNumber === "latest") {
self.latestBlock(runCall.bind(null, tx));
} else {
self.getBlock(blockNumber, runCall.bind(null, tx));
}
};
/**
* processBlock
*
* Process the passed in block and included transactions
*
* @param {VM} vm the vm to use when running the block
* @param {Block} block block to process
* @param {Boolean} commit Whether or not changes should be committed to the state
* trie and the block appended to the end of the chain.
* @param {Function} callback Callback function when transaction processing is completed.
* @return [type] [description]
*/
BlockchainDouble.prototype.processBlock = function(vm, block, commit, callback) {
var self = this;
if (typeof commit === "function") {
callback = commit;
commit = true;
}
vm.runBlock(
{
block: block,
generate: true,
skipBlockValidation: true
},
async function(vmerr, results) {
// This is a check that has been in there for awhile. I'm unsure if it's required, but it can't hurt.
if (vmerr && vmerr instanceof Error === false) {
vmerr = new Error("VM error: " + vmerr);
}
// If we're given an error back directly, it's worse than a runtime error. Expose it and get out.
if (vmerr) {
callback(vmerr);
return;
}
// If no error, check for a runtime error. This can return null if no runtime error.
vmerr = RuntimeError.fromResults(block.transactions, results);
// Note, even if we have an error, some transactions may still have succeeded.
// Process their logs if so, returning the error at the end.
var logs = [];
var receipts = [];
var totalBlockGasUsage = 0;
results.results.forEach(function(result) {
totalBlockGasUsage += to.number(result.gasUsed);
});
block.header.gasUsed = utils.toBuffer(to.hex(totalBlockGasUsage));
const txTrie = new Trie();
const rcptTrie = new Trie();
const promises = [];
const putInTrie = (trie, key, val) => promisify(trie.put.bind(trie))(key, val);
for (var v = 0; v < results.receipts.length; v++) {
var result = results.results[v];
var receipt = results.receipts[v];
var tx = block.transactions[v];
var txHash = tx.hash();
var txLogs = [];
// Only process the transaction's logs if it didn't error.
if (result.vm.exception === 1) {
for (var i = 0; i < receipt.logs.length; i++) {
var receiptLog = receipt.logs[i];
var address = to.hex(receiptLog[0]);
var topics = [];
for (var j = 0; j < receiptLog[1].length; j++) {
topics.push(to.hex(receiptLog[1][j]));
}
var data = to.hex(receiptLog[2]);
var log = new Log({
logIndex: to.hex(i),
transactionIndex: to.hex(v),
transactionHash: txHash,
block: block,
address: address,
data: data,
topics: topics,
type: "mined"
});
logs.push(log);
txLogs.push(log);
}
}
let rcpt = new Receipt(
tx,
block,
txLogs,
result.gasUsed.toArrayLike(Buffer),
receipt.gasUsed,
result.createdAddress,
receipt.status,
to.hex(receipt.bitvector)
);
receipts.push(rcpt);
const rawReceipt = [receipt.status, receipt.gasUsed, receipt.bitvector, receipt.logs];
const rcptBuffer = utils.rlp.encode(rawReceipt);
const key = utils.rlp.encode(v);
promises.push(putInTrie(txTrie, key, tx.serialize()));
promises.push(putInTrie(rcptTrie, key, rcptBuffer));
}
await Promise.all(promises);
block.header.transactionsTrie = utils.toBuffer(txTrie.root);
block.header.receiptTrie = utils.toBuffer(rcptTrie.root);
if (commit) {
// Put that block on the end of the chain
self.putBlock(block, logs, receipts, done);
} else {
done();
}
function done(e) {
if (e) {
return callback(e);
}
// Note we return the vm err here too, if it exists.
callback(vmerr, block.transactions, results);
}
}
);
};
/**
* processNextBlock
*
* Process the next block like a normal blockchain, pulling from the list of
* pending transactions.
*
* @param {number} timestamp at which the block is mined
* @param {Function} callback Callback when transaction processing is finished.
* @return [type] [description]
*/
BlockchainDouble.prototype.processNextBlock = function(timestamp, callback) {
var self = this;
if (typeof timestamp === "function") {
callback = timestamp;
timestamp = undefined;
}
self.sortByPriceAndNonce();
// Grab only the transactions that can fit within the block
var currentTransactions = [];
var totalGasLimit = 0;
var maxGasLimit = to.number(self.blockGasLimit);
while (self.pending_transactions.length > 0) {
var tx = self.pending_transactions[0];
var gasLimit = to.number(tx.gasLimit);
if (totalGasLimit + gasLimit <= maxGasLimit) {
totalGasLimit += gasLimit;
self.pending_transactions.shift();
currentTransactions.push(tx);
} else {
// Next one won't fit. Break.
break;
}
}
// Remember, we ensured transactions had a valid gas limit when they were queued (in the state manager).
// If we run into a case where we can't process any because one is higher than the gas limit,
// then it's a serious issue. This should never happen, but let's check anyway.
if (currentTransactions.length === 0 && self.pending_transactions.length > 0) {
// Error like geth.
var error = "Unexpected error condition: next transaction exceeds block gas limit";
return callback(error);
}
// Create a new block meant to be the end of the chain
this.createBlock(function(err, block) {
if (err) {
return callback(err);
}
// Overwrite block timestamp
if (timestamp) {
self.data.blocks.last(function(err, last) {
if (err) {
return callback(err);
}
if (last && to.number(last.header.timestamp) > timestamp) {
self.logger.log(
"Waring: Setting the block timestamp (" + timestamp + ") that is earlier than the parent block one."
);
}
});
block.header.timestamp = to.hex(timestamp);
self.setTime(new Date(timestamp * 1000));
}
// Add transactions to the block.
Array.prototype.push.apply(block.transactions, currentTransactions);
self.processBlock(self.vm, block, true, callback);
});
};
/**
* processTransactionTrace
*
* Run a previously-run transaction in the same state in which it occurred at the time it was run.
* This will return the vm-level trace output for debugging purposes.
*
* Strategy:
*
* 1. Find block where transaction occurred
* 2. Set state root of that block
* 3. Rerun every transaction in that block prior to and including the requested transaction
* 4. Send trace results back.
*
* @param {[type]} tx [description]
* @param {Function} callback [description]
* @return [type] [description]
*/
BlockchainDouble.prototype.processTransactionTrace = async function(hash, params, callback) {
const self = this;
const targetHash = to.hex(hash);
let txHashCurrentlyProcessing = "";
let txCurrentlyProcessing = null;
let vm;
let storageStack = {
currentDepth: -1,
stack: []
};
let returnVal = {
gas: 0,
returnValue: "",
structLogs: []
};
function stepListener(event, next) {
// See these docs:
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs
const gasLeft = to.number(event.gasLeft);
const totalGasUsedAfterThisStep = to.number(txCurrentlyProcessing.gasLimit) - gasLeft;
const gasUsedThisStep = totalGasUsedAfterThisStep - returnVal.gas;
returnVal.gas += gasUsedThisStep;
let memory = null;
if (!params.disableMemory) {
// Get memory and break it up into 32-byte words.
// Note we may possibly have to pad the final word.
memory = Buffer.from(event.memory).toString("hex");
memory = memory.match(/.{1,64}/g) || [];
if (memory.length > 0) {
const lastItem = memory[memory.length - 1];
if (lastItem.length < 64) {
memory[memory.length - 1] = lastItem + new Array(64 - lastItem.length + 1).join("0");
}
}
}
let stack = null;
if (!params.disableStack) {
stack = event.stack.map((item) => {
return to.rpcDataHexString(item, 64).replace("0x", ""); // non-0x prefixed.
});
}
let structLog = {
depth: event.depth,
error: "",
gas: gasLeft,
gasCost: gasUsedThisStep,
memory,
op: event.opcode.name,
pc: event.pc,
stack,
storage: null
};
if (params.disableStorage) {
returnVal.structLogs.push(structLog);
next();
} else {
structLog = self.processStorageTrace(structLog, storageStack, event, vm, function(err, structLog) {
if (err) {
return next(err);
}
returnVal.structLogs.push(structLog);
next();
});
}
}
// #1 - get block via transaction receipt
this.getTransactionReceipt(targetHash, function(err, receipt) {
if (err) {
return callback(err);
}
if (!receipt) {
return callback(new Error("Unknown transaction " + targetHash));
}
let targetBlock = receipt.block;
// Get the parent of the target block
self.getBlock(targetBlock.header.parentHash, function(err, parent) {
if (err) {
return callback(err);
}
// #2 - Set state root of original block
var stateTrie = self.createStateTrie(self.data.trie_db, parent.header.stateRoot);
vm = self.createVMFromStateTrie(stateTrie);
// Prepare the "next" block with necessary transactions
self.createBlock(parent, function(err, block) {
if (err) {
return callback(err);
}
for (var i = 0; i < targetBlock.transactions.length; i++) {
var tx = targetBlock.transactions[i];
block.transactions.push(tx);
// After including the target transaction, that's all we need to do.
if (to.hex(tx.hash()) === targetHash) {
break;
}
}
function beforeTxListener(tx) {
txCurrentlyProcessing = tx;
txHashCurrentlyProcessing = to.hex(tx.hash());
if (txHashCurrentlyProcessing === targetHash) {
vm.on("step", stepListener);
}
}
// afterTxListener cleans up everything.
function afterTxListener() {
if (txHashCurrentlyProcessing === targetHash) {
removeListeners();
}
}
function removeListeners() {
vm.removeListener("step", stepListener);
vm.removeListener("beforeTx", beforeTxListener);
vm.removeListener("afterTx", afterTxListener);
}
// Listen to beforeTx and afterTx so we know when our target transaction
// is processing. These events will add the event listener for getting the trace data.
vm.on("beforeTx", beforeTxListener);
vm.on("afterTx", afterTxListener);
// #3 - Process the block without committing the data.
self.processBlock(vm, block, false, function(err) {
// Ignore runtime errors, or else erroneous transactions can't be traced.
if (err && err.message.indexOf("VM Exception") === 0) {
err = null;
}
// Just to be safe
removeListeners();
// #4 - send state results back
callback(err, returnVal);
});
});
});
});
};
BlockchainDouble.prototype.processStorageTrace = function(structLog, storageStack, event, vm, callback) {
var name = event.opcode.name;
var argsNum = event.opcode.in;
var args = event.stack.slice(-argsNum).map((arg) => to.hex(arg));
if (storageStack.currentDepth > event.depth) {
storageStack.stack.pop();
}
if (storageStack.currentDepth < event.depth) {
storageStack.stack.push({});
}
storageStack.currentDepth = event.depth;
var key;
var value;
switch (name) {
case "SSTORE":
key = to.rpcDataHexString(args[1], 64).replace("0x", "");
value = to.rpcDataHexString(args[0], 64).replace("0x", "");
// use Object.assign to prevent future steps from overwriting this step's storage values
structLog.storage = Object.assign({}, storageStack.stack[storageStack.currentDepth]);
callback(null, structLog);
// assign after callback because this storage change actually takes
// effect _after_ this opcode executes
storageStack.stack[storageStack.currentDepth][key] = value;
break;
case "SLOAD":
// this one's more fun, we need to get the value the contract is loading from current storage
key = to.rpcDataHexString(args[0], 64).replace("0x", "");
vm.stateManager.getContractStorage(event.address, "0x" + key, function(err, result) {
if (err) {
return callback(err);
}
value = to.rpcDataHexString(result, 64).replace("0x", "");
storageStack.stack[storageStack.currentDepth][key] = value;
// use Object.assign to prevent future steps from overwriting this step's storage values