forked from dtr-org/unit-e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_snapshot_finalization.py
More file actions
executable file
·133 lines (109 loc) · 4.65 KB
/
Copy pathfeature_snapshot_finalization.py
File metadata and controls
executable file
·133 lines (109 loc) · 4.65 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
#!/usr/bin/env python3
# Copyright (c) 2019 The Unit-e developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""
Test snapshot and commits integration.
After fast sync node should contain actual finalization state
"""
from test_framework.messages import (
CTransaction,
FromHex,
ToHex,
TxType,
)
from test_framework.test_framework import UnitETestFramework
from test_framework.util import (
assert_equal,
assert_finalizationstate,
assert_raises_rpc_error,
bytes_to_hex_str,
connect_nodes,
disconnect_nodes,
generate_block,
sync_blocks,
wait_until,
)
def setup_deposit(self, proposer, validators):
for i, n in enumerate(validators):
n.new_address = n.getnewaddress("", "legacy")
assert_equal(n.getbalance(), 10000)
for n in validators:
deptx = n.deposit(n.new_address, 1500)
self.wait_for_transaction(deptx, nodes=[proposer])
generate_block(proposer, count=21)
assert_equal(proposer.getblockcount(), 22)
class SnapshotFinalization(UnitETestFramework):
def set_test_params(self):
self.num_nodes = 3
self.extra_args = [
['-esperanzaconfig={"epochLength": 5, "minDepositSize": 1500}'],
['-esperanzaconfig={"epochLength": 5, "minDepositSize": 1500}', '-validating=1'],
['-esperanzaconfig={"epochLength": 5, "minDepositSize": 1500}', '-prune=1', '-isd=1'],
]
self.setup_clean_chain = True
def setup_network(self):
self.setup_nodes()
p, v, s = self.nodes
connect_nodes(p, v.index)
def run_test(self):
p, v, s = self.nodes
self.setup_stake_coins(p, v)
self.generate_sync(p, nodes=[p, v])
self.log.info("Setup deposit")
setup_deposit(self, p, [v])
disconnect_nodes(p, v.index)
self.log.info("Generate few epochs")
votes = self.generate_epoch(proposer=p, finalizer=v, count=2)
assert(len(votes) != 0)
assert_equal(p.getblockcount(), 32)
assert_finalizationstate(p, {'currentEpoch': 7,
'lastJustifiedEpoch': 6,
'lastFinalizedEpoch': 6,
'validators': 1})
self.log.info("Connect fast-sync node")
connect_nodes(s, p.index)
sync_blocks([p, s])
assert_finalizationstate(s, {'currentEpoch': 7,
'lastJustifiedEpoch': 6,
'lastFinalizedEpoch': 6,
'validators': 1})
self.log.info("Generate next epoch")
votes += self.generate_epoch(proposer=p, finalizer=v, count=1)
assert_equal(p.getblockcount(), 37)
assert_finalizationstate(p, {'currentEpoch': 8,
'lastJustifiedEpoch': 7,
'lastFinalizedEpoch': 7,
'validators': 1})
sync_blocks([p, s])
assert_finalizationstate(s, {'currentEpoch': 8,
'lastJustifiedEpoch': 7,
'lastFinalizedEpoch': 7,
'validators': 1})
self.log.info("Check slashing condition")
# Create new vote with input=votes[-1] which attempts to make a double vote
# To detect double vote, it's enough having two votes which are:
# 1. from same validator
# 2. with same source epoch
# 3. with same target epoch
# 4. with different target hash
# So, make target hash different.
vote = s.extractvotefromsignature(bytes_to_hex_str(votes[0].vin[0].scriptSig))
target_hash = list(vote['target_hash'])
target_hash[0] = '0' if target_hash[0] == '1' else '1'
vote['target_hash'] = "".join(target_hash)
prev_tx = s.decoderawtransaction(ToHex(votes[-1]))
vtx = v.createvotetransaction(vote, prev_tx['txid'])
vtx = v.signrawtransactionwithwallet(vtx)
vtx = FromHex(CTransaction(), vtx['hex'])
assert_raises_rpc_error(-26, 'bad-vote-invalid', s.sendrawtransaction, ToHex(vtx))
wait_until(lambda: len(s.getrawmempool()) > 0, timeout=20)
slash = FromHex(CTransaction(), s.getrawtransaction(s.getrawmempool()[0]))
assert_equal(slash.get_type(), TxType.SLASH)
self.log.info("Slashed")
self.log.info("Restart fast-sync node")
self.restart_node(s.index)
connect_nodes(s, p.index)
sync_blocks([s, p])
if __name__ == '__main__':
SnapshotFinalization().main()