Repository navigation
Expand file tree
/
Copy pathtest_nonce_validate.py
More file actions
233 lines (210 loc) · 10.1 KB
/
Copy pathtest_nonce_validate.py
File metadata and controls
233 lines (210 loc) · 10.1 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Copyright 2026 Quantova Inc
# SPDX-License-Identifier: Apache-2.0 OR MIT
import json
import os
import sys
import threading
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
try:
import quantova_qcore as qcore
except ModuleNotFoundError:
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "python"))
import quantova_qcore as qcore
state = {"nonce": 0, "submitted": 0, "head": 10, "verdict": "accepted"}
class Handler(BaseHTTPRequestHandler):
def log_message(self, *args):
pass
def do_POST(self):
length = int(self.headers.get("Content-Length", 0))
body = json.loads(self.rfile.read(length) or b"{}")
def send(obj, code=200):
payload = json.dumps(obj).encode()
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
if self.path == "/v1/node_info":
send({"chain_id": "Q-dev-net-1", "head_height": state["head"], "genesis_hash": "0000000000000000000000000000000000000000000000000000000000000000", "denomination": "Quon",
"fee": {"transfer_quon": "500", "quon_per_qtov": "1000000"}, "version": "test", "protocol": 2})
elif self.path == "/v1/get_account":
reply = {"nonce": state["nonce"], "balance": "0", "scheme": 1, "has_key": True}
if not state.get("anonymous"):
reply["address"] = body["address"]
send(reply)
elif self.path == "/v1/submit_transaction":
state["submitted"] += 1
if state["verdict"] == "rejected":
send({"verdict": "rejected", "reason": "insufficient_funds"})
else:
send({"verdict": "accepted", "state": "fresh", "tx_id": "Qtxabc"})
else:
send({"error": "unknown_method", "message": self.path}, 404)
def clocked(port):
url = f"http://127.0.0.1:{port}"
return qcore.Client(url, network=qcore.Network(name="custom", rpc_url=url, genesis_secs=int(time.time()) - 1000, blocks_per_sec=1))
def fail(message):
print("FAIL " + message)
sys.exit(1)
UNSIGNED_TAIL_HEX = 2 * (1 + 8 + 3309)
def unsigned(tx_hex):
return tx_hex[:len(tx_hex) - UNSIGNED_TAIL_HEX]
def main():
for bad in (1.5, 4.9, 2.0, -1, 2 ** 64, 2 ** 64 - 1, 2 ** 53, 9007199254740993, "1.5", "5", " 5", "+5", "5_0", "\u0665", True):
try:
qcore._gateway_integer(bad, "nonce")
fail(f"a bad nonce {bad!r} was accepted")
except RuntimeError:
pass
for good, expected in ((5, 5), (2 ** 53 - 1, 2 ** 53 - 1)):
if qcore._gateway_integer(good, "nonce") != expected:
fail(f"a good nonce {good!r} was not accepted exactly")
for bad in ("+500", " 500", "500 ", "5_00", "\u0665\u0660\u0660", "", 500, None):
try:
qcore._transfer_fee({"fee": {"transfer_quon": bad}})
fail(f"a bad transfer fee {bad!r} was accepted")
except RuntimeError:
pass
if qcore._transfer_fee({"fee": {"transfer_quon": "500"}}) != 500:
fail("a plain digit transfer fee was not accepted")
server = HTTPServer(("127.0.0.1", 0), Handler)
threading.Thread(target=server.serve_forever, daemon=True).start()
client = clocked(server.server_address[1])
seed = bytearray.fromhex("0b" * 32)
to = qcore.address(seed, 1)
state["nonce"] = 4.9
try:
client.transfer(seed, 0, to, "1000", "1000000")
fail("a float nonce from the gateway was signed")
except RuntimeError:
pass
if state["submitted"] != 0:
fail("a refused float nonce still reached submit")
state["nonce"] = 7
client.transfer(seed, 0, to, "1000", "1000000")
if state["submitted"] != 1:
fail("an honest nonce did not submit exactly once")
state["nonce"] = 0
fresh = clocked(server.server_address[1])
try:
clocked(server.server_address[1]).transfer(seed, 0, to, "1000", "1000000")
fail("a second client in the process signed again at a nonce the first one used")
except RuntimeError as err:
if "already reached" not in str(err):
fail("unclear shared session error: " + str(err))
signed, _ = fresh.transfer(seed, 2, to, "1000", "1000000")
chain_id = qcore.chain_id_from_name("Q-dev-net-1")
expected = json.loads(qcore.sign_transfer(seed, 2, to, 1000, 0, 500, chain_id, 310, 0, "00" * 32))
if unsigned(signed["tx_hex"]) != unsigned(expected["tx_hex"]):
fail("a client transfer did not expire 300 blocks past the head")
later = json.loads(qcore.sign_transfer(seed, 2, to, 1000, 0, 500, chain_id, 311, 0, "00" * 32))
if unsigned(later["tx_hex"]) == unsigned(expected["tx_hex"]):
fail("the validity window is not part of what is signed")
try:
qcore.sign_transfer(seed, 0, to, 1000, 0, 500, chain_id, 0, 0, "00" * 32)
fail("a deadline of zero that never expires was signed")
except ValueError:
pass
held = state["submitted"]
for reported in (7, 3):
state["nonce"] = reported
for path in (
lambda: client.call(seed, 0, to, "01", 21000, "1000000", expected_nonce=5),
lambda: client.register(seed, 0, "1000000", expected_nonce=5),
):
try:
path()
fail("an expected nonce other than the reported one was signed")
except RuntimeError as err:
if "you expected 5" not in str(err):
fail("unclear expected nonce error: " + str(err))
if state["submitted"] != held:
fail("a contradicted expected nonce still reached submit")
state["nonce"] = 0
try:
client.call(seed, 0, to, "01", 21000, "499", expected_nonce=0)
fail("a call whose locked fee passes the ceiling was signed")
except ValueError as err:
if "above the maximum" not in str(err):
fail("unclear locked fee error: " + str(err))
client.call(seed, 0, to, "01", 21000, "1000000", expected_nonce=0)
stuck = clocked(server.server_address[1])
state["nonce"] = 3
stuck.transfer(seed, 3, to, "1000", "1000000")
try:
stuck.transfer(seed, 3, to, "1000", "1000000")
fail("a second signature at a held nonce was produced without the caller naming it")
except RuntimeError as err:
if "already signed" not in str(err) and "already reached" not in str(err):
fail("unclear held nonce error: " + str(err))
signed, _ = stuck.transfer(seed, 3, to, "1000", "1000000", expected_nonce=3)
again = json.loads(qcore.sign_transfer(seed, 3, to, 1000, 3, 500, chain_id, 310, 0, "00" * 32))
if unsigned(signed["tx_hex"]) != unsigned(again["tx_hex"]):
fail("a submission that never landed pushed the next one past the nonce the chain admits")
state["nonce"] = 9
signed, _ = stuck.transfer(seed, 3, to, "1000", "1000000")
at_nine = json.loads(qcore.sign_transfer(seed, 3, to, 1000, 9, 500, chain_id, 310, 0, "00" * 32))
if unsigned(signed["tx_hex"]) != unsigned(at_nine["tx_hex"]):
fail("a gateway nonce above the local one must be signed at, not refused")
if stuck._nonces.next_nonce("Q-dev-net-1/" + signed["from"]) != 10:
fail("the local next nonce did not follow the highest nonce seen")
retry = clocked(server.server_address[1])
state["nonce"] = 3
state["verdict"] = "rejected"
_, outcome = retry.transfer(seed, 4, to, "1", "1000000")
if outcome["verdict"] != "rejected":
fail("the stub gateway should reject this send")
state["verdict"] = "accepted"
try:
retry.transfer(seed, 4, to, "2", "1000000")
fail("a gateway rejection must not free a signed nonce on its own")
except RuntimeError as err:
if "already signed" not in str(err) and "already reached" not in str(err):
fail("unclear held nonce error: " + str(err))
_, outcome = retry.transfer(seed, 4, to, "2", "1000000", expected_nonce=3)
if outcome["verdict"] != "accepted":
fail("naming the nonce explicitly did not override the hold")
retry._validity({"chain_id": "Q-dev-net-1", "head_height": 250, "genesis_hash": "0000000000000000000000000000000000000000000000000000000000000000"})
try:
retry._validity({"chain_id": "Q-dev-net-1", "head_height": 200, "genesis_hash": "0000000000000000000000000000000000000000000000000000000000000000"})
fail("a head below the highest one seen was accepted")
except RuntimeError as err:
if "below the 250" not in str(err):
fail("unclear head floor error: " + str(err))
state["head"] = 400
try:
retry.transfer(seed, 4, to, "4", "1000000")
fail("a gateway head past the deadline freed a nonce before blocks could have been made")
except RuntimeError as err:
if "already signed" not in str(err) and "already reached" not in str(err):
fail("unclear held nonce error: " + str(err))
state["nonce"] = 4
_, outcome = retry.transfer(seed, 4, to, "4", "1000000")
if outcome["verdict"] != "accepted":
fail("a nonce the gateway reports past an accepted send was not signed")
state["verdict"] = "rejected"
state["nonce"] = 5
retry.transfer(seed, 4, to, "5", "1000000")
state["verdict"] = "accepted"
state["nonce"] = 6
try:
retry.transfer(seed, 4, to, "5", "1000000")
fail("a gateway that rejected a send and then reported the next nonce got a second signature")
except RuntimeError as err:
if "was not accepted" not in str(err):
fail("unclear skipped nonce error: " + str(err))
_, outcome = retry.transfer(seed, 4, to, "5", "1000000", expected_nonce=6)
if outcome["verdict"] != "accepted":
fail("naming the nonce did not let the caller vouch for the earlier send")
state["head"] = 10
state["anonymous"] = True
try:
client.account(to)
fail("an account reply naming no address was trusted")
except RuntimeError:
pass
state["anonymous"] = False
print("ok nonce validate")
if __name__ == "__main__":
main()