def test3(count=1000, msg='wasm'): import time import json code = N('counter') counter_id = N('counter') counter_begin = 0 itr = db.find_i64(code, code, code, counter_id) if itr >= 0: counter_begin = db.get_i64(itr) counter_begin = int.from_bytes(counter_begin, 'little') print('counter begin: ', counter_begin) transactions = [] for i in range(count): action = ['counter', 'count', msg+':'+str(i), {'counter':'active'}] transactions.append([action]) ret, cost = eosapi.push_transactions(transactions) assert ret print('total cost time:%.3f s, cost per action: %.3f ms, actions per second: %.3f'%(cost/1e6, cost/count/1000, 1*1e6/(cost/count))) counter_end = 0 itr = db.find_i64(code, code, code, counter_id) if itr >= 0: counter_end = db.get_i64(itr) counter_end = int.from_bytes(counter_end, 'little') print('counter end: ', counter_end) itr = db.find_i64(code, code, code, N('msg')) if itr >= 0: msg = db.get_i64(itr) print('msg', msg)
def test3(count=100): actions = [] for i in range(count): act = [N('storagetest'), N('sayhello'), [[N('storagetest'), N('active')]], b'hello,world%d'%(i,)] actions.append([act]) r, cost = eosapi.push_transactions(actions, True) print('total cost time:%.3f s, cost per TS: %.3f ms, TS per second: %.3f'%(cost/1e6, cost/count/1000, 1*1e6/(cost/count)))
def test2(count=100): actions = [] for i in range(count): act = [N('storagetest'), N('sayhello'), [[N('storagetest'), N('active')]], b'hello,world%d'%(i,)] actions.append([act]) cost_time = eosapi.push_transactions2(actions, True) print(1.0/(cost_time/1e6/100.0), cost_time)
def activatevm(): msg = int.to_bytes(N('vm.py.1'), 8, 'little') #vm name, include platform id msg += int.to_bytes(1, 8, 'little') #type: py msg += int.to_bytes(V, 8, 'little') #type: version act = [N('eosio'), N('activatevm'), [[N('eosio'), N('active')]], msg] r = eosapi.push_transactions([[act]]) assert r[0]
def change_active_key(): key = 'EOS8muoEdY4L9WRYXYB55WmEzYw9A314fW1uMPUqUrFUBMMjWNpxd' auth = eosapi.pack_updateauth('test', 'active', 'owner', key, 0) act = [N('eosio'), N('updateauth'), [[N('test'), N('active')]], auth] r = eosapi.push_transactions2([[act]]) assert r eosapi.produce_block()
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 test2(count=100): import time import json code = N('counter') counter_id = N('counter') counter_begin = 0 itr = db.find_i64(code, code, code, counter_id) if itr >= 0: counter_begin = db.get_i64(itr) counter_begin = int.from_bytes(counter_begin, 'little') print('counter begin: ', counter_begin) actions = [] for i in range(count): action = ['counter', 'count', str(i), {'counter': 'active'}] actions.append(action) ret, cost = eosapi.push_actions(actions) assert ret print(ret['elapsed']) print(cost) print( 'total cost time:%.3f s, cost per action: %.3f ms, actions per second: %.3f' % (cost / 1e6, cost / count / 1000, 1 * 1e6 / (cost / count))) counter_end = 0 itr = db.find_i64(code, code, code, counter_id) if itr >= 0: counter_end = db.get_i64(itr) counter_end = int.from_bytes(counter_end, 'little') print('counter end: ', counter_end) assert counter_begin + count == counter_end
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 sign_trx(): act = [N('hello'), N('sayhello'), [[N('hello'), N('active')]], b'jack'] r = eosapi.gen_transaction([act]) print(r) r = eosapi.sign_transaction(r, '5JbDP55GXN7MLcNYKCnJtfKi9aD2HvHAdY7g8m67zFTAFkY1uBB') r = eosapi.JsonStruct(r) print(r)
def setabi(): _src_dir = os.path.dirname(__file__) setabi = eosapi.pack_setabi(os.path.join(_src_dir, 'lockunlock.abi'), eosapi.N('lockunlock')) action = [N('eosio'), N('setabi'), [[N('lockunlock'), N('active')]], setabi] cost_time = eosapi.push_transactions2([[action]], sign) eosapi.produce_block()
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 push_sign_trx(): act = [N('hello'), N('sayhello'), [[N('hello'), N('active')]], b'jack'] r = eosapi.gen_transaction([act]) print(json.dumps(r, sort_keys=False, indent=4, separators=(',', ': '))) r = eosapi.sign_transaction(r, '5JbDP55GXN7MLcNYKCnJtfKi9aD2HvHAdY7g8m67zFTAFkY1uBB') eosapi.push_raw_transaction(r) eosapi.produce_block()
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 test2(count=100): actions = [] for i in range(count): args = '55241077' args += int.to_bytes(i, 32, 'big').hex() args = bytes.fromhex(args) act = [eosapi.s2n('evm'), N('call'), [[N('evm'), N('active')]], args] actions.append(act) outputs, cost_time = eosapi.push_transactions([actions], True) print(1e6/(cost_time/count))
def test_util(): import util keys = struct.pack('Q', N('currency')) values = bytes(16) eos = N('eos') ret = util.load(eos, eos, N('test'), keys, 0, 0, values) print('+++++++eoslib.load return:', ret) print(values) results = struct.unpack('QQ', values) print(results)
def show_rent_result(from_, to_): balance = eosapi.get_account(from_).balance eos = N(b'eos') keys = struct.pack('Q', N(to_)) values = bytes(16) ret = util.load(eos, eos, N(from_), keys, 0, 0, values) result = struct.unpack('QQ', values) print(ret, balance, result)
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 publish(): contracts_path = os.path.join(os.getcwd(), '..', 'contracts') _path = os.path.join(contracts_path, 'eosio.system', 'eosio.system') abi = _path + '.abi' setabi = eosapi.pack_setabi(abi, eosapi.N('eosio')) assert setabi setabi_action = [N('eosio'), N('setabi'), [[N('eosio'), N('active')]], setabi] r = eosapi.push_transactions([[setabi_action]]) assert r[0]
def test3(count=100): actions = [] for i in range(count): act = [ N('hello'), N('sayhello'), [[N('hello'), N('active')]], b'hello,world%d' % (i, ) ] actions.append([act]) r, cost_time = eosapi.push_transactions(actions, True) print(1e6 / (cost_time / count), cost_time)
def change_owner_key(): ''' struct updateauth { account_name account; permission_name permission; permission_name parent; authority data; uint32_t delay; ''' key = 'EOS8muoEdY4L9WRYXYB55WmEzYw9A314fW1uMPUqUrFUBMMjWNpxd' auth = eosapi.pack_updateauth('hello', 'owner', '', key, 0) act = [N('eosio'), N('updateauth'), [[N('hello'), N('owner')]], auth] r = eosapi.push_transactions2([[act]]) assert r eosapi.produce_block()
def send_actions(sign=True): actions = [] for i in range(TRX_COUNT): act = [ N('actiontest'), N('sayhello'), [[N('actiontest'), N('active')]], b'hello,world%d' % (i, ) ] actions.append([act]) cost_time = eosapi.push_transactions2(actions, sign) print(1.0 / (cost_time / 1e6 / 100.0)) start = time.time() eosapi.produce_block() print('produce_block: ', time.time() - start) return cost_time
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 create_multisig_account(): #PW5KKNC8zM2KVLrb1cw4YNXZ69NLK7Fr5B35wHmsPt35tyiYkY4RR test_keys = { 'EOS8mqCBdG8yiaULU3YyiD1ZhySzt64KjNb1gQiy4GdUPMEQMxVJJ': '5KZB2UCfpD3qCEUviXrgMdwDb6TDpuvuWvZKH85u9mBYK85Rqzd', 'EOS8mrwBdRz1zyaBiMexqJw6UybR9gNLzBC1tdLqqcfVdA2TFcgAb': '5KM6MvhsNRUtafGCghEYWXYXqWidaGTKqfsida6h5mNg5ouQUTv', 'EOS8mqgm5kx88XzsxsAAKH5vnq1vGNcSZDfJyar17FnTm1HvBRuXi': '5JiwrohmpRR3PjUcf6NpSLBE2QdAUTJZf1tAHYvK7iAUTWnaGgZ', 'EOS8msM7326VykwCSdEXZrj8U3goUPYWZ84KxBTy6GajGkpQCXQNX': '5K6HPpxgq5qFkRpUcaVdZVr4hmT61EzU1AFEfkm428pEQP59neB', } all_keys = wallet.list_keys() for pub in test_keys: priv = test_keys[pub] if not pub in all_keys: wallet.import_key('mywallet', priv) keys = [ [ initeos.key1, initeos.key2 ], [ 'EOS8mqCBdG8yiaULU3YyiD1ZhySzt64KjNb1gQiy4GdUPMEQMxVJJ', 'EOS8mrwBdRz1zyaBiMexqJw6UybR9gNLzBC1tdLqqcfVdA2TFcgAb' ], [ 'EOS8mqgm5kx88XzsxsAAKH5vnq1vGNcSZDfJyar17FnTm1HvBRuXi', 'EOS8msM7326VykwCSdEXZrj8U3goUPYWZ84KxBTy6GajGkpQCXQNX' ] ] accounts = ['test', 'test1', 'test2'] index = 0 for key in keys: a = accounts[index] if not eosapi.get_account(a): eosapi.create_account('eosio', a, key[0], key[1]) index += 1 eosapi.produce_block() #"test2" must come first to satisfy the tricky verifycation in authority.hpp:validate per = '{"threshold" : 100, "keys" : [], "accounts" : [{"permission":{"actor":"test2","permission":"active"},"weight":50},{"permission":{"actor":"test1","permission":"active"},"weight":50}]}' # key = 'EOS8muoEdY4L9WRYXYB55WmEzYw9A314fW1uMPUqUrFUBMMjWNpxd' auth = eosapi.pack_updateauth('test', 'active', 'owner', per, 0) act = [N('eosio'), N('updateauth'), [[N('test'), N('active')]], auth] r = eosapi.push_transactions2([[act]]) assert r print(r) eosapi.produce_block()
def test2(count=100): import time import json code = N('counter') counter_id = N('counter') counter_begin = 0 itr = eoslib.db_find_i64(code, code, code, counter_id) if itr >= 0: counter_begin = eoslib.db_get_i64(itr) counter_begin = int.from_bytes(counter_begin, 'little') print('counter begin: ', counter_begin) actions = [] for i in range(count): action = ['hello', 'sayhello', {'hello': 'active'}, str(i)] actions.append(action) ret, cost = eosapi.push_actions(actions, True) assert ret print( 'total cost time:%.3f s, cost per action: %.3f ms, actions per second: %.3f' % (cost / 1e6, cost / count / 1000, 1 * 1e6 / (cost / count))) eosapi.produce_block() actions = [] for i in range(count): action = ['counter', 'count', {'counter': 'active'}, str(i)] actions.append(action) ret, cost = eosapi.push_actions(actions, True) assert ret print( 'total cost time:%.3f s, cost per action: %.3f ms, actions per second: %.3f' % (cost / 1e6, cost / count / 1000, 1 * 1e6 / (cost / count))) eosapi.produce_block() counter_end = 0 itr = eoslib.db_find_i64(code, code, code, counter_id) if itr >= 0: counter_end = eoslib.db_get_i64(itr) counter_end = int.from_bytes(counter_end, 'little') print('counter end: ', counter_end) assert counter_begin + count == counter_end
def call_wasm_without_setcode(sign=True): _src_dir = os.path.dirname(__file__) actions = [] for i in range(TRX_COUNT): act = [ N('actiontest'), N('sayhello'), [[N('actiontest'), N('active')]], b'hello,world%d' % (i, ) ] actions.append([act]) 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 test3(count=100): actions = [] for i in range(count): msg = { "from": "eosio", "to": "eosio.token", "quantity": "0.0001 EOS", "memo": str(i) } msg = eosapi.pack_args('eosio.token', 'transfer', msg) act = [ N('eosio.token'), N('transfer'), [[N('eosio'), N('active')]], msg ] actions.append([act]) r, cost = eosapi.push_transactions(actions, True) print('total cost time:%.3f s, cost per TS: %.3f ms, TS per second: %.3f' % (cost / 1e6, cost / count / 1000, 1 * 1e6 / (cost / count)))
def test2(count): import time import json if not eosapi.get_account('counter').permissions: init() code = N('counter') counter_id = N('counter') counter_begin = 0 itr = eoslib.db_find_i64(code, code, code, counter_id) if itr >= 0: counter_begin = eoslib.db_get_i64(itr) counter_begin = int.from_bytes(counter_begin, 'little') print('counter begin: ', counter_begin) contracts = [] functions = [] args = [] per = [] for i in range(count): functions.append('count') arg = str(i) args.append(arg) contracts.append('counter') per.append({'counter': 'active'}) ret = eosapi.push_messages(contracts, functions, args, per, True) assert ret cost = ret['cost_time'] print( 'total cost time:%.3f s, cost per action: %.3f ms, actions per second: %.3f' % (cost / 1e6, cost / count / 1000, 1 * 1e6 / (cost / count))) eosapi.produce_block() counter_end = 0 itr = eoslib.db_find_i64(code, code, code, counter_id) if itr >= 0: counter_end = eoslib.db_get_i64(itr) counter_end = int.from_bytes(counter_end, 'little') print('counter end: ', counter_end) assert counter_begin + count == counter_end
def test_setcode1(): hello_code = b'hello,world' args = { 'account': 'hello', 'code_name': 'hello', 'code_type': 1, 'code': hello_code.hex() } r, cost = eosapi.push_action('eosio.code', 'setcode', args, {'hello': 'active'}) print('cost time:', cost) code = N('eosio.code') scope = code table = N('hello') id = N('hello') itr = db.find_i64(code, scope, table, id) print(itr) assert itr >= 0 co = db.get_i64(itr) print(co) assert co == hello_code
def test(name=None): code = N('counter') counter_id = N('counter') counter_begin = 0 itr = db.find_i64(code, code, code, counter_id) if itr >= 0: counter_begin = db.get_i64(itr) counter_begin = int.from_bytes(counter_begin, 'little') print('counter begin: ', counter_begin) r, cost = eosapi.push_action('counter', 'count', '', {'counter': 'active'}) counter_end = 0 itr = db.find_i64(code, code, code, counter_id) if itr >= 0: counter_end = db.get_i64(itr) counter_end = int.from_bytes(counter_end, 'little') print('counter end: ', counter_end) assert counter_begin + 1 == counter_end
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)