-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdatabase.py
More file actions
71 lines (60 loc) · 1.92 KB
/
database.py
File metadata and controls
71 lines (60 loc) · 1.92 KB
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
"""
Author: Aleksandra Sokolowska
for Validity Labs AG
"""
from web3 import Web3
from organize import *
import time
#uncomment one of the options below
# 1. connection via Infura
#web3 = Web3(Web3.HTTPProvider("https://mainnet.infura.io/your-personal-number"))
# 2. or connection via local node
#web3 = Web3(Web3.IPCProvider('/your-path-to/geth.ipc'))
# load a block.
Nblocks = 10000
output_every = 2
start_time = time.time()
try:
with open('lastblock.txt', 'r') as f:
start = int(f.read())+1
except FileNotFoundError:
start = 2000000
#define tables that will go to the SQLite database
table_quick = []
table_tx = []
table_block = []
count = 0
#loop over all blocks
for block in range(start, start+Nblocks):
block_table, block_data = order_table_block(block,web3)
#list of block data that will go to the DB
table_block.append(block_table)
#all transactions on the block
for hashh in block_data['transactions']:
#print(web3.toHex(hashh))
quick_table, tx_data = order_table_quick(hashh,block, web3)
table_quick.append(quick_table)
#list of tx data that will go to the DB
TX_table = order_table_tx(tx_data,hashh, web3)
table_tx.append(TX_table)
count = count + 1
#print(count)
#dump output every 2 blocks
if (count % output_every) == 0:
execute_sql(table_quick, table_tx, table_block)
#free up memory
del table_quick
del table_tx
del table_block
table_quick = []
table_tx = []
table_block = []
#update the current block number to a file
with open('lastblock.txt', 'w') as f:
f.write("%d" % block)
if (count % 10) == 0:
end = time.time()
with open('timeperXblocks.txt', 'a') as f:
f.write("%d %f \n" % (block, end-start_time))
if (count % 100) == 0:
print("100 new blocks completed.")