-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-blocks.js
89 lines (86 loc) · 2.89 KB
/
test-blocks.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
'use strict';
const fs = require('fs');
const readline = require('readline');
const Contract = require('./index-collision');
const serverUrl = process.argv[2];
let txhashes = [];
for (let i = 3; i < process.argv.length; ++i) {
txhashes.push(process.argv[i]);
}
console.log('txhashes:' + JSON.stringify(txhashes));
Contract.setServer(serverUrl);
Contract.getWeb3().eth.getBlockNumber(function(error, blockCount) {
if (error) {
console.log(error);
} else {
console.log('getBlockCount:' + blockCount);
findInBlocks(blockCount, txhashes);
}
});
function findInBlocks(blockCount) {
if (blockCount > 0) {
Contract.getWeb3().eth.getBlock(blockCount, function(error, block) {
if (error) {
console.log(error);
} else {
console.log('blockNumber:' + blockCount + ";transactions.length:" + block.transactions.length);
for (let i = txhashes.length - 1; i >= 0; --i) {
const txhash = txhashes[i];
if (block.transactions.indexOf(txhash) > -1) {
console.log('txhash:' + txhash + "is transactions in blockInfo " + getBlockInfo(block));
txhashes.splice(i, 1);
}
if (0 == txhashes.length) {
console.log('end');
process.exit();
}
}
if (block.uncles.length > 0) {
console.log('blockNumber:' + blockCount + ";uncles:" + JSON.stringify(block.uncles));
findInUncles(0, block.uncles, block, function(error) {
if (error) {
console.log(error);
} else {
findInBlocks(blockCount - 1);
}
});
} else {
findInBlocks(blockCount - 1);
}
}
});
} else {
console.log('end');
}
}
function findInUncles(uncleNumber, uncles, parentBlock, callback) {
if (uncleNumber < uncles.length) {
Contract.getWeb3().eth.getBlock(uncles[uncleNumber], function(error, block) {
if (error) {
callback(error);
} else {
if(block){
console.log('uncle index:' + uncleNumber + ";blockInfo:" + getBlockInfo(block));
for (let i = txhashes.length - 1; i >= 0; --i) {
const txhash = txhashes[i];
if (block.transactions.indexOf(txhash) > -1) {
console.log('txhash:' + txhash + "is uncle in blockInfo " + getBlockInfo(block) + ';parent blockInfo ' +
getBlockInfo(parentBlock));
}
}
if (0 == txhashes.length) {
console.log('end');
process.exit();
}
}
findInUncles(uncleNumber + 1, uncles, parentBlock, callback);
}
});
} else {
callback();
}
}
function getBlockInfo(block) {
return 'number:' + block.number + ';hash:' + block.hash + ';miner:' + block.miner + ';timestamp:' + block.timestamp +
';transactionCount:' + block.transactions.length + ';unclesCount:' + block.uncles.length;
}