Changan chain parallel scheduling mechanism (2): DAG construction and slave node execution process

Changan Chain uses efficient parallel scheduling to execute transactions. Understanding Changan Chain transaction scheduling, conflict detection and DAG construction process will help developers better understand the operating mechanism of Changan Chain parallel scheduling, and help developers write high-quality, low-conflict Smart contracts to better build blockchain applications.

In the previous article, we explained the Changan chain transaction scheduling and conflict detection process. In this article, we will further introduce the DAG construction and slave node execution process in the Changan chain.

I. Overview

The previous article introduced the process and mechanism of transaction scheduling and transaction conflict detection in the Changan chain to ensure that the execution results of all transactions by the master node are "correct". However, in the process of transaction scheduling execution, some transactions have front-to-back dependencies. The master node needs to tell the slave node which transactions need to be executed in accordance with the execution order of the master node, and which transactions can be executed in parallel Execution, these contents will be introduced in this article, to ensure that the slave nodes execute the transactions in the order of execution of the master node, and then obtain a consistent world state.

2. DAG construction process

The previous article has introduced how to ensure that a single transaction can be executed correctly during the parallel scheduling and execution of transactions. However, after passing the transaction conflict detection of the master node, the execution order of transactions will become dependent. As shown in the example in the previous article, tx1 needs to be executed after tx0 after scheduling.

Therefore, in order to ensure that other slave nodes can also execute the transactions in the block in the same order, and finally each node forms a consistent world state, it is necessary for the master node to construct a DAG for the transactions in the block according to the read-write set of transactions ( Directed acyclic graph), and put the DAG into the block, and after other slave nodes receive the block, execute the transaction directly according to the DAG in the block, so as to ensure the orderly execution of the transaction by each node.

Image

Figure 2.1 Transaction sequence after scheduling

We assume that after the master node schedules and executes, the order of transactions in ExecutedTxs of Snapshot is as shown in the figure above, which are tx0, tx1, tx2 and tx3. Next, we will introduce the specific process of building DAG based on the above example.

1. Construct read-set-write-set dictionary and read-set-write-set location index

Image

Figure 2.2 Read-write set dictionary and read-write set position index constructed according to the above four transactions

First introduce the meaning of these four structures:

Read set field and write set dictionary : The data structure is a dictionary, that is, map.

● The keys in the dictionary are the keys in the transaction read set or write set;

● The value in the dictionary is the transaction number, which refers to the index in ExecutedTxs, starting from 0, that is, 0 means tx0, 1 means tx1, 2 means tx2, and 3 means tx3;

● The meaning of the entire dictionary is that this key is read or written by those transactions;

Read set position index and write set position index : The data structure is a two-dimensional array.

● The row header in the two-dimensional array represents the transaction index, and the column header represents the key of the transaction read/write or write set in the entire block;

● The value in the two-digit array is before this transaction, and the previous transactions in the read set dictionary or write set dictionary referenced the key;

● The meaning of the two-dimensional array represents the key in a certain transaction, which has been read or written by the previous transactions in the read set dictionary or write set dictionary;

The specific construction process:

According to the order of transactions in tx0, tx1, tx2 and tx3 in ExecutedTxs, the values ​​in the above four structures are calculated according to the read-write set information of each transaction.

How to deal with read sets:

1. Fill in readPos first, and judge whether the key has been referenced by the read set of the transaction before according to readDict, and if so, write the number of elements len corresponding to the key in readDict into the position corresponding to readPos;

2. Fill in writePos again, and judge according to writeDict whether the key has been referenced by the write set of the transaction before, and if so, write the number of elements len corresponding to the key in writeDict to the position corresponding to writePos;

3. Write the transaction index to the corresponding key in readDict;

How to handle write sets:

1. Fill in writePos first, and judge according to writeDict whether the key has been referenced by the write set of the transaction before, and if so, write the number of elements len corresponding to the key in writeDict to the position corresponding to writePos;

2. Fill in readPos again, and judge whether the key has been referenced by the read set of the transaction before according to readDict, and if so, write the number of elements len corresponding to the key in readDict into the position corresponding to readPos;

3. Write the transaction index to the corresponding key in writeDict;

Taking tx0 and tx1 as an example, fill in the relevant information into the above four structures as shown below. First fill in the information of tx0, and then continue to fill in the information of tx1.

Image

Figure 2.3 The flow chart of filling the above four structures in the read-write set of tx0

Image

Figure 2.4 The flow chart of filling the above four structures in the read-write set of tx1

If you are interested, you can continue to fill in the read-write set information of tx2 and tx3 into the above four structures on the basis of Figure 2.4, and finally you will get Figure 2.2. After filling out the material map for building the DAG, we will start to actually build the DAG.

2. Build DAG

Image

Figure 2.5 DAG construction flow chart

In the same way, first introduce the meaning of the three structures:

DAG : The data structure is a dictionary, that is, a map.

● The key in the dictionary represents the transaction number, which represents the transaction. This number also refers to the index in ExecutedTxs, starting from 0, that is, 0 means tx0, 1 means tx1, 2 means tx2, and 3 means tx3;

● The value in the dictionary is the transaction number;

● The meaning of the entire dictionary is which transactions this transaction conflicts with, and only those conflicting transactions are executed, this transaction can be executed;

Cumulative conflict bitmap and direct conflict bitmap : the data structure is a bitmap, each transaction will build a cumulative conflict bitmap and direct conflict bitmap

● The cumulative conflict bitmap indicates the transactions that have accumulated conflicts with this transaction. The role of the cumulative conflict bitmap is that if there is a read-write and write-write conflict between a certain transaction B and transaction A, then transaction B will be related to all indexes before transaction A And transactions that have write-write conflicts with transaction A also have conflicts. In this way, it is only necessary to identify the read-write conflicts between transaction B and transaction A. Note that this mainly uses the conflict between write and write transactions. For Write-read conflicts need to be checked in full, because there is no conflict in read-read transactions, but there are conflicts between these read transactions and this write transaction;

● The direct conflict bitmap represents the transaction that directly conflicts with this transaction, and is used to calculate the dictionary value in the DAG;

The specific construction process:

According to the order of transactions in tx0, tx1, tx2, and tx3 in ExecutedTxs, according to the four structures of the read set dictionary, write set dictionary, read set position index, and write set position index in Figure 3.2 constructed in the previous step, efficiently through the bitmap Which transactions retrieved conflict with this transaction.

How to deal with read sets:

It is only necessary to judge the conflict between reading and writing, because there is no conflict in reading and reading.

1. First check whether the key of the read set has been written by other transactions in the writeDict. If so, use writePos to clarify which transactions (assuming txn-1, txn, txn+1) are involved in this transaction I have written this key before, and those transactions have read and write conflicts with this transaction;

2. Because the transactions txn-1, txn, and txn+1 that have read-write conflicts with this transaction have write-write conflicts, you only need to add the corresponding cumulative conflict bitmap of the last txn+1 to this transaction The cumulative conflict bitmap, just add the transaction txn+1 to the direct conflict bitmap of this transaction. Because after marking the conflict between this transaction and txn+1, it is also marked that there is also a conflict with txn-1 and txn;

How to handle write sets:

Write-write conflicts and write-read conflicts need to be judged.

1. First judge the write-write conflict, and check whether the key in the write-set has been written by other transactions in the writeDict. If so, use writePos to clarify which transactions (assuming txn-1, txn, txn+1 ) wrote this key before this transaction;

2. The same as the above-mentioned read-write conflict detection method, because there are write-write conflicts among txn-1, txn, and txn+1, you only need to add the corresponding cumulative conflict bitmap of the last txn+1 to the cumulative value of this transaction Conflict bitmap, just add the transaction txn+1 to the direct conflict bitmap of this transaction;

3. Then determine the conflict between writing and reading, and check whether the key in the write concentration has been read by other transactions in readDict. If so, use readPos to clarify which transactions (assuming txn-1, txn, txn+1 ) read this key before this transaction;

4. Here, because readPos reflects the transactions that have been read on this key before this transaction, and there is no conflict between these read transactions, it is necessary to add txn-1, txn, txn+1 Add the cumulative conflict bitmap of each transaction to the cumulative conflict bitmap of this transaction, and add the three transactions txn-1, txn, and txn+1 to the direct conflict bitmap of this transaction;

Finally, after processing the read set and write set of this transaction, calculate the corresponding conflict transaction of this transaction in the DAG according to the direct conflict bitmap.

Next, taking tx0 and tx1 as an example, fill in the relevant information into the above three structures.

Image

Figure 2.6 DAG calculation flow chart of tx0

Image

Figure 2.7 DAG calculation flow chart of tx1

The other two transactions can calculate their DAG values ​​by themselves according to the above logic, and finally the results in Figure 2.5 will be obtained.

3. DAG sequential execution process

After the slave node receives the block, it can execute the transaction directly according to the order described by the DAG in the block, and the execution result must be consistent with the execution result of the master node.

The specific execution process is:

● Parallel execution of transactions that do not depend on other transactions;

● Other transactions pop out the transactions that have been executed, such as the transaction can be executed without relying on other transactions;

● Until all transactions are executed;

Image

Figure 3.1 Slave node transaction execution process

In the above example, the initial tx0 does not depend on other transactions and can be executed directly;

Subsequently, after tx1 and tx3 pop off the dependence on tx0, tx1 can continue to execute;

Finally, after tx2 and tx3 pop drop their dependence on tx1, tx2 and tx3 can be executed in parallel.

4. Extended thinking

We know that when the master node executes the transaction, it is scheduling + execution (not counting the DAG construction time, and assuming that the parallel execution of n transactions is 1/n of the serial execution time), the slave node only needs to process the transactions in the order in the DAG Execute it. Under what circumstances will the execution time of the slave node alone be longer than the master node scheduling + execution time?

Answer: In the scenario of write-write conflict, the master node executes in parallel, and the slave nodes execute in serial.

Guess you like

Origin blog.csdn.net/weixin_55760491/article/details/132564585