示例#1
0
def geth_wait_and_check(privatekeys, rpc_ports):
    """ Wait until the geth cluster is ready. """
    address = address_encoder(privatekey_to_address(privatekeys[0]))
    jsonrpc_running = False
    tries = 5
    rpc_port = rpc_ports[0]
    jsonrpc_client = JSONRPCClient(host="0.0.0.0", port=rpc_port, privkey=privatekeys[0], print_communication=False)

    while not jsonrpc_running and tries > 0:
        try:
            jsonrpc_client.call("eth_getBalance", address, "latest")
            jsonrpc_running = True
        except ConnectionError:
            gevent.sleep(0.5)
            tries -= 1

    if jsonrpc_running is False:
        raise ValueError("geth didnt start the jsonrpc interface")

    for key in set(privatekeys):
        address = address_encoder(privatekey_to_address(key))
        jsonrpc_client = JSONRPCClient(host="0.0.0.0", port=rpc_port, privkey=key, print_communication=False)

        tries = 10
        balance = "0x0"
        while balance == "0x0" and tries > 0:
            balance = jsonrpc_client.call("eth_getBalance", address, "latest")
            gevent.sleep(1)
            tries -= 1

        if balance == "0x0":
            raise ValueError("account is with a balance of 0")
示例#2
0
class Client():

    def __init__(self, tokens):
        self.client = JSONRPCClient(
            privkey=None,
            host=host,
            port=port,
            print_communication=False,
        )
        self.wallet_translator = ContractTranslator(wallet_abi)
        self.tokens = tokens

    def get_block(self, num):
        return self.client.call(
            'eth_getBlockByNumber',
            quantity_encoder(num),
            True
        )

    def get_transaction(self, hash):
        return self.client.eth_getTransactionByHash(hash)

    def decode_token_transfer(self, txdata, to_address):
        if len(txdata) < 8 or txdata[:8] != 'a9059cbb':
            return None

        # get rid of signature
        txdata = txdata[8:]

        # here we got ourselves a token transfer
        # transfer(address _from, uint256 _value)
        token_name = self.tokens.address_is_token(to_address)
        if token_name is None:
            print('WARNING: Unknown token {} transferred'.format(to_address))
            token_name = 'UNKNOWN'

        hexdata = txdata.decode('hex')
        transfer_to = decode_abi(['address'], hexdata[:32])[0]
        transfer_value = decode_abi(['uint256'], hexdata[32:])[0]

        if address_is_whitehat(transfer_to) is None:
            print('WARNING: {} token sent to non-whitehat address?'.format(token_name))

        return transfer_value

    def decode_execute(self, txdata):
        # get rid of signature and 0x
        txdata = txdata[10:]

        # unfortunately the pyethapp way does not work
        # fndata = c.wallet_translator.function_data['execute']
        # return decode_abi(fndata['encode_types'], txdata.decode('hex'))

        # ... but decoding each arg individually does work
        sent_to = decode_abi(['address'], txdata.decode('hex')[:32])[0]
        amount_in_wei = decode_abi(['uint256'], txdata.decode('hex')[32:64])[0]

        token_value = self.decode_token_transfer(txdata[256:], sent_to)

        return sent_to, amount_in_wei, token_value
示例#3
0
def hydrachain_wait(privatekeys, number_of_nodes):
    """ Wait until the hydrchain cluster is ready. """
    jsonrpc_client = JSONRPCClient(
        host='0.0.0.0',
        privkey=privatekeys[0],
        print_communication=False,
    )

    quantity = jsonrpc_client.call('net_peerCount')
    tries = 5

    while quantity != number_of_nodes and tries > 0:
        gevent.sleep(0.5)
        quantity = quantity_decoder(jsonrpc_client.call('net_peerCount'))

    if quantity != number_of_nodes:
        raise Exception('hydrachain is taking to long to initialize')
示例#4
0
def hydrachain_wait(privatekeys, number_of_nodes):
    """ Wait until the hydrchain cluster is ready. """
    jsonrpc_client = JSONRPCClient(
        host='0.0.0.0',
        privkey=privatekeys[0],
        print_communication=False,
    )

    quantity = jsonrpc_client.call('net_peerCount')
    tries = 5

    while quantity != number_of_nodes and tries > 0:
        gevent.sleep(0.5)
        quantity = quantity_decoder(jsonrpc_client.call('net_peerCount'))

    if quantity != number_of_nodes:
        raise Exception('hydrachain is taking to long to initialize')
示例#5
0
class BuiltinDriver(base.Base):
	
	@property
	def type(self):
		return 'tx_driver'

	def __init__(self):
		self.logger = logger
		self.rpc_cli = JSONRPCClient(host = FLAGS.rpc_host, port = FLAGS.rpc_port, print_communication = False)
		super(BuiltinDriver, self).__init__()

	def pending_txs(self):
		self.filter_id = self.rpc_cli.call(constant.METHOD_NEW_PENDING_TX_FILTER)
		while True:
			res = self.rpc_cli.call(constant.METHOD_GET_FILTER_CHANGES, self.filter_id)
			if res:
				print res
			else:
				time.sleep(FLAGS.poll_interval)
示例#6
0
def geth_wait_and_check(privatekeys, rpc_ports):
    """ Wait until the geth cluster is ready. """
    address = address_encoder(privatekey_to_address(privatekeys[0]))
    jsonrpc_running = False
    tries = 5
    rpc_port = rpc_ports[0]
    jsonrpc_client = JSONRPCClient(
        host='0.0.0.0',
        port=rpc_port,
        privkey=privatekeys[0],
        print_communication=False,
    )

    while not jsonrpc_running and tries > 0:
        try:
            jsonrpc_client.call('eth_getBalance', address, 'latest')
            jsonrpc_running = True
        except ConnectionError:
            gevent.sleep(0.5)
            tries -= 1

    if jsonrpc_running is False:
        raise ValueError('geth didnt start the jsonrpc interface')

    for key in set(privatekeys):
        address = address_encoder(privatekey_to_address(key))
        jsonrpc_client = JSONRPCClient(
            host='0.0.0.0',
            port=rpc_port,
            privkey=key,
            print_communication=False,
        )

        tries = 10
        balance = '0x0'
        while balance == '0x0' and tries > 0:
            balance = jsonrpc_client.call('eth_getBalance', address, 'latest')
            gevent.sleep(1)
            tries -= 1

        if balance == '0x0':
            raise ValueError('account is with a balance of 0')
示例#7
0
def geth_wait_and_check(privatekeys):
    """ Wait until the geth cluster is ready. """
    address = address_encoder(privtoaddr(privatekeys[0]))
    jsonrpc_running = False
    tries = 5
    jsonrpc_client = JSONRPCClient(
        host='0.0.0.0',
        privkey=privatekeys[0],
        print_communication=False,
    )

    while not jsonrpc_running and tries > 0:
        try:
            jsonrpc_client.call('eth_getBalance', address, 'latest')
            jsonrpc_running = True
        except ConnectionError:
            gevent.sleep(0.5)
            tries -= 1

    if jsonrpc_running is False:
        raise ValueError('geth didnt start the jsonrpc interface')

    for key in set(privatekeys):
        address = address_encoder(privtoaddr(key))
        jsonrpc_client = JSONRPCClient(
            host='0.0.0.0',
            privkey=key,
            print_communication=False,
        )

        tries = 10
        balance = '0x0'
        while balance == '0x0' and tries > 0:
            balance = jsonrpc_client.call('eth_getBalance', address, 'latest')
            gevent.sleep(1)
            tries -= 1

        if balance == '0x0':
            raise ValueError('account is with a balance of 0')
示例#8
0
def test_blockchain(blockchain_backend, private_keys, number_of_nodes,
                    poll_timeout):
    # pylint: disable=too-many-locals
    addresses = [privatekey_to_address(priv) for priv in private_keys]

    privatekey = private_keys[0]
    address = privatekey_to_address(privatekey)
    total_asset = 100

    jsonrpc_client = JSONRPCClient(
        privkey=privatekey,
        print_communication=False,
    )
    patch_send_transaction(jsonrpc_client)

    humantoken_path = get_contract_path('HumanStandardToken.sol')
    humantoken_contracts = compile_file(humantoken_path, libraries=dict())
    token_proxy = jsonrpc_client.deploy_solidity_contract(
        address,
        'HumanStandardToken',
        humantoken_contracts,
        dict(),
        (total_asset, 'raiden', 2, 'Rd'),
        timeout=poll_timeout,
    )

    registry_path = get_contract_path('Registry.sol')
    registry_contracts = compile_file(registry_path)
    registry_proxy = jsonrpc_client.deploy_solidity_contract(
        address,
        'Registry',
        registry_contracts,
        dict(),
        tuple(),
        timeout=poll_timeout,
    )

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 0

    # pylint: disable=no-member

    assert token_proxy.balanceOf(address) == total_asset
    transaction_hash = registry_proxy.addAsset.transact(
        token_proxy.address,
        gasprice=denoms.wei,
    )
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout)

    assert len(registry_proxy.assetAddresses.call()) == 1

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 1

    channel_manager_address_encoded = registry_proxy.channelManagerByAsset.call(
        token_proxy.address)
    channel_manager_address = channel_manager_address_encoded.decode('hex')

    log = log_list[0]
    log_topics = [
        decode_topic(topic) for topic in log['topics']  # pylint: disable=invalid-sequence-index
    ]
    log_data = log['data']
    event = registry_proxy.translator.decode_event(
        log_topics,
        log_data[2:].decode('hex'),
    )

    assert channel_manager_address == event['channelManagerAddress'].decode(
        'hex')
    assert token_proxy.address == event['assetAddress'].decode('hex')

    channel_manager_proxy = jsonrpc_client.new_contract_proxy(
        registry_contracts['ChannelManagerContract']['abi'],
        channel_manager_address,
    )

    transaction_hash = channel_manager_proxy.newChannel.transact(
        addresses[1],
        10,
        gasprice=denoms.wei,
    )
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 2
示例#9
0
def test_blockchain(
        blockchain_type,
        blockchain_backend,  # required to start the geth backend
        blockchain_rpc_ports,
        private_keys,
        poll_timeout):
    # pylint: disable=too-many-locals

    # this test is for interaction with a blockchain using json-rpc, so it
    # doesnt make sense to execute it against mock or tester
    if blockchain_type not in ('geth',):
        return

    addresses = [
        privatekey_to_address(priv)
        for priv in private_keys
    ]

    privatekey = private_keys[0]
    address = privatekey_to_address(privatekey)
    total_token = 100

    jsonrpc_client = JSONRPCClient(
        port=blockchain_rpc_ports[0],
        privkey=privatekey,
        print_communication=False,
    )
    patch_send_transaction(jsonrpc_client)
    patch_send_message(jsonrpc_client)

    humantoken_path = get_contract_path('HumanStandardToken.sol')
    humantoken_contracts = compile_file(humantoken_path, libraries=dict())
    token_proxy = jsonrpc_client.deploy_solidity_contract(
        address,
        'HumanStandardToken',
        humantoken_contracts,
        dict(),
        (total_token, 'raiden', 2, 'Rd'),
        contract_path=humantoken_path,
        gasprice=default_gasprice,
        timeout=poll_timeout,
    )

    registry_path = get_contract_path('Registry.sol')
    registry_contracts = compile_file(registry_path)
    registry_proxy = jsonrpc_client.deploy_solidity_contract(
        address,
        'Registry',
        registry_contracts,
        dict(),
        tuple(),
        contract_path=registry_path,
        gasprice=default_gasprice,
        timeout=poll_timeout,
    )

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 0

    # pylint: disable=no-member

    assert token_proxy.balanceOf(address) == total_token
    transaction_hash = registry_proxy.addToken.transact(
        token_proxy.address,
        gasprice=denoms.wei,
    )
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout)

    assert len(registry_proxy.tokenAddresses.call()) == 1

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 1

    channel_manager_address_encoded = registry_proxy.channelManagerByToken.call(
        token_proxy.address,
    )
    channel_manager_address = channel_manager_address_encoded.decode('hex')

    log = log_list[0]
    log_topics = [
        decode_topic(topic)
        for topic in log['topics']  # pylint: disable=invalid-sequence-index
    ]
    log_data = log['data']
    event = registry_proxy.translator.decode_event(
        log_topics,
        log_data[2:].decode('hex'),
    )

    assert channel_manager_address == event['channel_manager_address'].decode('hex')
    assert token_proxy.address == event['token_address'].decode('hex')

    channel_manager_proxy = jsonrpc_client.new_contract_proxy(
        CHANNEL_MANAGER_ABI,
        channel_manager_address,
    )

    transaction_hash = channel_manager_proxy.newChannel.transact(
        addresses[1],
        10,
        gasprice=denoms.wei,
    )
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 2
def test_blockchain(
        blockchain_type,
        blockchain_backend,  # required to start the geth backend
        blockchain_rpc_ports,
        private_keys,
        poll_timeout):
    # pylint: disable=too-many-locals

    # this test is for interaction with a blockchain using json-rpc, so it
    # doesnt make sense to execute it against mock or tester
    if blockchain_type not in ('geth',):
        return

    addresses = [
        privatekey_to_address(priv)
        for priv in private_keys
    ]

    privatekey = private_keys[0]
    address = privatekey_to_address(privatekey)
    total_asset = 100

    jsonrpc_client = JSONRPCClient(
        port=blockchain_rpc_ports[0],
        privkey=privatekey,
        print_communication=False,
    )
    patch_send_transaction(jsonrpc_client)

    humantoken_path = get_contract_path('HumanStandardToken.sol')
    humantoken_contracts = compile_file(humantoken_path, libraries=dict())
    token_proxy = jsonrpc_client.deploy_solidity_contract(
        address,
        'HumanStandardToken',
        humantoken_contracts,
        dict(),
        (total_asset, 'raiden', 2, 'Rd'),
        timeout=poll_timeout,
    )

    registry_path = get_contract_path('Registry.sol')
    registry_contracts = compile_file(registry_path)
    registry_proxy = jsonrpc_client.deploy_solidity_contract(
        address,
        'Registry',
        registry_contracts,
        dict(),
        tuple(),
        timeout=poll_timeout,
    )

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 0

    # pylint: disable=no-member

    assert token_proxy.balanceOf(address) == total_asset
    transaction_hash = registry_proxy.addAsset.transact(
        token_proxy.address,
        gasprice=denoms.wei,
    )
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout)

    assert len(registry_proxy.assetAddresses.call()) == 1

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 1

    channel_manager_address_encoded = registry_proxy.channelManagerByAsset.call(
        token_proxy.address,
    )
    channel_manager_address = channel_manager_address_encoded.decode('hex')

    log = log_list[0]
    log_topics = [
        decode_topic(topic)
        for topic in log['topics']  # pylint: disable=invalid-sequence-index
    ]
    log_data = log['data']
    event = registry_proxy.translator.decode_event(
        log_topics,
        log_data[2:].decode('hex'),
    )

    assert channel_manager_address == event['channel_manager_address'].decode('hex')
    assert token_proxy.address == event['asset_address'].decode('hex')

    channel_manager_proxy = jsonrpc_client.new_contract_proxy(
        registry_contracts['ChannelManagerContract']['abi'],
        channel_manager_address,
    )

    transaction_hash = channel_manager_proxy.newChannel.transact(
        addresses[1],
        10,
        gasprice=denoms.wei,
    )
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=poll_timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 2
示例#11
0
class BlockTest(unittest.TestCase):
    
    def setUp(self):
        self.db_proxy = dbproxy.MongoDBProxy()
        self.rpc_cli = JSONRPCClient(host = FLAGS.rpc_host, port = FLAGS.rpc_port, print_communication = False)
        self.handler = handler.BlockHandler(self.rpc_cli, logger, self.db_proxy)
        
    
    def test_fork(self):
        # old
        self.handler.blk_number = 2074903
        old_block = self.rpc_cli.call("eth_getBlockByNumber", 2074903, True)
        self.handler.process_block(old_block)

        # new
        new_block = self.rpc_cli.call("eth_getBlockByNumber", 2074903, True)
        new_block['uncles'] = []
        tx = self.rpc_cli.call("eth_getTransactionByHash", "0xa2d7bdf90e507979d7005399f2af77918a538d5288076b0e2a1308e7a419f1bc")
        tx["blockNumber"] = hex(2074903)
        new_block['hash'] = "0x123"
        new_block['transactions'] = [tx]

        self.handler.process_fork(old_block, new_block)
        
        # tx
        origin_tx_hash = "0xec50f325f70e08de1750c2655d867217a49dbba75ef09c66e1661be75e5fcafe"
        acc = self.db_proxy.get(FLAGS.accounts, {"address":"0x03da00938219676af361cfc22a49ab1e4a64fd6f"}, block_height = self.handler.blk_number)
        self.assertNotIn(origin_tx_hash, acc['tx_out'])
        txs = self.db_proxy.get(FLAGS.txs, {"blockNumber": hex(2074903)}, multi = True, block_height = 2074903)
        tx_hashes = [tx['hash'] for tx in txs]
        self.assertNotIn(origin_tx_hash, tx_hashes)
        self.assertIn(tx['hash'], tx_hashes)

        # block info
        block = self.db_proxy.get(FLAGS.blocks, {"number":2074903}, block_height = self.handler.blk_number)
        self.assertEqual(block['hash'], "0x123")

        # uncle
        buncle1 = self.db_proxy.get(FLAGS.uncles, {"mainNumber":2074903, "hash":"0xe83ede60f9ee37d506101d542578d7a26236829364a36652c0bd0d9e6652a0db"}, 
            block_height = self.handler.blk_number)
        self.assertEqual(buncle1, None)


    def test_process_block(self):
        self.handler.blk_number = 2074903
        block = self.rpc_cli.call("eth_getBlockByNumber", 2074903, True)
        self.handler.process_block(block)
        # block
        blk = self.db_proxy.get(FLAGS.blocks, {"number":self.handler.blk_number}, block_height = self.handler.blk_number)
        self.assertEqual(blk['hash'], '0xcb3d7de2ed7817fb5c5763c7cf8429ad0efb12ad4f14420c9ab56b71664f77d4')
        # account
        res = self.db_proxy.get(FLAGS.accounts, {"address":"0x2a65aca4d5fc5b5c859090a6c34d164135398226"}, block_height = self.handler.blk_number)
       
        # 1 mine
        mine = res['mine']
        self.assertIn(blk['hash'], mine)
        # 2 uncle
        buncle = self.db_proxy.get(FLAGS.uncles, {"mainNumber":2074903}, block_height = self.handler.blk_number)
        self.assertEqual(buncle['mainNumber'], 2074903)
        # 3 tx
        txs = self.db_proxy.search(FLAGS.txs, {"blockNumber": hex(2074903)}, multi = True)
        self.assertEqual(len(txs), 2)

    def test_process_by_hash(self):
        self.handler.blk_number = 2074903
        block = "0xcb3d7de2ed7817fb5c5763c7cf8429ad0efb12ad4f14420c9ab56b71664f77d4"
        self.handler.execute(block, True)
        # block
        blk = self.db_proxy.get(FLAGS.blocks, {"number":self.handler.blk_number}, block_height = self.handler.blk_number)
        self.assertEqual(blk['hash'], '0xcb3d7de2ed7817fb5c5763c7cf8429ad0efb12ad4f14420c9ab56b71664f77d4')
        # account
        res = self.db_proxy.get(FLAGS.accounts, {"address":"0x2a65aca4d5fc5b5c859090a6c34d164135398226"}, block_height = self.handler.blk_number)
        # 1 mine
        mine = res['mine']
        self.assertIn(blk['hash'], mine)
        # 2 uncle
        buncle = self.db_proxy.get(FLAGS.uncles, {"mainNumber":2074903}, block_height = self.handler.blk_number)
        self.assertEqual(buncle['mainNumber'], 2074903)
        # 3 tx
        txs = self.db_proxy.search(FLAGS.txs, {"blockNumber": hex(2074903)}, multi = True)
        self.assertEqual(len(txs), 2)

    def test_revert(self):
        self.handler.blk_number = 2074903
        block = self.rpc_cli.call("eth_getBlockByNumber", 2074903, True)
        txs = block['transactions']
        self.handler.process_block(block)

        blk = self.db_proxy.get(FLAGS.blocks, {"number":self.handler.blk_number}, block_height = self.handler.blk_number)
        self.handler.revert(blk,txs)

        # block
        blk = self.db_proxy.get(FLAGS.blocks, {"number":self.handler.blk_number}, block_height = self.handler.blk_number)
        self.assertEqual(blk, None)
        # account
        res = self.db_proxy.get(FLAGS.accounts, {"address":"0x2a65aca4d5fc5b5c859090a6c34d164135398226"}, block_height = self.handler.blk_number)
        
        # 1 mine
        mine = res['mine']
        self.assertEqual(mine, [])
        # 2 tx
        txs = self.db_proxy.search(FLAGS.txs, {"blockNumber": hex(2074903)}, multi = True)
        self.assertEqual(len(txs), 0)

    
    def test_add_genesis(self):
        FLAGS.genesis_data = "../genesisdata/genesis_frontier.json"
        driver = builtin.BuiltinDriver()
        driver.add_genesis_data()
        
        # block
        blk = self.db_proxy.get(FLAGS.blocks, {"number":0}, block_height = 0)
        self.assertEqual(blk['hash'], '0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3')
        # account
        acct = self.db_proxy.get(FLAGS.accounts, {"address":"0x3282791d6fd713f1e94f4bfd565eaa78b3a0599d"}, block_height = 0)
        # 1 tx
        res = self.db_proxy.get(FLAGS.txs, None, block_height=0, multi = True)
        self.assertEqual(len(res), 8893)
    
    def test_process_tx(self):
        # block 1700002
        tx_hash = "0xa2d7bdf90e507979d7005399f2af77918a538d5288076b0e2a1308e7a419f1bc"
        tx_obj = self.rpc_cli.call("eth_getTransactionByHash", tx_hash)
        self.handler.blk_number = 1700002
        self.handler.process_tx(tx_obj, None)
        # tx
        tx = self.db_proxy.get(FLAGS.txs, {"hash":tx_hash}, block_height = 1700002, multi = False)
        self.assertEqual(tx["hash"], tx_hash)
        self.assertEqual(utils.convert_to_int(tx['gasUsed']), 21000)
        # 1 account
        acc1 = self.db_proxy.get(FLAGS.accounts, {"address":"0x4bb96091ee9d802ed039c4d1a5f6216f90f81b01"}, block_height = 1700002, multi = False)
        acc2 = self.db_proxy.get(FLAGS.accounts, {"address":"0xae8f3c8d1134e50a7c63c39d78406ab7334149ac"}, block_height = 1700002, multi = False)
        self.assertIn(tx['hash'], acc1['tx_out'])
        self.assertIn(tx['hash'], acc2['tx_in'])
        
        # create contract tx
        # insert
        tx_hash = "0xfeae1ff3cf9b6927d607744e3883ea105fb16042d4639857d9cfce3eba644286"
        tx_obj = self.rpc_cli.call("eth_getTransactionByHash", tx_hash)
        self.handler.blk_number = 1883496
        self.handler.process_tx(tx_obj, None)

        # tx
        tx = self.db_proxy.get(FLAGS.txs, {"hash":tx_hash}, block_height = 1883496, multi = False)
        self.assertEqual(tx["hash"], tx_hash)
        self.assertEqual(utils.convert_to_int(tx['gasUsed']), 368040)
        # account
        # 1 account
        acc1 = self.db_proxy.get(FLAGS.accounts, {"address":"0x2ef1f605af5d03874ee88773f41c1382ac71c239"}, block_height = 1883496, multi = False)
        acc2 = self.db_proxy.get(FLAGS.accounts, {"address":"0xbf4ed7b27f1d666546e30d74d50d173d20bca754"}, block_height = 1883496, multi = False)
        
        self.assertIn(tx['hash'], acc1['tx_out'])
        self.assertIn(tx['hash'], acc2['tx_in'])
    
        self.assertEqual(acc2['is_contract'], 1)
        
    def test_revert_tx(self):
        # insert
        tx_hash = "0xa2d7bdf90e507979d7005399f2af77918a538d5288076b0e2a1308e7a419f1bc"
        tx_obj = self.rpc_cli.call("eth_getTransactionByHash", tx_hash)
        self.handler.blk_number = 1700002
        self.handler.process_tx(tx_obj, None)

        # revert
        tx = self.db_proxy.get(FLAGS.txs, {"hash":tx_hash}, block_height = 1700002, multi = False)
        self.handler.revert_tx(tx, 1700002)
        # tx
        res = self.db_proxy.get(FLAGS.txs, {"hash":tx_hash}, block_height = 1700002, multi = False)
        self.assertEqual(res, None)
        # account
        # 1 account
        acc1 = self.db_proxy.get(FLAGS.accounts, {"address":"0x4bb96091ee9d802ed039c4d1a5f6216f90f81b01"}, block_height = 1700002, multi = False)
        acc2 = self.db_proxy.get(FLAGS.accounts, {"address":"0xae8f3c8d1134e50a7c63c39d78406ab7334149ac"}, block_height = 1700002, multi = False)
        self.assertNotIn(tx['hash'], acc1['tx_out'])
        self.assertNotIn(tx['hash'], acc2['tx_in'])
    
        # create contract tx
        # insert
        tx_hash = "0xfeae1ff3cf9b6927d607744e3883ea105fb16042d4639857d9cfce3eba644286"
        tx_obj = self.rpc_cli.call("eth_getTransactionByHash", tx_hash)
        self.handler.blk_number = 1883496
        self.handler.process_tx(tx_obj, None)

        # revert
        tx = self.db_proxy.get(FLAGS.txs, {"hash":tx_hash}, block_height = 1883496, multi = False)
        self.handler.revert_tx(tx, 1883496)
        # tx
        res = self.db_proxy.get(FLAGS.txs, {"hash":tx_hash}, block_height = 1883496, multi = False)
        self.assertEqual(res, None)
        # account
        # 1 account
        acc1 = self.db_proxy.get(FLAGS.accounts, {"address":"0x2ef1f605af5d03874ee88773f41c1382ac71c239"}, block_height = 1883496, multi = False)
        acc2 = self.db_proxy.get(FLAGS.accounts, {"address":"0xbf4ed7b27f1d666546e30d74d50d173d20bca754"}, block_height = 1883496, multi = False)
        self.assertEqual(acc2, None)
        self.assertNotIn(tx['hash'], acc1['tx_out'])
        

    def test_process_uncle(self):
        # block 2122962
        blk_hash = "0xa9389966cec0062be52f16440e9ee9447e849698934b62aac93138fdfdb751b1"
        blk_obj = self.rpc_cli.call("eth_getBlockByHash", blk_hash, False)
        self.handler.blk_number = 2122962
        self.handler.process_uncle(blk_obj)
        # uncle
        uncle = self.db_proxy.get(FLAGS.uncles, {"hash":"0xa05ba9c6f686d92ef62a1adf18c3c97ed9041b3341de74c20d3cb421216a7f48"}, block_height = 2122962, multi = False)
        self.assertEqual(uncle['mainNumber'], 2122962)
        self.assertEqual(uncle['hash'], "0xa05ba9c6f686d92ef62a1adf18c3c97ed9041b3341de74c20d3cb421216a7f48")
        self.assertEqual(uncle['reward'], utils.unit_convert_from_ether(4.375))
        # account
        acc = self.db_proxy.get(FLAGS.accounts, {"address":"0x2a65aca4d5fc5b5c859090a6c34d164135398226"}, block_height = 2122962, multi = False)
        # 1 mine
        mine_uncles = acc['uncles']
        self.assertIn(uncle['hash'], mine_uncles)

    def test_revert_uncle(self):
        # block 2122962
        blk_hash = "0xa9389966cec0062be52f16440e9ee9447e849698934b62aac93138fdfdb751b1"
        blk_obj = self.rpc_cli.call("eth_getBlockByHash", blk_hash, False)
        self.handler.blk_number = 2122962
        self.handler.process_uncle(blk_obj)
        self.handler.revert_uncle(blk_obj)
        # uncle
        uncle = self.db_proxy.get(FLAGS.uncles, {"hash":"0xa05ba9c6f686d92ef62a1adf18c3c97ed9041b3341de74c20d3cb421216a7f48"}, block_height = 2122962, multi = False)
        self.assertEqual(uncle, None)
        # account
        acc = self.db_proxy.get(FLAGS.accounts, {"address":"0x2a65aca4d5fc5b5c859090a6c34d164135398226"}, block_height = 2122962, multi = False)
       
        # 1 mine
        mine_uncles = acc['uncles']
        self.assertEqual(mine_uncles, [])

    def test_set_balance(self):
        block_num = 2142168
        block = self.rpc_cli.call("eth_getBlockByNumber", block_num, True)
        shandler = handler.BlockHandler(self.rpc_cli, logger, self.db_proxy, True)
        shandler.blk_number = block_num
        shandler.process_block(block)
        # miner, tx-out acct tx-in acct uncle-miner
        accts = ["0xbcdfc35b86bedf72f0cda046a3c16829a2ef41d1","0x362db1e4830bf2c401d7f9f45034f5e6e1c46a0b", "0xbfc39b6f805a9e40e77291aff27aee3c96915bdd", "0x6cafe7473925998db07a497ac3fd10405637a46d"]
        balances = [self.rpc_cli.call("eth_getBalance", acct, block_num) for acct in accts]

        for _,acct in enumerate(accts):
            res = self.db_proxy.get(FLAGS.accounts, {"address":acct}, multi = False, block_height = block_num)
            self.assertEqual(res['balance'], balances[_])

    def test_sync_balance(self):
        # insert account data
        accounts = ["0x3282791d6fd713f1e94f4bfd565eaa78b3a0599d", "0x17961d633bcf20a7b029a7d94b7df4da2ec5427f", "0x493a67fe23decc63b10dda75f3287695a81bd5ab"]

        for _, acct in enumerate(accounts):
            self.db_proxy.insert(FLAGS.accounts, {"address":acct}, block_height = _ * FLAGS.table_capacity)

        self.handler._sync_balance(100)
        res = self.db_proxy.get(FLAGS.meta, {"sync_record":"ethereum"}, multi = False)
        self.assertEqual(res["last_sync_block"], 100)

    def tearDown(self):
        self.db_proxy.drop_db(FLAGS.mongodb_default_db)
     
        
        
示例#12
0
class TestDriverThread(Thread):
    def __init__(self,
                 group=None,
                 target=None,
                 name=None,
                 args=(),
                 kwargs=None,
                 verbose=None,
                 gasprice=None,
                 evt=None,
                 port=4000):
        super(TestDriverThread, self).__init__(group, target, name, args,
                                               kwargs, verbose)
        self.gasprice = gasprice
        self.log = slogging.getLogger('test_working_app')
        self.test_successful = False
        self.finished = False
        self.evt = evt
        self.port = port

    def wait_for_blocknumber(self, number, retry=20):
        block = self.client.call('eth_getBlockByNumber', hex(number), False)
        while block is None and retry > 0:
            block = self.client.call('eth_getBlockByNumber', hex(number),
                                     False)
            time.sleep(.5)
            retry -= 1
        assert retry > 0, "could not find block {}".format(number)
        return block

    def connect_client(self):
        while True:
            try:
                self.client = JSONRPCClient(port=self.port,
                                            print_communication=False)
                self.client.call('web3_clientVersion')
                break
            except ConnectionError:
                time.sleep(0.5)

    def run(self):
        self.log.debug('test started')

        try:
            self.connect_client()
            self.log.debug('client connected')

            # Read initial blocks created by HydraChain on startup
            self.wait_for_blocknumber(10)
            self.log.debug("found block number 10")

            # Create a contract
            params = {
                'from': self.client.coinbase.encode('hex'),
                'to': '',
                'data': contract_code,
                'gasPrice': '0x{}'.format(self.gasprice)
            }
            self.client.call('eth_sendTransaction', params)
            self.log.debug('eth_sendTransaction OK')

            # Wait for new block
            recent_block = self.wait_for_blocknumber(11)

            self.log.debug('recent_block_hash {}'.format(recent_block))

            block = self.client.call('eth_getBlockByHash',
                                     recent_block['hash'], True)
            self.log.debug('eth_getBlockByHash OK {}'.format(block))

            assert block['transactions'], 'no transactions in block'
            tx = block['transactions'][0]
            assert tx['to'] == '0x'
            assert tx['gasPrice'] == params['gasPrice']
            assert len(tx['input']) > len('0x')
            assert tx['input'].startswith('0x')

            # Get transaction receipt to have the address of contract
            receipt = self.client.call('eth_getTransactionReceipt', tx['hash'])
            self.log.debug('eth_getTransactionReceipt OK {}'.format(receipt))

            assert receipt['transactionHash'] == tx['hash']
            assert receipt['blockHash'] == tx['blockHash']
            assert receipt['blockHash'] == block['hash']

            # Get contract address from receipt
            contract_address = receipt['contractAddress']
            code = self.client.call('eth_getCode', contract_address)
            self.log.debug('eth_getCode OK {}'.format(code))

            assert code.startswith('0x')
            assert len(code) > len('0x')

            # Perform some action on contract (set value to random number)
            rand_value = random.randint(64, 1024)
            contract = self.client.new_abi_contract(contract_interface,
                                                    contract_address)
            contract.set(rand_value, gasprice=self.gasprice)
            self.log.debug('contract.set({}) OK'.format(rand_value))

            # Wait for new block
            recent_block = self.wait_for_blocknumber(12)
            # recent_block_hash = self.wait_for_new_block()

            block = self.client.call('eth_getBlockByHash',
                                     recent_block['hash'], True)

            # Check that value was correctly set on contract
            res = contract.get()
            self.log.debug('contract.get() OK {}'.format(res))
            assert res == rand_value

            self.test_successful = True
        except Exception as ex:
            print("Exception", ex)
            import traceback
            traceback.print_exc()
            self.log.exception("Exception in test thread")
        finally:
            self.evt.set()
            self.finished = True
示例#13
0
def test_blockchain(request):
    # pylint: disable=too-many-locals
    from hydrachain import app
    app.slogging.configure(':ERROR')

    quantity = 3
    base_port = 29870
    timeout = 3  # seconds
    tmp_datadir = tempfile.mktemp()

    private_keys = [
        mk_privkey('raidentest:{}'.format(position))
        for position in range(quantity)
    ]

    addresses = [
        privtoaddr(priv)
        for priv in private_keys
    ]

    hydrachain_apps = hydrachain_network(private_keys, base_port, tmp_datadir)

    privatekey = private_keys[0]
    address = privtoaddr(privatekey)

    jsonrpc_client = JSONRPCClient(privkey=private_keys[0], print_communication=False)

    humantoken_path = get_contract_path('HumanStandardToken.sol')
    humantoken_contracts = compile_file(humantoken_path, libraries=dict())
    token_abi = jsonrpc_client.deploy_solidity_contract(
        address,
        'HumanStandardToken',
        humantoken_contracts,
        dict(),
        (9999, 'raiden', 2, 'Rd'),
        timeout=timeout,
    )

    registry_path = get_contract_path('Registry.sol')
    registry_contracts = compile_file(registry_path, libraries=dict())
    registry_abi = jsonrpc_client.deploy_solidity_contract(
        address,
        'Registry',
        registry_contracts,
        dict(),
        tuple(),
        timeout=timeout,
    )

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 0

    # pylint: disable=no-member

    assert token_abi.balanceOf(address) == 9999
    transaction_hash = registry_abi.addAsset(token_abi.address)
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 1

    channel_manager_address_encoded = registry_abi.channelManagerByAsset.call(token_abi.address)
    channel_manager_address = channel_manager_address_encoded.decode('hex')

    log_channel_manager_address_encoded = log_list[0]['data']
    log_channel_manager_address = log_channel_manager_address_encoded[2:].lstrip('0').rjust(40, '0').decode('hex')

    assert channel_manager_address == log_channel_manager_address

    channel_manager_abi = jsonrpc_client.new_contract_proxy(
        registry_contracts['ChannelManagerContract']['abi'],
        channel_manager_address,
    )

    transaction_hash = channel_manager_abi.newChannel(addresses[1], 10)
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 2

    channel_manager_abi.get.call(
        address.encode('hex'),
        addresses[1].encode('hex'),
    )
示例#14
0
class BuiltinDriver(base.TokenBuiltinBase):

    @property
    def type(self):
        return 'DGD'

    @property
    def event(self):
        return constant.DGD_EVENT

    @property
    def last_block(self):
        return self._last_block

    def __init__(self):
        self.logger = logger
        self.rpc_cli = JSONRPCClient(host = FLAGS.rpc_host, port = FLAGS.rpc_port, print_communication = False)
        super(BuiltinDriver, self).__init__()


    def initialize(self):
        self.add_indexes_for_token(self.type)

        res = self.db_proxy.get(FLAGS.token_prefix + self.type, {"type": self.event}, multi = True, sort_key = "block", ascend = False, limit = 1)
        if res:
            self._last_block = res[0]['block']
        else:
            self._last_block = None

        # add token basic info
        source_code = self.get_source_code(self.type)
        abi = self.get_abi(self.type, constant.DGD_CONTRACT_NAME)
        basic_info = dict(
            token=self.type,
            source_code=source_code,
            abi=abi,
            address=constant.DGD_ADDR
        )
        self.db_proxy.update(FLAGS.token_basic, {"token":self.type}, {"$set":basic_info}, upsert = True)

    def get_past_logs(self):
        self.abi = self.get_abi(self.type, constant.DGD_CONTRACT_NAME)
        
        event_id = self.get_event_id(self.abi, self.event)
        from_block = hex(self.last_block) if self.last_block else "0x0" 

        params =  {
          "fromBlock": from_block,
          "toBlock": "latest",
          "address": constant.DGD_ADDR,
          "topics": [event_id, None, None]
        }
        self.filter_id = self.rpc_cli.call(constant.METHOD_NEW_FILTER, params)
        
        res = self.rpc_cli.call(constant.METHOD_GET_FILTER_LOGS, self.filter_id)

    
        for log in res:
            self.handle_log(log)
        

    def handle_log(self, log):
        if log.has_key("removed"): del log['removed']
        hash = _utils.hash_log(log)
        transfer_table = FLAGS.token_prefix + self.type
        balance_table = FLAGS.balance_prefix + self.type

        data = log['data'][2:].decode('hex')
        data_params = self.data_params(self.abi, self.event)

        value = _abi.decode_abi(data_params, data)[0]

        f = '0x' + log['topics'][1].lower()[26:]
        to = '0x' + log['topics'][2].lower()[26:]

        operation = {
            "$set": {
                "hash" : hash,
                "from" : f,
                "to" : to,
                "value" : value,
                "transactionHash" : log["transactionHash"],
                "logIndex" : log["logIndex"],
                "block" : int(log["blockNumber"], 16),
                "blockHash" : log["blockHash"],
                "type" : self.event
            }
        }

        objectId = self.db_proxy.update(transfer_table, {"hash":hash}, operation, multi = False, upsert = True).upserted_id

        if objectId is  None:
            self.logger.info("event log %s has been add, ignore it", hash)
            return

        # update balance
        # TODO parse demical of token
        operation = {
            "$inc" : {"balance" : value  * -1}
        }
        self.db_proxy.update(balance_table, {"account" : f}, operation, upsert = True)

        operation2 = {
            "$inc" : {"balance" : value }
        }
        self.db_proxy.update(balance_table, {"account" : to}, operation2, upsert = True)

    def revert_log(self, log):
        if log.has_key("removed"): del log['removed']
        hash = _utils.hash_log(log)
        transfer_table = FLAGS.token_prefix + self.type
        balance_table = FLAGS.balance_prefix + self.type

        data = log['data'][2:].decode('hex')
        data_params = self.data_params(self.abi, self.event)

        value = _abi.decode_abi(data_params, data)[0]

        f = '0x' + log['topics'][1].lower()[26:]
        to = '0x' + log['topics'][2].lower()[26:]

        deleted_count = self.db_proxy.delete(transfer_table, {
            "hash" : hash,
        }, multi = False).deleted_count

        if deleted_count == 0:
            self.logger.info("event log %s has been deleted, ignore it", hash)
            return

        # update balance
        # TODO parse demical of token
        operation = {
            "$inc" : {"balance" : value  * 1}
        }
        self.db_proxy.update(balance_table, {"account" : f}, operation, upsert = True)

        operation2 = {
            "$inc" : {"balance" : value * -1}
        }
        self.db_proxy.update(balance_table, {"account" : to}, operation2, upsert = True)

    def wait(self):
        self.listener.join()
示例#15
0
def test_blockchain(request):
    # pylint: disable=too-many-locals
    from hydrachain import app
    app.slogging.configure(':ERROR')

    quantity = 3
    base_port = 29870
    timeout = 3  # seconds
    tmp_datadir = tempfile.mktemp()

    private_keys = [
        mk_privkey('raidentest:{}'.format(position))
        for position in range(quantity)
    ]

    addresses = [privtoaddr(priv) for priv in private_keys]

    hydrachain_apps = hydrachain_network(private_keys, base_port, tmp_datadir)

    privatekey = private_keys[0]
    address = privtoaddr(privatekey)

    jsonrpc_client = JSONRPCClient(privkey=private_keys[0],
                                   print_communication=False)

    humantoken_path = get_contract_path('HumanStandardToken.sol')
    humantoken_contracts = compile_file(humantoken_path, libraries=dict())
    token_abi = jsonrpc_client.deploy_solidity_contract(
        address,
        'HumanStandardToken',
        humantoken_contracts,
        dict(),
        (9999, 'raiden', 2, 'Rd'),
        timeout=timeout,
    )

    registry_path = get_contract_path('Registry.sol')
    registry_contracts = compile_file(registry_path, libraries=dict())
    registry_abi = jsonrpc_client.deploy_solidity_contract(
        address,
        'Registry',
        registry_contracts,
        dict(),
        tuple(),
        timeout=timeout,
    )

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 0

    # pylint: disable=no-member

    assert token_abi.balanceOf(address) == 9999
    transaction_hash = registry_abi.addAsset(token_abi.address)
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 1

    channel_manager_address_encoded = registry_abi.channelManagerByAsset.call(
        token_abi.address)
    channel_manager_address = channel_manager_address_encoded.decode('hex')

    log_channel_manager_address_encoded = log_list[0]['data']
    log_channel_manager_address = log_channel_manager_address_encoded[
        2:].lstrip('0').rjust(40, '0').decode('hex')

    assert channel_manager_address == log_channel_manager_address

    channel_manager_abi = jsonrpc_client.new_contract_proxy(
        registry_contracts['ChannelManagerContract']['abi'],
        channel_manager_address,
    )

    transaction_hash = channel_manager_abi.newChannel(addresses[1], 10)
    jsonrpc_client.poll(transaction_hash.decode('hex'), timeout=timeout)

    log_list = jsonrpc_client.call(
        'eth_getLogs',
        {
            'fromBlock': '0x0',
            'toBlock': 'latest',
            'topics': [],
        },
    )
    assert len(log_list) == 2

    channel_manager_abi.get.call(
        address.encode('hex'),
        addresses[1].encode('hex'),
    )