BCOS-based historical data query scheme

BCOS-based historical data query scheme

Target

Complete the function of gethistoryfromkey in the class fabric, that is, you can query the table of the data corresponding to the key to update the records according to the key.

program exploration

Smart contract added logic

Write the logic in the smart contract:

//在数据上链时
1.创建一个交易hash数组
2.查询该key最新的交易hash   //新增
3.将该hash写入数组         //新增
4,执行写入数据操作

Compared with the original contract logic, steps 2 and 3 have been added.

Through this document (see the English document ), we can query the interfaces related to the block's tradable attributes :insert image description here

Does not contain the interface we expect to get the transaction hash (transaction address).

Borrow fabric source code logic

View the logic of the fabric source code. Fabric does not record historical data during the calculation phase.

Instead, it is recorded during the commit phase, when a block is generated. In the system process, all key and historical data related information are included in the historical database.

image source

insert image description here

solution

As can be seen from the first two solutions, the first solution recorded in the smart contract is that there is no ready-made interface to implement it, and the second is to put the complex process on the smart contract for execution, which does not meet the design principles of saving resources on the chain. so we can achieve

plan 1:

After each transaction is completed, the transaction hash (or even the transaction data) of this transaction and the corresponding key are recorded in a special history database (either on-chain or off-chain) in real time.

Scenario 2:

Referring to this answer , in addition to obtaining transaction data from the transaction return, we can also use event monitoring to obtain transaction data and its changes.

However, this method still cannot obtain the transaction hash, and can only directly store the transaction data.
insert image description here

Summarize

​ Researching this issue, we found that the blockchain system itself does not provide the function of "recording history", which seems to violate its "traceability" feature at first glance. Users cannot directly trace historical data based on a key value, but after careful consideration, it is not necessarily so.

​ When discussing [non-tamperable] and [traceable], we often ignore that we are talking about features rather than functions . Functions are used, while features are more reflected in the generation process.

​ In the blockchain system, the object in the generation process is data. So when we talk about the characteristics of the system , we are actually talking about the characteristics of the data, that is, the data of the blockchain system is [untamperable] and [traceable].

a guess

​ Because of the special mechanism of the blockchain, storage has always been a key indicator of each blockchain system. On-chain storage of historical data by default will increase storage pressure. So this feature has also become an option. However, for a long-running project, it may be a better compromise to do some historical storage according to your own needs.

Maybe this is why some blockchains do not directly carry historical data queries

                                                               注:文中出现的【区块链】专指联盟链,公链不在讨论范围。

Guess you like

Origin blog.csdn.net/qq_42750537/article/details/127449418