def call_wasm_with_setcode(sign=True): _src_dir = os.path.dirname(__file__) actions = [] for i in range(TRX_COUNT): code = struct.pack('QBB', N('actiontest'), 0, 0) #break the wasm cache key_words = b"hello,world" wast_file = os.path.join(_src_dir, 'actiontest.wast') # wast_file = os.path.join(_src_dir, '/Users/newworld/dev/pyeos/build/contracts/eosio.token/eosio.token.wast') with open(wast_file, 'rb') as f: data = f.read() replace_str = b"hhh%d" % (i, ) replace_str.zfill(len(key_words)) data = data.replace(key_words, replace_str) wasm = eosapi.wast2wasm(data) code += eosapi.pack_bytes(wasm) act = [ N('eosio'), N('setcode'), [[N('actiontest'), N('active')]], code ] setabi_action = gen_setabi_action() actions.append([act, setabi_action]) cost_time = eosapi.push_transactions2(actions, sign) start = time.time() eosapi.produce_block() print('produce_block: ', time.time() - start) print(1e6 / (cost_time / TRX_COUNT)) return 1e6 / (cost_time / TRX_COUNT)
def call_mpy_with_setcode(sign=True): _src_dir = os.path.dirname(__file__) actions = [] for i in range(TRX_COUNT): code = struct.pack('QBB', N('actiontest'), 1, 0) py_file = os.path.join(_src_dir, 'actiontest.py') with open(py_file, 'rb') as f: src_code = f.read() src_code += b'\n1==(%d)' % (i, ) with open(py_file[:-3] + '2.py', 'wb') as f: f.write(src_code) mpy_code = eosapi.mp_compile(py_file[:-3] + '2.py') code += eosapi.pack_bytes(b'\x01' + mpy_code) act = [ N('eosio'), N('setcode'), [[N('actiontest'), N('active')]], code ] setabi_action = gen_setabi_action() actions.append([act, setabi_action]) cost_time = eosapi.push_transactions2(actions, sign) start = time.time() eosapi.produce_block() print('produce_block: ', time.time() - start) print(1e6 / (cost_time / TRX_COUNT)) return 1e6 / (cost_time / TRX_COUNT)
def gen_setcode_action_py(nonce): _src_dir = os.path.dirname(__file__) code = struct.pack('QBB', N('actiontest'), 1, 0) with open(os.path.join(_src_dir, 'actiontest.py'), 'rb') as f: code += eosapi.pack_bytes(b'\x00' + f.read() + b'\n1==(%d)' % (nonce, )) act = [N('eosio'), N('setcode'), [[N('actiontest'), N('active')]], code] return act
def setcode(sign=True): _src_dir = os.path.dirname(__file__) code = struct.pack('QBB', N('lockunlock'), 1, 0) with open(os.path.join(_src_dir, 'lockunlock.py'), 'rb') as f: code += eosapi.pack_bytes(b'\x00'+f.read()) act = [N('eosio'), N('setcode'), [[N('lockunlock'), N('active')]], code] cost_time = eosapi.push_transactions2([[act]], sign) eosapi.produce_block()
def t3(count=100, sign=True): # tracemalloc.start() # snapshot1 = tracemalloc.take_snapshot() wast = '../../build/contracts/eosio.token/eosio.token.wast' key_words = b"hello,world" r = eosapi.set_contract('bugs', wast, '../../build/contracts/eosio.token/eosio.token.abi', 0) msg = {"issuer":"eosio","maximum_supply":"1000000000.0000 EOS","can_freeze":0,"can_recall":0, "can_whitelist":0} r = eosapi.push_action('bugs', 'create', msg, {'bugs':'active'}) r = eosapi.push_action('bugs','issue',{"to":"bugs","quantity":"1000000.0000 EOS","memo":""},{'eosio':'active'}) _src_dir = os.path.dirname(__file__) for i in range(count): actions = [] #break the wasm cache key_words = b"hello,world" wast_file = os.path.join(_src_dir, '/Users/newworld/dev/pyeos/build/contracts/eosio.token/eosio.token.wast') with open(wast_file, 'rb') as f: data = f.read() replace_str = b"%d"%(int(time.time()),) replace_str.zfill(len(key_words)) data = data.replace(key_words, replace_str) wasm = eosapi.wast2wasm(data) raw_code = eosapi.pack_bytes(wasm) code = struct.pack('QBB', N('bugs'), 0, 0) code += raw_code act = [N('eosio'), N('setcode'), [[N('bugs'), N('active')]], code] setabi = eosapi.pack_setabi('../../build/contracts/eosio.token/eosio.token.abi', eosapi.N('bugs')) setabi_action = [N('eosio'), N('setabi'), [[N('bugs'), N('active')]], setabi] actions.append([act, setabi_action]) code = struct.pack('QBB', N('eosio.token'), 0, 0) code += raw_code act = [N('eosio'), N('setcode'), [[N('eosio.token'), N('active')]], code] setabi = eosapi.pack_setabi('../../build/contracts/eosio.token/eosio.token.abi', eosapi.N('eosio.token')) setabi_action = [N('eosio'), N('setabi'), [[N('eosio.token'), N('active')]], setabi] actions.append([act, setabi_action]) print('&'*50) cost_time = eosapi.push_transactions2(actions, sign) print('*'*50) msg = {"from":"bugs", "to":"eosio", "quantity":"0.0001 EOS", "memo":"%d"%(i,)} r = eosapi.push_action('bugs', 'transfer', msg, {'bugs':'active'}) print('='*20, 'cached module should be decreased by 1 as eosio.token load the same code as bugs') msg = {"from":"bugs", "to":"eosio", "quantity":"0.0001 EOS", "memo":"%d"%(i,)} r = eosapi.push_action('eosio.token', 'transfer', msg, {'bugs':'active'}) if i % 50 == 0: cost_time = eosapi.produce_block() eosapi.produce_block()
def test_module_memory_leak(sign=True): _src_dir = os.path.dirname(__file__) actions = [] for i in range(10000): code = struct.pack('QBB', N('bugs'), 1, 0) with open(os.path.join(_src_dir, 'bugs.py'), 'rb') as f: code += eosapi.pack_bytes(b'\x00'+f.read() + b'\n1==(%d)'%(i+100,)) act = [N('eosio'), N('setcode'), [[N('bugs'), N('active')]], code] cost_time = eosapi.push_transactions2([[act]], sign) if i % 50 == 0: eosapi.produce_block()
def test_async(sign=True): _src_dir = os.path.dirname(__file__) actions = [] eosapi.produce_block() for i in range(TRX_COUNT): code = struct.pack('QBB', N('async'), 1, 0) with open(os.path.join(_src_dir, 'async.py'), 'rb') as f: code += eosapi.pack_bytes(b'\x00' + f.read() + b'\n1==(%d)'%(i,)) setcode_action = [N('eosio'), N('setcode'), [[N('async'), N('active')]], code] setabi_action = gen_setabi_action() callcode_action = [N('async'), N('sayhello'), [[N('async'), N('active')]], b'hello,world%d'%(i,)] actions.append([setcode_action, setabi_action, callcode_action]) eosapi.push_transactions2(actions, sign, 0, True)
def call_py_with_setcode(sign=True): _src_dir = os.path.dirname(__file__) actions = [] for i in range(TRX_COUNT): code = struct.pack('QBB', N('actiontest'), 1, 0) with open(os.path.join(_src_dir, 'actiontest.py'), 'rb') as f: code += eosapi.pack_bytes(b'\x00' + f.read() + b'\n1==(%d)' % (i + 100, )) act = [ N('eosio'), N('setcode'), [[N('actiontest'), N('active')]], code ] setabi_action = gen_setabi_action() actions.append([act, setabi_action]) cost_time = eosapi.push_transactions2(actions, sign) print(1e6 / (cost_time / TRX_COUNT)) start = time.time() eosapi.produce_block() print('produce_block: ', time.time() - start) return 1e6 / (cost_time / TRX_COUNT)
def set_contract(account, src_file, abi_file, vmtype=1, sign=True): '''Set code and abi for the account Args: account (str) : account name src_file (str) : source file path abi_file (str) : abi file path vmtype : virtual machine type, 0 for wasm, 1 for micropython, 2 for evm sign (bool) : True to sign transaction Returns: JsonStruct|None: ''' account = eosapi.N(account) code = struct.pack('QBB', account, vmtype, 0) if vmtype == 0: with open(src_file, 'rb') as f: wasm = eosapi.wast2wasm(f.read()) code += eosapi.pack_bytes(wasm) setcode = [N('eosio'), N('setcode'), [[account, N('active')]], code] return push_transactions([[setcode]], sign, compress = True)