forked from dtr-org/unit-e
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp2p_invalid_stake.py
More file actions
executable file
·71 lines (53 loc) · 2.34 KB
/
Copy pathp2p_invalid_stake.py
File metadata and controls
executable file
·71 lines (53 loc) · 2.34 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
#!/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 the wallet accounts properly when there is a double-spend conflict."""
from test_framework.test_framework import UnitETestFramework
from test_framework.util import assert_equal
from test_framework.mininode import P2PInterface
from test_framework.messages import msg_block
from test_framework.blocktools import (get_tip_snapshot_meta, create_coinbase, sign_transaction, create_block)
class TestNode(P2PInterface):
reject_messages = []
def on_reject(self, message):
self.reject_messages.append(message)
def get_staking_coin(node):
unspent_outputs = node.listunspent()
assert len(unspent_outputs) > 0
return unspent_outputs[0]
def build_block_on_tip(node, staking_coin):
tip = node.getblockheader(node.getbestblockhash())
height = tip['height']
snapshot_meta = get_tip_snapshot_meta(node)
coinbase = create_coinbase(height + 1, staking_coin, snapshot_meta.hash)
coinbase = sign_transaction(node, coinbase)
coinbase.rehash()
block = create_block(int(tip['hash'], 16), coinbase, tip["mediantime"] + 1)
block.solve()
return block
class InvalidStakeTest(UnitETestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [["-whitelist=127.0.0.1"]]
def run_test(self):
node = self.nodes[0]
self.setup_stake_coins(node)
p2p = node.add_p2p_connection(TestNode())
p2p.wait_for_verack()
assert_equal(node.getblockcount(), 0)
coin = get_staking_coin(node)
# Check that already-spent coin is not accepted
block = build_block_on_tip(node, coin)
p2p.send_and_ping(msg_block(block))
assert_equal(len(p2p.reject_messages), 0)
assert_equal(node.getblockcount(), 1)
block = build_block_on_tip(node, coin)
p2p.send_and_ping(msg_block(block))
assert_equal(len(p2p.reject_messages), 1)
assert_equal(p2p.reject_messages[-1].message, b'block')
assert_equal(p2p.reject_messages[-1].reason, b'bad-stake-not-found')
assert_equal(node.getblockcount(), 1)
if __name__ == '__main__':
InvalidStakeTest().main()