Пример #1
0
def main():
    svcoin = '''\
def init():
    sstore(tx.origin, 21*10**9)

def sendSVCoins(to, amount):
    with my_bal = sload(msg.sender):
        if amount < my_bal:
            sstore(msg.sender, my_bal - amount)
            sstore(to, sload(to) + amount)
            return(1)
        return(-1)

def mySVCoinBalance():
    return(sload(msg.sender))

def getSVCoinBalance(address):
    return(sload(address))
'''

    evm = '0x' + serpent.compile(svcoin).encode('hex')
    fullsig = json.loads(serpent.mk_full_signature(svcoin))

    node = TestNode(log=open(os.devnull, 'w'), verbose=False)
    node.start()

    rpc = RPC_Client((node.rpchost, node.rpcport), 0)
    password = os.urandom(32).encode('hex')
    account = rpc.personal_newAccount(password)['result']
    rpc.personal_unlockAccount(account, password, hex(500))
    rpc.miner_start(2)

    balance = 0
    while balance < int(MAXGAS, 16):
        balance = int(rpc.eth_getBalance(account)['result'], 16)

    txhash = rpc.eth_sendTransaction(sender=account, data=evm,
                                     gas=MAXGAS)['result']

    while True:
        response = rpc.eth_getTransactionReceipt(txhash)
        receipt = response.get('result', False)
        if receipt:
            blocknumber = receipt.get('blockNumber', False)
            if blocknumber:
                address = receipt['contractAddress']
                break

    contract = Contract(address, fullsig, rpc)
    print 'My balance is', contract.mySVCoinBalance(call=True)
    receipt = contract.sendSVCoins(2, 10000, send=True, receipt=True)
    print 'Sent coins to address 2, receipt:'
    print json.dumps(receipt, indent=4, sort_keys=True)
    print 'Balance at address 2 is', contract.getSVCoinBalance(2, call=True)
Пример #2
0
def main():
    svcoin = '''\
def init():
    sstore(tx.origin, 21*10**9)

def sendSVCoins(to, amount):
    with my_bal = sload(msg.sender):
        if amount < my_bal:
            sstore(msg.sender, my_bal - amount)
            sstore(to, sload(to) + amount)
            return(1)
        return(-1)

def mySVCoinBalance():
    return(sload(msg.sender))

def getSVCoinBalance(address):
    return(sload(address))
'''

    evm = '0x' + serpent.compile(svcoin).encode('hex')
    fullsig = json.loads(serpent.mk_full_signature(svcoin))

    node = TestNode(log=open(os.devnull, 'w'), verbose=False)
    node.start()

    rpc = RPC_Client((node.rpchost, node.rpcport), 0)
    password = os.urandom(32).encode('hex')
    account = rpc.personal_newAccount(password)['result']
    rpc.personal_unlockAccount(account, password, hex(500))
    rpc.miner_start(2)
    
    balance = 0
    while balance < int(MAXGAS, 16):
        balance = int(rpc.eth_getBalance(account)['result'], 16)
    
    txhash = rpc.eth_sendTransaction(sender=account, data=evm, gas=MAXGAS)['result']

    while True:
        response = rpc.eth_getTransactionReceipt(txhash)
        receipt = response.get('result', False)
        if receipt:
            blocknumber = receipt.get('blockNumber', False)
            if blocknumber:
                address = receipt['contractAddress']
                break

    contract = Contract(address, fullsig, rpc)
    print 'My balance is', contract.mySVCoinBalance(call=True)
    receipt = contract.sendSVCoins(2, 10000, send=True, receipt=True)
    print 'Sent coins to address 2, receipt:'
    print json.dumps(receipt, indent=4, sort_keys=True)
    print 'Balance at address 2 is', contract.getSVCoinBalance(2, call=True)
Пример #3
0
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'))
Пример #5
0
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'))