github.com/platonnetwork/platon-go@v0.7.6/cases/tests/rpc/test_rpc_personal.py (about) 1 # -*- coding: utf-8 -*- 2 ''' 3 @Description: rpc cases 4 ''' 5 import time 6 7 import allure 8 import pytest 9 from client_sdk_python import Web3 10 from client_sdk_python.eth import Eth 11 from hexbytes import HexBytes 12 password = "88888888" 13 to_address = "0xdfdbb962a03bd270e1e8235a3d11b5775334c7d7" 14 g_txHash = None 15 16 17 @allure.title("List all account addresses") 18 @pytest.mark.P1 19 @pytest.mark.compatibility 20 def test_personal_listAccounts(global_running_env): 21 node = global_running_env.get_rand_node() 22 assert len(node.personal.listAccounts) >= 0 23 24 25 @allure.title("List all wallet information") 26 @pytest.mark.P1 27 @pytest.mark.compatibility 28 def test_personal_listWallets(global_running_env): 29 node = global_running_env.get_rand_node() 30 listWallets = node.personal.listWallets 31 assert len(listWallets) >= 0 32 33 34 @allure.title("Create a new account") 35 @pytest.mark.P1 36 @pytest.mark.compatibility 37 @pytest.fixture() 38 def test_personal_newAccount(global_running_env): 39 node = global_running_env.get_rand_node() 40 before = len(node.eth.accounts) 41 new_account = node.personal.newAccount(password) 42 time.sleep(2) 43 to_account = Web3.toChecksumAddress(new_account) 44 after = len(node.eth.accounts) 45 assert len(to_account) == 42 46 assert after == (before + 1) 47 yield node 48 49 50 @allure.title("Open a wallet") 51 @pytest.mark.P1 52 @pytest.mark.compatibility 53 def test_personal_openWallet(global_running_env): 54 node = global_running_env.get_rand_node() 55 listWallet = node.personal.listWallets 56 if len(listWallet) > 0: 57 assert None == node.personal.openWallet(listWallet[0]["url"], password) 58 59 60 @allure.title("Unlock account") 61 @pytest.mark.P1 62 @pytest.mark.compatibility 63 def test_personal_unlockAccount(test_personal_newAccount): 64 listWallet = test_personal_newAccount.personal.listWallets 65 if len(listWallet) > 0: 66 addr1 = Web3.toChecksumAddress(listWallet[0]["accounts"][0]["address"]) 67 assert True == test_personal_newAccount.personal.unlockAccount(addr1, password) 68 listWallet = test_personal_newAccount.personal.listWallets 69 assert "Unlocked" == listWallet[0]["status"] 70 71 72 @allure.title("Lock account") 73 @pytest.mark.P1 74 @pytest.mark.compatibility 75 def test_personal_lockAccount(test_personal_newAccount): 76 listWallet = test_personal_newAccount.personal.listWallets 77 addr1 = Web3.toChecksumAddress(listWallet[0]["accounts"][0]["address"]) 78 assert True == test_personal_newAccount.personal.lockAccount(addr1) 79 listWallet = test_personal_newAccount.personal.listWallets 80 assert "Locked" == listWallet[0]["status"] 81 82 83 @allure.title("Import raw key") 84 @pytest.mark.P1 85 @pytest.mark.compatibility 86 @pytest.fixture() 87 def test_personal_importRawKey(global_running_env): 88 node = global_running_env.get_rand_node() 89 account = global_running_env.account.get_rand_account() 90 prikey = account["prikey"] 91 addr = node.personal.importRawKey(prikey, password) 92 assert 42 == len(addr) 93 yield node 94 95 96 @allure.title("data sign and ecRecover") 97 @pytest.mark.P1 98 @pytest.mark.compatibility 99 def test_personal_sign_ecRecover(global_running_env): 100 node = global_running_env.get_rand_node() 101 test_data = "0x11" 102 signer = Web3.toChecksumAddress(node.eth.accounts[0]) 103 sign_data = node.personal.sign(test_data, signer, password) 104 assert len(sign_data) == 132 105 assert Web3.toChecksumAddress(node.personal.ecRecover(test_data, sign_data)) == signer 106 107 108 @allure.title("Sign transaction") 109 @pytest.mark.P1 110 @pytest.mark.compatibility 111 def test_personal_signTransaction(global_running_env): 112 node = global_running_env.get_rand_node() 113 account = global_running_env.account 114 platon = Eth(node.web3) 115 addr = account.account_with_money["address"] 116 117 nonce = hex(platon.getTransactionCount(Web3.toChecksumAddress(addr))) 118 transaction_dict = { 119 "from": Web3.toChecksumAddress(addr), 120 "to": Web3.toChecksumAddress(to_address), 121 "value": "0x10000000000000", 122 "data": "0x11", 123 "gasPrice": "0x8250de00", 124 "gas": "0x6fffffff", 125 "nonce": nonce, 126 } 127 ret = node.personal.signTransaction(transaction_dict, password) 128 assert ret is not None 129 130 131 def transaction_func(node, from_addr="", to_addr=to_address, value=1000, data='', gasPrice='100000000', 132 gas='21068', nonce=0, password=password): 133 transaction_dict = { 134 "from": Web3.toChecksumAddress(from_addr), 135 "to": Web3.toChecksumAddress(to_addr), 136 "value": value, 137 "data": data, 138 "gasPrice": gasPrice, 139 "nonce": nonce, 140 } 141 if gas == '': 142 gas = node.eth.estimateGas(transaction_dict) 143 return gas 144 transaction_dict["gas"] = gas 145 global g_txHash 146 g_txHash = node.personal.sendTransaction(transaction_dict, password) 147 return g_txHash 148 149 150 # GetTransactionByHash 151 # GetRawTransactionByHash 152 # GetTransactionReceipt 153 @allure.title("Get raw transaction") 154 @pytest.mark.P1 155 def test_platon_getTransaction(global_running_env): 156 node = global_running_env.get_rand_node() 157 ret = node.eth.getTransaction("0x1111111111111111111111111111111111111111111111111111111111111111") 158 assert ret is None 159 print("check succeed: getTransaction by not exist hash!,ret:{}".format(ret)) 160 ret = node.eth.getRawTransaction( 161 HexBytes("0x1111111111111111111111111111111111111111111111111111111111111111").hex()) 162 assert ret == "0x" 163 164 165 @allure.title("Send transaction based on gasprice's recommendations") 166 @pytest.mark.P1 167 def test_platon_gasPrice(global_running_env): 168 node = global_running_env.get_rand_node() 169 platon = Eth(node.web3) 170 account = global_running_env.account 171 from_address = account.account_with_money["address"] 172 173 nCount = platon.getTransactionCount(Web3.toChecksumAddress(from_address)) 174 nonce = hex(nCount) 175 gasprice = node.eth.gasPrice 176 tx_hash = transaction_func(node=node, from_addr=from_address, to_addr=to_address, nonce=nonce, gasPrice=gasprice) 177 assert len(tx_hash) == 32 178 179 gasprice = node.eth.gasPrice * 2 180 nCount = nCount + 1 181 nonce = hex(nCount) 182 tx_hash = transaction_func(node=node, from_addr=from_address, to_addr=to_address, nonce=nonce, gasPrice=gasprice) 183 assert len(tx_hash) == 32 184 185 gasprice = int(node.eth.gasPrice / 2) 186 nCount = nCount + 1 187 nonce = hex(nCount) 188 189 status = 0 190 try: 191 transaction_func(node=node, from_addr=from_address, to_addr=to_address, nonce=nonce, gasPrice=gasprice) 192 status = 1 193 except Exception as e: 194 print("\nUse less than the recommended gasprice:{}, nonce:{}, Send transaction failed,error message:{}".format(gasprice, nonce, e)) 195 assert status == 0 196 197 198 @allure.title("Get the block based on block number and block hash") 199 @pytest.mark.P1 200 def test_platon_GetBlock(global_running_env): 201 node = global_running_env.get_rand_node() 202 account = global_running_env.account 203 platon = Eth(node.web3) 204 address = account.account_with_money["address"] 205 nCount = platon.getTransactionCount(Web3.toChecksumAddress(address)) 206 nonce = hex(nCount) 207 208 gasprice = node.eth.gasPrice 209 # send transaction 210 if g_txHash is None: 211 tx_hash = transaction_func(node=node, from_addr=address, to_addr=to_address, nonce=nonce, gasPrice=gasprice) 212 else: 213 tx_hash = g_txHash 214 215 assert len(tx_hash) == 32 216 tx_hash = HexBytes(tx_hash).hex() 217 print("\ntransaction hash:{}".format(tx_hash)) 218 # Waiting for transaction on the chain 219 result = node.eth.waitForTransactionReceipt(tx_hash) 220 assert None != result 221 # get block info by transaction receipt 222 blockHash = result["blockHash"] 223 blockNumber = result["blockNumber"] 224 assert len(blockHash) == 32 225 blockHash = HexBytes(blockHash).hex() 226 # get block by blockHash 227 # fullTx:Flase 228 blockInfo = node.eth.getBlock(blockHash, False) 229 blockHash = blockInfo['hash'] 230 blockNumber = blockInfo['number'] 231 assert len(blockHash) == 32 232 assert blockNumber > 0 233 fullTransaction = blockInfo["transactions"] 234 assert len(fullTransaction) > 0 235 # fullTx:True 236 blockInfo = node.eth.getBlock(blockHash, True) 237 blockHash = blockInfo['hash'] 238 assert len(blockHash) == 32 239 fullTransaction = blockInfo["transactions"] 240 assert len(fullTransaction) > 0 241 # get block by blockNumber 242 # fullTx:Flase 243 blockInfo = node.eth.getBlock(blockNumber, False) 244 blockHash = blockInfo['hash'] 245 assert len(blockHash) == 32 246 fullTransaction = blockInfo["transactions"] 247 assert len(fullTransaction) > 0 248 # fullTx:True 249 blockInfo = node.eth.getBlock(blockNumber, True) 250 blockHash = blockInfo['hash'] 251 assert len(blockHash) == 32 252 fullTransaction = blockInfo["transactions"] 253 assert len(fullTransaction) > 0 254 255 256 @allure.title("Send the trade based on the gas estimate") 257 @pytest.mark.P1 258 def test_platon_estimateGas(global_running_env): 259 node = global_running_env.get_rand_node() 260 account = global_running_env.account 261 platon = Eth(node.web3) 262 address = account.account_with_money["address"] 263 264 nCount = platon.getTransactionCount(Web3.toChecksumAddress(address)) 265 nonce = hex(nCount) 266 estimateGas = transaction_func(node=node, from_addr=address, to_addr=to_address, nonce=nonce, gas='') 267 gas = estimateGas 268 269 tx_hash = transaction_func(node=node, from_addr=address, to_addr=to_address, nonce=nonce, gas=gas) 270 assert len(tx_hash) == 32 271 nCount = nCount + 1 272 nonce = hex(nCount) 273 274 gas = int(estimateGas * 2) 275 tx_hash = transaction_func(node=node, from_addr=address, to_addr=to_address, nonce=nonce, gas=gas) 276 assert len(tx_hash) == 32 277 nCount = nCount + 1 278 nonce = hex(nCount) 279 280 gas = int(estimateGas / 2) 281 status = 0 282 try: 283 transaction_func(node=node, from_addr=address, to_addr=to_address, nonce=nonce, gas=gas) 284 status = 1 285 except Exception as e: 286 print("\nUse less gas than expected:【{}】,Send transaction failed,error message:{}".format(gas, e)) 287 assert status == 0 288 289 290 if __name__ == '__main__': 291 pytest.main(['-v', 'test_rpc_personal.py'])