| 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 |
from __future__ import print_function |
| 32 |
import subprocess |
| 33 |
import os |
| 34 |
import time |
| 35 |
|
| 36 |
"""Test daemon RPC payment calls |
| 37 |
""" |
| 38 |
|
| 39 |
from framework.daemon import Daemon |
| 40 |
from framework.wallet import Wallet |
| 41 |
|
| 42 |
class RPCPaymentTest(): |
| 43 |
def run_test(self): |
| 44 |
self.make_test_signature = os.environ['MAKE_TEST_SIGNATURE'] |
| 45 |
assert len(self.make_test_signature) > 0 |
| 46 |
self.secret_key, self.public_key = self.get_keys() |
| 47 |
self.signatures = [] |
| 48 |
self.reset() |
| 49 |
self.test_access_tracking() |
| 50 |
self.test_access_mining() |
| 51 |
self.test_access_payment() |
| 52 |
self.test_access_account() |
| 53 |
self.test_free_access() |
| 54 |
|
| 55 |
def get_keys(self): |
| 56 |
output = subprocess.check_output([self.make_test_signature]).decode('utf-8').rstrip() |
| 57 |
fields = output.split() |
| 58 |
assert len(fields) == 2 |
| 59 |
return fields |
| 60 |
|
| 61 |
def refill_signatures(self): |
| 62 |
self.signatures_time = time.time() |
| 63 |
self.signatures = [] |
| 64 |
signatures = subprocess.check_output([self.make_test_signature, self.secret_key, '256']).decode('utf-8') |
| 65 |
for line in signatures.split(): |
| 66 |
self.signatures.append(line.rstrip()) |
| 67 |
|
| 68 |
def get_signature(self): |
| 69 |
if len(self.signatures) == 0 or self.signatures_time + 10 < time.time(): |
| 70 |
self.refill_signatures() |
| 71 |
s = self.signatures[0] |
| 72 |
self.signatures = self.signatures[1:] |
| 73 |
return s |
| 74 |
|
| 75 |
def reset(self): |
| 76 |
print('Resetting blockchain') |
| 77 |
daemon = Daemon(idx=1) |
| 78 |
res = daemon.get_height() |
| 79 |
daemon.pop_blocks(res.height - 1) |
| 80 |
daemon.flush_txpool() |
| 81 |
|
| 82 |
def test_access_tracking(self): |
| 83 |
print('Testing access tracking') |
| 84 |
daemon = Daemon(idx=1) |
| 85 |
|
| 86 |
res = daemon.rpc_access_tracking(True) |
| 87 |
|
| 88 |
res = daemon.rpc_access_tracking() |
| 89 |
data = sorted(res.data, key = lambda k: k['rpc']) |
| 90 |
assert len(data) == 1 |
| 91 |
entry = data[0] |
| 92 |
assert entry.rpc == 'rpc_access_tracking' |
| 93 |
assert entry.count == 1 |
| 94 |
assert entry.time >= 0 |
| 95 |
assert entry.credits == 0 |
| 96 |
|
| 97 |
daemon.get_connections() |
| 98 |
res = daemon.rpc_access_tracking() |
| 99 |
data = sorted(res.data, key = lambda k: k['rpc']) |
| 100 |
assert len(data) == 2 |
| 101 |
entry = data[0] |
| 102 |
assert entry.rpc == 'get_connections' |
| 103 |
assert entry.count == 1 |
| 104 |
assert entry.time >= 0 |
| 105 |
assert entry.credits == 0 |
| 106 |
|
| 107 |
daemon.get_connections() |
| 108 |
res = daemon.rpc_access_tracking() |
| 109 |
data = sorted(res.data, key = lambda k: k['rpc']) |
| 110 |
assert len(data) == 2 |
| 111 |
entry = data[0] |
| 112 |
assert entry.rpc == 'get_connections' |
| 113 |
assert entry.count == 2 |
| 114 |
assert entry.time >= 0 |
| 115 |
assert entry.credits == 0 |
| 116 |
|
| 117 |
daemon.get_alternate_chains() |
| 118 |
res = daemon.rpc_access_tracking() |
| 119 |
data = sorted(res.data, key = lambda k: k['rpc']) |
| 120 |
assert len(data) == 3 |
| 121 |
entry = data[0] |
| 122 |
assert entry.rpc == 'get_alternate_chains' |
| 123 |
assert entry.count == 1 |
| 124 |
assert entry.time >= 0 |
| 125 |
assert entry.credits == 0 |
| 126 |
entry = res.data[1] |
| 127 |
assert entry.rpc == 'get_connections' |
| 128 |
assert entry.count == 2 |
| 129 |
assert entry.time >= 0 |
| 130 |
assert entry.credits == 0 |
| 131 |
|
| 132 |
res = daemon.rpc_access_tracking(True) |
| 133 |
res = daemon.rpc_access_tracking() |
| 134 |
data = sorted(res.data, key = lambda k: k['rpc']) |
| 135 |
assert len(data) == 1 |
| 136 |
entry = data[0] |
| 137 |
assert entry.rpc == 'rpc_access_tracking' |
| 138 |
assert entry.count == 1 |
| 139 |
|
| 140 |
def test_access_mining(self): |
| 141 |
print('Testing access mining') |
| 142 |
daemon = Daemon(idx=1) |
| 143 |
wallet = Wallet(idx=3) |
| 144 |
|
| 145 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 146 |
assert len(res.hashing_blob) > 39 |
| 147 |
assert res.height == 1 |
| 148 |
assert res.top_hash == '418015bb9ae982a1975da7d79277c2705727a56894ba0fb246adaabb1f4632e3' |
| 149 |
assert res.credits_per_hash_found == 5000 |
| 150 |
assert res.diff == 10 |
| 151 |
assert res.credits == 0 |
| 152 |
cookie = res.cookie |
| 153 |
|
| 154 |
|
| 155 |
nonce = 0 |
| 156 |
found_valid = 0 |
| 157 |
found_invalid = 0 |
| 158 |
last_credits = 0 |
| 159 |
loop_time = time.time() |
| 160 |
while found_valid == 0 or found_invalid == 0: |
| 161 |
nonce += 1 |
| 162 |
try: |
| 163 |
res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature()) |
| 164 |
found_valid += 1 |
| 165 |
assert res.credits == last_credits + 5000 |
| 166 |
except Exception as e: |
| 167 |
found_invalid += 1 |
| 168 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 169 |
cookie = res.cookie |
| 170 |
loop_time = time.time() |
| 171 |
assert res.credits < last_credits or res.credits == 0 |
| 172 |
assert nonce < 1000 |
| 173 |
last_credits = res.credits |
| 174 |
|
| 175 |
if time.time() >= loop_time + 10: |
| 176 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 177 |
cookie = res.cookie |
| 178 |
loop_time = time.time() |
| 179 |
|
| 180 |
|
| 181 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 182 |
assert len(res.hashing_blob) > 39 |
| 183 |
assert res.height > 1 |
| 184 |
assert res.top_hash != '418015bb9ae982a1975da7d79277c2705727a56894ba0fb246adaabb1f4632e3' |
| 185 |
assert res.credits_per_hash_found == 5000 |
| 186 |
assert res.diff == 10 |
| 187 |
cookie = res.cookie |
| 188 |
|
| 189 |
res = daemon.rpc_access_data() |
| 190 |
assert len(res.entries) > 0 |
| 191 |
e = [x for x in res.entries if x['client'] == self.public_key] |
| 192 |
assert len(e) == 1 |
| 193 |
e = e[0] |
| 194 |
assert e.nonces_stale == 0 |
| 195 |
assert e.nonces_bad == found_invalid |
| 196 |
assert e.nonces_good == found_valid |
| 197 |
assert e.nonces_dupe == 0 |
| 198 |
|
| 199 |
|
| 200 |
loop_time = time.time() |
| 201 |
while last_credits == 0: |
| 202 |
nonce += 1 |
| 203 |
try: |
| 204 |
res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature()) |
| 205 |
found_valid += 1 |
| 206 |
last_credits = res.credits |
| 207 |
break |
| 208 |
except: |
| 209 |
found_invalid += 1 |
| 210 |
assert nonce < 1000 |
| 211 |
if time.time() >= loop_time + 10: |
| 212 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 213 |
cookie = res.cookie |
| 214 |
loop_time = time.time() |
| 215 |
|
| 216 |
|
| 217 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 218 |
assert res.credits == last_credits |
| 219 |
assert res.credits >= 5000 |
| 220 |
|
| 221 |
res = daemon.rpc_access_data() |
| 222 |
assert len(res.entries) > 0 |
| 223 |
e = [x for x in res.entries if x['client'] == self.public_key] |
| 224 |
assert len(e) == 1 |
| 225 |
e = e[0] |
| 226 |
assert e.nonces_stale == 0 |
| 227 |
assert e.nonces_bad == found_invalid |
| 228 |
assert e.nonces_good == found_valid |
| 229 |
assert e.nonces_dupe == 0 |
| 230 |
assert e.balance == 5000 |
| 231 |
assert e.credits_total >= 5000 |
| 232 |
|
| 233 |
|
| 234 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 235 |
cookie = res.cookie |
| 236 |
old_cookie = cookie |
| 237 |
loop_time = time.time() |
| 238 |
while True: |
| 239 |
nonce += 1 |
| 240 |
try: |
| 241 |
res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature()) |
| 242 |
found_valid += 1 |
| 243 |
break |
| 244 |
except: |
| 245 |
found_invalid += 1 |
| 246 |
assert nonce < 1000 |
| 247 |
|
| 248 |
if time.time() >= loop_time + 10: |
| 249 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 250 |
cookie = res.cookie |
| 251 |
loop_time = time.time() |
| 252 |
|
| 253 |
res = daemon.rpc_access_data() |
| 254 |
assert len(res.entries) > 0 |
| 255 |
e = [x for x in res.entries if x['client'] == self.public_key] |
| 256 |
assert len(e) == 1 |
| 257 |
e = e[0] |
| 258 |
assert e.nonces_stale == 0 |
| 259 |
assert e.nonces_bad == found_invalid |
| 260 |
assert e.nonces_good == found_valid |
| 261 |
assert e.nonces_dupe == 0 |
| 262 |
|
| 263 |
ok = False |
| 264 |
try: |
| 265 |
res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature()) |
| 266 |
except: |
| 267 |
ok = True |
| 268 |
assert ok |
| 269 |
|
| 270 |
res = daemon.rpc_access_data() |
| 271 |
assert len(res.entries) > 0 |
| 272 |
e = [x for x in res.entries if x['client'] == self.public_key] |
| 273 |
assert len(e) == 1 |
| 274 |
e = e[0] |
| 275 |
assert e.nonces_stale == 0 |
| 276 |
assert e.nonces_bad == found_invalid |
| 277 |
assert e.nonces_good == found_valid |
| 278 |
assert e.nonces_dupe == 1 |
| 279 |
|
| 280 |
|
| 281 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 282 |
cookie = res.cookie |
| 283 |
found_close_stale = 0 |
| 284 |
found_late_stale = 0 |
| 285 |
loop_time = time.time() |
| 286 |
while found_close_stale == 0 or found_late_stale == 0: |
| 287 |
nonce += 1 |
| 288 |
try: |
| 289 |
res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature()) |
| 290 |
found_close_stale += 1 |
| 291 |
found_valid += 1 |
| 292 |
time.sleep(15) |
| 293 |
except Exception as e: |
| 294 |
|
| 295 |
if "'code': -18" in str(e): |
| 296 |
found_late_stale += 1 |
| 297 |
else: |
| 298 |
found_invalid += 1 |
| 299 |
assert nonce < 1000 |
| 300 |
|
| 301 |
if time.time() >= loop_time + 10: |
| 302 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 303 |
|
| 304 |
loop_time = time.time() |
| 305 |
|
| 306 |
res = daemon.rpc_access_data() |
| 307 |
assert len(res.entries) > 0 |
| 308 |
e = [x for x in res.entries if x['client'] == self.public_key] |
| 309 |
assert len(e) == 1 |
| 310 |
e = e[0] |
| 311 |
assert e.nonces_stale == found_late_stale |
| 312 |
assert e.nonces_bad == found_invalid |
| 313 |
assert e.nonces_good == found_valid |
| 314 |
assert e.nonces_dupe == 1 |
| 315 |
|
| 316 |
|
| 317 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 318 |
nonce += 1 |
| 319 |
ok = False |
| 320 |
try: |
| 321 |
res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = old_cookie, client = self.get_signature()) |
| 322 |
except: |
| 323 |
found_late_stale += 1 |
| 324 |
ok = True |
| 325 |
assert ok |
| 326 |
|
| 327 |
res = daemon.rpc_access_data() |
| 328 |
assert len(res.entries) > 0 |
| 329 |
e = [x for x in res.entries if x['client'] == self.public_key] |
| 330 |
assert len(e) == 1 |
| 331 |
e = e[0] |
| 332 |
assert e.nonces_stale == found_late_stale |
| 333 |
assert e.nonces_bad == found_invalid |
| 334 |
assert e.nonces_good == found_valid |
| 335 |
assert e.nonces_dupe == 1 |
| 336 |
|
| 337 |
def test_access_payment(self): |
| 338 |
print('Testing access payment') |
| 339 |
daemon = Daemon(idx=1) |
| 340 |
wallet = Wallet(idx=3) |
| 341 |
|
| 342 |
|
| 343 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 344 |
credits = res.credits |
| 345 |
cookie = res.cookie |
| 346 |
nonce = 0 |
| 347 |
while credits <= 100: |
| 348 |
nonce += 1 |
| 349 |
try: |
| 350 |
res = daemon.rpc_access_submit_nonce(nonce = nonce, cookie = cookie, client = self.get_signature()) |
| 351 |
break |
| 352 |
except: |
| 353 |
pass |
| 354 |
assert nonce < 1000 |
| 355 |
|
| 356 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 357 |
credits = res.credits |
| 358 |
assert credits > 0 |
| 359 |
|
| 360 |
res = daemon.get_info(client = self.get_signature()) |
| 361 |
assert res.credits == credits - 1 |
| 362 |
credits = res.credits |
| 363 |
|
| 364 |
res = daemon.generateblocks('42ey1afDFnn4886T7196doS9GPMzexD9gXpsZJDwVjeRVdFCSoHnv7KPbBeGpzJBzHRCAs9UxqeoyFQMYbqSWYTfJJQAWDm', 100) |
| 365 |
block_hashes = res.blocks |
| 366 |
|
| 367 |
|
| 368 |
res = daemon.getblockheadersrange(0, 0, client = self.get_signature()) |
| 369 |
assert res.credits == credits - 1 |
| 370 |
credits = res.credits |
| 371 |
|
| 372 |
|
| 373 |
res = daemon.getblockheadersrange(1, 100, client = self.get_signature()) |
| 374 |
assert res.credits < credits - 1 |
| 375 |
credits = res.credits |
| 376 |
|
| 377 |
|
| 378 |
res = daemon.rpc_access_pay(payment = 1, paying_for = 'foo', client = self.get_signature()) |
| 379 |
assert res.credits == credits - 1 |
| 380 |
res = daemon.rpc_access_pay(payment = 4, paying_for = 'bar', client = self.get_signature()) |
| 381 |
assert res.credits == credits - 5 |
| 382 |
res = daemon.rpc_access_pay(payment = credits, paying_for = 'baz', client = self.get_signature()) |
| 383 |
assert "PAYMENT REQUIRED" in res.status |
| 384 |
res = daemon.rpc_access_pay(payment = 2, paying_for = 'quux', client = self.get_signature()) |
| 385 |
assert res.credits == credits - 7 |
| 386 |
res = daemon.rpc_access_pay(payment = 3, paying_for = 'bar', client = self.get_signature()) |
| 387 |
assert res.credits == credits - 10 |
| 388 |
|
| 389 |
|
| 390 |
ok = False |
| 391 |
try: res = daemon.get_output_histogram(amounts = [], client = self.get_signature()) |
| 392 |
except Exception as e: print('e: ' + str(e)); ok = "PAYMENT REQUIRED" in e.status |
| 393 |
assert ok or "PAYMENT REQUIRED" in res.status |
| 394 |
|
| 395 |
def test_access_account(self): |
| 396 |
print('Testing access account') |
| 397 |
daemon = Daemon(idx=1) |
| 398 |
wallet = Wallet(idx=3) |
| 399 |
|
| 400 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 401 |
credits = res.credits |
| 402 |
res = daemon.rpc_access_account(self.get_signature(), 0) |
| 403 |
assert res.credits == credits |
| 404 |
res = daemon.rpc_access_account(self.get_signature(), 50) |
| 405 |
assert res.credits == credits + 50 |
| 406 |
res = daemon.rpc_access_account(self.get_signature(), -10) |
| 407 |
assert res.credits == credits + 40 |
| 408 |
res = daemon.rpc_access_account(self.get_signature(), -(credits + 50)) |
| 409 |
assert res.credits == 0 |
| 410 |
res = daemon.rpc_access_account(self.get_signature(), 2**63 - 5) |
| 411 |
assert res.credits == 2**63 - 5 |
| 412 |
res = daemon.rpc_access_account(self.get_signature(), 2**63 - 1) |
| 413 |
assert res.credits == 2**64 - 6 |
| 414 |
res = daemon.rpc_access_account(self.get_signature(), 2) |
| 415 |
assert res.credits == 2**64 - 4 |
| 416 |
res = daemon.rpc_access_account(self.get_signature(), 8) |
| 417 |
assert res.credits == 2**64 - 1 |
| 418 |
res = daemon.rpc_access_account(self.get_signature(), -1) |
| 419 |
assert res.credits == 2**64 - 2 |
| 420 |
res = daemon.rpc_access_account(self.get_signature(), -(2**63 - 1)) |
| 421 |
assert res.credits == 2**64 - 2 -(2**63 - 1) |
| 422 |
res = daemon.rpc_access_account(self.get_signature(), -(2**63 - 1)) |
| 423 |
assert res.credits == 0 |
| 424 |
|
| 425 |
def test_free_access(self): |
| 426 |
print('Testing free access') |
| 427 |
daemon = Daemon(idx=0) |
| 428 |
wallet = Wallet(idx=0) |
| 429 |
|
| 430 |
res = daemon.rpc_access_info(client = self.get_signature()) |
| 431 |
assert res.credits_per_hash_found == 0 |
| 432 |
assert res.diff == 0 |
| 433 |
assert res.credits == 0 |
| 434 |
|
| 435 |
res = daemon.get_info(client = self.get_signature()) |
| 436 |
assert res.credits == 0 |
| 437 |
|
| 438 |
|
| 439 |
res = daemon.rpc_access_submit_nonce(nonce = 0, cookie = 0, client = self.get_signature()) |
| 440 |
assert res.credits == 0 |
| 441 |
|
| 442 |
|
| 443 |
class Guard: |
| 444 |
def __enter__(self): |
| 445 |
for i in range(4): |
| 446 |
Wallet(idx = i).auto_refresh(False) |
| 447 |
def __exit__(self, exc_type, exc_value, traceback): |
| 448 |
for i in range(4): |
| 449 |
Wallet(idx = i).auto_refresh(True) |
| 450 |
|
| 451 |
if __name__ == '__main__': |
| 452 |
with Guard() as guard: |
| 453 |
RPCPaymentTest().run_test() |