forked from namecoin/namecoin-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
name_reorg.py
executable file
·83 lines (70 loc) · 3.28 KB
/
name_reorg.py
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
#!/usr/bin/env python3
# Copyright (c) 2014-2019 Daniel Kraft
# Distributed under the MIT/X11 software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
# Test that reorgs (undoing) work for names. This also checks that
# cleaning the mempool with respect to conflicting name registrations works.
from test_framework.names import NameTestFramework
from test_framework.util import *
class NameReorgTest (NameTestFramework):
def set_test_params (self):
self.setup_clean_chain = True
self.setup_name_test ([["-namehistory"]])
def run_test (self):
node = self.nodes[0]
node.generate (200)
# Register a name prior to forking the chain. This is used
# to test unrolling of updates (as opposed to registrations).
newA = node.name_new ("a")
newBshort = node.name_new ("b")
newBlong = node.name_new ("b")
newC = node.name_new ("c")
node.generate (10)
self.firstupdateName (0, "a", newA, "initial value")
node.generate (5)
# Build a long chain that registers "b" (to clash with
# the same registration on the short chain).
self.firstupdateName (0, "b", newBlong, "b long")
undoBlk = node.generate (20)[0]
self.checkName (0, "a", "initial value", None, False)
self.checkName (0, "b", "b long", None, False)
self.checkNameHistory (0, "a", ["initial value"])
self.checkNameHistory (0, "b", ["b long"])
node.invalidateblock (undoBlk)
# Build a short chain with an update to "a" and registrations.
assert_equal (node.getrawmempool (), [])
node.generate (1)
txidA = node.name_update ("a", "changed value")
txidB = self.firstupdateName (0, "b", newBshort, "b short")
txidC = self.firstupdateName (0, "c", newC, "c registered")
node.generate (1)
self.checkName (0, "a", "changed value", None, False)
self.checkName (0, "b", "b short", None, False)
self.checkName (0, "c", "c registered", None, False)
self.checkNameHistory (0, "a", ["initial value", "changed value"])
self.checkNameHistory (0, "b", ["b short"])
self.checkNameHistory (0, "c", ["c registered"])
# Reconsider the long chain to reorg back to it.
node.reconsiderblock (undoBlk)
self.checkName (0, "a", "initial value", None, False)
self.checkName (0, "b", "b long", None, False)
self.checkNameHistory (0, "a", ["initial value"])
self.checkNameHistory (0, "b", ["b long"])
assert_raises_rpc_error (-4, 'name not found', node.name_show, "c")
assert_raises_rpc_error (-4, 'name not found', node.name_history, "c")
# Mine another block. This should at least perform the
# non-conflicting transactions.
assert_equal (set (node.getrawmempool ()), set ([txidA, txidC]))
node.generate (1)
self.checkName (0, "a", "changed value", None, False)
self.checkName (0, "b", "b long", None, False)
self.checkName (0, "c", "c registered", None, False)
self.checkNameHistory (0, "a", ["initial value", "changed value"])
self.checkNameHistory (0, "b", ["b long"])
self.checkNameHistory (0, "c", ["c registered"])
# Check that the conflicting tx got handled properly.
assert_equal (node.getrawmempool (), [])
data = node.gettransaction (txidB)
assert data['confirmations'] <= 0
if __name__ == '__main__':
NameReorgTest ().main ()