def giveRightToVote(name=None): with producer: if not name: name = 'mike' msg = struct.pack('QQ', eosapi.N('vote'), eosapi.N('hello')) r = eosapi.push_message('vote', 'giveright', msg, {'vote': 'active'}) assert r
def delegate(name=None): with producer: if not name: name = 'hello' msg = struct.pack('QQ', eosapi.N('vote'), eosapi.N(name)) r = eosapi.push_message('vote', 'delegate', msg, {'vote': 'active'}) assert r
def test4(count): start = time.time() for i in range(count): n = eosapi.N('hello') n = eosapi.N('hello') n**100 end = time.time() print(1 / ((end - start) / count))
def deploy_vm(self, vm_name, type, version, file_name): vm_name = eosapi.N(vm_name) account = eosapi.N(self.account) msg = int.to_bytes(account, 8, 'little') #scope msg += int.to_bytes(account, 8, 'little') #table msg += int.to_bytes(vm_name, 8, 'little') #id msg += int.to_bytes(type, 4, 'little') msg += int.to_bytes(version, 4, 'little') with open(file_name, 'rb') as f: src_code = f.read() msg += src_code print('++++++++++++++++deply:', file_name) r = eosapi.push_action(self.account,'deploy',msg,{self.account:'active'}) assert r
def deploy(account, src): if not eosapi.get_account(account): print('account does not exists, create it.') r = eosapi.create_account2('eosio', account, initeos.key1, initeos.key2) assert r ''' abs_src_file = os.path.dirname(__file__) last_update = eosapi.get_code_update_time_ms(account) modify_time = os.path.getmtime(abs_src_file)*1000 if last_update >= modify_time: return ''' actions = [] _src_dir = os.path.dirname(__file__) abi_file = os.path.join(_src_dir, 'tester.abi') setabi = eosapi.pack_setabi(abi_file, eosapi.N(account)) act = ['eosio', 'setabi', setabi, {account: 'active'}] actions.append(act) args = eosapi.pack_args( "eosio", 'setcode', { 'account': account, 'vmtype': 10, 'vmversion': 0, 'code': src.encode('utf8').hex() }) act = ['eosio', 'setcode', args, {account: 'active'}] actions.append(act) r, cost = eosapi.push_actions(actions) print(r['except']) print(r['elapsed'])
def deploy_mpy(): src_dir = os.path.dirname(os.path.abspath(__file__)) code = eosapi.N('backyard') for file_name in libs: print('deploy ', file_name) src_code = eosapi.mp_compile(os.path.join(src_dir, file_name)) file_name = file_name.replace('.py', '.mpy') src_id = eosapi.hash64(file_name, 0) itr = eoslib.db_find_i64(code, code, code, src_id) if itr >= 0: old_src = eoslib.db_get_i64(itr) if old_src[1:] == src_code: continue mod_name = file_name msg = int.to_bytes(len(mod_name), 1, 'little') msg += mod_name.encode('utf8') msg += int.to_bytes(1, 1, 'little') # compiled code msg += src_code print('++++++++++++++++deply:', file_name) r = eosapi.push_message('backyard', 'deploy', msg, {'backyard': 'active'}) assert r producer.produce_block()
def deploy_wast(): src_dir = os.path.dirname(os.path.abspath(__file__)) code = eosapi.N('backyard') for file_name in ('math.wast', ): print('deploy ', file_name) bin_code = None with open(os.path.join(src_dir, file_name), 'rb') as f: bin_code = eosapi.wast2wasm(f.read()) src_id = eosapi.hash64(file_name, 0) itr = db.find_i64(code, code, code, src_id) if itr >= 0: old_src = db.get_i64(itr) if old_src[1:] == bin_code: continue mod_name = file_name msg = int.to_bytes(len(mod_name), 1, 'little') msg += mod_name.encode('utf8') msg += int.to_bytes(3, 1, 'little') # compiled cpp bytecode msg += bin_code print(msg) print('++++++++++++++++deply:', file_name) r = eosapi.push_action('backyard', 'deploy', msg, {'backyard': 'active'}) assert r
def test(): main_class = '<stdin>:Greeter' greeter = os.path.join(os.path.dirname(__file__), 'greeter.sol') with open(greeter, 'r') as f: contract_source_code = f.read() contract_interface = compile(contract_source_code, main_class) # deploy(contract_interface) bin = contract_interface['bin'] print(bin) account = 'evm' actions = [] _src_dir = os.path.dirname(__file__) abi_file = os.path.join(_src_dir, 'evm.abi') setabi = eosapi.pack_setabi(abi_file, eosapi.N(account)) act = ['eosio', 'setabi', setabi, {account: 'active'}] actions.append(act) # bin = '608060405234801561001057600080fd5b506103e76000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506122b86000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610221806100aa6000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063209652551461005c5780635524107714610087578063a5b248d9146100b4575b600080fd5b34801561006857600080fd5b5061007161010b565b6040518082815260200191505060405180910390f35b34801561009357600080fd5b506100b260048036038101908080359060200190929190505050610151565b005b3480156100c057600080fd5b506100f5600480360381019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506101dd565b6040518082815260200191505060405180910390f35b60008060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905090565b806000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550606381016000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555050565b600060205280600052604060002060009150905054815600a165627a7a7230582027ddd8a5407c4fad4a070848746c3d9e2313600c2a052b6e11b3416b35d72cda0029' args = eosapi.pack_args("eosio", 'setcode', { 'account': account, 'vmtype': 10, 'vmversion': 0, 'code': bin }) act = ['eosio', 'setcode', args, {'evm': 'active'}] actions.append(act) r, cost = eosapi.push_actions(actions) print(r['except']) print(r['elapsed'])
def deploy_mpy(): src_dir = os.path.dirname(os.path.abspath(__file__)) code = eosapi.N('backyard') for file_name in libs: print('deploy ', file_name) src_code = eosapi.mp_compile(os.path.join(src_dir, file_name)) file_name = file_name.replace('.py', '.mpy') src_id = eosapi.hash64(file_name, 0) itr = db.find_i64(code, code, code, src_id) if itr >= 0: #Something went wrong in mp_compile. #Generating different bytecode each time with the same source code, walkaround this problem for now continue old_src = db.get_i64(itr) if old_src[1:] == src_code: continue mod_name = file_name msg = int.to_bytes(len(mod_name), 1, 'little') msg += mod_name.encode('utf8') msg += int.to_bytes(1, 1, 'little') # compiled python bytecode msg += src_code print('++++++++++++++++deply:', file_name) r = eosapi.push_action('backyard','deploy',msg,{'backyard':'active'}) assert r
def start(beneficiary='hello', auctionEnd=None): if not auctionEnd: auctionEnd = int(time.time()+60) beneficiary = eosapi.N(beneficiary) msg = struct.pack('QQ', beneficiary, auctionEnd) r = eosapi.push_action('auction1','start', msg, {'auction1':'active'}) assert r
def delete(vm='vm.py.1'): debug.mp_set_max_execution_time(10000_000) account = 'vmstore' #eosapi.N('vmstore') msg = int.to_bytes(eosapi.N(vm), 8, 'little') #scope r = eosapi.push_action(account,'delete',msg,{account:'active'}) assert r
def test2(count=100): contracts = [] functions = [] args = [] per = [] for i in range(count): functions.append('setgreeting') arg = str(i) args.append(arg) contracts.append('greeter') per.append({'greeter': '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() code = eosapi.N('greeter') id = eosapi.hash64('greeting', 0) greeting = 0 itr = eoslib.db_find_i64(code, code, code, id) if itr >= 0: greeting = eoslib.db_get_i64(itr) print(greeting[1:])
def start(beneficiary='hello', auctionEnd=10000): with producer: beneficiary = eosapi.N(beneficiary) msg = int.to_bytes(beneficiary, 8, 'little') msg += int.to_bytes(auctionEnd, 8, 'little') r = eosapi.push_action('auction1', 'start', msg, {'auction1': 'active'}) assert r
def test_delete_value(): n = eosapi.N('evm') itr = rodb.find_i256(n, n, n, 0) assert itr >= 0 call('testDeleteValue', ()) itr = rodb.find_i256(n, n, n, 0) assert itr < 0
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 test_set_value(v=119000): call('testSetValue', (v,)) n = eosapi.N('evm') itr = rodb.find_i256(n, n, n, 0) assert itr >= 0 value = rodb.get_i256(itr) print(value) assert value == v
def addProposal(name=None): with producer: if not name: name = 'mike' sender = eosapi.N('hello') msg = int.to_bytes(sender, 8, 'little') msg += 'mike'.encode('utf8') r = eosapi.push_message('vote', 'addproposal', msg, {'vote': 'active'}) assert r
def deploy_native(self, contract, version, file_name): contract = eosapi.N(contract) msg = int.to_bytes(contract, 8, 'little') msg += int.to_bytes(version, 4, 'little') with open(file_name, 'rb') as f: src_code = f.read() msg += src_code print('++++++++++++++++deply:', file_name) r = eosapi.push_action(self.account,'deploy',msg,{self.account:'active'}) assert r
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 __init__(self, _account, _dir=None, _ignore=[]): self.ignore_files = _ignore if not isinstance(_account, int): self.account = eosapi.N(_account) else: self.account = _account if _dir: if not os.path.exists(_dir): raise Exception(_dir + ' not found') self.src_dir = _dir else: self.src_dir = os.path.dirname(os.path.abspath(__file__))
def deploy_vm(vm_name, type, version, file_name): account = 'vmstore' #eosapi.N('vmstore') print("++++++++++++++++++++deploy vm: ", vm_name) vm_name = eosapi.N(vm_name) f = open(file_name, 'rb') data = f.read() compressed = eosapi.zlib_compress_data(data) file_size = len(data) compressed_file_size = len(compressed) piece = 128*1024 index = 1 for i in range(0, len(compressed), piece): data = compressed[i:i+piece] msg = int.to_bytes(eosapi.N(account), 8, 'little') #scope msg += int.to_bytes(vm_name, 8, 'little') #table msg += int.to_bytes(index, 8, 'little') #id msg += data print(account) r = eosapi.push_action(account,'deploy',msg,{account:'active'}) assert r index += 1 msg = int.to_bytes(eosapi.N(account), 8, 'little') #scope msg += int.to_bytes(eosapi.N(account), 8, 'little') #table msg += int.to_bytes(vm_name, 8, 'little') #id msg += int.to_bytes(type, 4, 'little') msg += int.to_bytes(version, 4, 'little') msg += int.to_bytes(os.path.getsize(file_name), 4, 'little') msg += int.to_bytes(compressed_file_size, 4, 'little') print('++++++++++++++++deply:', file_name) r = eosapi.push_action(account,'deploy',msg,{account:'active'}) assert r
def deploy(account, bin): actions = [] _src_dir = os.path.dirname(__file__) abi_file = os.path.join(_src_dir, 'evm.abi') setabi = eosapi.pack_setabi(abi_file, eosapi.N(account)) act = ['eosio', 'setabi', setabi, {account:'active'}] actions.append(act) args = eosapi.pack_args("eosio", 'setcode', {'account':account,'vmtype':2, 'vmversion':0, 'code':bin}) act = ['eosio', 'setcode', args, {account:'active'}] actions.append(act) r, cost = eosapi.push_actions(actions) print(r['except']) print(r['elapsed'])
def test2(count=100): actions = [] for i in range(count): action = ['greeter', 'setgreeting', str(i), {'greeter':'active'}] 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() code = eosapi.N('greeter') id = eosapi.hash64('greeting', 0) greeting = 0 itr = db.find_i64(code, code, code, id) if itr >= 0: greeting = db.get_i64(itr) print(greeting[1:])
def test_call(): src_file = 'callee.sol' main_class = '<stdin>:Callee' account = 'callee' if not eosapi.get_account(account): print('account not exist, create it.') r = eosapi.create_account2('eosio', account, initeos.key1, initeos.key2) assert r abs_src_file = os.path.join(os.path.dirname(__file__), src_file) last_update = eosapi.get_code_update_time_ms(account) modify_time = os.path.getmtime(abs_src_file)*1000 if last_update < modify_time: contract_abi, bin = compile(src_file, main_class) deploy(account, bin) call('testCall', ('0x00000000000000000000000041a3152800000000', 120)) n = eosapi.N(account) itr = rodb.find_i256(n, n, n, 0) assert itr >= 0 assert rodb.get_i256(itr) == 120
def need_update(self, src_dir, src_file, mpy=False): if mpy: id = eosapi.hash64(src_file.replace('.py', '.mpy')) else: id = eosapi.hash64(src_file) code = eosapi.N(self.account) itr = eoslib.db_find_i64(code, code, code, id) if itr < 0: return True old_src = eoslib.db_get_i64(itr) _full_file_path = os.path.join(src_dir, src_file) if not mpy: with open(_full_file_path, 'rb') as f: if old_src[1:] == f.read(): return False else: src_code = eosapi.mp_compile(_full_file_path) if old_src[1:] == src_code: return False return True
def deploy(): src_dir = os.path.dirname(os.path.abspath(__file__)) code = eosapi.N('backyard') for file_name in libs: src_code = open(os.path.join(src_dir, file_name), 'rb').read() src_id = eosapi.hash64(file_name, 0) itr = db.find_i64(code, code, code, src_id) if itr >= 0: old_src = db.get_i64(itr) if old_src[1:] == src_code: continue mod_name = file_name msg = int.to_bytes(len(mod_name), 1, 'little') msg += mod_name.encode('utf8') msg += int.to_bytes(0, 1, 'little') # source code msg += src_code print('++++++++++++++++deply:', file_name) r = eosapi.push_action('backyard','deploy',msg,{'backyard':'active'}) assert r producer.produce_block()
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)
def gen_setabi_action(): _src_dir = os.path.dirname(__file__) setabi = eosapi.pack_setabi(os.path.join(_src_dir, 'actiontest.abi'), eosapi.N('actiontest')) return [N('eosio'), N('setabi'), [[N('actiontest'), N('active')]], setabi]
def vote(voter, proposal_index): with producer: msg = struct.pack('QQ', eosapi.N(voter), proposal_index) r = eosapi.push_message('vote', 'vote', msg, {'vote': 'active'}) assert r
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()