def process_transaction(self, trx): for msg in trx.get_messages(): code_info = eosapi.get_code(msg.code) if code_info: ctx = blockchain.apply_context(msg.code, trx, msg) blockchain.apply_message(ctx) '''
def init(): psw = 'PW5Kk1h8RqDwf8CB8mKcz7WZ8r6MF9of9CYvrC96XdBL5Z1SwBVx9' if not os.path.exists('data-dir/mywallet.wallet'): psw = wallet.create('mywallet') print(psw) wallet.open('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) src_dir = os.path.dirname(os.path.abspath(__file__)) sys.path.append(os.path.join(src_dir, '/contracts/cryptokitties')) contracts_path = os.path.join(src_dir, '../../build', 'contracts') for account in ['eosio.bios', 'eosio.msig', 'eosio.system', 'eosio.token']: if not eosapi.get_account(account).permissions: r = eosapi.create_account('eosio', account, key1, key2) assert r if not eosapi.get_code(account): 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()
def set_sys_contract(): if not eosapi.get_code('eosio')[0]: contracts_path = os.path.join(os.getcwd(), '..', 'contracts', 'eosio.system', 'eosio.system') wast = contracts_path + '.wast' abi = contracts_path + '.abi' r = eosapi.set_contract('eosio', wast, abi, 0) assert r and not r['except'] r = eosapi.push_action('eosio','setpriv',{'account':'eosio.msig', 'is_priv':1},{'eosio':'active'}) assert r
def _create_account(account): actions = [] newaccount = { 'creator': 'eosio', 'name': account, 'owner': { 'threshold': 1, 'keys': [{ 'key': initeos.key1, 'weight': 1 }], 'accounts': [], 'waits': [] }, 'active': { 'threshold': 1, 'keys': [{ 'key': initeos.key2, 'weight': 1 }], 'accounts': [], 'waits': [] } } _newaccount = eosapi.pack_args('eosio', 'newaccount', newaccount) act = ['eosio', 'newaccount', _newaccount, {'eosio': 'active'}] actions.append(act) ''' args = {'payer':'eosio', 'receiver':account, 'quant':"1.0000 EOS"} args = eosapi.pack_args('eosio', 'buyram', args) act = ['eosio', 'buyram', {'eosio':'active'}, args] actions.append(act) ''' if eosapi.get_code('eosio')[0]: args = {'payer': 'eosio', 'receiver': account, 'bytes': 64 * 1024} args = eosapi.pack_args('eosio', 'buyrambytes', args) act = ['eosio', 'buyrambytes', args, {'eosio': 'active'}] actions.append(act) args = { 'from': 'eosio', 'receiver': account, 'stake_net_quantity': '1.0050 EOS', 'stake_cpu_quantity': '1.0050 EOS', 'transfer': 1 } args = eosapi.pack_args('eosio', 'delegatebw', args) act = ['eosio', 'delegatebw', args, {'eosio': 'active'}] actions.append(act) rr, cost = eosapi.push_actions(actions) assert_ret(rr)
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 compare_performance(): with producer: r = eosapi.set_contract('currency', '../../build/contracts/currency/currency.wast', '../../build/contracts/currency/currency.abi',0) assert r time.sleep(3.0) test_performance() info = eosapi.get_code('currency') with producer: r = eosapi.set_contract('currency','../../programs/pyeos/contracts/currency/currency.py','../../contracts/currency/currency.abi', eosapi.py_vm_type) assert r time.sleep(3.0) test_performance()
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 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 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')