===================
防篡改
===================

防篡改合了区块链思想，将用户操作记录至两种历史表中：用户历史表和全局区块表。
本章节内容包括：

- 特性描述_
- 创建防篡改用户表_
- 修改数据_
- 查看账本历史操作记录_
- 校验账本数据一致性_
- 归档账本数据库_

特性描述
------------------------------

防篡改采用表级属性blockchain对普通表和防篡改用户表进行隔离。用户在创建表时需要显式指定表的属性 blockchain=true 时，创建的行存表具有防篡改属性，即为防篡改用户表。

用户向防篡改用户表中插入数据时，系统会自动生成少量行级校验信息。

在用户执行DML时，系统会在全局区块表(sys_global_chain)中记录用户的操作、在用户表相应的历史表中记录数据的更改等信息，操作记录、数据变化记录和用户表中的数据三者严格保持一致。

防篡改提供高性能校验接口，能够供用户方便的校验数据的一致性，如果一致性校验失败，则说明数据可能发生篡改。

防篡改还提供历史表归档接口，能够将历史表中的记录在不影响校验结果的前提下进行压缩归档，防止历史表数据过于膨胀。

创建防篡改用户表
------------------------------

创建防篡改用户表

   .. code:: 

      create extension kdb_ledger;
      CREATE EXTENSION
      create table usertable(id int, name text) with(blockchain=true);
      CREATE TABLE
      #查看表结构，系统会自动为其添加一个hash列用于保存每行的hash摘要
      \d+ usertable 
                                       Table "public.usertable"
       Column |  Type   | Collation | Nullable | Default | Storage  | Stats target | Description
      --------+---------+-----------+----------+---------+----------+--------------+-------------
       id     | integer |           |          |         | plain    |              |
       name   | text    |           |          |         | extended |              |
      Access method: heap
      Options: blockchain=true
      #会在blockchain模式下创建一个与之对用的用户历史表
       \d+ blockchain.usertable_16396_hist
                          Table "blockchain.usertable_16396_hist"
        Column  |  Type  | Collation | Nullable | Default | Storage | Stats target | Description
      ----------+--------+-----------+----------+---------+---------+--------------+-------------
       rec_num  | bigint |           |          |         | plain   |              |
       hash_ins | hash16 |           |          |         | plain   |              |
       hash_del | hash16 |           |          |         | plain   |              |
       pre_hash | hash32 |           |          |         | plain   |              |
      Access method: heap

修改数据
------------------------------

修改防篡改表中的数据：

   .. code:: 

      #插入数据
      insert into usertable values(1,'bob');
      INSERT 0 1

      
      select * from usertable ;
       id | name 
      ----+------
        1 | bob
      (1 row)

      #会在历史表中插入一条记录，hash_ins为新插入行的hash摘要
      select * from blockchain.usertable_16396_hist;
       rec_num |     hash_ins     | hash_del |                             pre_hash
      
      ---------+------------------+----------+------------------------------------------------------------------
             1 | a52e208daa349b22 |          | 7b86e114529638fb19faf3d7eddb8e569a2581fd8df8d0f4dee36dd7633480d1
      
      
      #更新数据，在历史表中会删除原来的记录并插入一条记录，其中hash_ins字段记录更新的新纪录插入的hash摘要，hash_del字段记录更新的新纪录删除的hash摘要。

      #注意：历史表只能追加不能修改
      update usertable set name='bob2' where id=1;
      UPDATE 1

      select *,hash from usertable;
       id | name |       hash
      ----+------+------------------
        1 | bob2 | 39a127af7a15f0ec
      (1 row)

      select * from blockchain.usertable_16396_hist;
       rec_num |     hash_ins     |     hash_del     |                             pre_hash
      ---------+------------------+------------------+------------------------------------------------------------------
             1 | a52e208daa349b22 |                  | 7b86e114529638fb19faf3d7eddb8e569a2581fd8df8d0f4dee36dd7633480d1
             2 | 39a127af7a15f0ec | a52e208daa349b22 | 9399570d113f6863ac3e8d4d0a8ec3f5a0bc424a8019781040becbb2057d0d0a
      
      
      #删除数据，会在对应的历史表中增加一条记录，hash_del字段记录的是删除的记录的hash摘要
      #pre_hash表示用户历史表当前数据整体的hash摘要。
      delete from usertable where id=1;
      DELETE 1

      select * from blockchain.usertable_16396_hist;
       rec_num |     hash_ins     |     hash_del     |                             pre_hash
      ---------+------------------+------------------+------------------------------------------------------------------
             1 | a52e208daa349b22 |                  | 7b86e114529638fb19faf3d7eddb8e569a2581fd8df8d0f4dee36dd7633480d1
             2 | 39a127af7a15f0ec | a52e208daa349b22 | 9399570d113f6863ac3e8d4d0a8ec3f5a0bc424a8019781040becbb2057d0d0a
             3 |                  | 39a127af7a15f0ec | 962cabacb90e2250f6b09569129f264da802d38ff38993fed1c5990401223635
      (3 rows)
      
      #从上面的记录中，我们可以大致的知道对于usertable进行了哪些操作
      #先插入一条记录，然后更新刚刚插入的数据，最后将其删掉。
 
查看账本历史操作记录
------------------------------

注意：初始用户、系统管理员、拥有auditadmin属性的用户可以查看，三权分立后则是审计管理员或拥有auditadmin属性的用户可以查看。

查看全局区块表sys_global_chain ：

该表记录用户对防篡改用户表的修改操作信息，每条记录对应一次表级修改操作:

   .. code:: 

      select * from sys_global_chain ;
       blocknum |  dbname  | username |           starttime           | relid | relnsp  |  relname  |     relhash      |            globalhash            |                      txcommand                       
      ----------+----------+----------+-------------------------------+-------+---------+-----------+------------------+----------------------------------+------------------------------------------------------
              0 | postgres | omm      | 2022-03-22 10:55:17.551399+08 | 33224 | bc_test | usertable | 6fc67a13c179043e | 418b70fe7787f7ba1c2ab4e20de60425 | insert into bc_test.usertable values(1,'bob');
              1 | postgres | omm      | 2022-03-22 10:57:54.899249+08 | 33224 | bc_test | usertable | 7a2443be8ba5dfaa | 3cce190f8f4b9f99365e66c081057e23 | update bc_test.usertable set name='bob2' where id=1;
              2 | postgres | omm      | 2022-03-22 11:01:30.489774+08 | 33224 | bc_test | usertable | 1615422db2e11c18 | cef15caca2df7b856f7f9529c9d08103 | delete from bc_test.usertable where id=1;
      (3 rows)


校验账本数据一致性
------------------------------

KES提供两种校验接口，包括普通用户接口和安全员接口:

1、普通用户接口是提供给普通用户来校验防篡改用户表的接口，该接口仅能校验用户有权限的表：

   .. code:: 

      #校验防篡改用户表与历史表是否一致，一致则返回t
      select blockchain.ledger_hist_check('bc_test','usertable');
       ledger_hist_check 
      -------------------
       t
      (1 row)

      #校验防篡改用户表、用户历史表和全局区块表三者是否一致
      select blockchain.ledger_gchain_check('bc_test','usertable');
       ledger_gchain_check 
      ---------------------
       t
      (1 row)

2、安全员接口是提供给安全员来校验防篡改用户表的接口，该接口可以校验当前数据库下的所有防篡改用户表：

   .. code:: 

      #校验防篡改用户表与历史表是否一致，一致则返回t
      select * from blockchain.all_ledger_hist_check();
        relname  | schname | result
      -----------+---------+---------
       usertable | bc_test | success
      (1 row)

      #校验防篡改用户表、用户历史表和全局区块表三者是否一致
      select * from blockchain.all_ledger_gchain_check();
        relname  | schname | result
      -----------+---------+---------
       usertable | bc_test | success
      (1 row)

归档账本数据库
------------------------------

对指定的用户历史表进行归档操作：


   .. code:: 

      select * from blockchain.usertable_16396_hist;
       rec_num |     hash_ins     |     hash_del     |                             pre_hash
      ---------+------------------+------------------+------------------------------------------------------------------
             1 | a52e208daa349b22 |                  | 7b86e114529638fb19faf3d7eddb8e569a2581fd8df8d0f4dee36dd7633480d1
             2 | 39a127af7a15f0ec | a52e208daa349b22 | 9399570d113f6863ac3e8d4d0a8ec3f5a0bc424a8019781040becbb2057d0d0a
             3 |                  | 39a127af7a15f0ec | 962cabacb90e2250f6b09569129f264da802d38ff38993fed1c5990401223635
      (3 rows)
      select blockchain.ledger_hist_archive('bc_test','usertable');
       ledger_hist_archive 
      ---------------------
       t
      (1 row)
      
用户历史表将归档成一条数据

   .. code:: 

      select * from blockchain.usertable_16396_hist;
       rec_num |     hash_ins     |     hash_del     |                             pre_hash
      ---------+------------------+------------------+------------------------------------------------------------------
             3 | decf483d244a8c0e | decf483d244a8c0e | 7518e265f6c9c8b8bedffa208ed710ac8206ee369ade32c72ae03566f9403188
      (1 row)

      
安全员对所有用户历史表进行归档操作：

   .. code:: 

      select * from  blockchain.all_ledger_hist_archive(1);
             relname        | result  | record_num
      ----------------------+---------+------------
       usertable_16396_hist | success |          1
      (1 row)

所有的用户历史表都将归档成一条数据：

   .. code:: 

      select * from blockchain.usertable_16396_hist;
       rec_num |     hash_ins     |     hash_del     |                             pre_hash
      ---------+------------------+------------------+------------------------------------------------------------------
             3 | decf483d244a8c0e | decf483d244a8c0e | 7518e265f6c9c8b8bedffa208ed710ac8206ee369ade32c72ae03566f9403188
      (1 row)

