Updated on 2025-05-29 GMT+08:00

Checking Ledger Data Consistency

Prerequisites

The database is running properly, and a series of addition, deletion, and modification operations are performed on the tamper-proof database to ensure that operation records are generated in the ledger for query.

Context

  • Currently, the ledger database provides two verification APIs: ledger_hist_check(text, text) and ledger_gchain_check(text, text). When a common user calls a verification API, only the tables that the user has the permission to access can be verified.
  • The API for verifying the tamper-proof user table and user history table is pg_catalog.ledger_hist_check. To verify a table, run the following command:
    SELECT pg_catalog.ledger_hist_check(schema_name text,table_name text);

    If the verification is successful, the function returns t. Otherwise, the function returns f and the cause of failure.

  • The pg_catalog.ledger_gchain_check API is used to check whether the tamper-proof user table, user history table, and global blockchain table are consistent. To verify consistency, run the following command:

    SELECT pg_catalog.ledger_gchain_check(schema_name text, table_name text);

    If the verification is successful, the function returns t. Otherwise, the function returns f and the cause of failure.

Procedure

  1. Check whether the tamper-proof user table ledgernsp.usertable is consistent with the corresponding user history table.

    1
    gaussdb=# SELECT pg_catalog.ledger_hist_check('ledgernsp', 'usertable');
    
    The query result is as follows:
     ledger_hist_check
    -------------------
     t
    (1 row)

    The query result shows that the results recorded in the tamper-proof user table and user history table are consistent.

  2. Check whether the records in the tamper-proof table ledgernsp.usertable are the same as those in the corresponding user history table and global blockchain table.

    1
    gaussdb=# SELECT pg_catalog.ledger_gchain_check('ledgernsp', 'usertable');
    

    The query result is as follows:

     ledger_gchain_check
    ---------------------
     t
    (1 row)

    The query result shows that the records of ledgernsp.usertable in the preceding three tables are consistent and no tampering occurs.