Exemple #1
0
def create_block(
        parent_hash=default_config["GENESIS_PREVHASH"],
        height=0,
        timestamp=None,
        difficulty=TEST_DIFFICULTY,
        transactions=[],
        gas_limit=3000000000,
        referee_hashes=[],
        author=default_config["GENESIS_COINBASE"],
        deferred_state_root=default_config["GENESIS_STATE_ROOT"],
        deferred_receipts_root=default_config["GENESIS_RECEIPTS_ROOT"],
        adaptive=0):
    if timestamp is None:
        timestamp = int(time.time())
    tx_root = utils.sha3(rlp.encode(Transactions(transactions)))
    nonce = 0
    while True:
        header = BlockHeader(parent_hash=parent_hash,
                             height=height,
                             difficulty=difficulty,
                             timestamp=timestamp,
                             author=author,
                             transactions_root=tx_root,
                             gas_limit=gas_limit,
                             referee_hashes=referee_hashes,
                             nonce=nonce,
                             deferred_state_root=deferred_state_root,
                             deferred_receipts_root=deferred_receipts_root,
                             adaptive=adaptive)
        if header.pow_decimal() * difficulty < HASH_MAX:
            break
        nonce += 1
    block = Block(block_header=header, transactions=transactions)
    return block
 def wait_for_tx(self, all_txs, check_status):
     for tx in all_txs:
         for i in range(3):
             try:
                 retry = True
                 while retry:
                     try:
                         wait_until(lambda: checktx(self.nodes[0], tx.hash_hex()), timeout=20)
                         retry = False
                     except CannotSendRequest:
                         time.sleep(0.01)
                 break
             except AssertionError as _:
                 self.nodes[0].p2p.send_protocol_msg(Transactions(transactions=[tx]))
             if i == 2:
                 raise AssertionError("Tx {} not confirmed after 30 seconds".format(tx.hash_hex()))
     # After having optimistic execution, get_receipts may get receipts with not deferred block, these extra blocks
     # ensure that later get_balance can get correct executed balance for all transactions
     client = RpcClient(self.nodes[0])
     for _ in range(5):
         client.generate_block()
     receipts = [client.get_transaction_receipt(tx.hash_hex()) for tx in all_txs]
     self.log.debug("Receipts received: {}".format(receipts))
     if check_status:
         map(lambda x: assert_equal(x['outcomeStatus'], 0), receipts)
     return receipts
Exemple #3
0
def create_block_with_nonce(
        parent_hash=default_config["GENESIS_PREVHASH"],
        height=0,
        timestamp=None,
        difficulty=TEST_DIFFICULTY,
        transactions=[],
        gas_limit=3000000000,
        referee_hashes=[],
        author=default_config["GENESIS_COINBASE"],
        deferred_state_root=default_config["GENESIS_STATE_ROOT"],
        deferred_receipts_root=default_config["GENESIS_RECEIPTS_ROOT"],
        deferred_logs_bloom_hash=default_config["GENESIS_LOGS_BLOOM_HASH"],
        adaptive=0,
        nonce=0):
    if timestamp is None:
        timestamp = int(time.time())
    tx_root = utils.sha3(rlp.encode(Transactions(transactions)))
    header = BlockHeader(parent_hash=parent_hash,
                         height=height,
                         difficulty=difficulty,
                         timestamp=timestamp,
                         author=author,
                         transactions_root=tx_root,
                         gas_limit=gas_limit,
                         referee_hashes=referee_hashes,
                         nonce=nonce,
                         deferred_state_root=deferred_state_root,
                         deferred_receipts_root=deferred_receipts_root,
                         deferred_logs_bloom_hash=deferred_logs_bloom_hash,
                         adaptive=adaptive)
    block = Block(block_header=header, transactions=transactions)
    return block
 def _send_transaction(self, transaction, wait, check_status):
     self.nodes[0].p2p.send_protocol_msg(Transactions(transactions=[transaction]))
     if wait:
         self.wait_for_tx([transaction], check_status)