2021-02-08 14:39:27 +03:00
|
|
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
# Licensed under the Apache 2.0 License.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
from loguru import logger as LOG
|
2021-03-18 14:25:55 +03:00
|
|
|
import json
|
2021-04-12 12:54:42 +03:00
|
|
|
import random
|
2021-02-08 14:39:27 +03:00
|
|
|
|
|
|
|
# Change default log format
|
|
|
|
LOG.remove()
|
|
|
|
LOG.add(
|
|
|
|
sys.stdout,
|
|
|
|
format="<green>[{time:HH:mm:ss.SSS}]</green> {message}",
|
|
|
|
)
|
|
|
|
|
|
|
|
if len(sys.argv) < 2:
|
|
|
|
print("Error: Ledger directory should be specified as first argument")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2021-04-20 13:05:42 +03:00
|
|
|
ledger_dirs = sys.argv[1:]
|
2021-02-08 14:39:27 +03:00
|
|
|
|
|
|
|
|
2021-04-28 15:05:57 +03:00
|
|
|
# Because all ledger files are closed and are no longer being
|
|
|
|
# written to, it is safe to read all of them, even those that may
|
|
|
|
# contain uncommitted transactions.
|
2021-06-15 11:09:21 +03:00
|
|
|
# SNIPPET_START: create_ledger
|
|
|
|
import ccf.ledger
|
|
|
|
|
2021-04-28 15:05:57 +03:00
|
|
|
ledger = ccf.ledger.Ledger(ledger_dirs, committed_only=False)
|
2021-06-15 11:09:21 +03:00
|
|
|
# SNIPPET_END: create_ledger
|
2021-02-08 14:39:27 +03:00
|
|
|
|
|
|
|
# SNIPPET: target_table
|
2021-02-08 16:06:59 +03:00
|
|
|
target_table = "public:ccf.gov.nodes.info"
|
2021-02-08 14:39:27 +03:00
|
|
|
|
|
|
|
# SNIPPET_START: iterate_over_ledger
|
|
|
|
for chunk in ledger:
|
|
|
|
for transaction in chunk:
|
|
|
|
# Retrieve all public tables changed in transaction
|
|
|
|
public_tables = transaction.get_public_domain().get_tables()
|
|
|
|
|
|
|
|
if target_table in public_tables:
|
2021-02-22 17:11:40 +03:00
|
|
|
# Ledger verification is happening implicitly in ccf.ledger.Ledger()
|
2021-02-08 14:39:27 +03:00
|
|
|
for key, value in public_tables[target_table].items():
|
2021-03-18 14:25:55 +03:00
|
|
|
# Note: `key` and `value` are raw bytes here.
|
|
|
|
# This code needs to have knowledge of the serialisation format for each table.
|
|
|
|
# In this case, the target table 'public:ccf.gov.nodes.info' is raw bytes to JSON.
|
|
|
|
LOG.info(f"{key.decode()} : {json.loads(value)}")
|
2021-02-08 14:39:27 +03:00
|
|
|
# SNIPPET_END: iterate_over_ledger
|
2021-04-12 12:54:42 +03:00
|
|
|
|
|
|
|
# Read state of ledger
|
|
|
|
latest_state, latest_seqno = ledger.get_latest_public_state()
|
|
|
|
|
|
|
|
seqnos = [1, 2, 3, latest_seqno // 2, latest_seqno]
|
|
|
|
random.shuffle(seqnos)
|
|
|
|
for seqno in seqnos:
|
|
|
|
transaction = ledger.get_transaction(seqno)
|
|
|
|
|
|
|
|
# Confirm latest state can still be accessed, and is unchanged
|
|
|
|
latest_state1, latest_seqno1 = ledger.get_latest_public_state()
|
|
|
|
assert latest_seqno == latest_seqno1
|
|
|
|
assert latest_state == latest_state1
|