github.com/platonnetwork/platon-go@v0.7.6/cases/tests/rpc/test_platon.py (about) 1 import pytest 2 from client_sdk_python.eth import Eth 3 import allure 4 5 from eth_utils import ( 6 is_boolean, 7 is_bytes, 8 is_checksum_address, 9 is_dict, 10 is_integer, 11 is_list_like, 12 is_same_address, 13 is_string, 14 ) 15 from hexbytes import HexBytes 16 from client_sdk_python.exceptions import ( 17 InvalidAddress, 18 ) 19 20 from common.log import log 21 import time 22 23 UNKNOWN_ADDRESS = '0xdEADBEeF00000000000000000000000000000000' 24 UNKNOWN_HASH = '0xdeadbeef00000000000000000000000000000000000000000000000000000000' 25 COMMON_ADDRESS = '0x55bfd49472fd41211545b01713a9c3a97af78b05' 26 27 28 @pytest.fixture(scope="module") 29 def unlocked_account(global_test_env): 30 env = global_test_env 31 node = env.get_rand_node() 32 address = env.account.generate_account_in_node(node, '123456', 100000000000000000000000) 33 env.account.unlock_account(node, address) 34 return { 35 'address': address, 36 'node': node 37 } 38 39 40 @pytest.fixture(scope="module") 41 def platon_connect(global_test_env): 42 env = global_test_env 43 node = env.get_rand_node() 44 yield node.eth 45 46 47 @pytest.fixture(scope="module") 48 def block_with_txn(global_test_env): 49 env = global_test_env 50 node = env.get_rand_node() 51 account = env.account.account_with_money 52 res = env.account.sendTransaction(node.web3, '', account['address'], account['address'], node.eth.gasPrice, 21000, 10000) 53 platon = Eth(node.web3) 54 return platon.getBlock(res['blockNumber']) 55 56 57 @pytest.fixture(scope="module") 58 def empty_block(platon_connect): 59 return platon_connect.getBlock(3) 60 61 62 # return block with contract_address 63 @pytest.fixture(scope="module") 64 def block_with_txn_with_log(global_test_env): 65 env = global_test_env 66 node = env.get_rand_node() 67 plan = [{"Epoch": 1, "Amount": 1000000}] 68 res = env.account.create_restricting_plan(node.web3, COMMON_ADDRESS, plan, env.account.account_with_money['address'], node.eth.gasPrice * 2, 300000) 69 platon = Eth(node.web3) 70 return platon.getBlock(res['blockNumber']) 71 72 73 class TestPlaton(): 74 75 @pytest.mark.P1 76 def test_getbalance(self, global_test_env, platon_connect): 77 env = global_test_env 78 account = env.account.get_rand_account() 79 balance = platon_connect.getBalance(account['address']) 80 assert balance >= 0, 'The balance of the account is equal' 81 82 @pytest.mark.P1 83 def test_getbalance_without_money(self, global_test_env): 84 node = global_test_env.get_rand_node() 85 address = global_test_env.account.generate_account_in_node(node, "123456") 86 balance = node.eth.getBalance(address) 87 assert balance == 0, 'The balance of the account is equal' 88 89 @allure.title("Get block number") 90 @pytest.mark.P1 91 def test_BlockNumber(self, platon_connect): 92 """ 93 test platon.getBlockNumber() 94 """ 95 block_number = platon_connect.blockNumber 96 assert is_integer(block_number) 97 assert block_number >= 0 98 99 @allure.title("Get protocol version") 100 @pytest.mark.P1 101 def test_ProtocolVersion(self, platon_connect): 102 protocol_version = platon_connect.protocolVersion 103 assert is_string(protocol_version) 104 assert protocol_version.isdigit() 105 106 @allure.title("Get synchronization status") 107 @pytest.mark.P1 108 def test_syncing(self, platon_connect): 109 syncing = platon_connect.syncing 110 assert is_boolean(syncing) or is_dict(syncing) 111 if is_boolean(syncing): 112 assert syncing is False 113 elif is_dict(syncing): 114 assert 'startingBlock' in syncing 115 assert 'currentBlock' in syncing 116 assert 'highestBlock' in syncing 117 118 assert is_integer(syncing['startingBlock']) 119 assert is_integer(syncing['currentBlock']) 120 assert is_integer(syncing['highestBlock']) 121 122 @allure.title("Get gas price") 123 @pytest.mark.P1 124 def test_gasPrice(self, platon_connect): 125 gas_price = platon_connect.gasPrice 126 assert is_integer(gas_price) 127 assert gas_price > 0 128 129 @allure.title("Get the number of node accounts") 130 @pytest.mark.P1 131 def test_accounts(self, global_test_env): 132 env = global_test_env 133 node = env.get_rand_node() 134 135 platon = Eth(node.web3) 136 137 accounts_before = platon.accounts 138 i = 0 139 while i < 10: 140 env.account.generate_account_in_node(node, '123456') 141 i += 1 142 accounts_after = platon.accounts 143 144 assert is_list_like(accounts_after) 145 assert len(accounts_after) == len(accounts_before) + 10 146 assert all(( 147 is_checksum_address(account) 148 for account 149 in accounts_after 150 )) 151 152 # @allure.title(get storage") 153 # @pytest.mark.P1 154 # def test_getStorageAt(self, global_test_env): 155 # env = global_test_env 156 # node = env.get_rand_node() 157 # account = env.account.get_rand_account() 158 # platon = Eth(node.web3) 159 # 160 # storage = platon.getStorageAt(account['address'], 0) 161 # assert isinstance(storage, HexBytes) 162 163 # @allure.title("get storage with a nonexistent address") 164 # @pytest.mark.P1 165 # def test_getStorageAt_invalid_address(self, platon_connect): 166 # with pytest.raises(InvalidAddress): 167 # platon_connect.getStorageAt(UNKNOWN_ADDRESS.lower(), 0) 168 169 @allure.title("Get transaction count") 170 @pytest.mark.P1 171 def test_getTransactionCount(self, global_test_env): 172 env = global_test_env 173 node = env.get_rand_node() 174 platon = Eth(node.web3) 175 transaction_count = platon.getTransactionCount(env.account.get_rand_account()['address']) 176 assert is_integer(transaction_count) 177 assert transaction_count >= 0 178 179 @allure.title("Get the number of transactions using a nonexistent account") 180 @pytest.mark.P1 181 def test_getTransactionCount_invalid_address(self, platon_connect): 182 with pytest.raises(InvalidAddress): 183 platon_connect.getTransactionCount(UNKNOWN_ADDRESS.lower()) 184 185 @allure.title("Get the number of empty block transactions using hash") 186 @pytest.mark.P1 187 def test_getBlockTransactionCountByHash_empty_block(self, platon_connect, empty_block): 188 transaction_count = platon_connect.getBlockTransactionCount(empty_block['hash']) 189 assert is_integer(transaction_count) 190 assert transaction_count == 0 191 192 @allure.title("Get the number of empty block transactions using block number") 193 @pytest.mark.P1 194 def test_platon_getBlockTransactionCountByNumber_empty_block(self, platon_connect, empty_block): 195 transaction_count = platon_connect.getBlockTransactionCount(empty_block['number']) 196 assert is_integer(transaction_count) 197 assert transaction_count == 0 198 199 @pytest.mark.P1 200 def test_platon_getBlockTransactionCountByHash_block_with_txn(self, platon_connect, block_with_txn): 201 transaction_count = platon_connect.getBlockTransactionCount(block_with_txn['hash']) 202 assert is_integer(transaction_count) 203 assert transaction_count >= 1 204 205 @pytest.mark.P1 206 def test_platon_getBlockTransactionCountByNumber_block_with_txn(self, platon_connect, block_with_txn): 207 transaction_count = platon_connect.getBlockTransactionCount(block_with_txn['number']) 208 assert is_integer(transaction_count) 209 assert transaction_count >= 1 210 211 # def test_eth_getCode(self, global_test_env): 212 # # Todo: create a contract 213 # env = global_test_env 214 # node = env.get_rand_node() 215 # platon = Eth(node.connect_node()) 216 # 217 # account = env.aacount.get_rand_account() 218 # 219 # code = platon.getCode(account) 220 # assert isinstance(code, HexBytes) 221 # assert len(code) > 0 222 223 # def test_eth_getCode_invalid_address(self, global_test_env): 224 # env = global_test_env 225 # node = env.get_rand_node() 226 # platon = Eth(node.connect_node()) 227 # with pytest.raises(InvalidAddress): 228 # platon.getCode(UNKNOWN_ADDRESS) 229 # 230 # def test_eth_getCode_with_block_identifier(self, global_test_env): 231 # #code = web3.eth.getCode(emitter_contract.address, block_identifier=web3.eth.blockNumber) 232 # assert isinstance(code, HexBytes) 233 # assert len(code) > 0 234 235 @pytest.mark.P1 236 def test_platon_sign(self, unlocked_account): 237 238 platon = Eth(unlocked_account['node'].web3) 239 240 signature = platon.sign( 241 unlocked_account['address'], text='Message tö sign. Longer than hash!' 242 ) 243 assert is_bytes(signature) 244 assert len(signature) == 32 + 32 + 1 245 246 # test other formats 247 hexsign = platon.sign( 248 unlocked_account['address'], 249 hexstr='0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821' 250 ) 251 assert hexsign == signature 252 253 intsign = platon.sign( 254 unlocked_account['address'], 255 0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821 256 ) 257 assert intsign == signature 258 259 bytessign = platon.sign( 260 unlocked_account['address'], b'Message t\xc3\xb6 sign. Longer than hash!' 261 ) 262 assert bytessign == signature 263 264 new_signature = platon.sign( 265 unlocked_account['address'], text='different message is different' 266 ) 267 assert new_signature != signature 268 269 @pytest.mark.P1 270 def test_platon_sendTransaction_addr_checksum_required(self, unlocked_account): 271 272 platon = Eth(unlocked_account['node'].web3) 273 274 address = unlocked_account['address'].lower() 275 txn_params = { 276 'from': address, 277 'to': address, 278 'value': 1, 279 'gas': 21000, 280 'gasPrice': platon.gasPrice, 281 } 282 with pytest.raises(InvalidAddress): 283 invalid_params = dict(txn_params, **{'from': UNKNOWN_ADDRESS}) 284 platon.sendTransaction(invalid_params) 285 286 with pytest.raises(InvalidAddress): 287 invalid_params = dict(txn_params, **{'to': UNKNOWN_ADDRESS}) 288 platon.sendTransaction(invalid_params) 289 290 @pytest.mark.P1 291 def test_platon_sendTransaction(self, unlocked_account): 292 293 platon = Eth(unlocked_account['node'].web3) 294 295 txn_params = { 296 'from': unlocked_account['address'], 297 'to': unlocked_account['address'], 298 'value': 1, 299 'gas': 21000, 300 'gasPrice': platon.gasPrice, 301 } 302 txn_hash = platon.sendTransaction(txn_params) 303 txn = platon.getTransaction(txn_hash) 304 305 assert is_same_address(txn['from'], txn_params['from']) 306 assert is_same_address(txn['to'], txn_params['to']) 307 assert txn['value'] == 1 308 assert txn['gas'] == 21000 309 assert txn['gasPrice'] == txn_params['gasPrice'] 310 311 @pytest.mark.P1 312 def test_platon_sendTransaction_withWrongAddress(self, unlocked_account): 313 314 platon = Eth(unlocked_account['node'].web3) 315 316 txn_params = { 317 'from': UNKNOWN_ADDRESS, 318 'to': unlocked_account['address'], 319 'value': 1, 320 'gas': 21000, 321 'gasPrice': platon.gasPrice, 322 } 323 with pytest.raises(ValueError): 324 platon.sendTransaction(txn_params) 325 326 @pytest.mark.P1 327 def test_platon_sendTransaction_withoutUnlock(self, global_test_env, platon_connect): 328 account = global_test_env.account.get_rand_account() 329 txn_params = { 330 'from': account['address'], 331 'to': account['address'], 332 'value': 1, 333 'gas': 21000, 334 'gasPrice': platon_connect.gasPrice, 335 } 336 with pytest.raises(ValueError): 337 platon_connect.sendTransaction(txn_params) 338 339 @pytest.mark.P1 340 def test_platon_sendTransaction_with_nonce(self, unlocked_account): 341 342 platon = Eth(unlocked_account['node'].web3) 343 txn_params = { 344 'from': unlocked_account['address'], 345 'to': unlocked_account['address'], 346 'value': 1, 347 'gas': 21000, 348 # Increased gas price to ensure transaction hash different from other tests 349 'gasPrice': platon.gasPrice * 2, 350 'nonce': platon.getTransactionCount(unlocked_account['address']), 351 } 352 txn_hash = platon.sendTransaction(txn_params) 353 txn = platon.getTransaction(txn_hash) 354 355 assert is_same_address(txn['from'], txn_params['from']) 356 assert is_same_address(txn['to'], txn_params['to']) 357 assert txn['value'] == 1 358 assert txn['gas'] == 21000 359 assert txn['gasPrice'] == txn_params['gasPrice'] 360 # assert txn['nonce'] == txn_params['nonce'] 361 362 @pytest.mark.P1 363 def test_platon_replaceTransaction(self, unlocked_account): 364 platon = Eth(unlocked_account['node'].web3) 365 txn_params = { 366 'from': unlocked_account['address'], 367 'to': unlocked_account['address'], 368 'value': 3, 369 'gas': 21000, 370 'gasPrice': platon.gasPrice, 371 'nonce': 1000, 372 } 373 txn_hash = platon.sendTransaction(txn_params) 374 txn_params['gasPrice'] = platon.gasPrice * 2 375 replace_txn_hash = platon.replaceTransaction(txn_hash, txn_params) 376 replace_txn = platon.getTransaction(replace_txn_hash) 377 378 assert is_same_address(replace_txn['from'], txn_params['from']) 379 assert is_same_address(replace_txn['to'], txn_params['to']) 380 assert replace_txn['value'] == 3 381 assert replace_txn['gas'] == 21000 382 assert replace_txn['gasPrice'] == txn_params['gasPrice'] 383 384 @pytest.mark.P1 385 def test_platon_replaceTransaction_non_existing_transaction(self, unlocked_account): 386 platon = Eth(unlocked_account['node'].web3) 387 388 txn_params = { 389 'from': unlocked_account['address'], 390 'to': unlocked_account['address'], 391 'value': 1, 392 'gas': 21000, 393 'gasPrice': platon.gasPrice, 394 } 395 with pytest.raises(ValueError): 396 platon.replaceTransaction( 397 '0x98e8cc09b311583c5079fa600f6c2a3bea8611af168c52e4b60b5b243a441997', 398 txn_params 399 ) 400 401 # auto mine is enabled for this test 402 @pytest.mark.P1 403 def test_platon_replaceTransaction_already_mined(self, unlocked_account): 404 405 platon = Eth(unlocked_account['node'].web3) 406 address = unlocked_account['address'] 407 408 txn_params = { 409 'from': address, 410 'to': address, 411 'value': 76, 412 'gas': 21000, 413 'gasPrice': platon.gasPrice * 4, 414 } 415 txn_hash = platon.sendTransaction(txn_params) 416 txn_params['gasPrice'] = platon.gasPrice * 5 417 platon.waitForTransactionReceipt(txn_hash) 418 with pytest.raises(ValueError): 419 platon.replaceTransaction(txn_hash, txn_params) 420 421 @pytest.mark.P1 422 def test_platon_replaceTransaction_incorrect_nonce(self, unlocked_account): 423 platon = Eth(unlocked_account['node'].web3) 424 address = unlocked_account['address'] 425 txn_params = { 426 'from': address, 427 'to': address, 428 'value': 1, 429 'gas': 21000, 430 'gasPrice': platon.gasPrice, 431 } 432 txn_hash = platon.sendTransaction(txn_params) 433 txn = platon.getTransaction(txn_hash) 434 435 txn_params['gasPrice'] = platon.gasPrice * 2 436 txn_params['nonce'] = int(txn['nonce'], 16) + 1 437 with pytest.raises(ValueError): 438 platon.replaceTransaction(txn_hash, txn_params) 439 440 @pytest.mark.P1 441 def test_platon_replaceTransaction_gas_price_too_low(self, unlocked_account): 442 platon = Eth(unlocked_account['node'].web3) 443 address = unlocked_account['address'] 444 txn_params = { 445 'from': address, 446 'to': address, 447 'value': 1, 448 'gas': 21000, 449 'gasPrice': platon.gasPrice, 450 } 451 txn_hash = platon.sendTransaction(txn_params) 452 453 txn_params['gasPrice'] = 9 454 with pytest.raises(ValueError): 455 platon.replaceTransaction(txn_hash, txn_params) 456 457 @pytest.mark.P1 458 def test_platon_replaceTransaction_gas_price_defaulting_minimum(self, unlocked_account): 459 platon = Eth(unlocked_account['node'].web3) 460 address = unlocked_account['address'] 461 txn_params = { 462 'from': address, 463 'to': address, 464 'value': 1, 465 'gas': 21000, 466 'gasPrice': platon.gasPrice, 467 } 468 txn_hash = platon.sendTransaction(txn_params) 469 470 txn_params.pop('gasPrice') 471 replace_txn_hash = platon.replaceTransaction(txn_hash, txn_params) 472 replace_txn = platon.getTransaction(replace_txn_hash) 473 474 # Todo: minimum gas price is what 475 assert replace_txn['gasPrice'] == 110000000 476 477 @pytest.mark.P1 478 def test_platon_replaceTransaction_gas_price_defaulting_strategy_higher(self, unlocked_account): 479 node = unlocked_account['node'] 480 platon = Eth(node.web3) 481 price = platon.gasPrice 482 483 txn_params = { 484 'from': unlocked_account['address'], 485 'to': UNKNOWN_ADDRESS, 486 'value': 1, 487 'gas': 21000, 488 'gasPrice': price * 10, 489 'nonce': 1000, 490 } 491 492 txn_hash = platon.sendTransaction(txn_params) 493 494 def higher_gas_price_strategy(web3, txn): 495 return price * 20 496 497 platon.setGasPriceStrategy(higher_gas_price_strategy) 498 node.web3.eth = platon 499 500 txn_params.pop('gasPrice') 501 502 replace_txn_hash = platon.replaceTransaction(txn_hash, txn_params) 503 replace_txn = platon.getTransaction(replace_txn_hash) 504 log.info(replace_txn) 505 assert replace_txn['gasPrice'] == price * 20 # Strategy provides higher gas price 506 507 @pytest.mark.P1 508 def test_platon_replaceTransaction_gas_price_defaulting_strategy_lower(self, unlocked_account): 509 510 node = unlocked_account['node'] 511 platon = Eth(node.web3) 512 price = platon.gasPrice 513 txn_params = { 514 'from': unlocked_account['address'], 515 'to': unlocked_account['address'], 516 'value': 3, 517 'gas': 21000, 518 'gasPrice': price * 2, 519 'nonce': 3000, 520 } 521 522 txn_hash = platon.sendTransaction(txn_params) 523 524 def lower_gas_price_strategy(web3, txn): 525 return price 526 527 platon.setGasPriceStrategy(lower_gas_price_strategy) 528 529 node.web3.eth = platon 530 531 txn_params.pop('gasPrice') 532 replace_txn_hash = platon.replaceTransaction(txn_hash, txn_params) 533 534 replace_txn = platon.getTransaction(replace_txn_hash) 535 536 # Strategy provices lower gas price - minimum preferred 537 assert replace_txn['gasPrice'] == int(price * 2 * 1.1) 538 # Todo: Need an environment with slow speed of out block 539 # def test_platon_modifyTransaction(self, unlocked_account): 540 # node = unlocked_account['node'] 541 # platon = Eth(node.web3) 542 # txn_params = { 543 # 'from': unlocked_account['address'], 544 # 'to': unlocked_account['address'], 545 # 'value': 1, 546 # 'gas': 21000, 547 # 'gasPrice': platon.gasPrice, 548 # 'nonce': platon.getTransactionCount(unlocked_account['address']) 549 # } 550 # txn_hash = platon.sendTransaction(txn_params) 551 # 552 # modified_txn_hash =platon.modifyTransaction( 553 # txn_hash, gasPrice=(txn_params['gasPrice'] * 2), value=2 554 # ) 555 # modified_txn = platon.getTransaction(modified_txn_hash) 556 # 557 # assert is_same_address(modified_txn['from'], txn_params['from']) 558 # assert is_same_address(modified_txn['to'], txn_params['to']) 559 # assert modified_txn['value'] == 2 560 # assert modified_txn['gas'] == 21000 561 # assert modified_txn['gasPrice'] == txn_params['gasPrice'] * 2 562 563 @pytest.mark.P1 564 @pytest.mark.compatibility 565 def test_platon_sendRawTransaction(self, global_test_env): 566 env = global_test_env 567 node = env.get_rand_node() 568 account = env.account.account_with_money 569 platon = Eth(node.web3) 570 571 transaction_dict = { 572 "to": account['address'], 573 "gasPrice": platon.gasPrice, 574 "gas": 21000, 575 "nonce": account['nonce'], 576 "data": '', 577 "chainId": global_test_env.account.chain_id, 578 "value": '0x10' 579 } 580 signedTransactionDict = platon.account.signTransaction( 581 transaction_dict, account['prikey'] 582 ) 583 584 data = signedTransactionDict.rawTransaction 585 586 txn_hash = platon.sendRawTransaction(data) 587 588 assert txn_hash == signedTransactionDict.hash 589 590 # Todo: Call the contract 591 # def test_platon_call(self, web3, math_contract): 592 # coinbase = web3.eth.coinbase 593 # txn_params = math_contract._prepare_transaction( 594 # fn_name='add', 595 # fn_args=(7, 11), 596 # transaction={'from': coinbase, 'to': math_contract.address}, 597 # ) 598 # call_result = web3.eth.call(txn_params) 599 # assert is_string(call_result) 600 # result = decode_single('uint256', call_result) 601 # assert result == 18 602 603 # def test_eth_call_with_0_result(self, web3, math_contract): 604 # coinbase = web3.eth.coinbase 605 # txn_params = math_contract._prepare_transaction( 606 # fn_name='add', 607 # fn_args=(0, 0), 608 # transaction={'from': coinbase, 'to': math_contract.address}, 609 # ) 610 # call_result = web3.eth.call(txn_params) 611 # assert is_string(call_result) 612 # result = decode_single('uint256', call_result) 613 # assert result == 0 614 615 @pytest.mark.P1 616 def test_platon_estimateGas(self, unlocked_account): 617 node = unlocked_account['node'] 618 platon = Eth(node.web3) 619 620 gas_estimate = platon.estimateGas({ 621 'from': unlocked_account['address'], 622 'to': unlocked_account['address'], 623 'value': 1, 624 }) 625 assert is_integer(gas_estimate) 626 assert gas_estimate > 0 627 628 hash = platon.sendTransaction({ 629 'from': unlocked_account['address'], 630 'to': unlocked_account['address'], 631 'value': 1, 632 'gas': gas_estimate, 633 }) 634 res = platon.waitForTransactionReceipt(hash) 635 assert res['blockNumber'] != 0 636 637 def test_platon_estimateGas_high(self, unlocked_account): 638 node = unlocked_account['node'] 639 platon = Eth(node.web3) 640 641 gas_estimate = platon.estimateGas({ 642 'from': unlocked_account['address'], 643 'to': unlocked_account['address'], 644 'value': 1, 645 }) 646 assert is_integer(gas_estimate) 647 assert gas_estimate > 0 648 649 hash = platon.sendTransaction({ 650 'from': unlocked_account['address'], 651 'to': unlocked_account['address'], 652 'value': 1, 653 'gas': gas_estimate + 2000, 654 }) 655 res = platon.waitForTransactionReceipt(hash) 656 assert res['blockNumber'] != 0 657 658 def test_platon_estimateGas_low(self, unlocked_account): 659 node = unlocked_account['node'] 660 platon = Eth(node.web3) 661 662 gas_estimate = platon.estimateGas({ 663 'from': unlocked_account['address'], 664 'to': unlocked_account['address'], 665 'value': 1, 666 }) 667 assert is_integer(gas_estimate) 668 assert gas_estimate > 0 669 status = True 670 try: 671 platon.sendTransaction({ 672 'from': unlocked_account['address'], 673 'to': unlocked_account['address'], 674 'value': 1, 675 'gas': gas_estimate - 2000, 676 }) 677 status = False 678 except BaseException: 679 ... 680 assert status 681 682 @pytest.mark.P1 683 def test_platon_getBlockByHash(self, platon_connect): 684 empty_block = platon_connect.getBlock(1) 685 686 block = platon_connect.getBlock(empty_block['hash']) 687 assert block['hash'] == empty_block['hash'] 688 689 @pytest.mark.P1 690 def test_platon_getBlockByHash_not_found(self, platon_connect): 691 block = platon_connect.getBlock(UNKNOWN_HASH) 692 assert block is None 693 694 @pytest.mark.P1 695 def test_platon_getBlockByNumber_with_integer(self, platon_connect): 696 block = platon_connect.getBlock(1) 697 assert block['number'] == 1 698 699 @pytest.mark.P1 700 def test_platon_getBlockByNumber_latest(self, platon_connect): 701 block = platon_connect.getBlock('latest') 702 assert block['number'] > 0 703 704 @pytest.mark.P1 705 def test_platon_getBlockByNumber_not_found(self, platon_connect): 706 block = platon_connect.getBlock(123456789) 707 assert block is None 708 709 @pytest.mark.P1 710 def test_platon_getBlockByNumber_pending(self, platon_connect): 711 block = platon_connect.getBlock('pending') 712 latest = platon_connect.getBlock('latest') 713 714 assert block['number'] == latest['number'] + 2 715 716 @pytest.mark.P1 717 def test_platon_getBlockByNumber_earliest(self, platon_connect): 718 genesis_block = platon_connect.getBlock(0) 719 block = platon_connect.getBlock('earliest') 720 assert block['number'] == 0 721 assert block['hash'] == genesis_block['hash'] 722 723 @pytest.mark.P1 724 def test_platon_getBlockByNumber_full_transactions(self, platon_connect, block_with_txn): 725 block = platon_connect.getBlock(block_with_txn['number'], True) 726 transaction = block['transactions'][0] 727 assert transaction['hash'] == block_with_txn['transactions'][0] 728 729 @pytest.mark.P1 730 def test_platon_getTransactionByHash(self, block_with_txn, platon_connect): 731 732 transaction = platon_connect.getTransaction(block_with_txn['transactions'][0]) 733 assert is_dict(transaction) 734 assert transaction['hash'] == block_with_txn['transactions'][0] 735 736 def test_platon_getTransactionByHash_notfound(self, platon_connect): 737 transaction = platon_connect.getTransaction(UNKNOWN_HASH) 738 assert transaction is None 739 740 # def test_platon_getTransactionByHash_contract_creation(self, 741 # web3, 742 # math_contract_deploy_txn_hash): 743 # transaction = web3.eth.getTransaction(math_contract_deploy_txn_hash) 744 # assert is_dict(transaction) 745 # assert transaction['to'] is None, "to field is %r" % transaction['to'] 746 747 @pytest.mark.P1 748 def test_platon_getTransactionFromBlockHashAndIndex(self, platon_connect, block_with_txn): 749 transaction = platon_connect.getTransactionFromBlock(block_with_txn['hash'], 0) 750 assert is_dict(transaction) 751 assert transaction['hash'] == HexBytes(block_with_txn['transactions'][0]) 752 753 @pytest.mark.P1 754 def test_platon_getTransactionFromBlockHashAndIndex_withwrongindex(self, platon_connect, block_with_txn): 755 transaction = platon_connect.getTransactionFromBlock(block_with_txn['hash'], 1000) 756 assert transaction is None 757 758 @pytest.mark.P1 759 def test_platon_getTransactionFromBlockHashAndIndex_withwrongHash(self, platon_connect, block_with_txn): 760 transaction = platon_connect.getTransactionFromBlock(UNKNOWN_HASH, 0) 761 assert transaction is None 762 763 @pytest.mark.P1 764 def test_platon_getTransactionFromBlockNumberAndIndex(self, platon_connect, block_with_txn): 765 transaction = platon_connect.getTransactionFromBlock(block_with_txn['number'], 0) 766 assert is_dict(transaction) 767 assert transaction['hash'] == HexBytes(block_with_txn['transactions'][0]) 768 transaction = platon_connect.getTransactionFromBlock(block_with_txn['number'], 200) 769 assert is_dict(transaction) == False 770 771 def test_platon_getTransactionFromBlockNumberAndIndex_with_wrong_index(self, platon_connect): 772 with pytest.raises(ValueError): 773 platon_connect.getTransactionFromBlock(UNKNOWN_ADDRESS, 100) 774 775 @pytest.mark.P1 776 def test_platon_getTransactionReceipt_mined(self, platon_connect, block_with_txn): 777 receipt = platon_connect.getTransactionReceipt(block_with_txn['transactions'][0]) 778 assert is_dict(receipt) 779 assert receipt['blockNumber'] == block_with_txn['number'] 780 assert receipt['blockHash'] == block_with_txn['hash'] 781 assert receipt['transactionIndex'] == 0 782 assert receipt['transactionHash'] == HexBytes(block_with_txn['transactions'][0]) 783 784 @pytest.mark.P1 785 def test_platon_getTransactionReceipt_unmined(self, unlocked_account): 786 787 platon = Eth(unlocked_account['node'].web3) 788 789 txn_hash = platon.sendTransaction({ 790 'from': unlocked_account['address'], 791 'to': unlocked_account['address'], 792 'value': 1, 793 'gas': 21000, 794 'gasPrice': platon.gasPrice, 795 }) 796 receipt = platon.getTransactionReceipt(txn_hash) 797 assert receipt is None 798 799 @pytest.mark.P1 800 def test_platon_getTransactionReceipt_with_log_entry(self, platon_connect, block_with_txn_with_log): 801 receipt = platon_connect.getTransactionReceipt(block_with_txn_with_log['transactions'][0]) 802 log.info(receipt) 803 assert is_dict(receipt) 804 assert receipt['blockNumber'] == block_with_txn_with_log['number'] 805 assert receipt['blockHash'] == block_with_txn_with_log['hash'] 806 assert receipt['transactionIndex'] == 0 807 # assert receipt['transactionHash'] == HexBytes(block_with_txn_with_log['receiptsRoot']) 808 809 assert len(receipt['logs']) == 1 810 log_entry = receipt['logs'][0] 811 812 assert log_entry['blockNumber'] == block_with_txn_with_log['number'] 813 assert log_entry['blockHash'] == block_with_txn_with_log['hash'] 814 assert log_entry['logIndex'] == 0 815 # assert is_same_address(log_entry['address'], block_with_txn_with_log['contract_address']) 816 assert log_entry['transactionIndex'] == 0 817 # assert log_entry['transactionHash'] == HexBytes(block_with_txn_with_log['transactionsRoot'])