账本数据库 ¶
账本数据库融合了区块链思想,将用户操作记录至两种历史表中:用户历史表和全局区块表。 本章节内容包括:
创建防篡改模式 ¶
通过CREATE SCHEMA语句创建防篡改模式。
create schema bc_test with blockchain; CREATE SCHEMA
创建防篡改用户表 ¶
在防篡改模式中创建防篡改用户表
create table bc_test.usertable(id int, name text); CREATE TABLE #查看表结构,系统会自动为其添加一个hash列用于保存每行的hash摘要 \d+ bc_test.usertable Table "bc_test.usertable" Column | Type | Modifiers | Storage | Stats target | Description --------+---------+-----------+----------+--------------+------------- id | integer | | plain | | name | text | | extended | | hash | hash16 | | plain | | Has OIDs: no Options: orientation=row, compression=no History table name: bc_test_usertable_hist #会在blockchain模式下创建一个与之对用的用户历史表 \d+ blockchain.bc_test_usertable_hist Table "blockchain.bc_test_usertable_hist" Column | Type | Modifiers | Storage | Stats target | Description ----------+--------+-----------+---------+--------------+------------- rec_num | bigint | | plain | | hash_ins | hash16 | | plain | | hash_del | hash16 | | plain | | pre_hash | hash32 | | plain | | Indexes: "gs_hist_33224_index" PRIMARY KEY, btree (rec_num int4_ops) TABLESPACE pg_default Has OIDs: no Options: internal_mask=263
修改数据 ¶
修改防篡改表中的数据:
#插入数据 insert into bc_test.usertable values(1,'bob'); INSERT 0 1 select * from bc_test.usertable ; id | name ----+------ 1 | bob (1 row) #会在历史表中插入一条记录,hash_ins为新插入行的hash摘要 select * from blockchain.bc_test_usertable_hist ; rec_num | hash_ins | hash_del | pre_hash ---------+------------------+----------+---------------------------------- 0 | 6fc67a13c179043e | | 11f968b411f7b82fe511f322aafe5b70 #更新数据,在历史表中会删除原来的记录并插入一条记录,其中hash_ins字段记录更新的新纪录插入的hash摘要,hash_del字段记录更新的新纪录删除的hash摘要。 #注意:历史表只能追加不能修改 update bc_test.usertable set name='bob2' where id=1; UPDATE 1 select *,hash from bc_test.usertable ; id | name | hash ----+------+------------------ 1 | bob2 | e9eabdd24d1ee3e8 (1 row) select * from blockchain.bc_test_usertable_hist ; rec_num | hash_ins | hash_del | pre_hash ---------+------------------+------------------+---------------------------------- 0 | 6fc67a13c179043e | | 11f968b411f7b82fe511f322aafe5b70 1 | e9eabdd24d1ee3e8 | 6fc67a13c179043e | 342e784d0367c325cd522b2be85c6a8c #删除数据,会在对应的历史表中增加一条记录,hash_del字段记录的是删除的记录的hash摘要 #pre_hash表示用户历史表当前数据整体的hash摘要。 delete from bc_test.usertable where id=1; DELETE 1 select * from blockchain.bc_test_usertable_hist; rec_num | hash_ins | hash_del | pre_hash ---------+------------------+------------------+---------------------------------- 0 | 6fc67a13c179043e | | 11f968b411f7b82fe511f322aafe5b70 1 | e9eabdd24d1ee3e8 | 6fc67a13c179043e | 342e784d0367c325cd522b2be85c6a8c 2 | | e9eabdd24d1ee3e8 | a3179b0bc90a96aef927c8be4fbb399e (3 rows) #从上面的记录中,我们可以大致的知道对于usertable进行了哪些操作 #先插入一条记录,然后更新刚刚插入的数据,最后将其删掉。
查看账本历史操作记录 ¶
注意:初始用户、系统管理员、拥有auditadmin属性的用户可以查看,三权分立后则是审计管理员或拥有auditadmin属性的用户可以查看。
查看全局区块表sys_global_chain :
该表记录用户对防篡改用户表的修改操作信息,每条记录对应一次表级修改操作:
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、校验防篡改用户表与历史表是否一致,一致则返回t:
select pg_catalog.ledger_hist_check('bc_test','usertable'); ledger_hist_check ------------------- t (1 row)
2、校验防篡改用户表、用户历史表和全局区块表三者是否一致:
select ledger_gchain_check('bc_test','usertable'); ledger_gchain_check --------------------- t (1 row)
归档账本数据库 ¶
对指定的用户历史表进行归档操作:
select * from blockchain.bc_test_usertable_hist; rec_num | hash_ins | hash_del | pre_hash ---------+------------------+------------------+---------------------------------- 0 | 6fc67a13c179043e | | 11f968b411f7b82fe511f322aafe5b70 1 | e9eabdd24d1ee3e8 | 6fc67a13c179043e | 342e784d0367c325cd522b2be85c6a8c 2 | | e9eabdd24d1ee3e8 | a3179b0bc90a96aef927c8be4fbb399e (3 rows) select pg_catalog.ledger_hist_archive('bc_test','usertable'); ledger_hist_archive --------------------- t (1 row)
用户历史表将归档成一条数据
select * from blockchain.bc_test_usertable_hist; rec_num | hash_ins | hash_del | pre_hash ---------+------------------+------------------+---------------------------------- 2 | 59b137e60e97e826 | 59b137e60e97e826 | a3179b0bc90a96aef927c8be4fbb399e (1 row)
同时会在audit_directory指定的目录中生成hist_bak目录:
cd hist_bak/ ll total 4 -rw------- 1 omm dbgrp 179 Mar 22 11:47 bc_test_usertable_hist_33224_20220322114724.hist
对全局区块表进行归档操作:
select pg_catalog.ledger_gchain_archive(); ledger_gchain_archive ----------------------- t (1 row)
全局历史表将以用户表为单位归档为N(用户表数量)条数据:
select * from sys_global_chain ; blocknum | dbname | username | starttime | relid | relnsp | relname | relhash | globalhash | txcommand ----------+----------+----------+-------------------------------+-------+---------+-----------+------------------+----------------------------------+----------- 2 | postgres | omm | 2022-03-22 10:55:17.551399+08 | 33224 | bc_test | usertable | 0000000000000000 | 3702546b5ed6900ad11356f5b59a8bbf | Archived. (1 row)
同时会在hist_bak目录中生成如下文件:
-rw------- 1 omm dbgrp 499 Mar 22 11:54 sys_global_chain _20220322115409.bak
修复账本数据库 ¶
当在异常情况或表被损坏时,可以使用如下函数进行修复:
ledger_hist_repair()对用户历史表进行修复, ledger_gchain_repair()对全局区块表进行修复。
# 对历史表进行修复: select ledger_hist_repair('bc_test','usertable'); ledger_hist_repair -------------------- 0000000000000000 (1 row) 返回值为:修复造成的历史表的hash增量,结果为0说明不需要修复。 #对全局区块表进行修复: select ledger_gchain_repair('bc_test','usertable'); ledger_gchain_repair ---------------------- 0000000000000000 (1 row) 返回值为:修复造成的全局区块表的hash增量。