-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_loader.py
42 lines (38 loc) · 1.47 KB
/
database_loader.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
import bitcoin_tools as bt
import pymysql as mdb
NULL_HASH = 64*'0'
con = mdb.connect('localhost','root','','bitcoin') #host, user, password, #database
with con:
cur = con.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS outputs ('+
'transactionHash CHAR(64),'+
'outputIndex INT, PRIMARY KEY (transactionHash,outputIndex),'+
'value BIGINT,'+
'blockNumber int,'+
'spendHash CHAR(64), INDEX (spendHash),'+
'address VARCHAR(35), INDEX (address))')
cur.execute('CREATE TABLE IF NOT EXISTS blocks ('+
'hash CHAR(64) PRIMARY KEY,'+
'height INT)')
bc = bt.BlockChain()
for height,block in enumerate(bc):
if height % 1e3 == 0:
print height
elif height > 2.5e5:
break
with con:
cur = con.cursor()
cur.execute("SELECT COUNT(*) FROM blocks WHERE hash = '%s'"%block.hash)
if cur.fetchone()[0] == 1:
continue
for i,transaction in enumerate(block):
if i > 0:
for inTx in transaction.inputs:
command = "UPDATE outputs SET spendHash = '%s' WHERE transactionHash = '%s' AND outputIndex = %i"
command = command % (transaction.hash,inTx.tx_hash,inTx.tx_index)
cur.execute(command)
for index,outTx in enumerate(transaction.outputs):
command = "INSERT IGNORE INTO outputs VALUES ('%s',%i,%i,'%s','%s','%s')"
command = command%(transaction.hash,index,outTx.value,height,NULL_HASH,outTx.address)
cur.execute(command)
cur.execute("INSERT INTO blocks VALUES ('%s',%i)"%(block.hash,height))