test_quorum.py (19778B)
1 # standard imports 2 import os 3 import unittest 4 import logging 5 6 # external imports 7 from chainlib.eth.nonce import RPCNonceOracle 8 from chainlib.eth.address import is_same_address 9 from chainlib.eth.tx import receipt 10 11 # local imports 12 from erc20_transfer_authorization import TransferAuthorization 13 from erc20_transfer_authorization.unittest import TestBase 14 15 logg = logging.getLogger() 16 17 testdir = os.path.dirname(__file__) 18 19 20 class TestQuorum(TestBase): 21 22 def setUp(self): 23 super(TestQuorum, self).setUp() 24 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) 25 self.c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 26 27 for i in range(1, 5): 28 (tx_hash_hex, o) = self.c.add_writer(self.address, self.accounts[0], self.accounts[i]) 29 self.rpc.do(o) 30 o = receipt(tx_hash_hex) 31 r = self.rpc.do(o) 32 self.assertEqual(r['status'], 1) 33 34 35 def test_basic(self): 36 o = self.c.quorum_threshold(self.address, sender_address=self.accounts[0]) 37 r = self.rpc.do(o) 38 count = self.c.parse_count(r) 39 self.assertEqual(count, 1) 40 41 o = self.c.veto_threshold(self.address, sender_address=self.accounts[0]) 42 r = self.rpc.do(o) 43 count = self.c.parse_count(r) 44 self.assertEqual(count, 0) 45 46 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 3, 1) 47 self.rpc.do(o) 48 o = receipt(tx_hash_hex) 49 r = self.rpc.do(o) 50 self.assertEqual(r['status'], 1) 51 52 o = self.c.quorum_threshold(self.address, sender_address=self.accounts[0]) 53 r = self.rpc.do(o) 54 count = self.c.parse_count(r) 55 self.assertEqual(count, 3) 56 57 o = self.c.veto_threshold(self.address, sender_address=self.accounts[0]) 58 r = self.rpc.do(o) 59 count = self.c.parse_count(r) 60 self.assertEqual(count, 1) 61 62 63 def test_signer_sanity(self): 64 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 0, 0) 65 self.rpc.do(o) 66 o = receipt(tx_hash_hex) 67 r = self.rpc.do(o) 68 self.assertEqual(r['status'], 0) 69 70 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 0, 1) 71 self.rpc.do(o) 72 o = receipt(tx_hash_hex) 73 r = self.rpc.do(o) 74 self.assertEqual(r['status'], 0) 75 76 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 5, 0) 77 self.rpc.do(o) 78 o = receipt(tx_hash_hex) 79 r = self.rpc.do(o) 80 self.assertEqual(r['status'], 1) 81 82 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 6, 0) 83 self.rpc.do(o) 84 o = receipt(tx_hash_hex) 85 r = self.rpc.do(o) 86 self.assertEqual(r['status'], 0) 87 88 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 5, 5) 89 self.rpc.do(o) 90 o = receipt(tx_hash_hex) 91 r = self.rpc.do(o) 92 self.assertEqual(r['status'], 1) 93 94 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 5, 6) 95 self.rpc.do(o) 96 o = receipt(tx_hash_hex) 97 r = self.rpc.do(o) 98 self.assertEqual(r['status'], 0) 99 100 101 def test_register(self): 102 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 2, 2) 103 self.rpc.do(o) 104 105 (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[1], self.accounts[2], self.token_address, 1024) 106 self.rpc.do(o) 107 o = receipt(tx_hash_hex) 108 r = self.rpc.do(o) 109 self.assertEqual(r['status'], 1) 110 111 (tx_hash_hex, o) = self.c.yay(self.address, self.accounts[0], 1) 112 self.rpc.do(o) 113 o = receipt(tx_hash_hex) 114 r = self.rpc.do(o) 115 self.assertEqual(r['status'], 1) 116 117 o = self.c.requests(self.address, 1, sender_address=self.accounts[0]) 118 r = self.rpc.do(o) 119 request = self.c.parse_request(r) 120 self.assertTrue(is_same_address(request.sender, self.accounts[1])) 121 self.assertTrue(is_same_address(request.recipient, self.accounts[2])) 122 self.assertTrue(is_same_address(request.token, self.token_address)) 123 self.assertEqual(request.value, 1024) 124 self.assertEqual(request.serial, 1) 125 self.assertEqual(request.yay, 1) 126 self.assertEqual(request.nay, 0) 127 128 (tx_hash_hex, o) = self.c.check_result(self.address, self.accounts[0], 1) 129 self.rpc.do(o) 130 o = receipt(tx_hash_hex) 131 r = self.rpc.do(o) 132 self.assertEqual(r['status'], 1) 133 134 135 def test_veto(self): 136 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 1, 1) 137 self.rpc.do(o) 138 o = receipt(tx_hash_hex) 139 r = self.rpc.do(o) 140 self.assertEqual(r['status'], 1) 141 142 (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[1], self.accounts[2], self.token_address, 1024) 143 self.rpc.do(o) 144 o = receipt(tx_hash_hex) 145 r = self.rpc.do(o) 146 self.assertEqual(r['status'], 1) 147 148 (tx_hash_hex, o) = self.c.nay(self.address, self.accounts[0], 1) 149 self.rpc.do(o) 150 o = receipt(tx_hash_hex) 151 r = self.rpc.do(o) 152 self.assertEqual(r['status'], 1) 153 154 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 155 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 156 (tx_hash_hex, o) = c.yay(self.address, self.accounts[1], 1) 157 self.rpc.do(o) 158 o = receipt(tx_hash_hex) 159 r = self.rpc.do(o) 160 self.assertEqual(r['status'], 0) 161 162 o = self.c.requests(self.address, 1, sender_address=self.accounts[0]) 163 r = self.rpc.do(o) 164 request = self.c.parse_request(r) 165 self.assertTrue(request.is_rejected()) 166 167 168 def test_simple_yay(self): 169 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 2, 2) 170 self.rpc.do(o) 171 o = receipt(tx_hash_hex) 172 r = self.rpc.do(o) 173 self.assertEqual(r['status'], 1) 174 175 (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[1], self.accounts[2], self.token_address, 1024) 176 self.rpc.do(o) 177 o = receipt(tx_hash_hex) 178 r = self.rpc.do(o) 179 self.assertEqual(r['status'], 1) 180 181 (tx_hash_hex, o) = self.c.yay(self.address, self.accounts[0], 1) 182 self.rpc.do(o) 183 o = receipt(tx_hash_hex) 184 r = self.rpc.do(o) 185 self.assertEqual(r['status'], 1) 186 187 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 188 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 189 (tx_hash_hex, o) = c.yay(self.address, self.accounts[1], 1) 190 self.rpc.do(o) 191 o = receipt(tx_hash_hex) 192 r = self.rpc.do(o) 193 self.assertEqual(r['status'], 1) 194 195 o = self.c.requests(self.address, 1, sender_address=self.accounts[0]) 196 r = self.rpc.do(o) 197 request = self.c.parse_request(r) 198 self.assertTrue(request.is_accepted()) 199 200 201 def test_nay_before_yay(self): 202 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 2, 2) 203 self.rpc.do(o) 204 o = receipt(tx_hash_hex) 205 r = self.rpc.do(o) 206 self.assertEqual(r['status'], 1) 207 208 (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[1], self.accounts[2], self.token_address, 1024) 209 self.rpc.do(o) 210 o = receipt(tx_hash_hex) 211 r = self.rpc.do(o) 212 self.assertEqual(r['status'], 1) 213 214 (tx_hash_hex, o) = self.c.yay(self.address, self.accounts[0], 1) 215 self.rpc.do(o) 216 o = receipt(tx_hash_hex) 217 r = self.rpc.do(o) 218 self.assertEqual(r['status'], 1) 219 220 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 221 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 222 (tx_hash_hex, o) = c.nay(self.address, self.accounts[1], 1) 223 self.rpc.do(o) 224 o = receipt(tx_hash_hex) 225 r = self.rpc.do(o) 226 self.assertEqual(r['status'], 1) 227 228 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 229 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 230 (tx_hash_hex, o) = c.nay(self.address, self.accounts[2], 1) 231 self.rpc.do(o) 232 o = receipt(tx_hash_hex) 233 r = self.rpc.do(o) 234 self.assertEqual(r['status'], 1) 235 236 o = self.c.requests(self.address, 1, sender_address=self.accounts[0]) 237 r = self.rpc.do(o) 238 request = self.c.parse_request(r) 239 self.assertFalse(request.is_accepted()) 240 241 242 def test_yay_nay_yay(self): 243 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 2, 2) 244 self.rpc.do(o) 245 o = receipt(tx_hash_hex) 246 r = self.rpc.do(o) 247 self.assertEqual(r['status'], 1) 248 249 (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[1], self.accounts[2], self.token_address, 1024) 250 self.rpc.do(o) 251 o = receipt(tx_hash_hex) 252 r = self.rpc.do(o) 253 self.assertEqual(r['status'], 1) 254 255 (tx_hash_hex, o) = self.c.yay(self.address, self.accounts[0], 1) 256 self.rpc.do(o) 257 o = receipt(tx_hash_hex) 258 r = self.rpc.do(o) 259 self.assertEqual(r['status'], 1) 260 261 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 262 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 263 (tx_hash_hex, o) = c.nay(self.address, self.accounts[1], 1) 264 self.rpc.do(o) 265 o = receipt(tx_hash_hex) 266 r = self.rpc.do(o) 267 self.assertEqual(r['status'], 1) 268 269 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 270 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 271 (tx_hash_hex, o) = c.yay(self.address, self.accounts[2], 1) 272 self.rpc.do(o) 273 o = receipt(tx_hash_hex) 274 r = self.rpc.do(o) 275 self.assertEqual(r['status'], 1) 276 277 o = self.c.requests(self.address, 1, sender_address=self.accounts[0]) 278 r = self.rpc.do(o) 279 request = self.c.parse_request(r) 280 self.assertTrue(request.is_accepted()) 281 282 283 def test_yay_yay_nay(self): 284 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 2, 2) 285 self.rpc.do(o) 286 o = receipt(tx_hash_hex) 287 r = self.rpc.do(o) 288 self.assertEqual(r['status'], 1) 289 290 (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[1], self.accounts[2], self.token_address, 1024) 291 self.rpc.do(o) 292 o = receipt(tx_hash_hex) 293 r = self.rpc.do(o) 294 self.assertEqual(r['status'], 1) 295 296 (tx_hash_hex, o) = self.c.yay(self.address, self.accounts[0], 1) 297 self.rpc.do(o) 298 o = receipt(tx_hash_hex) 299 r = self.rpc.do(o) 300 self.assertEqual(r['status'], 1) 301 302 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 303 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 304 (tx_hash_hex, o) = c.yay(self.address, self.accounts[1], 1) 305 self.rpc.do(o) 306 o = receipt(tx_hash_hex) 307 r = self.rpc.do(o) 308 self.assertEqual(r['status'], 1) 309 310 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 311 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 312 (tx_hash_hex, o) = c.nay(self.address, self.accounts[2], 1) 313 self.rpc.do(o) 314 o = receipt(tx_hash_hex) 315 r = self.rpc.do(o) 316 self.assertEqual(r['status'], 0) 317 318 o = self.c.requests(self.address, 1, sender_address=self.accounts[0]) 319 r = self.rpc.do(o) 320 request = self.c.parse_request(r) 321 logg.debug('request {}'.format(request)) 322 self.assertTrue(request.is_accepted()) 323 324 325 def test_nay_yay_nay(self): 326 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 2, 2) 327 self.rpc.do(o) 328 o = receipt(tx_hash_hex) 329 r = self.rpc.do(o) 330 self.assertEqual(r['status'], 1) 331 332 (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[1], self.accounts[2], self.token_address, 1024) 333 self.rpc.do(o) 334 o = receipt(tx_hash_hex) 335 r = self.rpc.do(o) 336 self.assertEqual(r['status'], 1) 337 338 (tx_hash_hex, o) = self.c.nay(self.address, self.accounts[0], 1) 339 self.rpc.do(o) 340 o = receipt(tx_hash_hex) 341 r = self.rpc.do(o) 342 self.assertEqual(r['status'], 1) 343 344 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 345 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 346 (tx_hash_hex, o) = c.yay(self.address, self.accounts[1], 1) 347 self.rpc.do(o) 348 o = receipt(tx_hash_hex) 349 r = self.rpc.do(o) 350 self.assertEqual(r['status'], 1) 351 352 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 353 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 354 (tx_hash_hex, o) = c.nay(self.address, self.accounts[2], 1) 355 self.rpc.do(o) 356 o = receipt(tx_hash_hex) 357 r = self.rpc.do(o) 358 self.assertEqual(r['status'], 1) 359 360 o = self.c.requests(self.address, 1, sender_address=self.accounts[0]) 361 r = self.rpc.do(o) 362 request = self.c.parse_request(r) 363 self.assertTrue(request.is_rejected()) 364 365 366 def test_nay_yay_nay_yay(self): 367 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 2, 2) 368 self.rpc.do(o) 369 o = receipt(tx_hash_hex) 370 r = self.rpc.do(o) 371 self.assertEqual(r['status'], 1) 372 373 (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[1], self.accounts[2], self.token_address, 1024) 374 self.rpc.do(o) 375 o = receipt(tx_hash_hex) 376 r = self.rpc.do(o) 377 self.assertEqual(r['status'], 1) 378 379 (tx_hash_hex, o) = self.c.nay(self.address, self.accounts[0], 1) 380 self.rpc.do(o) 381 o = receipt(tx_hash_hex) 382 r = self.rpc.do(o) 383 self.assertEqual(r['status'], 1) 384 385 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 386 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 387 (tx_hash_hex, o) = c.yay(self.address, self.accounts[1], 1) 388 self.rpc.do(o) 389 o = receipt(tx_hash_hex) 390 r = self.rpc.do(o) 391 self.assertEqual(r['status'], 1) 392 393 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 394 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 395 (tx_hash_hex, o) = c.nay(self.address, self.accounts[2], 1) 396 self.rpc.do(o) 397 o = receipt(tx_hash_hex) 398 r = self.rpc.do(o) 399 self.assertEqual(r['status'], 1) 400 401 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 402 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 403 (tx_hash_hex, o) = c.yay(self.address, self.accounts[2], 1) 404 self.rpc.do(o) 405 o = receipt(tx_hash_hex) 406 r = self.rpc.do(o) 407 self.assertEqual(r['status'], 0) 408 409 o = self.c.requests(self.address, 1, sender_address=self.accounts[0]) 410 r = self.rpc.do(o) 411 request = self.c.parse_request(r) 412 logg.debug('request {}'.format(request)) 413 self.assertTrue(request.is_rejected()) 414 415 416 def test_nay_yay_nay_nay(self): 417 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 2, 2) 418 self.rpc.do(o) 419 o = receipt(tx_hash_hex) 420 r = self.rpc.do(o) 421 self.assertEqual(r['status'], 1) 422 423 (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[1], self.accounts[2], self.token_address, 1024) 424 self.rpc.do(o) 425 o = receipt(tx_hash_hex) 426 r = self.rpc.do(o) 427 self.assertEqual(r['status'], 1) 428 429 (tx_hash_hex, o) = self.c.nay(self.address, self.accounts[0], 1) 430 self.rpc.do(o) 431 o = receipt(tx_hash_hex) 432 r = self.rpc.do(o) 433 self.assertEqual(r['status'], 1) 434 435 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 436 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 437 (tx_hash_hex, o) = c.yay(self.address, self.accounts[1], 1) 438 self.rpc.do(o) 439 o = receipt(tx_hash_hex) 440 r = self.rpc.do(o) 441 self.assertEqual(r['status'], 1) 442 443 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 444 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 445 (tx_hash_hex, o) = c.nay(self.address, self.accounts[2], 1) 446 self.rpc.do(o) 447 o = receipt(tx_hash_hex) 448 r = self.rpc.do(o) 449 self.assertEqual(r['status'], 1) 450 451 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 452 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 453 (tx_hash_hex, o) = c.nay(self.address, self.accounts[2], 1) 454 self.rpc.do(o) 455 o = receipt(tx_hash_hex) 456 r = self.rpc.do(o) 457 self.assertEqual(r['status'], 0) 458 459 o = self.c.requests(self.address, 1, sender_address=self.accounts[0]) 460 r = self.rpc.do(o) 461 request = self.c.parse_request(r) 462 logg.debug('request {}'.format(request)) 463 self.assertTrue(request.is_rejected()) 464 465 466 def test_nay_majority(self): 467 (tx_hash_hex, o) = self.c.set_thresholds(self.address, self.accounts[0], 3, 0) 468 self.rpc.do(o) 469 o = receipt(tx_hash_hex) 470 r = self.rpc.do(o) 471 self.assertEqual(r['status'], 1) 472 473 (tx_hash_hex, o) = self.c.create_request(self.address, self.accounts[0], self.accounts[1], self.accounts[2], self.token_address, 1024) 474 self.rpc.do(o) 475 o = receipt(tx_hash_hex) 476 r = self.rpc.do(o) 477 self.assertEqual(r['status'], 1) 478 479 (tx_hash_hex, o) = self.c.nay(self.address, self.accounts[0], 1) 480 self.rpc.do(o) 481 o = receipt(tx_hash_hex) 482 r = self.rpc.do(o) 483 self.assertEqual(r['status'], 1) 484 485 nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) 486 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 487 (tx_hash_hex, o) = c.nay(self.address, self.accounts[1], 1) 488 self.rpc.do(o) 489 o = receipt(tx_hash_hex) 490 r = self.rpc.do(o) 491 self.assertEqual(r['status'], 1) 492 493 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 494 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 495 (tx_hash_hex, o) = c.nay(self.address, self.accounts[2], 1) 496 self.rpc.do(o) 497 o = receipt(tx_hash_hex) 498 r = self.rpc.do(o) 499 self.assertEqual(r['status'], 1) 500 501 nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) 502 c = TransferAuthorization(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) 503 (tx_hash_hex, o) = c.nay(self.address, self.accounts[2], 1) 504 self.rpc.do(o) 505 o = receipt(tx_hash_hex) 506 r = self.rpc.do(o) 507 self.assertEqual(r['status'], 0) 508 509 o = self.c.requests(self.address, 1, sender_address=self.accounts[0]) 510 r = self.rpc.do(o) 511 request = self.c.parse_request(r) 512 logg.debug('request {}'.format(request)) 513 self.assertTrue(request.is_rejected()) 514 515 516 if __name__ == '__main__': 517 unittest.main()