Esempio n. 1
0
def test_genesis_hash(genesis_fixture):
    set_db()
    genesis = blocks.genesis()
    """
    YP: https://raw.githubusercontent.com/ethereum/latexpaper/master/Paper.tex
    0256 , SHA3RLP(), 0160 , stateRoot, 0256 , 2**22 , 0, 0, 1000000, 0, 0, (),
    SHA3(42), (), ()

    Where 0256 refers to the parent and state and transaction root hashes,
    a 256-bit hash which is all zeroes;
    0160 refers to the coinbase address,
    a 160-bit hash which is all zeroes;
    2**22 refers to the difficulty;
    0 refers to the timestamp (the Unix epoch);
    () refers to the extradata and the sequences of both uncles and
    transactions, all empty.
    SHA3(42) refers to the SHA3 hash of a byte array of length one whose first
    and only byte is of value 42.
    SHA3RLP() values refer to the hashes of the transaction and uncle lists
    in RLP
    both empty.
    The proof-of-concept series include a development premine, making the state
    root hash some value stateRoot. The latest documentation should be
    consulted for the value of the state root.
    """

    h256 = '\00' * 32
    sr = genesis_fixture['genesis_state_root'].decode('hex')
    genesis_block_defaults = [
        ["prevhash", "bin", h256],  # h256()
        ["uncles_hash", "bin", utils.sha3(rlp.encode([]))],  # sha3EmptyList
        ["coinbase", "addr", "0" * 40],  # h160()
        ["state_root", "trie_root", sr],  # stateRoot
        ["tx_list_root", "trie_root", trie.BLANK_ROOT],  # h256()
        ["difficulty", "int", 2 ** 22],  # c_genesisDifficulty
        ["number", "int", 0],  # 0
        ["min_gas_price", "int", 0],  # 0
        ["gas_limit", "int", 10 ** 6],  # 10**6 for genesis
        ["gas_used", "int", 0],  # 0
        ["timestamp", "int", 0],  # 0
        ["extra_data", "bin", ""],  # ""
        ["nonce", "bin", utils.sha3(chr(42))],  # sha3(bytes(1, 42));
    ]

    cpp_genesis_block = rlp.decode(
        genesis_fixture['genesis_rlp_hex'].decode('hex'))
    cpp_genesis_header = cpp_genesis_block[0]

    for i, (name, typ, genesis_default) in enumerate(genesis_block_defaults):
        assert utils.decoders[typ](cpp_genesis_header[i]) == genesis_default
        assert getattr(genesis, name) == genesis_default

    assert genesis.hex_hash() == genesis_fixture['genesis_hash']

    assert genesis.hex_hash() == utils.sha3(
        genesis_fixture['genesis_rlp_hex'].decode('hex')
    ).encode('hex')
Esempio n. 2
0
def test_genesis_hash(genesis_fixture):
    set_db()
    genesis = blocks.genesis()
    """
    YP: https://raw.githubusercontent.com/ethereum/latexpaper/master/Paper.tex
    0256 , SHA3RLP(), 0160 , stateRoot, 0256 , 2**22 , 0, 0, 1000000, 0, 0, (),
    SHA3(42), (), ()

    Where 0256 refers to the parent and state and transaction root hashes,
    a 256-bit hash which is all zeroes;
    0160 refers to the coinbase address,
    a 160-bit hash which is all zeroes;
    2**22 refers to the difficulty;
    0 refers to the timestamp (the Unix epoch);
    () refers to the extradata and the sequences of both uncles and
    transactions, all empty.
    SHA3(42) refers to the SHA3 hash of a byte array of length one whose first
    and only byte is of value 42.
    SHA3RLP() values refer to the hashes of the transaction and uncle lists
    in RLP
    both empty.
    The proof-of-concept series include a development premine, making the state
    root hash some value stateRoot. The latest documentation should be
    consulted for the value of the state root.
    """

    h256 = '\00' * 32
    sr = genesis_fixture['genesis_state_root'].decode('hex')
    genesis_block_defaults = [
        ["prevhash", "bin", h256],  # h256()
        ["uncles_hash", "bin",
         utils.sha3(rlp.encode([]))],  # sha3EmptyList
        ["coinbase", "addr", "0" * 40],  # h160()
        ["state_root", "trie_root", sr],  # stateRoot
        ["tx_list_root", "trie_root", trie.BLANK_ROOT],  # h256()
        ["difficulty", "int", 2**22],  # c_genesisDifficulty
        ["number", "int", 0],  # 0
        ["min_gas_price", "int", 0],  # 0
        ["gas_limit", "int", 10**6],  # 10**6 for genesis
        ["gas_used", "int", 0],  # 0
        ["timestamp", "int", 0],  # 0
        ["extra_data", "bin", ""],  # ""
        ["nonce", "bin", utils.sha3(chr(42))],  # sha3(bytes(1, 42));
    ]

    cpp_genesis_block = rlp.decode(
        genesis_fixture['genesis_rlp_hex'].decode('hex'))
    cpp_genesis_header = cpp_genesis_block[0]

    for i, (name, typ, genesis_default) in enumerate(genesis_block_defaults):
        assert utils.decoders[typ](cpp_genesis_header[i]) == genesis_default
        assert getattr(genesis, name) == genesis_default

    assert genesis.hex_hash() == genesis_fixture['genesis_hash']

    assert genesis.hex_hash() == utils.sha3(
        genesis_fixture['genesis_rlp_hex'].decode('hex')).encode('hex')
Esempio n. 3
0
def deserialize_child(parent, rlpdata):
    """
    deserialization w/ replaying transactions
    """
    header_args, transaction_list, uncles = rlp.decode(rlpdata)
    assert len(header_args) == len(blocks.block_structure)
    kargs = dict(transaction_list=transaction_list, uncles=uncles)
    # Deserialize all properties
    for i, (name, typ, default) in enumerate(blocks.block_structure):
        kargs[name] = utils.decoders[typ](header_args[i])

    block = blocks.Block.init_from_parent(parent,
                                          kargs['coinbase'],
                                          extra_data=kargs['extra_data'],
                                          timestamp=kargs['timestamp'])
    block.finalize()  # this is the first potential state change
    # replay transactions
    for tx_lst_serialized, _state_root, _gas_used_encoded in transaction_list:

        tx = transactions.Transaction.create(tx_lst_serialized)
        logger.debug("data %r", tx.data)
        logger.debug('applying %r', tx)
        logger.debug('applying %r', tx.to_dict())
        logger.debug('block.gas_used before: %r', block.gas_used)
        success, output = processblock.apply_transaction(block, tx)
        logger.debug('block.gas_used after: %r', block.gas_used)
        logger.debug('success: %r', success)
        diff = utils.decode_int(_gas_used_encoded) - block.gas_used
        logger.debug("GAS_USED DIFF %r", diff)
        assert utils.decode_int(_gas_used_encoded) == block.gas_used
        assert _state_root.encode('hex') == block.state.root_hash.encode('hex')

    # checks
    assert block.prevhash == parent.hash
    assert block.tx_list_root == kargs['tx_list_root']
    assert block.gas_used == kargs['gas_used']
    assert block.gas_limit == kargs['gas_limit']
    assert block.timestamp == kargs['timestamp']
    assert block.difficulty == kargs['difficulty']
    assert block.number == kargs['number']
    assert block.extra_data == kargs['extra_data']
    assert utils.sha3(rlp.encode(block.uncles)) == kargs['uncles_hash']
    assert block.state.root_hash.encode('hex') == kargs['state_root'].encode(
        'hex')

    block.uncles_hash = kargs['uncles_hash']
    block.nonce = kargs['nonce']
    block.min_gas_price = kargs['min_gas_price']

    return block
Esempio n. 4
0
def deserialize_child(parent, rlpdata):
    """
    deserialization w/ replaying transactions
    """
    header_args, transaction_list, uncles = rlp.decode(rlpdata)
    assert len(header_args) == len(blocks.block_structure)
    kargs = dict(transaction_list=transaction_list, uncles=uncles)
    # Deserialize all properties
    for i, (name, typ, default) in enumerate(blocks.block_structure):
        kargs[name] = utils.decoders[typ](header_args[i])

    block = blocks.Block.init_from_parent(parent, kargs['coinbase'],
                                          extra_data=kargs['extra_data'],
                                          timestamp=kargs['timestamp'])
    block.finalize()  # this is the first potential state change
    # replay transactions
    for tx_lst_serialized, _state_root, _gas_used_encoded in transaction_list:

        tx = transactions.Transaction.create(tx_lst_serialized)
        logger.debug("data %r", tx.data)
        logger.debug('applying %r', tx)
        logger.debug('applying %r', tx.to_dict())
        logger.debug('block.gas_used before: %r', block.gas_used)
        success, output = processblock.apply_transaction(block, tx)
        logger.debug('block.gas_used after: %r', block.gas_used)
        logger.debug('success: %r', success)
        diff = utils.decode_int(_gas_used_encoded) - block.gas_used
        logger.debug("GAS_USED DIFF %r", diff)
        assert utils.decode_int(_gas_used_encoded) == block.gas_used
        assert _state_root.encode('hex') == block.state.root_hash.encode('hex')

    # checks
    assert block.prevhash == parent.hash
    assert block.tx_list_root == kargs['tx_list_root']
    assert block.gas_used == kargs['gas_used']
    assert block.gas_limit == kargs['gas_limit']
    assert block.timestamp == kargs['timestamp']
    assert block.difficulty == kargs['difficulty']
    assert block.number == kargs['number']
    assert block.extra_data == kargs['extra_data']
    assert utils.sha3(rlp.encode(block.uncles)) == kargs['uncles_hash']
    assert block.state.root_hash.encode(
        'hex') == kargs['state_root'].encode('hex')

    block.uncles_hash = kargs['uncles_hash']
    block.nonce = kargs['nonce']
    block.min_gas_price = kargs['min_gas_price']

    return block
Esempio n. 5
0
def step_impl(context, data):
    context.data = data
    context.encoded_data = rlp.encode(
        recursive_int_to_big_endian(context.data))
Esempio n. 6
0
def step_impl(context, data):
    context.data = data
    context.encoded_data = rlp.encode(recursive_int_to_big_endian(
        context.data))
Esempio n. 7
0
def step_impl(context):
    with AssertException(TypeError):
        rlp.encode(context.src)
Esempio n. 8
0
def step_impl(context):
    encodeds = [rlp.encode(x) for x in context.src]
    assert context.dst[1 + len(context.length_bin):] == ''.join(encodeds)
Esempio n. 9
0
def step_impl(context):
    total_payload_length = sum(len(rlp.encode(x)) for x in context.src)
    context.length_bin = int_to_big_endian(total_payload_length)
    assert context.dst[0] == chr(0xf7 + len(context.length_bin))
Esempio n. 10
0
def step_impl(context):
    assert context.dst[1:] == ''.join(rlp.encode(x) for x in context.src)
Esempio n. 11
0
def step_impl(context):
    total_payload_length = sum(len(rlp.encode(x)) for x in context.src)
    assert context.dst[0] == chr(0xc0 + total_payload_length)
Esempio n. 12
0
def step_impl(context):
    context.dst = rlp.encode(context.src)