예제 #1
0
파일: t.py 프로젝트: beaquant/pyeos
    def func_wrapper(*args, **kwargs):
        cpp2wast.set_src_path(os.path.dirname(__file__))
        if not cpp2wast.build('math.cpp'):
            raise Exception("build {0} failed".format('math.cpp'))

        prepare('backyard', 'backyard.py', 'backyard.abi', __file__)
        return func(*args, **kwargs)
예제 #2
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'
예제 #3
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'
예제 #4
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'
예제 #5
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'