forked from StabilityNexus/MiniChain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_persistence.py
More file actions
174 lines (131 loc) · 5.44 KB
/
Copy pathtest_persistence.py
File metadata and controls
174 lines (131 loc) · 5.44 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""
Tests for chain persistence (save / load round-trip).
"""
import os
import tempfile
import unittest
from nacl.signing import SigningKey
from nacl.encoding import HexEncoder
from minichain import Blockchain, Transaction, Block, mine_block
from minichain.persistence import save, load
def _make_keypair():
sk = SigningKey.generate()
pk = sk.verify_key.encode(encoder=HexEncoder).decode()
return sk, pk
class TestPersistence(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
# Helpers
def _chain_with_tx(self):
"""Return a Blockchain that has one mined block with a transfer."""
bc = Blockchain()
alice_sk, alice_pk = _make_keypair()
_, bob_pk = _make_keypair()
bc.state.credit_mining_reward(alice_pk, 100)
tx = Transaction(alice_pk, bob_pk, 30, 0)
tx.sign(alice_sk)
block = Block(
index=1,
previous_hash=bc.last_block.hash,
transactions=[tx],
difficulty=1,
)
mine_block(block, difficulty=1)
bc.add_block(block)
return bc, alice_pk, bob_pk
# Tests
def test_save_creates_files(self):
bc = Blockchain()
save(bc, path=self.tmpdir)
self.assertTrue(os.path.exists(os.path.join(self.tmpdir, "blockchain.json")))
self.assertTrue(os.path.exists(os.path.join(self.tmpdir, "state.json")))
def test_chain_length_preserved(self):
bc, _, _ = self._chain_with_tx()
save(bc, path=self.tmpdir)
restored = load(path=self.tmpdir)
self.assertEqual(len(restored.chain), len(bc.chain))
def test_block_hashes_preserved(self):
bc, _, _ = self._chain_with_tx()
save(bc, path=self.tmpdir)
restored = load(path=self.tmpdir)
for original, loaded in zip(bc.chain, restored.chain):
self.assertEqual(original.hash, loaded.hash)
self.assertEqual(original.index, loaded.index)
self.assertEqual(original.previous_hash, loaded.previous_hash)
def test_account_balances_preserved(self):
bc, alice_pk, bob_pk = self._chain_with_tx()
save(bc, path=self.tmpdir)
restored = load(path=self.tmpdir)
self.assertEqual(
bc.state.get_account(alice_pk)["balance"],
restored.state.get_account(alice_pk)["balance"],
)
self.assertEqual(
bc.state.get_account(bob_pk)["balance"],
restored.state.get_account(bob_pk)["balance"],
)
def test_account_nonces_preserved(self):
bc, alice_pk, _ = self._chain_with_tx()
save(bc, path=self.tmpdir)
restored = load(path=self.tmpdir)
self.assertEqual(
bc.state.get_account(alice_pk)["nonce"],
restored.state.get_account(alice_pk)["nonce"],
)
def test_transaction_data_preserved(self):
bc, _, _ = self._chain_with_tx()
save(bc, path=self.tmpdir)
restored = load(path=self.tmpdir)
original_tx = bc.chain[1].transactions[0]
loaded_tx = restored.chain[1].transactions[0]
self.assertEqual(original_tx.sender, loaded_tx.sender)
self.assertEqual(original_tx.receiver, loaded_tx.receiver)
self.assertEqual(original_tx.amount, loaded_tx.amount)
self.assertEqual(original_tx.nonce, loaded_tx.nonce)
self.assertEqual(original_tx.signature, loaded_tx.signature)
def test_loaded_chain_can_add_new_block(self):
"""Restored chain must still accept new valid blocks."""
bc, alice_pk, bob_pk = self._chain_with_tx()
save(bc, path=self.tmpdir)
restored = load(path=self.tmpdir)
# Build a second transfer on top of the loaded chain
alice_sk, alice_pk2 = _make_keypair()
_, carol_pk = _make_keypair()
restored.state.credit_mining_reward(alice_pk2, 50)
tx2 = Transaction(alice_pk2, carol_pk, 10, 0)
tx2.sign(alice_sk)
block2 = Block(
index=len(restored.chain),
previous_hash=restored.last_block.hash,
transactions=[tx2],
difficulty=1,
)
mine_block(block2, difficulty=1)
self.assertTrue(restored.add_block(block2))
self.assertEqual(len(restored.chain), len(bc.chain) + 1)
def test_load_missing_file_raises(self):
with self.assertRaises(FileNotFoundError):
load(path=self.tmpdir) # nothing saved yet
def test_genesis_only_chain(self):
bc = Blockchain()
save(bc, path=self.tmpdir)
restored = load(path=self.tmpdir)
self.assertEqual(len(restored.chain), 1)
self.assertEqual(restored.chain[0].hash, "0" * 64)
def test_contract_storage_preserved(self):
"""Contract accounts and storage survive a save/load cycle."""
from minichain import State, Transaction as Tx
bc = Blockchain()
deployer_sk, deployer_pk = _make_keypair()
bc.state.credit_mining_reward(deployer_pk, 100)
code = "storage['hits'] = storage.get('hits', 0) + 1"
tx_deploy = Tx(deployer_pk, None, 0, 0, data=code)
tx_deploy.sign(deployer_sk)
contract_addr = bc.state.apply_transaction(tx_deploy)
self.assertIsInstance(contract_addr, str)
save(bc, path=self.tmpdir)
restored = load(path=self.tmpdir)
contract = restored.state.get_account(contract_addr)
self.assertEqual(contract["code"], code)
if __name__ == "__main__":
unittest.main()