forked from dtr-org/unit-e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalization_slash.py
More file actions
executable file
·217 lines (188 loc) · 8.69 KB
/
Copy pathfinalization_slash.py
File metadata and controls
executable file
·217 lines (188 loc) · 8.69 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
# Copyright (c) 2018-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.
"""
SlashTest checks:
1. double vote with invalid vote signature is ignored
2. double vote with valid vote signature but invalid tx signature creates slash transaction
"""
from test_framework.regtest_mnemonics import regtest_mnemonics
from test_framework.script import CScript
from test_framework.test_framework import UnitETestFramework
from test_framework.blocktools import (
CBlock,
CTransaction,
FromHex,
ToHex,
TxType,
sign_transaction)
from test_framework.util import (
assert_equal,
assert_finalizationstate,
assert_raises_rpc_error,
connect_nodes,
disconnect_nodes,
generate_block,
sync_blocks,
wait_until,
)
import time
class SlashTest(UnitETestFramework):
def set_test_params(self):
self.num_nodes = 4
self.extra_args = [
[],
[],
['-validating=1'],
['-validating=1'],
]
self.setup_clean_chain = True
def setup_network(self):
self.setup_nodes()
def test_double_votes(self):
def corrupt_script(script, n_byte):
script = bytearray(script)
script[n_byte] = 1 if script[n_byte] == 0 else 0
return bytes(script)
# initial topology where arrows denote the direction of connections
# finalizer2 ← fork1 → finalizer1
# ↓ ︎
# fork2
fork1 = self.nodes[0]
fork2 = self.nodes[1]
fork1.importmasterkey(regtest_mnemonics[0]['mnemonics'])
fork2.importmasterkey(regtest_mnemonics[1]['mnemonics'])
finalizer1 = self.nodes[2]
finalizer2 = self.nodes[3]
connect_nodes(fork1, fork2.index)
connect_nodes(fork1, finalizer1.index)
connect_nodes(fork1, finalizer2.index)
# leave IBD
generate_block(fork1)
sync_blocks([fork1, fork2, finalizer1, finalizer2])
# clone finalizer
finalizer2.importmasterkey(regtest_mnemonics[2]['mnemonics'])
finalizer1.importmasterkey(regtest_mnemonics[2]['mnemonics'])
disconnect_nodes(fork1, finalizer2.index)
addr = finalizer1.getnewaddress('', 'legacy')
txid1 = finalizer1.deposit(addr, 1500)
wait_until(lambda: txid1 in fork1.getrawmempool())
finalizer2.setlabel(addr, '')
txid2 = finalizer2.deposit(addr, 1500)
assert_equal(txid1, txid2)
connect_nodes(fork1, finalizer2.index)
generate_block(fork1)
sync_blocks([fork1, fork2, finalizer1, finalizer2])
disconnect_nodes(fork1, finalizer1.index)
disconnect_nodes(fork1, finalizer2.index)
# pass instant finalization
# F F F
# e0 - e1 - e2 - e3 - e4[16] fork1, fork2
generate_block(fork1, count=3 + 5 + 5 + 1)
assert_equal(fork1.getblockcount(), 16)
assert_finalizationstate(fork1, {'currentEpoch': 4,
'lastJustifiedEpoch': 2,
'lastFinalizedEpoch': 2,
'validators': 1})
# change topology where forks are not connected
# finalizer1 → fork1
#
# finalizer2 → fork2
sync_blocks([fork1, fork2])
disconnect_nodes(fork1, fork2.index)
# test that same vote included on different forks
# doesn't create a slash transaction
# v1
# - e4[17, 18, 19, 20] fork1
# F F F F /
# e0 - e1 - e2 - e3 - e4[16]
# \ v1
# - e4[17, 18, 19, 20] fork2
self.wait_for_vote_and_disconnect(finalizer=finalizer1, node=fork1)
v1 = fork1.getrawtransaction(fork1.getrawmempool()[0])
generate_block(fork1, count=4)
assert_equal(fork1.getblockcount(), 20)
assert_finalizationstate(fork1, {'currentEpoch': 4,
'lastJustifiedEpoch': 3,
'lastFinalizedEpoch': 3,
'validators': 1})
self.wait_for_vote_and_disconnect(finalizer=finalizer2, node=fork2)
generate_block(fork2)
assert_raises_rpc_error(-27, 'transaction already in block chain', fork2.sendrawtransaction, v1)
assert_equal(len(fork2.getrawmempool()), 0)
generate_block(fork2, count=3)
assert_equal(fork2.getblockcount(), 20)
assert_finalizationstate(fork1, {'currentEpoch': 4,
'lastJustifiedEpoch': 3,
'lastFinalizedEpoch': 3,
'validators': 1})
self.log.info('same vote on two forks was accepted')
# test that double-vote with invalid vote signature is ignored
# and doesn't cause slashing
# v1 v2a
# - e4 - e5[21, 22] fork1
# F F F F F /
# e0 - e1 - e2 - e3 - e4[16]
# \ v1 v2a
# - e4 - e5[21, 22] fork2
generate_block(fork1)
self.wait_for_vote_and_disconnect(finalizer=finalizer1, node=fork1)
v2a = fork1.getrawtransaction(fork1.getrawmempool()[0])
generate_block(fork1)
assert_equal(fork1.getblockcount(), 22)
assert_finalizationstate(fork1, {'currentEpoch': 5,
'lastJustifiedEpoch': 4,
'lastFinalizedEpoch': 4,
'validators': 1})
generate_block(fork2)
tx_v2a = FromHex(CTransaction(), v2a)
# corrupt the 1st byte of the validator's pubkey in the commit script
# see schema in CScript::CommitScript
tx_v2a.vout[0].scriptPubKey = corrupt_script(script=tx_v2a.vout[0].scriptPubKey, n_byte=2)
assert_raises_rpc_error(-26, 'bad-vote-signature', fork2.sendrawtransaction, ToHex(tx_v2a))
assert_equal(len(fork2.getrawmempool()), 0)
self.wait_for_vote_and_disconnect(finalizer=finalizer2, node=fork2)
time.sleep(10) # slash transactions are processed every 10 sec. UNIT-E TODO: remove once optimized
assert_equal(len(fork2.getrawmempool()), 1)
v2b = fork2.getrawtransaction(fork2.getrawmempool()[0])
tx_v2b = FromHex(CTransaction(), v2b)
assert_equal(tx_v2b.get_type(), TxType.VOTE)
generate_block(fork2)
assert_equal(len(fork2.getrawmempool()), 0)
assert_equal(fork2.getblockcount(), 22)
assert_finalizationstate(fork1, {'currentEpoch': 5,
'lastJustifiedEpoch': 4,
'lastFinalizedEpoch': 4,
'validators': 1})
self.log.info('double-vote with invalid signature is ignored')
# test that valid double-vote but corrupt withdraw address
# creates slash tx it is included in the next block
# v1 v2a
# - e4 - e5[21, 22] fork1
# F F F F F /
# e0 - e1 - e2 - e3 - e4[16]
# \ v1 v2a s1
# - e4 - e5[21, 22, 23] fork2
# corrupt the 1st byte of the address in the scriptpubkey
# but keep the correct vote signature see schema in CScript::CommitScript
tx_v2a = FromHex(CTransaction(), v2a)
# Remove the signature
tx_v2a.vin[0].scriptSig = list(CScript(tx_v2a.vin[0].scriptSig))[1]
tx_v2a.vout[0].scriptPubKey = corrupt_script(script=tx_v2a.vout[0].scriptPubKey, n_byte=42)
tx_v2a = sign_transaction(finalizer2, tx_v2a)
assert_raises_rpc_error(-26, 'bad-vote-invalid', fork2.sendrawtransaction, ToHex(tx_v2a))
wait_until(lambda: len(fork2.getrawmempool()) == 1, timeout=20)
s1_hash = fork2.getrawmempool()[0]
s1 = FromHex(CTransaction(), fork2.getrawtransaction(s1_hash))
assert_equal(s1.get_type(), TxType.SLASH)
b23 = generate_block(fork2)[0]
block = FromHex(CBlock(), fork2.getblock(b23, 0))
assert_equal(len(block.vtx), 2)
block.vtx[1].rehash()
assert_equal(block.vtx[1].hash, s1_hash)
self.log.info('slash tx for double-vote was successfully created')
def run_test(self):
self.test_double_votes()
if __name__ == '__main__':
SlashTest().main()