当前位置:网站首页>17. how to understand multi version concurrency control and read / write sets of fabric smart contracts? (vernacular version)

17. how to understand multi version concurrency control and read / write sets of fabric smart contracts? (vernacular version)

2022-06-13 07:56:00 Mingshen special worry

The previous chapters build step by step Fabric2.2 Version of blockchain network , from 0 Start setting up the server environment 、 To the operation official Demo colony 、 Create channels 、 Installation contract , And complete the development of agricultural product traceability project based on blockchain from zero .

Today, let's have a chat Fabric1.0 Why do we design read / write sets in the future ? What is a read-write set ?

1) Transaction execution process , yes Fabric An important factor in using read-write sets .

Over the years, the alliance chain has developed into two types of transaction processes , and Fabric0.6 And Fabric1.0 Each represents a type of transaction process , This involves the core knowledge points of blockchain , Let's study hard .
Fabric0.6

1) Suppose the blockchain network has four consensus nodes , The four nodes have the same height , Good network communication .
2) The content of the smart contract is the transfer system , Zhang San's initial account 500 element , Li Si's initial account 0 element .
3) The client sends a transaction to one of the consensus nodes A, The blockchain network broadcasts this transaction to the other three consensus nodes B、C、D, And record to the transaction pool of each node , There is no consensus yet . transaction tx1 Content : Zhang San transfers money to Li Si 20 element .
4) There was another deal at the same time tx2, Zhang San transfers money to Li Si 50 element , The pattern is the same as the steps 3. At this point, each consensus node contains two transactions , Waiting to pack blocks .
5) here A The node is responsible for outputting blocks , take tx1、tx2 Packed in a block , And pre execute to get Zhang San :430 element , Li Si :70 element , And recorded in the blockchain world state .

The above execution process is often perfect , I didn't feel anything wrong , There is no doubt .

Fabric1.0

1) Suppose the blockchain network has four consensus nodes , The four nodes have the same height , Good network communication .
2) The content of the smart contract is the transfer system , Zhang San's initial account 500 element , Li Si's initial account 0 element .
3)( This step begins to change ) Client sends transaction tx1( Zhang San transfers money to Li Si 20 element ) Give one of the proposal nodes , The proposal node pre executes the smart contract to get the result , Zhang San :480 element , Li Si :20 element , And return the result to the client , During this process, the transaction did not enter the block , The proposal node does not update any databases and caches .
4) There was another deal at the same time tx2( Zhang San transfers money to Li Si 50 element ), The pattern is the same as the steps 3, Zhang San :450 element , Li Si :50 Meta results returned to the client .
5) The client sends the returned results of the two transactions to Orderer node ,Orderer Nodes are only responsible for sorting , Generate blocks , Not responsible for smart contract logic detection , These two transactions are packaged into blocks , Send to commit node
6)commit Node resolution tx1、tx2 Result , Write to the status database in order , Final Zhang San :450 element , Li Si :50 element , There is a problem with the system transaction amount ( Hope the result : Zhang San 430, Li Si 70), And the system does not report an error , Think everything's OK .

Through analysis Fabric1.0 Process discovery , because Fabric1.0 Change the mode to generate blocks after pre execution , And the roles are separated , This leads to transaction execution exceptions in the concurrent scenario , How to solve this problem ? There are two solutions :
1) Front hard rigid , Can make the result Zhangsan 430, Li Si 70, But it's too difficult , There is no good method on the official website .
2) Introducing read / write sets + Multi version concurrency control , Consider one of the deals a failure , Notify the front-end system to resend .( Take a step back and there will be a bigger world in front of you )

2) Read-write set 、 Multi version concurrency control

Assuming the current Key: Zhang San The version is V1,Key: Li Si The version is V1, Multi version concurrency control only works with Key of , and Value irrelevant .
1) The client sends tx1( Zhang San transfers money to Li Si 20 element ) To the proposal node , Contract logic : Read the amount of Zhang San , Record Read Zhang San -V1, Read the amount of Li Si , Record Read Li Si -V1, Write Zhang Sanxin's amount , Record Write Zhang San -V2, Write the amount of Li Sixin , Record Write Li Si -V2. And will (Read Zhang San -V1、Read Li Si -V1、Write Zhang San -V2、Write Li Si -V2) Send to client .
2) The client sends tx2( Zhang San transfers money to Li Si 50 element ) To the proposal node , Contract logic : Read the amount of Zhang San , Record Read Zhang San -V1, Read the amount of Li Si , Record Read Li Si -V1, Write Zhang Sanxin's amount , Record Write Zhang San -V2, Write the amount of Li Sixin , Record Write Li Si -V2. In the same way (Read Zhang San -V1、Read Li Si -V1、Write Zhang San -V2、Write Li Si -V2) Send to client .
3) The two trade fairs were sent one after the other Orderer node ,Orderer Nodes can ignore the contents , Directly packed into blocks , Send to commit node
4)commit Node parsing block transaction discovery tx1 take Key: Zhang San Version of Write To V2, analysis tx2 Find out Key: Zhang San Read The version of is still V1, There are version inconsistencies , Think tx2 Transaction execution failed .

3) If the smart contract has only read sets 、 Only the writeset ?

Suppose a deposit logic , Write data to the blockchain regardless , There is no need to judge whether it has been written , So the contract is only a write set , Only write set contracts are not blocked by multi version concurrency control .

4) Expand

After the subsequent development of blockchain , Changan chain 、BCOS、 Baidu super chain wants to be in Fabric0.6 In this mode, the read / write set is applied to improve the blockchain processing performance , Students who want to know can learn .

原网站

版权声明
本文为[Mingshen special worry]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270545394904.html