def run_limit_case(tx_manager: TransactionManager): miner_w3 = create_web3_instance("http://127.0.0.1:1111") normal_w3 = create_web3_instance("http://127.0.0.1:2222") miner_addr = "0x040e18b1bbb5dead8120fec5885fdf56498bcd6a" # mainacc = get_rand_account() mainacc = Account.privateKeyToAccount( "15bac02dbaff32a8da673c490753a070b054b9d62adbc52cc474f2b49370613b") targetacc = get_rand_account() subaccs = [] acc3 = get_rand_account() acc_addrs = [] acc_addrs.append(miner_addr) acc_addrs.append(str(mainacc.address)) acc_addrs.append(str(targetacc.address)) acc_addrs.append(str(acc3.address)) for i in range(1000): acc = get_rand_account() subaccs.append(acc) acc_addrs.append(str(acc.address)) # rechargestartmoney # res = rechange_by_address(tx_manager, mainacc.address, 700000000000, miner_w3) # print("privatekey", str(binascii.b2a_hex(mainacc.privateKey), 'utf-8')) # return # if not res: # log_print("Rechange Failed") # log_print_is_case_pass(False, case_name) # return show_address_balance("start_send_txs", miner_w3, acc_addrs) mainacc_balance = miner_w3.eth.getBalance(mainacc.address) log_print("--------------Start create Txs-----------------") start_mutiple_send_txs(normal_w3, mainacc, subaccs, targetacc, 100000, 10000000000000, mainacc_balance) # send_normal_txs(acc1, acc2, 100000, 10000000000000000, acc1_start) log_print("--------------End create Txs-----------------") # res = rechange_by_address(tx_manager, acc3.address, 10000000000, miner_w3) # if not res: # log_print("Rechange Failed") # log_print_is_case_pass(False, case_name) # return global thread_tps if thread_tps: log_print("**********join tps*************") thread_tps.join() else: log_print("***********not join tps*******************") log_print("================show address result===================") for i in range(40): log_print("Show result at index ", i) show_address_balance("end_send_txs", miner_w3, acc_addrs) # get_address_balance(acc1.address, acc2.address, acc3.address, miner_addr) time.sleep(1)
def create_and_recharge(num): w3 = create_web3_instance("http://52.205.30.16:8545") acc = Account.privateKeyToAccount("f87fdefd98ef1347f4ee213d80e3495e4beaba5991a18bfee0fa80f0fba05b1a") nonce = w3.eth.getTransactionCount(acc.address) addrs = [] for i in range(num): addr = create_acc() print(addr) transaction = { 'to': addr, 'value': value, 'nonce': nonce, } gas = get_buffered_gas_estimate(w3, transaction) gas_price = w3.eth.gasPrice transaction['gas'] = gas transaction['gasPrice'] = gas_price signed = acc.signTransaction(transaction) w3.eth.sendRawTransaction(signed.rawTransaction) addrs.append(addr) nonce += 1 time.sleep(100) for t in addrs: print(t, w3.eth.getBalance(t))
def get_tps_eth(): log_print("Get_tps_eth start") nowTime = lambda: int(round(time.time() * 1000)) w3 = create_web3_instance("http://127.0.0.1:1111") is_start = False time_start = time_end = None timecount = 10 while True: status = w3.txpool.status pend_num = int(status['pending'], 16) queued_num = int(status['queued'], 16) sum = pend_num + queued_num if sum > 0: if not is_start: log_print("Start time count") time_start = nowTime() is_start = True else: time_end = None if sum == 0 and is_start: log_print("End time count") if not time_end: time_end = nowTime() if timecount < 0: break timecount -= 1 time.sleep(1) time.sleep(0.01) passtime = int((time_end - time_start) / 1000.0) log_print( "Side ETH tps result: txs is 400000, time cast: {}, tps:{}".format( str(passtime), str(400000 / passtime))) log_print("get_tps_eth end")
def send_normal_txs(acc, targetaddr, txnumber, txvalue, basevalue): w3 = create_web3_instance("http://127.0.0.1:2222") if txnumber * txvalue * 1.2 > basevalue: log_print("txnumber or txvalue is too big!") return index = 0 nonce = w3.eth.getTransactionCount(acc.address) while index < txnumber: index = index + 1 transaction = { 'to': targetaddr, 'value': txvalue, 'nonce': nonce, } gas = get_buffered_gas_estimate(w3, transaction) gas_price = w3.eth.gasPrice # log_debug_print("use gas ", gas, "gasPrice", gas_price) transaction['gas'] = int(gas * 5) transaction['gasPrice'] = int(gas_price * 2) signed = acc.signTransaction(transaction) w3.eth.sendRawTransaction(signed.rawTransaction) nonce += 1 if (index * 1000) % txnumber == 0: complete = index * 100.0 / txnumber log_print("{}: Create txs complet:{}%".format( str(acc.address)[:5], complete))
def process_func(process_index, thread_num, accs, targetaddr, process_task_num, txvalue): log_print( "start process func at index: {}, thread_num:{}, process_task_num:{}". format(process_index, thread_num, process_task_num)) ports = ["2222", "3333", "4444", "5555", "6666"] port = ports[(process_index % 5)] w3 = create_web3_instance("http://127.0.0.1:{}".format(port)) thread_task_num = int(process_task_num / thread_num) acc_num = len(accs) thread_acc_num = int(acc_num / thread_num) batch_accs = [] index = 0 while len(accs) > 0: for acc in accs: try: balance = w3.eth.getBalance(acc.address) except Exception as e: # print("========> e", str(e), "==> acc", acc.address) continue if balance > 0 and len(batch_accs) < thread_acc_num: batch_accs.append(acc) accs.remove(acc) if len(batch_accs) == thread_acc_num: args_accs = batch_accs thread_id = "{}:{}".format(process_index, index) log_print("start thread id {}:{}".format(thread_id, index)) if process_task_num - thread_acc_num < thread_acc_num: task_num = process_task_num else: task_num = thread_task_num thread_name = "send_txs_thread:{}".format(thread_id) call_thread(send_txs_by_addrs, thread_name, [ process_index, thread_id, args_accs, targetaddr, task_num, txvalue, balance ]) process_task_num -= task_num batch_accs = [] index += 1 log_print("end start thread: {}".format(thread_id)) if len(accs) > thread_acc_num: # 等待一个区块的时间 time.sleep(15) if len(batch_accs) > 0: args_accs = batch_accs thread_id = "{}:{}".format(process_index, index) thread_name = "send_txs_thread:{}".format(thread_id) call_thread(send_txs_by_addrs, thread_name, [ thread_id, args_accs, targetaddr, process_task_num, txvalue, balance ]) log_print("end process func at index: {}".format(process_index))
def do_test(): acc1 = Account.privateKeyToAccount( "15bac02dbaff32a8da673c490753a070b054b9d62adbc52cc474f2b49370613b") normal_w3 = create_web3_instance("http://127.0.0.1:2222") miner_w3 = create_web3_instance("http://127.0.0.1:1111") balance_end = miner_w3.eth.getBalance(acc1.address) w3 = miner_w3 print(balance_end) print(miner_w3.eth.getTransactionCount(acc1.address)) # return nonce = w3.eth.getTransactionCount(acc1.address) accs = [] for i in range(100): print("nonec", nonce) acc2 = get_rand_account() accs.append(acc2) transaction = { 'to': acc2.address, 'value': 1000000000, 'nonce': nonce, } gas = get_buffered_gas_estimate(miner_w3, transaction) gas_price = w3.eth.gasPrice log_debug_print("use gas ", gas, "gasPrice", gas_price) transaction['gas'] = int(gas) transaction['gasPrice'] = int(gas_price) signed = acc1.signTransaction(transaction) res = w3.eth.sendRawTransaction(signed.rawTransaction) print(acc2.address, "0x" + str(binascii.b2a_hex(res), encoding='utf-8')) # time.sleep(15) print("---", w3.eth.getBalance(acc2.address)) print("--------------------") nonce += 1 time.sleep(40) for acc in accs: print("===>", acc.address, w3.eth.getBalance(acc.address))
def get_address_balance(acc1, acc2, acc3, miner_addr): miner_w3 = create_web3_instance("http://127.0.0.1:1111") acc1_end = miner_w3.eth.getBalance( miner_w3.toChecksumAddress(str(acc1).lower())) acc2_end = miner_w3.eth.getBalance( miner_w3.toChecksumAddress(str(acc2).lower())) acc3_end = miner_w3.eth.getBalance( miner_w3.toChecksumAddress(str(acc3).lower())) miner_end = miner_w3.eth.getBalance( miner_w3.toChecksumAddress(str(miner_addr).lower())) log_print("acc1_end", acc1, acc1_end) log_print("acc2_end", acc2, acc2_end) log_print("acc3_end", acc3, acc3_end) log_print("miner_end", miner_addr, miner_end) log_print("sum of end", acc1_end + acc3_end + acc2_end + miner_end)
def send_txs_by_addrs(process_id, thread_id, accs: list, targetaddr, txnumber, txvalue, basevalue): ports = ["2222", "3333", "4444", "5555", "6666"] port = ports[(process_id % 5)] w3 = create_web3_instance("http://127.0.0.1:{}".format(port)) log_print("thread_id:{}, len of accs:{}, txnumber:{}".format( thread_id, len(accs), txnumber)) # w3 = create_web3_instance("http://127.0.0.1:2222") if txnumber * txvalue * 1.2 > basevalue: log_print("txnumber or txvalue is too big!", txnumber, txvalue, basevalue) return index = 0 nonce = w3.eth.getTransactionCount(accs[0].address) tag = "Tread id:{}".format(thread_id) while index < txnumber: for acc in accs: transaction = { 'to': targetaddr, 'value': txvalue, 'nonce': nonce, } gas = get_buffered_gas_estimate(w3, transaction) gas_price = w3.eth.gasPrice # log_debug_print("use gas ", gas, "gasPrice", gas_price) transaction['gas'] = int(gas * 5) transaction['gasPrice'] = int(gas_price * 2) signed = acc.signTransaction(transaction) res = w3.eth.sendRawTransaction(signed.rawTransaction) index += 1 if (index * 1000) % txnumber == 0: complete = index * 100.0 / txnumber # log_print("{}: Create txs complet:{}%".format(tag, complete)) nonce += 1
def send_txs_by_addrs(threadindex, accs: list, targetaddr, txnumber, txvalue, basevalue): w3 = create_web3_instance("http://127.0.0.1:2222") if txnumber * txvalue * 1.2 > basevalue: log_print("txnumber or txvalue is too big!") return index = 0 nonce = w3.eth.getTransactionCount(accs[0].address) tag = "Tread inde:{}".format(threadindex) while index < txnumber: for acc in accs: if index > txnumber: break transaction = { 'to': targetaddr, 'value': txvalue, 'nonce': nonce, } gas = get_buffered_gas_estimate(w3, transaction) gas_price = w3.eth.gasPrice # log_debug_print("use gas ", gas, "gasPrice", gas_price) transaction['gas'] = int(gas * 5) transaction['gasPrice'] = int(gas_price * 2) signed = acc.signTransaction(transaction) res = w3.eth.sendRawTransaction(signed.rawTransaction) # if int(threadindex) == 0: # print(nonce, str(binascii.b2a_hex(res), 'utf-8'), targetaddr) index += 1 if (index * 100) % txnumber == 0: complete = index * 100.0 / txnumber log_print("{}: Create txs complet:{}% at:{}".format( tag, complete, index)) nonce += 1
nonce = w3.eth.getTransactionCount(acc.address) addrs = [] for i in range(num): addr = create_acc() print(addr) transaction = { 'to': addr, 'value': value, 'nonce': nonce, } gas = get_buffered_gas_estimate(w3, transaction) gas_price = w3.eth.gasPrice transaction['gas'] = gas transaction['gasPrice'] = gas_price signed = acc.signTransaction(transaction) w3.eth.sendRawTransaction(signed.rawTransaction) addrs.append(addr) nonce += 1 time.sleep(100) for t in addrs: print(t, w3.eth.getBalance(t)) if __name__ == '__main__': w3 = create_web3_instance("http://52.205.30.16:8545") acc = Account.privateKeyToAccount("f87fdefd98ef1347f4ee213d80e3495e4beaba5991a18bfee0fa80f0fba05b1a") print(w3.eth.getBalance(acc.address)) create_and_recharge(100)
if timecount < 0: break timecount -= 1 time.sleep(1) time.sleep(0.01) passtime = int((time_end - time_start) / 1000.0) log_print( "Side ETH tps result: txs is 400000, time cast: {}, tps:{}".format( str(passtime), str(400000 / passtime))) log_print("get_tps_eth end") if __name__ == '__main__': # get_tps_eth() # run_limit_case(None) w3 = create_web3_instance("http://127.0.0.1:1111") for i in range(0, 200): res = w3.eth.getBlock(i) print("{}:{}".format(i, len(res['transactions']))) # # acc1 = Account.privateKeyToAccount("15bac02dbaff32a8da673c490753a070b054b9d62adbc52cc474f2b49370613b") # print(acc1.address) # nonce = w3.eth.getTransactionCount(acc1.address) # targetaddress = w3.toChecksumAddress(str("0x040e18b1bbb5dead8120fec5885fdf56498bcd6a").lower()) # transaction = { # 'to': targetaddress, # 'value': 6000000000000000000000, # 'nonce': nonce, # } #