-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmerkletracker.py
More file actions
88 lines (80 loc) · 2.11 KB
/
Copy pathmerkletracker.py
File metadata and controls
88 lines (80 loc) · 2.11 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# start_debug
from ethereum import *
from contract_functions import *
from copy import deepcopy
class MERKLETRACKER(Contract):
def run(self, tx, block):
# variables for testing:
minfee = 0
# contract.blah is self-referential
contract = self
# end_debug
# actual contract
if tx.datan < 3:
self.stop
if tx.fee < minfee:
stop
def getmerkleroot(blockheader):
return blockheader[4+32:4+32+32]
if tx.data[0] == 0:
# add a merkle root
merkleroot = tx.data[1]
blockhash = tx.data[2]
blockdetails = block.contract_storage('CHAINHEADERS')[blockhash]
if blockdetails == 0:
self.stop("Blockhash doesn't exist in CHAINHEADERS")
blockheader = blockdetails[0]
if merkleroot != getmerkleroot(blockheader):
# fail
self.stop("merkleroot not in blockheader")
contract.storage[merkleroot] = 1
elif tx.data[0] == 1:
# fill in merkle branch
hash1 = tx.data[1]
hash2 = tx.data[2]
# first verify branch leads to existing entry
counter = 2;
while True:
counter += 1
hash3 = sha256(sha256(hash1.concat(hash2)))
if tx.datan > counter:
lr = tx.data[counter][0]
hash4 = tx.data[counter][1:33]
else:
pre = contract.storage[hash3]
if pre == 1:
# found merkle root
merkleroot = hash3
break
elif pre == 0:
# failed, not enough user input to confirm branch
self.stop('Incomplete or invalid merkle branch')
merkleroot = pre[2]
break
if lr == 0:
hash1 = hash3
hash2 = hash4
else:
hash1 = hash4
hash2 = hash3
# sweet, everything is valid so far, lets make some entries!
hash1 = tx.data[1]
hash2 = tx.data[2]
counter = 2;
while contract.storage[hash1] == 0:
contract.storage[hash1] = [0x00, hash2, merkleroot]
contract.storage[hash2] = [0x01, hash1, merkleroot]
counter += 1
hash3 = sha256(hash1.concat(hash2))
if tx.datan > counter:
lr = tx.data[counter][0]
hash4 = tx.data[counter][1:33]
else:
break
if lr == 0:
hash1 = hash3
hash2 = hash4
else:
hash1 = hash4
hash2 = hash3
# should be done, I think