2017-08-01 12:42:08 +00:00
|
|
|
from new_bintrie import Trie, EphemDB, encode_bin, encode_bin_path, decode_bin_path
|
|
|
|
from ethereum.utils import sha3, encode_hex
|
|
|
|
import random
|
2017-08-02 08:36:44 +00:00
|
|
|
import rlp
|
2017-08-01 12:42:08 +00:00
|
|
|
|
|
|
|
def shuffle_in_place(x):
|
|
|
|
y = x[::]
|
|
|
|
random.shuffle(y)
|
|
|
|
return y
|
|
|
|
|
2017-08-03 04:23:05 +00:00
|
|
|
kvpairs = [(sha3(str(i))[12:], str(i).encode('utf-8') * 5) for i in range(2000)]
|
2017-08-01 12:42:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
for path in ([], [1,0,1], [0,0,1,0], [1,0,0,1,0], [1,0,0,1,0,0,1,0], [1,0] * 8):
|
|
|
|
assert decode_bin_path(encode_bin_path(bytes(path))) == bytes(path)
|
|
|
|
|
|
|
|
r1 = None
|
|
|
|
|
2017-08-02 08:36:44 +00:00
|
|
|
for _ in range(3):
|
2017-08-01 12:42:08 +00:00
|
|
|
t = Trie(EphemDB(), b'')
|
2017-08-02 08:36:44 +00:00
|
|
|
for i, (k, v) in enumerate(shuffle_in_place(kvpairs)):
|
2017-08-01 12:42:08 +00:00
|
|
|
#print(t.to_dict())
|
|
|
|
t.update(k, v)
|
|
|
|
assert t.get(k) == v
|
2017-08-02 08:36:44 +00:00
|
|
|
if not i % 50:
|
|
|
|
if not i % 250:
|
|
|
|
t.to_dict()
|
|
|
|
print("Length of branch at %d nodes: %d" % (i, len(rlp.encode(t.get_branch(k)))))
|
2017-08-01 12:42:08 +00:00
|
|
|
assert r1 is None or t.root == r1
|
|
|
|
r1 = t.root
|
|
|
|
t.update(kvpairs[0][0], kvpairs[0][1])
|
|
|
|
assert t.root == r1
|
|
|
|
print(t.get_branch(kvpairs[0][0]))
|
2017-08-02 08:36:44 +00:00
|
|
|
print(t.get_branch(kvpairs[0][0][::-1]))
|
2017-08-01 12:42:08 +00:00
|
|
|
print(encode_hex(t.root))
|
2017-08-01 12:49:08 +00:00
|
|
|
for k, v in shuffle_in_place(kvpairs):
|
|
|
|
t.update(k, b'')
|
|
|
|
if not random.randrange(100):
|
|
|
|
t.to_dict()
|
2017-08-03 04:23:05 +00:00
|
|
|
#t.print_nodes()
|
2017-08-01 12:49:08 +00:00
|
|
|
assert t.root == b''
|
2017-08-01 12:42:08 +00:00
|
|
|
|