def publish_system_contract(): contracts_path = os.path.join(os.getcwd(), '..', 'contracts') sys.path.append(os.getcwd()) for account in ['eosio.bios', 'eosio.msig', 'eosio.system', 'eosio.token']: print('account', account) if not eosapi.get_account(account): with producer: r = eosapi.create_account('eosio', account, key1, key2) assert r old_code = eosapi.get_code(account) if old_code: old_code = old_code[0] need_update = not old_code if old_code: print('+++++++++old_code[:4]', old_code[:4]) if old_code[:4] != b'\x00asm': old_code = eosapi.wast2wasm(old_code) wast = os.path.join(contracts_path, account, account + '.wast') code = open(wast, 'rb').read() code = eosapi.wast2wasm(code) print(len(code), len(old_code), old_code[:20]) if code == old_code: need_update = False if need_update: print('+++++++++code update', account) wast = os.path.join(contracts_path, account, account + '.wast') abi = os.path.join(contracts_path, account, account + '.abi') with producer: r = eosapi.set_contract(account, wast, abi, 0) if account == 'eosio.token': # msg = {"issuer":"eosio","maximum_supply":"1000000000.0000 EOS","can_freeze":0,"can_recall":0, "can_whitelist":0} with producer: msg = { "issuer": "eosio", "maximum_supply": "10000000000.0000 EOS" } r = eosapi.push_action('eosio.token', 'create', msg, {'eosio.token': 'active'}) assert r r = eosapi.push_action('eosio.token', 'issue', { "to": "eosio", "quantity": "1000.0000 EOS", "memo": "" }, {'eosio': 'active'}) assert r
def prepare_(name, src, abi, full_src_path): _src_dir = os.path.dirname(os.path.abspath(full_src_path)) if src.endswith('.wast'): code_type = CODE_TYPE_WAST elif src.endswith('.py'): code_type = CODE_TYPE_PY else: raise Exception('unknown code type') if code_type == 0: cpp2wast.set_src_path(_src_dir) cpp_src_file = src.replace('.wast', '.cpp') if not cpp2wast.build(cpp_src_file): raise Exception("build {0} failed".format(cpp_src_file)) if src.find('/') < 0: src = os.path.join(_src_dir, src) if abi and abi.find('/') < 0: abi = os.path.join(_src_dir, abi) if not eosapi.get_account(name): print('*' * 20, 'create_account') r = eosapi.create_account('eosio', name, initeos.key1, initeos.key2) assert r old_code = eosapi.get_code(name) need_update = True if old_code: old_code = old_code[0] with open(src, 'rb') as f: code = f.read() if code_type == CODE_TYPE_WAST: code = eosapi.wast2wasm(code) old_code = eosapi.wast2wasm(old_code) if code == old_code: need_update = False elif CODE_TYPE_PY == code_type: code = eosapi.mp_compile(src) if code == old_code[1:]: need_update = False elif (code == old_code[1:] or code == old_code): need_update = False if need_update: print('Updating contract', src) r = eosapi.set_contract(name, src, abi, code_type) assert r, 'set_contract failed'
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 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 _set_contract(account, wast_file, abi_file): with open(wast_file, 'rb') as f: wasm = eosapi.wast2wasm(f.read()) code_hash = eosapi.sha256(wasm) with open(abi_file, 'rb') as f: abi = f.read() actions = [] _setcode = eosapi.pack_args('eosio', 'setcode', { 'account': account, 'vmtype': 0, 'vmversion': 0, 'code': wasm.hex() }) # _setabi = eosapi.pack_args('eosio', 'setabi', {'account':account, 'abi':abi.hex()}) _setabi = eosapi.pack_setabi(abi_file, account) old_hash = eosapi.get_code_hash(account) print(old_hash, code_hash) if code_hash != old_hash: setcode = ['eosio', 'setcode', _setcode, {account: 'active'}] actions.append(setcode) setabi = ['eosio', 'setabi', _setabi, {account: 'active'}] actions.append(setabi) rr, cost = eosapi.push_actions(actions) assert_ret(rr)
def prepare(name, src, abi, code_type, full_src_path): _src_dir = os.path.dirname(os.path.abspath(full_src_path)) if code_type == 0: cpp2wast.set_src_path(_src_dir) cpp_src_file = src.replace('.wast', '.cpp') if not cpp2wast.build(cpp_src_file): raise Exception("build {0} failed".format(cpp_src_file)) if src.find('/') < 0: src = os.path.join(_src_dir, src) if abi and abi.find('/') < 0: abi = os.path.join(_src_dir, abi) if code_type == CODE_TYPE_MPY: mpy_file = src[:-3] + '.mpy' with open(mpy_file, 'wb') as f: f.write(eosapi.mp_compile(src)) src = mpy_file if not eosapi.get_account(name).permissions: r = eosapi.create_account('eosio', name, initeos.key1, initeos.key2) assert r old_code = eosapi.get_code(name) need_update = True if old_code: old_code = old_code[0] with open(src, 'rb') as f: code = f.read() if code_type == CODE_TYPE_WAST: code = eosapi.wast2wasm(code) old_code = eosapi.wast2wasm(old_code) if code == old_code: need_update = False elif (code == old_code[1:] or code == old_code): need_update = False if need_update: with producer: if code_type == 0: r = eosapi.set_contract(name, src, abi, 0) else: r = eosapi.set_contract(name, src, abi, 1) assert r, 'set_contract failed'
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 t2(): contracts_path = os.path.join(os.getcwd(), '..', 'contracts') sys.path.append(os.getcwd()) account = 'eosio' path = 'eosio.system' accounts = gen_names(ACCOUNT_COUNT) _path = os.path.join(contracts_path, path, path) wast = _path + '.wast' abi_file = _path + '.abi' with open(wast, 'rb') as f: wasm = eosapi.wast2wasm(f.read()) code_hash = eosapi.sha256(wasm) with open(abi_file, 'rb') as f: abi = f.read() for account in accounts: _create_account(account) continue if not eosapi.get_account(account): _create_account(account) for account in accounts: print('+++++++++code update', account) actions = [] _setcode = eosapi.pack_args('eosio', 'setcode', { 'account': account, 'vmtype': 0, 'vmversion': 0, 'code': wasm.hex() }) # _setabi = eosapi.pack_args('eosio', 'setabi', {'account':account, 'abi':abi.hex()}) _setabi = pack_setabi(abi_file, account) old_hash = eosapi.get_code_hash(account) print(old_hash, code_hash) if code_hash != old_hash: setcode = ['eosio', 'setcode', _setcode, {account: 'active'}] actions.append(setcode) setabi = ['eosio', 'setabi', _setabi, {account: 'active'}] actions.append(setabi) rr, cost = eosapi.push_actions(actions) assert_ret(rr)
def publish_system_contracts(accounts_map): contracts_path = os.path.join(os.getcwd(), '..', 'contracts') sys.path.append(os.getcwd()) # accounts_map = {'eosio.token':'eosio.token', 'eosio.msig':'eosio.msig', 'eosio':'eosio.system'} for account in accounts_map: print('account', account) if not eosapi.get_account(account): r = eosapi.create_account('eosio', account, key1, key2) assert r _path = os.path.join(contracts_path, accounts_map[account], accounts_map[account]) wast = _path + '.wast' code = open(wast, 'rb').read() code = eosapi.wast2wasm(code) hash = eosapi.sha256(code) old_hash = eosapi.get_code_hash(account) if old_hash != hash: print('+++++++++code update', account) wast = _path + '.wast' abi = _path + '.abi' r = eosapi.set_contract(account, wast, abi, 0) print(wast, abi) time.sleep(1.0) if account == 'eosio.token' and eosapi.get_balance('eosio') <= 0.0: print('issue system token...') # msg = {"issuer":"eosio","maximum_supply":"1000000000.0000 EOS","can_freeze":0,"can_recall":0, "can_whitelist":0} msg = { "issuer": "eosio", "maximum_supply": "11000000000000.0000 EOS" } r = eosapi.push_action('eosio.token', 'create', msg, {'eosio.token': 'active'}) assert r r = eosapi.push_action( 'eosio.token', 'issue', { "to": "eosio", "quantity": "10000000000000.0000 EOS", "memo": "" }, {'eosio': 'active'}) assert r
def prepare(account, src, abi, full_src_path, code_type = None): _src_dir = os.path.dirname(os.path.abspath(full_src_path)) if not code_type: if src.endswith('.wast'): code_type = CODE_TYPE_WAST elif src.endswith('.py'): code_type = CODE_TYPE_PY else: raise Exception('unknown code type') if code_type == 0: cpp2wast.set_src_path(_src_dir) cpp_src_file = src.replace('.wast', '.cpp') if not cpp2wast.build(cpp_src_file): raise Exception("build {0} failed".format(cpp_src_file)) if src.find('/') < 0: src = os.path.join(_src_dir, src) if abi and abi.find('/') < 0: abi = os.path.join(_src_dir, abi) if not eosapi.get_account(account): print('*'*20, 'create_account') _create_account(account) with open(src, 'rb') as f: code = f.read() if code_type == CODE_TYPE_WAST: code = eosapi.wast2wasm(code) code_hash = eosapi.sha256(code) old_hash = eosapi.get_code_hash(account) if code_hash != old_hash: print('Updating contract', src) if code_type == 0: _set_contract(account, src, abi) else: r = eosapi.set_contract(account, src, abi, code_type) assert r, 'set_contract failed'
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 prepare(account, src, abi, full_src_path, code_type = None): print('++++src:', src) _src_dir = os.path.dirname(os.path.abspath(full_src_path)) if not code_type: if src.endswith('.wast'): code_type = CODE_TYPE_WAST elif src.endswith('.py'): code_type = CODE_TYPE_PY else: raise Exception('unknown code type') if code_type == 0: cpp2wast.set_src_path(_src_dir) cpp_src_file = src.replace('.wast', '.cpp') if not cpp2wast.build(cpp_src_file): raise Exception("build {0} failed".format(cpp_src_file)) if src.find('/') < 0: src = os.path.join(_src_dir, src) if abi and abi.find('/') < 0: abi = os.path.join(_src_dir, abi) if not eosapi.get_account(account): print('*'*20, 'create_account') _create_account(account) code = None with open(src, 'rb') as f: code = f.read() if code_type == CODE_TYPE_WAST: code = eosapi.wast2wasm(code) elif code_type == CODE_TYPE_JAVA: print(_src_dir, src) compile_java_code(_src_dir, src) src = src.replace('.java', '.class') with open(src, 'rb') as f: code = f.read() old_code, _abi, old_code_hash, vm_type = eosapi.get_code(account) if code_type == CODE_TYPE_PY: try: co = compile(code, account, 'exec') except Exception as e: print(e) return if old_code: try: old_co = marshal.loads(old_code) if compare_code_object(old_co, co): return else: print('no need to update!') except Exception as e: print(e) else: code_hash = eosapi.sha256(code) if code_hash == old_code_hash: return print('Updating contract', src) if code_type == 0: _set_contract(account, src, abi) else: r = eosapi.set_contract(account, src, abi, code_type) assert r, 'set_contract failed'
def init(): psw = 'PW5K87AKbRvFFMJJm4dU7Zco4fi6pQtygEU4iyajwyTvmELUDnFBK' if not os.path.exists('data-dir/mywallet.wallet'): psw = wallet.create('mywallet') print('wallet password:'******'mywallet') wallet.unlock('mywallet', psw) priv_keys = [ '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3', '5JEcwbckBCdmji5j8ZoMHLEUS8TqQiqBG1DRx1X9DN124GUok9s', '5JbDP55GXN7MLcNYKCnJtfKi9aD2HvHAdY7g8m67zFTAFkY1uBB' ] keys = wallet.list_keys() exist_priv_keys = keys.values() for priv_key in priv_keys: if not priv_key in exist_priv_keys: wallet.import_key('mywallet', priv_key) if eosapi.is_replay(): return src_dir = os.path.dirname(os.path.abspath(__file__)) ''' r = eosapi.push_message('eosio.token', 'create', {"to":"eosio", "quantity":"10000.0000 EOS", "memo":""},{'eosio':'active'}) r = eosapi.push_message('eosio.token','issue',{"to":"hello","quantity":"1000.0000 EOS","memo":""},{'hello':'active'}) assert r msg = {"from":"eosio", "to":"hello", "quantity":"25.0000 EOS", "memo":"m"} r = eosapi.push_message('eosio.token', 'transfer', msg, {'eosio':'active'}) assert r ''' contracts_path = os.path.join(src_dir, '../../build', 'contracts') sys.path.append(os.getcwd()) for account in ['eosio.bios', 'eosio.msig', 'eosio.system', 'eosio.token']: print('account', account) if not eosapi.get_account(account).permissions: r = eosapi.create_account('eosio', account, key1, key2) assert r eosapi.produce_block() old_code = eosapi.get_code(account)[0] need_update = not old_code if False: #old_code: print('+++++++++old_code[:4]', old_code[:4]) if old_code[:4] != b'\x00asm': old_code = eosapi.wast2wasm(old_code) wast = os.path.join(contracts_path, account, account + '.wast') code = open(wast, 'rb').read() code = eosapi.wast2wasm(code) print(len(code), len(old_code), old_code[:20]) if code == old_code: need_update = False if need_update: wast = os.path.join(contracts_path, account, account + '.wast') abi = os.path.join(contracts_path, account, account + '.abi') r = eosapi.set_contract(account, wast, abi, 0) eosapi.produce_block() if False: #account == 'eosio.token': msg = { "issuer": "eosio", "maximum_supply": "1000000000.0000 EOS", "can_freeze": 0, "can_recall": 0, "can_whitelist": 0 } r = eosapi.push_message('eosio.token', 'create', msg, {'eosio.token': 'active'}) assert r r = eosapi.push_message('eosio.token', 'issue', { "to": "eosio", "quantity": "1000.0000 EOS", "memo": "" }, {'eosio': 'active'}) assert r eosapi.produce_block() from backyard import t t.deploy_mpy() #load common libraries # t.load_all() console = PyEosConsole(locals=globals()) console.interact(banner='Welcome to PyEos')