def test_compile_imports():
    make_tree(test_code)
    node = start_test_node()

    rpc = RPC_Client((test_node.HOST, test_node.PORT), 0)
    coinbase = rpc.eth_coinbase()['result']
    gas_price = int(rpc.eth_gasPrice()['result'], 16)
    balance = 0

    while balance/gas_price < int(MAXGAS, 16):
        balance = int(rpc.eth_getBalance(coinbase)['result'], 16)
        time.sleep(1)

    subprocess.check_call(['python',
                           'load_contracts.py',
                           '-p', '9696',
                           '-b', '2',
                           '-d', 'test_load_contracts.json',
                           '-s', 'foobar'])

    db = json.load(open("test_load_contracts.json"))
    func1 = db['foo']['fullsig'][0]['name']
    prefix = sha3.sha3_256(func1.encode('ascii')).hexdigest()[:8]
    arg = hex(1 << 65)[2:].strip('L').rjust(64, '0')
    r1 = rpc.eth_call(sender=coinbase,
                      to=db['foo']['address'],
                      data=('0x' + prefix + arg),
                      gas=hex(3*10**6))['result']

    r1 = int(r1, 16)
    if r1 > 2**255:
        r1 -= 2**256
    r2 = bar(1 << 65)
    if r1 == r2:
        print 'TEST PASSED'
    else:
        print 'TEST FAILED: <r1 {}> <r2 {}>'.format(r1, r2)
    rm_tree(test_code)
    node.send_signal(signal.SIGINT)
    node.wait()
def test_compile_imports():
    test_dir = os.path.dirname(os.path.realpath(__file__))

    try:
        make_tree(test_code, dirname=test_dir)
    except:
        shutil.rmtree(os.path.join(test_dir, 'foobar'))
        make_tree(test_code, dirname=test_dir)

    node = TestNode(
        log=open(os.path.join(test_dir, 'test_compile_imports.log'), 'w'))
    node.start()

    try:
        rpc = RPC_Client((node.rpchost, node.rpcport), 0)
        password = os.urandom(32).encode('hex')
        coinbase = rpc.personal_newAccount(password)['result']
        rpc.personal_unlockAccount(coinbase, password, 10**10)
        rpc.miner_start(2)

        gas_price = int(rpc.eth_gasPrice()['result'], 16)
        balance = 0

        print Style.BRIGHT + 'Mining coins...' + Style.RESET_ALL
        while balance / gas_price < int(MAXGAS, 16):
            balance = int(rpc.eth_getBalance(coinbase)['result'], 16)
            time.sleep(1)

        load_contracts = os.path.join(os.path.dirname(test_dir),
                                      'load_contracts.py')

        subprocess.check_call([
            'python', load_contracts, '-C', test_dir, '-p', '9696', '-b', '2',
            '-d', 'test_load_contracts.json', '-s', 'foobar'
        ])

        db = json.load(open(os.path.join(test_dir,
                                         "test_load_contracts.json")))
        func1 = db['foo']['fullsig'][0]['name']
        prefix = sha3.sha3_256(func1.encode('ascii')).hexdigest()[:8]
        arg = 8
        expected = round((2 * 8**0.5) / (8**0.5 - 1), 6)
        encoded_arg = hex(arg * 2**64)[2:].strip('L').rjust(64, '0')
        result = rpc.eth_call(sender=coinbase,
                              to=db['foo']['address'],
                              data=('0x' + prefix + encoded_arg),
                              gas=hex(3 * 10**6))['result']

        result = int(result, 16)
        if result > 2**255:
            result -= 2**256

        #the different internal representations
        #used in the calculations lead to some difference,
        #but the answers should be approximately the same.
        result = round(float(result) / 2**64, 6)

        if result == expected:
            print 'TEST PASSED'
        else:
            print 'TEST FAILED: <expected {}> <result {}>'.format(
                expected, result)
    except Exception as exc:
        traceback.print_exc()
    finally:
        shutil.rmtree(os.path.join(test_dir, 'foobar'))
        os.remove(os.path.join(test_dir, 'math_macros.sm'))
        os.remove(os.path.join(test_dir, 'test_load_contracts.json'))
def test_compile_imports():
    test_dir = os.path.dirname(os.path.realpath(__file__))

    try:
        make_tree(test_code, dirname=test_dir)
    except:
        shutil.rmtree(os.path.join(test_dir, 'foobar'))
        make_tree(test_code, dirname=test_dir)

    node = TestNode(log=open(os.path.join(test_dir,
                                          'test_compile_imports.log'), 
                             'w'))
    node.start()

    try:
        rpc = RPC_Client((node.rpchost, node.rpcport), 0)
        password = os.urandom(32).encode('hex')
        coinbase = rpc.personal_newAccount(password)['result']
        rpc.personal_unlockAccount(coinbase, password, 10**10)
        rpc.miner_start(2)

        gas_price = int(rpc.eth_gasPrice()['result'], 16)
        balance = 0

        print Style.BRIGHT + 'Mining coins...' + Style.RESET_ALL
        while balance/gas_price < int(MAXGAS, 16):
            balance = int(rpc.eth_getBalance(coinbase)['result'], 16)
            time.sleep(1)

        load_contracts = os.path.join(os.path.dirname(test_dir), 'load_contracts.py')

        subprocess.check_call(['python',
                               load_contracts,
                               '-C', test_dir,
                               '-p', '9696',
                               '-b', '2',
                               '-d', 'test_load_contracts.json',
                               '-s', 'foobar'])

        db = json.load(open(os.path.join(test_dir, "test_load_contracts.json")))
        func1 = db['foo']['fullsig'][0]['name']
        prefix = sha3.sha3_256(func1.encode('ascii')).hexdigest()[:8]
        arg = 8
        expected = round((2 * 8**0.5) / (8**0.5 - 1), 6)
        encoded_arg = hex(arg*2**64)[2:].strip('L').rjust(64, '0')
        result = rpc.eth_call(sender=coinbase,
                              to=db['foo']['address'],
                              data=('0x' + prefix + encoded_arg),
                              gas=hex(3*10**6))['result']

        result = int(result, 16)
        if result > 2**255:
            result -= 2**256
    
        #the different internal representations
        #used in the calculations lead to some difference,
        #but the answers should be approximately the same.
        result = round(float(result)/2**64, 6)

        if result == expected:
            print 'TEST PASSED'
        else:
            print 'TEST FAILED: <expected {}> <result {}>'.format(expected, result)
    except Exception as exc:
        traceback.print_exc()
    finally:
        shutil.rmtree(os.path.join(test_dir, 'foobar'))
        os.remove(os.path.join(test_dir, 'math_macros.sm'))
        os.remove(os.path.join(test_dir, 'test_load_contracts.json'))
Exemple #4
0
def get_sig(args):
    '''Creates the corresponding serpent signature for the given list of objects.'''
    return reduce(lambda a, b: a+get_sym(b), args, '')

def confirmed_send(**kwds):
    txhash = RPC.eth_sendTransaction(**kwds)['result']
    tries = 0
    while tries < MAXTRIES:
        time.sleep(BLOCKTIME)
        result = RPC.eth_getTransactionByHash(txhash)
        if result['result'] == None:
            return confirmed_send(**kwds)
        if result['result']['blockHash'] != None:
            break
        tries += 1
    return tries < MAXTRIES

if __name__ == '__main__':
    contract_name = sys.argv[1]
    contract_addr = json.loads(DB.Get(contract_name))['address']
    args = get_args()
    sig = get_sig(args)
    funcname = sys.argv[2]
    abi = abi_data(funcname, sig, args)
    if '--call' in sys.argv:
        RPC.eth_call(sender=COINBASE, to=contract_addr, data=abi, gas=GAS)
    if '--sendtx' in sys.argv:
        result = confirmed_send(sender=COINBASE, to=contract_addr, data=abi, gas=GAS)
        if not result:
            raise ValueError('transaction failed!')