Beispiel #1
0
def deploy_contract(currency, contract_type=0):
    if contract_type <= 0:
        return
    if contract_type == 1:
        r = eosapi.set_contract(
            currency, '../../programs/pyeos/contracts/currency/currency.py',
            '../../contracts/currency/currency.abi', 1)
    elif contract_type == 2:
        with open('../../programs/pyeos/contracts/currency/currency.mpy',
                  'wb') as f:
            data = eosapi.mp_compile(
                '../../programs/pyeos/contracts/currency/currency.py')
            f.write(data)
        r = eosapi.set_contract(
            currency, '../../programs/pyeos/contracts/currency/currency.mpy',
            '../../contracts/currency/currency.abi', 1)
    elif contract_type == 3:
        wast = '../../build/contracts/eosio.token/eosio.token.wast'
        key_words = b"hello,world\\00"
        wast = '../../build/contracts/eosio.token/eosio.token.wast'
        with open(wast, 'rb') as f:
            data = f.read()
            #data.find(key_words)
            replace_str = b"%s\\00" % (currency.encode('utf8'), )
            replace_str.zfill(len(key_words))
            #replace key works with custom words to break the effect of code cache mechanism
            data = data.replace(key_words, replace_str)
            with open('currency2.wast', 'wb') as f:
                f.write(data)
        r = eosapi.set_contract(
            currency, 'currency2.wast',
            '../../build/contracts/eosio.token/eosio.token.abi', 0)
    else:
        assert 0
Beispiel #2
0
def init():
    psw = 'PW5Kd5tv4var9XCzvQWHZVyBMPjHEXwMjH1V19X67kixwxRpPNM4J'
    wallet.open('mywallet')
    wallet.unlock('mywallet', psw)
    
    key1 = 'EOS61MgZLN7Frbc2J7giU7JdYjy2TqnfWFjZuLXvpHJoKzWAj7Nst'
    key2 = 'EOS5JuNfuZPATy8oPz9KMZV2asKf9m8fb2bSzftvhW55FKQFakzFL'
    
    with producer:
        if not eosapi.get_account('currency'):
            r = eosapi.create_account('inita', 'currency', key1, key2)

        if not eosapi.get_account('test'):
            r = eosapi.create_account('inita', 'test', key1, key2)
            assert r

    with producer:
        r = eosapi.set_contract('currency', '../../programs/pyeos/contracts/currency/currency.py', '../../contracts/currency/currency.abi', eosapi.py_vm_type)
        assert r
        r = eosapi.set_contract('test','../../programs/pyeos/contracts/test/code.py','../../programs/pyeos/contracts/test/test.abi', eosapi.py_vm_type)
        assert r

    #transfer some "money" to test account for test
    with producer:
        r = eosapi.push_message('currency','transfer','{"from":"currency","to":"test","quantity":1000}',['currency','test'],{'currency':'active'})
        assert r

    #transfer some "money" to test account for test
    with producer:
        r = eosapi.push_message('eos','transfer',{"from":"inita","to":"test","amount":1000,"memo":"hello"},['inita','test'],{'inita':'active'})
        assert r
Beispiel #3
0
def init(mpy=True):
    with producer:
        if not eosapi.get_account('backyard').permissions:
            r = eosapi.create_account('eosio', 'backyard', initeos.key1,
                                      initeos.key2)
            assert r

    with producer:
        if mpy:
            with open('../../programs/pyeos/contracts/backyard/backyard.mpy',
                      'wb') as f:
                f.write(
                    eosapi.mp_compile(
                        '../../programs/pyeos/contracts/backyard/backyard.py'))
            r = eosapi.set_contract(
                'backyard',
                '../../programs/pyeos/contracts/backyard/backyard.mpy',
                '../../programs/pyeos/contracts/backyard/backyard.abi', 1)
        else:
            r = eosapi.set_contract(
                'backyard',
                '../../programs/pyeos/contracts/backyard/backyard.py',
                '../../programs/pyeos/contracts/backyard/backyard.abi', 1)
            #        r = eosapi.set_contract('currency', '../../build/contracts/currency/currency.wast', '../../build/contracts/currency/currency.abi',0)
            assert r
Beispiel #4
0
def init():
    psw = 'PW5Kd5tv4var9XCzvQWHZVyBMPjHEXwMjH1V19X67kixwxRpPNM4J'
    wallet.open('mywallet')
    wallet.unlock('mywallet', psw)

    key1 = 'EOS61MgZLN7Frbc2J7giU7JdYjy2TqnfWFjZuLXvpHJoKzWAj7Nst'
    key2 = 'EOS5JuNfuZPATy8oPz9KMZV2asKf9m8fb2bSzftvhW55FKQFakzFL'
    if not eosapi.get_account('currency'):
        with producer:
            r = eosapi.create_account('inita', 'currency', key1, key2)
            assert r

    if not eosapi.get_account('exchange'):
        with producer:
            r = eosapi.create_account('inita', 'exchange', key1, key2)
            assert r

    with producer:
        r = eosapi.set_contract(
            'currency', '../../programs/pyeos/contracts/currency/currency.py',
            '../../contracts/currency/currency.abi', 1)
        assert r
        r = eosapi.set_contract(
            'exchange', '../../programs/pyeos/contracts/exchange/exchange.py',
            '../../contracts/exchange/exchange.abi', 1)
        assert r
Beispiel #5
0
def test3(count, d=0):
    keys = list(wallet.list_keys().keys())
    for i in range(0, count):
        currency = 'curre'+n2s(i)
#        currency = 'currency'

        key1 = keys[i]
        key2 = keys[10000+i]

        if not eosapi.get_account(currency).permissions:
            r = eosapi.create_account('eosio', currency, key1, key2)
            assert r
        if d:
            if d==1:
                r = eosapi.set_contract(currency,'../../programs/pyeos/contracts/currency/currency.py','../../contracts/currency/currency.abi', 1)
            elif d == 2:
                with open('../../programs/pyeos/contracts/currency/currency.mpy', 'wb') as f:
                    data = eosapi.mp_compile('../../programs/pyeos/contracts/currency/currency.py')
                    f.write(data)
                r = eosapi.set_contract(currency,'../../programs/pyeos/contracts/currency/currency.mpy','../../contracts/currency/currency.abi', 1)
            elif d == 3:
                wast = '../../build/contracts/currency/currency.wast'
                key_words = b"hello,world\\00"
                wast = '../../build/contracts/currency/currency.wast'
                with open(wast, 'rb') as f:
                    data = f.read()
                    #data.find(key_words)
                    replace_str = b"%s\\00"%(currency.encode('utf8'),)
                    replace_str.zfill(len(key_words))
                    #replace key works with custom words to break the effect of code cache mechanism
                    data = data.replace(key_words, replace_str)
                    with open('currency2.wast', 'wb') as f:
                        f.write(data)
                r = eosapi.set_contract(currency, 'currency2.wast', '../../build/contracts/currency/currency.abi',0)
            else:
                assert 0

    eosapi.produce_block()

    accounts = []
    functions = []
    args = []
    per = []
    
    for i in range(0, count):
        currency = 'curre'+n2s(i)
        accounts.append(currency)
        per.append({currency:'active'})
        functions.append('issue')
        arg = str(i)
        args.append({"to":currency,"quantity":"1000.0000 CUR","memo":""})
    ret = eosapi.push_messages(accounts, functions, args, per, True, rawargs=False)

    assert ret
    if ret:
        cost = ret['cost_time']
    eosapi.produce_block()

    print('total cost time:%.3f s, cost per action: %.3f ms, actions per second: %.3f'%(cost/1e6, cost/count/1000, 1*1e6/(cost/count)))
Beispiel #6
0
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()
Beispiel #7
0
def update_eosio():
    '''
    account = 'eosio'
    actions = []
    args = {'payer':'eosio', 'receiver':account, 'quant':"1000.0000 EOS"}
    args = eosapi.pack_args('eosio', 'buyram', args)
    act = ['eosio', 'buyram', {'eosio':'active'}, args]
    actions.append(act)
    
    args = {'from': 'eosio',
     'receiver': account,
     'stake_net_quantity': '1000.0050 EOS',
     'stake_cpu_quantity': '1000.0050 EOS',
     'transfer': 0}
    args = eosapi.pack_args('eosio', 'delegatebw', args)
    act = ['eosio', 'delegatebw', args, {'eosio':'active'}]
    actions.append(act)
    rr, cost = eosapi.push_actions(actions)
    '''

    contracts_path = os.path.join(os.getcwd(), '..', 'contracts')
    sys.path.append(os.getcwd())
    account = 'eosio'

    _path = os.path.join(contracts_path, 'eosio.system', 'eosio.system')
    wast = _path + '.wast'
    abi = _path + '.abi'
    r = eosapi.set_contract(account, wast, abi, 0)
    assert r
Beispiel #8
0
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()
Beispiel #9
0
def init():
    if not eosapi.get_account('currency'):
        with producer:
            r = eosapi.create_account('inita', 'currency', initeos.key1, initeos.key2)
            assert r

    if not eosapi.get_account('exchange'):
        with producer:
            r = eosapi.create_account('inita', 'exchange', initeos.key1, initeos.key2)
            assert r

    with producer:
        r = eosapi.set_contract('currency', '../../programs/pyeos/contracts/currency/currency.py', '../../contracts/currency/currency.abi', eosapi.py_vm_type)
        assert r
        r = eosapi.set_contract('exchange', '../../programs/pyeos/contracts/exchange/exchange.py', '../../contracts/exchange/exchange.abi', eosapi.py_vm_type)
        assert r
Beispiel #10
0
def test2():
    with producer:
        if not eosapi.get_account('currency'):
            r = eosapi.create_account('inita', 'currency', key1, key2)
            assert r
        if not eosapi.get_account('test'):
            if not eosapi.get_account('test'):
                r = eosapi.create_account('inita', 'test', key1, key2)
                assert r
        r = eosapi.set_contract('currency',
                                '../../contracts/currency/currency.py',
                                '../../contracts/currency/currency.abi', 1)

    start = time.time()
    ts = PySignedTransaction()
    ts.reqire_scope(b'test')
    ts.reqire_scope(b'currency')

    for i in range(1):
        data = struct.pack("QQQ", N(b'currency'), N(b'test'), 1)
        msg = PyMessage()
        msg.init(b'currency', b'transfer', [[b'currency', b'active']], data)
        ts.add_message(msg)

    ctl = chain_controller()
    ctl.process_transaction(ts)
    r = eosapi.get_table(b'test', b'currency', b'account')
    print(r)
    print('cost:', time.time() - start)
Beispiel #11
0
def init(wasm = False):
    with producer:
        if not eosapi.get_account('currency').permissions:
                r = eosapi.create_account('eosio', 'currency', initeos.key1, initeos.key2)
                assert r
        if not eosapi.get_account('test').permissions:
            if not eosapi.get_account('test'):
                r = eosapi.create_account('eosio', 'test', initeos.key1, initeos.key2)
                assert r

    with producer:
        if wasm:
            r = eosapi.set_contract('currency', '../../build/contracts/currency/currency.wast', '../../build/contracts/currency/currency.abi',0)
            assert r
        else:
            r = eosapi.set_contract('currency','../../programs/pyeos/contracts/currency/currency.py','../../contracts/currency/currency.abi', 1)
            assert r
Beispiel #12
0
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'
Beispiel #13
0
 def func_wrapper(wasm=True, *args, **kwargs):
     eosapi.push_action('eosio', 'setpriv', {
         'account': 'eosio.prods',
         'is_priv': 1
     }, {'eosio': 'active'})
     r = eosapi.set_contract(
         'eosio.prods', '../contracts/eosio.prods/eosio.prods.wast',
         '../contracts/eosio.prods/eosio.prods.abi', 0)
     return func(*args, **kwargs)
Beispiel #14
0
def t():
    import time
    start = time.time()
    r = eosapi.set_contract('currency',
                            '../../build/contracts/currency/currency.wast',
                            '../../build/contracts/currency/currency.abi', 0)
    cost = time.time() - start
    print(cost, cost / 1, 1 / (cost / 1))
    eosapi.produce_block()
Beispiel #15
0
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()
Beispiel #16
0
def init():
    if not eosapi.get_account('test').permissions:
        with producer:
            r = eosapi.create_account('eosio', 'test', initeos.key1, initeos.key2)
            assert r

    with producer:
        r = eosapi.set_contract('test','../../programs/pyeos/contracts/apitest/apitest.py','../../programs/pyeos/contracts/apitest/test.abi', 1)
        assert r
    eosapi.produce_block()
Beispiel #17
0
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
Beispiel #18
0
def t():
    contracts_path = os.path.join(os.getcwd(), '..', 'contracts')
    sys.path.append(os.getcwd())
    account = 'eosio'
    path = 'eosio.system'
    _path = os.path.join(contracts_path, path, path)
    print('+++++++++code update', account)
    wast = _path + '.wast'
    abi = _path + '.abi'
    with producer:
        r = eosapi.set_contract(account, wast, abi, 0)
Beispiel #19
0
def init():
    if not eosapi.get_account('cache').permissions:
        with producer:
            r = eosapi.create_account('eosio', 'cache', initeos.key1,
                                      initeos.key2)
            assert r

    with producer:
        r = eosapi.set_contract(
            'cache', '../../programs/pyeos/contracts/cache/cache.py',
            '../../programs/pyeos/contracts/cache/test.abi', 1)
        assert r
Beispiel #20
0
def init():
    with producer:
        if not eosapi.get_account('rpctest').permissions:
            r = eosapi.create_account('eosio', 'rpctest', initeos.key1,
                                      initeos.key2)
            assert r

    with producer:
        r = eosapi.set_contract(
            'rpctest', '../../programs/pyeos/contracts/rpctest/rpctest.py',
            '../../programs/pyeos/contracts/rpctest/rpctest.abi', 3)
        #        r = eosapi.set_contract('currency', '../../build/contracts/currency/currency.wast', '../../build/contracts/currency/currency.abi',0)
        assert r
Beispiel #21
0
def init():
    with producer:
        if not eosapi.get_account('currency'):
            r = eosapi.create_account('inita', 'currency', initeos.key1,
                                      initeos.key2)

        if not eosapi.get_account('test'):
            r = eosapi.create_account('inita', 'test', initeos.key1,
                                      initeos.key2)
            assert r

    with producer:
        r = eosapi.set_contract(
            'currency', '../../programs/pyeos/contracts/currency/currency.py',
            '../../contracts/currency/currency.abi', eosapi.py_vm_type)
        assert r
        r = eosapi.set_contract(
            'test', '../../programs/pyeos/contracts/test/code.py',
            '../../programs/pyeos/contracts/test/test.abi', eosapi.py_vm_type)
        assert r

    #transfer some "money" to test account for test
    with producer:
        r = eosapi.push_message(
            'currency', 'transfer',
            '{"from":"currency","to":"test","quantity":1000}',
            ['currency', 'test'], {'currency': 'active'})
        assert r

    #transfer some "money" to test account for test
    with producer:
        r = eosapi.push_message('eos', 'transfer', {
            "from": "inita",
            "to": "test",
            "amount": 1000,
            "memo": "hello"
        }, ['inita', 'test'], {'inita': 'active'})
        assert r
Beispiel #22
0
def init():
    with producer:
        if not eosapi.get_account('micropython'):
                r = eosapi.create_account('inita', 'micropython', initeos.key1, initeos.key2)
                assert r
        if not eosapi.get_account('test'):
            if not eosapi.get_account('test'):
                r = eosapi.create_account('inita', 'test', initeos.key1, initeos.key2)
                assert r

    with producer:
        r = eosapi.set_contract('micropython','../../programs/pyeos/contracts/micropython/mp.py','../../contracts/currency/currency.abi',2)
#        r = eosapi.set_contract('currency', '../../build/contracts/currency/currency.wast', '../../build/contracts/currency/currency.abi',0)
        assert r
Beispiel #23
0
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
Beispiel #24
0
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'
Beispiel #25
0
def init():
    with producer:
        if not eosapi.get_account('kitties').permissions:
            r = eosapi.create_account('eosio', 'kitties', initeos.key1,
                                      initeos.key2)
            assert r
            sync.clean()

    with producer:
        r = eosapi.set_contract(
            'kitties', '../../programs/pyeos/contracts/cryptokitties/main.py',
            '../../programs/pyeos/contracts/cryptokitties/cryptokitties.abi',
            1)
        #        r = eosapi.set_contract('currency', '../../build/contracts/currency/currency.wast', '../../build/contracts/currency/currency.abi',0)
        assert r
        sync.clean()
Beispiel #26
0
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'
Beispiel #27
0
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
Beispiel #28
0
def deploy_wasm_code():
    with producer:
        r = eosapi.set_contract(
            'test', '../../build/programs/pyeos/contracts/test/code.wast',
            '../../build/programs/pyeos/contracts/test/test.abi', 0)
Beispiel #29
0
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'
Beispiel #30
0
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')