Ejemplo n.º 1
0
def kv_seek_plain_change_sets(kv_view: kv_remote.RemoteView,
                              block_height: int,
                              count: int = 1):
    """ Search for the provided block range in KV 'Account Changes' and 'Storage Changes' tables.
    """
    for index, block_number in enumerate(
            range(block_height, block_height + count)):
        change_set_key = timestamp.encode_timestamp(block_number)

        print('ACCOUNT CHANGES\nREQ1 block_number:', block_number,
              '(key: ' + str(change_set_key.hex()) + ')')
        key, value = kv_view.get(tables.PLAIN_ACCOUNTS_CHANGE_SET_LABEL,
                                 change_set_key)
        account_changeset = changeset.PlainAccountChangeSet(value)
        print('RSP1 key:', key.hex(), account_changeset, '[')
        for i, change in enumerate(account_changeset):
            print('account_change#' + str(i), change)
        print(']')

        print('STORAGE CHANGES\nREQ2 block_number:', block_number,
              '(key: ' + str(change_set_key.hex()) + ')')
        key, value = kv_view.get(tables.PLAIN_STORAGE_CHANGE_SET_LABEL,
                                 change_set_key)
        storage_changeset = changeset.PlainStorageChangeSet(value)
        print('RSP2 key:', key.hex(), storage_changeset, '[')
        for i, (address, incarnation,
                change_set) in enumerate(storage_changeset):
            print('storage_change#' + str(i), 'address:', address,
                  'incarnation:', incarnation, change_set)
        print(']\n')

        if index != count - 1:
            print('\n')
Ejemplo n.º 2
0
def kv_seek_block_receipt(kv_view: kv_remote.RemoteView,
                          block_height: int,
                          count: int = 1):
    """ Search for the provided block range in KV 'Receipts' bucket of turbo-geth/silkworm running at target.
    """
    for index, block_number in enumerate(
            range(block_height, block_height + count)):
        encoded_canonical_block_number = sedes.encode_canonical_block_number(
            block_number)
        print('CANONICAL HEADER\nREQ1 block_number:', block_number,
              '(key: ' + str(encoded_canonical_block_number.hex()) + ')')
        key, block_hash = kv_view.get(tables.BLOCK_HEADERS_LABEL,
                                      encoded_canonical_block_number)
        decoded_block_number = sedes.decode_canonical_block_number(key)
        assert decoded_block_number == block_number, 'ERR block number {} does not match!'.format(
            decoded_block_number)
        print('RSP1 block_hash:', block_hash.hex(), '\n')

        encoded_block_key = sedes.encode_block_key(block_number, block_hash)
        print('RECEIPT\nREQ2 block_number:', block_number,
              '(key: ' + str(encoded_block_key.hex()) + ')')
        key, value = kv_view.get(tables.BLOCK_RECEIPTS_LABEL,
                                 encoded_block_key)
        decoded_block_number, decoded_block_hash = sedes.decode_block_key(key)
        block_receipt_list = receipt.TransactionReceipt.from_bytes(value)
        assert decoded_block_number == block_number, 'ERR block number {} does not match!'.format(
            decoded_block_number)
        assert decoded_block_hash == block_hash, 'ERR block hash {} does not match!'.format(
            decoded_block_hash)
        print('RSP2 block_receipts(' + str(len(block_receipt_list)) + '): [')
        for block_receipt in block_receipt_list:
            print('receipt#' + str(block_receipt_list.index(block_receipt)),
                  block_receipt)
        print(']' if index == count - 1 else ']\n')
Ejemplo n.º 3
0
    def test_init(self):
        """ Unit test for __init__. """
        with pytest.raises(ValueError):
            RemoteView(None)

        mock_kv_stub = pytest_mock.mock.Mock()
        view = RemoteView(mock_kv_stub)
        assert view.kv_stub == mock_kv_stub
Ejemplo n.º 4
0
 def test_cursor(self, bucket_name: str, should_pass: bool):
     """ Unit test for cursor. """
     mock_kv_stub = pytest_mock.mock.Mock()
     view = RemoteView(mock_kv_stub)
     if should_pass:
         cursor = view.cursor(bucket_name)
         assert cursor is not None
         assert cursor.kv_stub == mock_kv_stub
         assert cursor.bucket_name == bucket_name
         assert cursor.prefix == DEFAULT_PREFIX
         assert cursor.streaming is False
     else:
         with pytest.raises(ValueError):
             view.cursor(bucket_name)
Ejemplo n.º 5
0
def kv_seek_block_header(kv_view: kv_remote.RemoteView,
                         block_height: int,
                         count: int = 1):
    """ Search for the provided block range in KV 'Headers' bucket of turbo-geth/silkworm running at target.
    """
    for index, block_number in enumerate(
            range(block_height, block_height + count)):
        encoded_canonical_block_number = sedes.encode_canonical_block_number(
            block_number)
        print('CANONICAL HEADER\nREQ1 block_number:', block_number,
              '(key: ' + str(encoded_canonical_block_number.hex()) + ')')
        key, block_hash = kv_view.get(tables.BLOCK_HEADERS_LABEL,
                                      encoded_canonical_block_number)
        decoded_block_number = sedes.decode_canonical_block_number(key)
        assert decoded_block_number == block_number, 'ERR block number {} does not match!'.format(
            decoded_block_number)
        print('RSP1 block_hash:', block_hash.hex(), '\n')

        encoded_block_key = sedes.encode_block_key(block_number, block_hash)
        print('FULL HEADER\nREQ2 block_number:', block_number,
              '(key: ' + str(encoded_block_key.hex()) + ')')
        key, value = kv_view.get(tables.BLOCK_HEADERS_LABEL, encoded_block_key)
        decoded_block_number, decoded_block_hash = sedes.decode_block_key(key)
        block_header = sedes.decode_block_header(value)
        assert decoded_block_number == block_number, 'ERR block number {} does not match!'.format(
            decoded_block_number)
        assert decoded_block_hash == block_hash, 'ERR2 block hash {} does not match!'.format(
            decoded_block_hash)
        print('RSP2 block_header:', block_header, '\n')

        encoded_difficulty_block_key = sedes.encode_difficulty_block_key(
            encoded_block_key)
        print('TOTAL DIFFICULTY HEADER\nREQ3 block_number:', block_number,
              '(key: ' + str(encoded_difficulty_block_key.hex()) + ')')
        key, value = kv_view.get(tables.BLOCK_HEADERS_LABEL,
                                 encoded_difficulty_block_key)
        decoded_block_number, decoded_block_hash = sedes.decode_difficulty_block_key(
            key)
        block_total_difficulty = sedes.decode_block_total_difficulty(value)
        assert decoded_block_number == block_number, 'ERR block number {} does not match!'.format(
            decoded_block_number)
        assert decoded_block_hash == block_hash, 'ERR block hash {} does not match!'.format(
            decoded_block_hash)
        print('RSP3 block_total_difficulty:', block_total_difficulty)

        if index != count - 1:
            print('\n')
Ejemplo n.º 6
0
def basic_view(mocker: pytest_mock.MockerFixture, key_out: str, value: str):
    """ basic_view """
    key_out_bytes = bytes.fromhex(key_out) if key_out is not None else None
    value_bytes = bytes.fromhex(value) if value is not None else None
    mock_kvstub = mocker.Mock()
    type(mock_kvstub).Seek = lambda k, p: iter(
        [kv_pb2.Pair(key=key_out_bytes, value=value_bytes)])
    return RemoteView(mock_kvstub)
Ejemplo n.º 7
0
def kv_seek_tx_senders(kv_view: kv_remote.RemoteView, block_height: int, count: int = 1):
    """ Search for the provided block range in KV 'Receipts' bucket of turbo-geth/silkworm running at target.
    """
    for index, block_number in enumerate(range(block_height, block_height + count)):
        encoded_canonical_block_number = sedes.encode_canonical_block_number(block_number)
        print('CANONICAL HEADER\nREQ1 block_number:', block_number, '(key: ' + str(encoded_canonical_block_number.hex()) + ')')
        key, block_hash = kv_view.get(tables.BLOCK_HEADERS_LABEL, encoded_canonical_block_number)
        decoded_block_number = sedes.decode_canonical_block_number(key)
        assert decoded_block_number == block_number, 'ERR block number {} does not match!'.format(decoded_block_number)
        print('RSP1 block_hash:', block_hash.hex(), '\n')

        encoded_block_key = sedes.encode_block_key(block_number, block_hash)
        print('TX_SENDERS\nREQ2 block_number:', block_number, '(key: ' + str(encoded_block_key.hex()) + ')')
        key, value = kv_view.get(tables.TRANSACTION_SENDERS_LABEL, encoded_block_key)
        account_address_list = kv_metadata.decode_account_address_list(value) if key == encoded_block_key else []
        print('RSP2 senders(' + str(len(account_address_list)) + '): [')
        for account_address in account_address_list:
            print('address#' + str(account_address_list.index(account_address)), account_address.hex())
        print(']' if index == count - 1 else ']\n')
Ejemplo n.º 8
0
def kv_seek_eth_supply(kv_view: kv_remote.RemoteView,
                       block_height: int,
                       count: int = 1):
    """ Search for the provided block range in 'ETH_SUPPLY.v2' table of Turbo-Geth/Silkworm running at target.
    """
    for block_number in range(block_height, block_height + count):
        encoded_block_number = sedes.encode_block_number(block_number)
        print('REQ block_number:', block_number,
              '(key: ' + str(encoded_block_number.hex()) + ')')
        key, value = kv_view.get(tables.ETH_SUPPLY_LABEL, encoded_block_number)
        assert key == encoded_block_number, 'ERR key `{0}` does not match!'.format(
            key.hex())
        supply = int.from_bytes(value, 'big')
        print('RSP supply:', supply, '\n')