forked from jgarzik/pynode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
testscript.py
executable file
·121 lines (93 loc) · 2.47 KB
/
testscript.py
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
#!/usr/bin/python
#
# testscript.py
#
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
#
import sys
import time
import Log
import MemPool
import ChainDb
import cStringIO
from bitcoin.coredefs import NETWORKS
from bitcoin.core import CBlock
from bitcoin.serialize import ser_uint256
from bitcoin.scripteval import VerifySignature
NET_SETTINGS = {
'mainnet' : {
'log' : '/spare/tmp/testscript.log',
'db' : '/spare/tmp/chaindb'
},
'testnet3' : {
'log' : '/spare/tmp/testtestscript.log',
'db' : '/spare/tmp/chaintest'
}
}
MY_NETWORK = 'mainnet'
SETTINGS = NET_SETTINGS[MY_NETWORK]
start_height = 0
end_height = -1
if len(sys.argv) > 1:
start_height = int(sys.argv[1])
if len(sys.argv) > 2:
end_height = int(sys.argv[2])
if len(sys.argv) > 3:
SETTINGS['log'] = sys.argv[3]
log = Log.Log(SETTINGS['log'])
mempool = MemPool.MemPool(log)
chaindb = ChainDb.ChainDb(SETTINGS['db'], log, mempool,
NETWORKS[MY_NETWORK], True)
chaindb.blk_cache.max = 500
if end_height < 0 or end_height > chaindb.getheight():
end_height = chaindb.getheight()
scanned = 0
scanned_tx = 0
failures = 0
opcount = {}
SKIP_TX = {
}
def scan_tx(tx):
tx.calc_sha256()
if tx.sha256 in SKIP_TX:
return True
# log.write("...Scanning TX %064x" % (tx.sha256,))
for i in xrange(len(tx.vin)):
txin = tx.vin[i]
txfrom = chaindb.gettx(txin.prevout.hash)
if not VerifySignature(txfrom, tx, i, 0):
log.write("TX %064x/%d failed" % (tx.sha256, i))
log.write("FROMTX %064x" % (txfrom.sha256,))
log.write(txfrom.__repr__())
log.write("TOTX %064x" % (tx.sha256,))
log.write(tx.__repr__())
return False
return True
for height in xrange(end_height):
if height < start_height:
continue
heightidx = ChainDb.HeightIdx()
heightidx.deserialize(chaindb.height[str(height)])
blkhash = heightidx.blocks[0]
ser_hash = ser_uint256(blkhash)
f = cStringIO.StringIO(chaindb.blocks[ser_hash])
block = CBlock()
block.deserialize(f)
start_time = time.time()
for tx_tmp in block.vtx:
if tx_tmp.is_coinbase():
continue
scanned_tx += 1
if not scan_tx(tx_tmp):
failures += 1
sys.exit(1)
end_time = time.time()
scanned += 1
# if (scanned % 1000) == 0:
log.write("Scanned %d tx, height %d (%d failures), %.2f sec" % (
scanned_tx, height, failures, end_time - start_time))
log.write("Scanned %d tx, %d blocks (%d failures)" % (
scanned_tx, scanned, failures))
#for k,v in opcount.iteritems():
# print k, v