WO2021139109A1 - Method and apparatus for writing into state database, electronic device, and storage medium - Google Patents

Method and apparatus for writing into state database, electronic device, and storage medium Download PDF

Info

Publication number
WO2021139109A1
WO2021139109A1 PCT/CN2020/099087 CN2020099087W WO2021139109A1 WO 2021139109 A1 WO2021139109 A1 WO 2021139109A1 CN 2020099087 W CN2020099087 W CN 2020099087W WO 2021139109 A1 WO2021139109 A1 WO 2021139109A1
Authority
WO
WIPO (PCT)
Prior art keywords
block
target block
read
write
state database
Prior art date
Application number
PCT/CN2020/099087
Other languages
French (fr)
Chinese (zh)
Inventor
陈沐豪
褚镇飞
Original Assignee
平安科技(深圳)有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 平安科技(深圳)有限公司 filed Critical 平安科技(深圳)有限公司
Publication of WO2021139109A1 publication Critical patent/WO2021139109A1/en

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/21Design, administration or maintenance of databases
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2455Query execution
    • G06F16/24552Database cache management
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/24Querying
    • G06F16/245Query processing
    • G06F16/2457Query processing with adaptation to user needs
    • G06F16/24578Query processing with adaptation to user needs using ranking
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/27Replication, distribution or synchronisation of data between databases or within a distributed database system; Distributed database system architectures therefor
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06QINFORMATION AND COMMUNICATION TECHNOLOGY [ICT] SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES; SYSTEMS OR METHODS SPECIALLY ADAPTED FOR ADMINISTRATIVE, COMMERCIAL, FINANCIAL, MANAGERIAL OR SUPERVISORY PURPOSES, NOT OTHERWISE PROVIDED FOR
    • G06Q40/00Finance; Insurance; Tax strategies; Processing of corporate or income taxes
    • G06Q40/04Trading; Exchange, e.g. stocks, commodities, derivatives or currency exchange
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Definitions

  • This application relates to the field of blockchain technology, and in particular to a method, device, electronic device, and storage medium for writing a state database.
  • the block chain completes the transaction packaging, that is, after the consensus is completed, a new block is generated. Afterwards, it is necessary to pre-execute the transaction in the block to obtain the pre-execution result to complete the transaction and update the database, that is, to settle the account.
  • the blockchain belongs to a chained data structure, and each block points to the previous block. Therefore, to verify the validity of the data, each block needs to be verified sequentially. In order to ensure that the assets in the transaction are consistent, the transactions in the block must be pre-executed in order, that is, the transactions related to the account need to be sequenced. In other words, for a node that newly joins the blockchain network, it needs to synchronize all historical block data and verify the validity one by one before starting to synchronize the latest block.
  • the inventor realizes that historical block data is often very large, and therefore, the synchronization process takes a long time, resulting in a waste of time and machine performance.
  • This application provides a method, device, electronic device, and computer-readable storage medium for writing a state database.
  • This application provides a method for writing a state database, and the method includes:
  • the receiving step after receiving the simulation results of several blocks sent by the ordering node, obtain the block information of the several blocks;
  • the first writing step is to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Writing set; and
  • the second writing step is to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
  • This application also provides a device for writing a state database, the device including:
  • the receiving module is used to obtain the block information of the several blocks after receiving the simulation results of the several blocks sent by the ordering node;
  • the first write module is used to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write of the target block based on the data in the preset cache and the state database Whether the centralized transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache valid transactions in each block in the blockchain network Read and write set; and
  • the second writing module is configured to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
  • the present application also provides an electronic device, the electronic device comprising: a memory, a processor, the memory stores a state database write program that can be run on the processor, and the state database write program is When the processor executes, the following steps are implemented:
  • the receiving step after receiving the simulation results of several blocks sent by the ordering node, obtain the block information of the several blocks;
  • the first writing step is to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Writing set; and
  • the second writing step is to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
  • the present application also provides a computer-readable storage medium that includes a state database writing program, and the state database writing program is executed by a processor to implement the following steps:
  • the receiving step after receiving the simulation results of several blocks sent by the ordering node, obtain the block information of the several blocks;
  • the first writing step is to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Writing set; and
  • the second writing step is to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
  • FIG. 1 is a flowchart of a preferred embodiment of a method for writing into a state database of this application
  • FIG. 2 is a schematic diagram of a preferred embodiment of the electronic device of this application.
  • Fig. 3 is a schematic diagram of modules of the writing device of the state database of the present application.
  • This application provides a method for writing a state database.
  • FIG. 1 it is a flowchart of a preferred embodiment of a method for writing a state database according to this application.
  • the method can be executed by an electronic device, and the electronic device can be implemented by software and/or hardware.
  • the method for writing the state database only includes steps S10-S30.
  • a method for writing a state database is applied to transaction nodes in a blockchain network, and the method includes:
  • Step S10 after receiving the simulation results of several blocks sent by the ordering node, obtain block information of the several blocks.
  • the blockchain network includes transaction nodes, submission nodes, and consensus nodes.
  • the transaction process based on the blockchain network is roughly as follows:
  • the client creates a transaction proposal and sends it to the transaction node (the endorsement node corresponding to the smart contract (chaindode), that is, the peer node designated by the smart contract that can verify the transaction);
  • the transaction node executes the smart contract and generates a read and write operation set (fabric read and write set) based on the read and write keys;
  • the transaction node returns the proposal result (the simulation result generated when the transaction node executes the smart contract) to the client;
  • the client submits the transaction data to the consensus node (Orderer), and the transaction data includes the read-write set from the proposal;
  • the consensus node packs the sorted transaction into the block
  • the block sends the proposal result to the submitting node (if the proposal result is successful, the proposal result is sent to the sorting node, and the sorting node will broadcast the transaction to other transaction nodes);
  • MVCC multi-version control Multi-Version Concurrency Control
  • state db latest value in the state database
  • each block executes steps 1 and 2 in sequence, and the performance is relatively low.
  • the steps before improvement are as follows: input a block, the transaction node reads the read-write set in the block, compares the data in the read-write set with the latest data in the state database, and judges which data in the read-write set is valid , Which are invalid. Specifically, the transaction node obtains transaction information from the ordering node and verifies whether the transaction is valid. By obtaining the value in the read-write set, compare it with the local state database to determine whether the transaction is valid. This process is called MVCC. Then write the block information into the ledger. Then, update the read-write set information of valid transactions in the previous step to the state database. After the information of the block is written, write the checkpoint information. Then, repeat the above steps for the next block.
  • the problem with the above steps is that the process of writing the state database is sequential, and writing the state database is an IO-intensive operation. If a single block is required to write the state database in order, the performance impact will be relatively large.
  • the transaction node After the transaction node receives the simulation result (proposal result) sent by the ordering node, it obtains the block information of each block in the order of the block number of each block.
  • the block information of each block includes: transaction information of multiple transactions, whether the transaction is valid, the read-write set corresponding to the transaction, the smart contract and the block chain corresponding to each transaction.
  • one chain corresponds to a state database, all blocks correspond to the same cache, and the data in the cache is stored in the form of a map.
  • the method further includes: predetermining and saving the number of each block; for each new block, a new number is generated based on the maximum value of the existing number as the new number. Increase the number of the block.
  • the existing blocks are numbered according to business requirements, and blocks 1, 2, 3, ..., n are obtained.
  • the order of each number is the order in which the block information of each block is obtained. In the process of generating a new block, the number of the new block is n+1, and so on.
  • Step S20 Read the read-write set of the target block from the block information of the several blocks in sequence, and determine the transaction data in the read-write set of the target block based on the data in the preset cache and the state database Whether it is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the read-write set of valid transactions in each block in the blockchain network .
  • the process in the prior art is: if there are 3 blocks 1, 2, 3 in sequence. To verify whether the transaction in block 2 is valid, you need to rely on the result of block 1, and to verify whether the transaction in block 3 is valid, you need to rely on the results of block 1 and block 2, and verify one block at a time After that, the results are written into the state database. It can be understood that the state database that was written last time contains the latest data in the blockchain. When processing a new block, you can go to the state database to query. The problem with this process is that it needs to wait for each block in order to write the transaction results into the state database, that is, verifying the validity of the data will make the process of writing the state database very slow.
  • a new cache is added for storing the read-write set of valid transactions in each block in the blockchain network.
  • the judging whether the transaction data in the read-write set of the target block is valid based on the data in the cache and the state database includes:
  • Determine the reference block corresponding to the target block obtain the data corresponding to the reference block from the cache, and compare the read/write set of the target block with the reference block in the cache The data is compared, and if there is data corresponding to the read-write set of the target block, it is determined that the transaction data in the read-write set of the target block is valid; if there is no read-write set with the target block
  • Corresponding data the data corresponding to the reference block is obtained from the state database, and the read-write set of the target block is compared with the data corresponding to the reference block in the state database. The data corresponding to the read-write set of the target block determines that the transaction data in the read-write set of the target block is valid.
  • the reference block corresponding to the target block is determined based on the sequence of block numbers. Taking blocks 1, 2, and 3 as an example, block 1 has no reference block, the reference block of block 2 is block 1, and the reference block of block 3 is block 1 and block 2. For example, after the transaction node has checked the read-write set in block 1, it writes the read-write set corresponding to the valid transaction data into the cache. When block 1 is or has not yet begun to write the state database, block 2 first Compare the read and write set in the block information with the data in block 1 in the cache. If there is no data in block 1 in the cache, compare it with the latest data in the state database, so that block 2 can be correctly judged Whether the transaction data in the read-write set is valid. In other words, to check the read and write set of a target block, not only compare the data in the state database, but also compare the data in the cache.
  • the order of writing the read-write set in the block information of each block into the cache is serialized according to the block number. For example, if the order of block 1, 2, 3,..., n is block 1, block 1, block 3,..., block n, then write the cache of block 1 first, and then write the cache of block 2. Caching,..., and so on.
  • step S30 the data corresponding to the target block is read from the cache in a background execution manner, and the data corresponding to the target block is written into the state database.
  • the operation of writing the data corresponding to the target block into the state database is executed concurrently, so as to make full use of the performance of the computer.
  • Each block has its own block number. Associate the block number with the read-write set of valid transactions in the block.
  • the block number is used to index the data corresponding to each target block from the cache, that is, the read-write set corresponding to the valid transaction data in each target block.
  • the read and write set corresponding to the valid transaction data of part of the target block can be written into the cache, and the operation of writing the state database can also be started after the valid transaction data of all target blocks is read and written. After the set is written into the cache, the operation of writing the state database is started. Therefore, it can be understood that step S20 and step S30 can be performed at the same time after writing the cache of the first target block.
  • the first target block is started in the background.
  • the block performs write state database operations. For example, after the read-write set corresponding to the valid transaction data of block 1 is written into the cache, it is determined whether the transaction data of the read-write set of block 2 is valid, and at the same time, the operation of writing the state database to block 1 is started in the background.
  • both the write cache and the write state database are executed by the transaction node.
  • the write cache and the write state database are completed by different coroutines of the transaction node.
  • the write cache is completed by the first coroutine created by the transaction node.
  • the writing of the state database is completed by the second coroutine created by the transaction node to realize the operation of writing the state database in the background.
  • the read and write set of valid blocks are written into the cache, and subsequent blocks can obtain relevant data from the cache during the process of verifying data.
  • the next block can be processed without waiting for the previous block to complete writing to the state database. Since the write cache speed is much faster than the write state database, the efficiency of subsequent block data verification can be improved; at the same time, By separating the write cache from the write state database, and execute the process of writing the state database in the background, the computer performance is fully utilized and the processing progress of the write state database is accelerated.
  • the step S30 further includes:
  • the data corresponding to the target block is written into the state database, the data corresponding to the target block is released from the cache.
  • the data in the cache is no longer needed, so data that is no longer needed in the cache can be deleted to free up storage space.
  • the current steps for writing a checkpoint are: input a block, check the block data, write the valid read and write set of the block into the state database, write the checkpoint, and then repeat the same steps for the next block.
  • the checkpoint function at this time will become invalid.
  • the state database of block 1, block 2, and block 3 is written concurrently, if the state database of block 2 is written and the checkpoint is written to 2, then the peer hangs down and restarts the peer When the checkpoint is 2, the program will start to restore from block 2, and restore the data in block 3 and beyond. It is impossible to find that the data of block 1 has not been written into the state database, which will cause the data in the filling database to be lost, resulting in inconsistencies with the data in the state database of other nodes.
  • the writing method of the state database further includes:
  • Step S40 After the data corresponding to the target block is written into the state database, a checkpoint write request is sent to the checkpoint coroutine, the request includes the block number corresponding to the target block, and the checkpoint is controlled The coroutine writes checkpoints for the target block based on the block number and preset analysis rules.
  • To write a checkpoint is to write the block number corresponding to the checkpoint into the state database. Each time a block is written, the checkpoint is written once, so that it is clear that the state database stores valid reads of blocks 0 ⁇ xx Write set.
  • the operation of writing the checkpoint is separated from the operation of writing the state database.
  • the transaction node sets up a separate coroutine (third coroutine) for writing checkpoints, which can be understood as a new process of the transaction node.
  • the checkpoint coroutine writes checkpoints for the target block based on the block number and preset analysis rules, including:
  • the checkpoint coroutine After receiving the request, the checkpoint coroutine obtains the block number of the target block, and determines whether the number is the expected number;
  • step a3 If not, write the request into the waiting queue in the order of numbers, and determine whether the expected number exists in the waiting queue; if so, perform step a2 to write a checkpoint for the block corresponding to the expected number, if not, Go to step a1.
  • a write checkpoint request carrying the block number is sent to the checkpoint coroutine.
  • the checkpoint coroutine only receives the sequential number (ie the expected number), Will actually write the checkpoint information. Since each block is written into the state database concurrently in this application, there may be discontinuous numbers. When there are discontinuous numbers, wait for the expected number before writing. Every time a checkpoint is written, the previous checkpoint will be overwritten, and only the latest value will be retained.
  • the effective read and write sets of block 0, block 1, and block 5 are all written into the state database, while the effective read and write sets of block 2, block 3, and block 4 have not been written into the state database. Therefore, when writing a checkpoint for block 5, it is necessary to ensure that block 2, block 3, and block 4 have been written into the state database. Therefore, the request numbered 5 is written into the waiting queue, and the number is to be numbered. After writing the checkpoint for the request of 4, write the checkpoint for the request of No. 5.
  • the expected number is the number of the last checkpoint written plus one. For example, the last time the checkpoint of the request with number 1 is written, the expected number at this time is 1+1.
  • the checkpoint is written for block 2. If the received None of the requests are 2, write these requests into the waiting queue in order of numbers, and write checkpoints for the blocks corresponding to the expected numbers until the expected numbers appear. Even if the peer is down at this time, the data in block 2 and beyond can be restored correctly.
  • the writing method of the state database proposed in the above embodiment writes the effective read-write set of each block into the state database through the background concurrently, and after the state database is written, the blocks corresponding to the expected numbers are sequentially listed in the order of the block numbers. Write checkpoints to avoid the situation that the peer cannot recover data correctly after the peer is down, and improve the efficiency and accuracy of writing the state database.
  • FIG. 2 is a schematic diagram of a preferred embodiment of the electronic device 1 of this application.
  • the electronic device 1 is an electronic device corresponding to a transaction node in the blockchain network.
  • the electronic device 1 may be a terminal device with data processing functions such as a server, a smart phone, a tablet computer, a portable computer, a desktop computer, etc.
  • the server may be a rack server, a blade server, a tower server or a cabinet server .
  • the electronic device 1 includes a memory 11, a processor 12 and a network interface 13.
  • the memory 11 includes at least one type of readable storage medium, and the readable storage medium includes flash memory, hard disk, multimedia card, card-type memory (for example, SD or DX memory, etc.), magnetic memory, magnetic disk, optical disk, and the like.
  • the memory 11 may be an internal storage unit of the electronic device 1 in some embodiments, such as a hard disk of the electronic device 1.
  • the memory 11 may also be an external storage device of the electronic device 1, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), and a secure digital (Secure Digital) equipped on the electronic device 1. , SD) card, flash card (Flash Card), etc.
  • the memory 11 may also include both an internal storage unit of the electronic device 1 and an external storage device.
  • the memory 11 can be used not only to store application software and various data installed in the electronic device 1, for example, the write program 10 of the state database, etc., but also to temporarily store data that has been output or will be output.
  • the processor 12 may be a central processing unit (Central Processing Unit) in some embodiments.
  • Central Processing Unit CPU
  • controller a controller
  • microcontroller a microprocessor
  • other data processing chips are used to run the program code or processing data stored in the memory 11, for example, the write program 10 of the state database.
  • the network interface 13 may optionally include a standard wired interface and a wireless interface (such as a WI-FI interface).
  • the components 11-13 of the electronic device 1 communicate with each other via a communication bus.
  • FIG. 2 only shows the electronic device 1 with the components 11-13. Those skilled in the art can understand that the structure shown in FIG. 2 does not constitute a limitation on the electronic device 1, and may include fewer or more components than shown in the figure. Multiple components, or a combination of certain components, or different component arrangements.
  • the electronic device 1 may further include a user interface.
  • the user interface may include a display (Display) and an input unit such as a keyboard (Keyboard).
  • the optional user interface may also include a standard wired interface and a wireless interface.
  • the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, an organic light-emitting diode (Organic Light-Emitting Diode, OLED) touch device, etc.
  • the display may also be called a display screen or a display unit, which is used to display the information processed in the electronic device 1 and to display a visualized user interface.
  • FIG. 3 this is a schematic diagram of modules of a preferred embodiment of an apparatus 100 for writing a state database according to this application.
  • the apparatus 100 for writing a state database may only include modules 110-130.
  • the receiving module 110 is configured to obtain the block information of the several blocks after receiving the simulation results of the several blocks sent by the ordering node;
  • the receiving module 110 After receiving the simulation result (proposal result) sent by the ordering node, the receiving module 110 obtains the block information of each block in the order of the block number of each block.
  • the block information of each block includes: transaction information of multiple transactions, whether the transaction is valid, the read-write set corresponding to the transaction, the smart contract and the block chain corresponding to each transaction.
  • one chain corresponds to a state database, all blocks correspond to the same cache, and the data in the cache is stored in the form of a map.
  • the number of each block is predetermined and stored, and each time a new block is added, a new number is generated based on the maximum value of the existing number as the number of the newly added block.
  • the existing blocks are numbered according to business requirements, and blocks 1, 2, 3, ..., n are obtained.
  • the order of each number is the order in which the block information of each block is obtained.
  • the number of the new block is n+1, and so on. .
  • the first write module 120 is configured to read the read and write set of the target block from the block information of the several blocks in sequence, and determine the read of the target block based on the data in the preset cache and the state database Whether the transaction data in the write set is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the valid blocks in the blockchain network The read-write set of the transaction.
  • the process in the prior art needs to wait for each block to write the transaction result into the state database in order, that is, verifying the validity of the data will make the process of writing the state database very slow.
  • the judging whether the transaction data in the read-write set of the target block is valid based on the data in the cache and the state database includes:
  • Determine the reference block corresponding to the target block obtain the data corresponding to the reference block from the cache, and compare the read/write set of the target block with the reference block in the cache The data is compared, and if there is data corresponding to the read-write set of the target block, it is determined that the transaction data in the read-write set of the target block is valid; if there is no read-write set with the target block
  • Corresponding data the data corresponding to the reference block is obtained from the state database, and the read-write set of the target block is compared with the data corresponding to the reference block in the state database. The data corresponding to the read-write set of the target block determines that the transaction data in the read-write set of the target block is valid.
  • the reference block corresponding to the target block is determined based on the sequence of block numbers. Taking blocks 1, 2, and 3 as an example, block 1 has no reference block, the reference block of block 2 is block 1, and the reference block of block 3 is block 1 and block 2. For example, after the transaction node has checked the read-write set in block 1, it writes the read-write set corresponding to the valid transaction data into the cache. When block 1 is or has not yet begun to write the state database, block 2 first Compare the read and write set in the block information with the data in block 1 in the cache. If there is no data in block 1 in the cache, compare it with the latest data in the state database, so that block 2 can be correctly judged Whether the transaction data in the read-write set is valid. In other words, to check the read and write set of a target block, not only compare the data in the state database, but also compare the data in the cache.
  • the order in which the first write module 120 writes the read and write sets in the block information of each block into the cache is serialized according to the block number. For example, if the order of block 1, 2, 3,..., n is block 1, block 1, block 3,..., block n, then write the cache of block 1 first, and then write the cache of block 2. Caching,..., and so on.
  • the second write module 130 is configured to read the target area from the cache in a background execution manner after the first write module 110 writes the read and write set corresponding to the valid transaction data of the target block into the cache Data corresponding to the block, and write the data corresponding to the target block into the state database.
  • the operation of writing the data corresponding to the target block into the state database by the second writing module 130 is performed concurrently, so as to make full use of the performance of the computer.
  • Each block has its own block number. Associate the block number with the read-write set of valid transactions in the block.
  • the block number is used to index the data corresponding to each target block from the cache, that is, the read-write set corresponding to the valid transaction data in each target block.
  • the read and write set corresponding to the valid transaction data of part of the target block can be written into the cache, and the operation of writing the state database can also be started after the valid transaction data of all target blocks is read and written. After the set is written into the cache, the operation of writing the state database is started. Therefore, it can be understood that the write cache and the write state database can be performed at the same time after the cache of the first target block is written.
  • the first target block is started in the background.
  • the block performs write state database operations. For example, after the read-write set corresponding to the valid transaction data of block 1 is written into the cache, it is determined whether the transaction data of the read-write set of block 2 is valid, and at the same time, the operation of writing the state database to block 1 is started in the background.
  • the electronic device 1 proposed in the above embodiment adds a cache to write the read and write set of valid blocks into the cache, and subsequent blocks can obtain relevant data from the cache for verification during the process of verifying data. , The next block can be processed without waiting for the previous block to finish writing the state database. Since the write cache speed is much faster than the write state database, the efficiency of subsequent block data verification can be improved; at the same time, by writing The cache is separated from the write state database, and the process of writing the state database is executed in the background, making full use of the computer performance and speeding up the processing progress of the writing state database.
  • the effective read and write set of each block stored in the cache will occupy a certain amount of memory.
  • the second write module 130 also It is used to release the data corresponding to the target block from the cache after the data corresponding to the target block is written into the state database.
  • the data in the cache is no longer needed, so data that is no longer needed in the cache can be deleted to free up storage space.
  • the current steps for writing a checkpoint are: input a block, check the block data, write the valid read and write set of the block into the state database, write the checkpoint, and then repeat the same steps for the next block.
  • the checkpoint function at this time will become invalid.
  • the state database of block 1, block 2, and block 3 is written concurrently, if the state database of block 2 is written and the checkpoint is written to 2, then the peer hangs down and restarts the peer When the checkpoint is 2, the program will start to restore from block 2, and restore the data in block 3 and beyond. It is impossible to find that the data of block 1 has not been written into the state database, which will cause the data in the filling database to be lost, resulting in inconsistencies with the data in the state database of other nodes.
  • the apparatus 100 for writing a state database further includes:
  • the third write module 140 is configured to receive a write checkpoint request sent by the second write module 130, where the request includes a block number corresponding to the target block, and is based on the block number and a preset analysis rule as the The target block write checkpoint.
  • the operation of writing the checkpoint is separated from the operation of writing the state database.
  • To write a checkpoint is to write the block number corresponding to the checkpoint into the state database. Every time the second writing module 130 finishes writing a block, the third writing module 140 writes the checkpoint once, so that it is clear that the state is stored in the database. It is the effective read and write set of blocks 0 ⁇ xx.
  • writing a checkpoint for the target block based on the block number and a preset analysis rule includes:
  • the checkpoint coroutine After receiving the request, the checkpoint coroutine obtains the block number of the target block, and determines whether the number is the expected number;
  • step a3 If not, write the request into the waiting queue in the order of numbers, and determine whether the expected number exists in the waiting queue; if so, perform step a2 to write a checkpoint for the block corresponding to the expected number, if not, Go to step a1.
  • the second writing module 130 After the second writing module 130 writes the effective read-write set of the target block into the state database, it sends a write checkpoint request carrying the block number to the third writing module 140, and the third writing module 140 only receives the sequential number (That is, the expected number), the checkpoint information is actually written. Since each block is written into the state database concurrently in this application, there may be discontinuous numbers. When there are discontinuous numbers, wait for the expected number before writing. Every time a checkpoint is written, the previous checkpoint will be overwritten, and only the latest value will be retained.
  • the effective read and write sets of block 0, block 1, and block 5 are all written into the state database, while the effective read and write sets of block 2, block 3, and block 4 have not been written into the state database. Therefore, when writing a checkpoint for block 5, it is necessary to ensure that block 2, block 3, and block 4 have been written into the state database. Therefore, the request numbered 5 is written into the waiting queue, and the number is to be numbered. After writing the checkpoint for the request of 4, write the checkpoint for the request of No. 5.
  • the expected number is the number of the last checkpoint written plus one. For example, the last time the checkpoint of the request with number 1 is written, the expected number at this time is 1+1.
  • the checkpoint is written for block 2. If the received None of the requests are 2, write these requests into the waiting queue in order of numbers, and write checkpoints for the blocks corresponding to the expected numbers until the expected numbers appear. Even if the peer is down at this time, the data in block 2 and beyond can be restored correctly.
  • the electronic device 1 proposed in the above embodiment concurrently writes the effective read-write set of each block into the state database through the background, and after writing the state database, write checkpoints for the blocks corresponding to the expected numbers in the order of the block numbers , To avoid the situation that the data cannot be recovered correctly after the peer is down, and to improve the efficiency and accuracy of writing the state database.
  • the embodiment of the present application also proposes a computer-readable storage medium.
  • the computer-readable storage medium may be volatile or non-volatile.
  • the computer-readable storage medium includes the writing of the state database. Program 10, when the state database writing program 10 is executed by the processor, the following operations are implemented:
  • the first writing step read the read-write set of the target block sequentially from the block information of the several blocks, and determine the read-write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Write set
  • the second writing step after the read-write set corresponding to the valid transaction data of the target block is written into the cache, the data corresponding to the target block is read from the cache in a background execution manner, and the data corresponding to the target block is read The data corresponding to the target block is written into the state database;
  • the third writing step receiving a request for writing a checkpoint, the request including a block number corresponding to the target block, and writing a checkpoint for the target block based on the block number and a preset analysis rule.

Abstract

A method and apparatus for writing into a state database, an electronic device, and a storage medium, which relate to blockchain technology. The method comprises: after receiving simulation results for multiple blocks that are sent by a sort node, acquiring block information of the multiple blocks; sequentially reading read and write sets of a target block, determining, on the basis of data in a cache and a state database, whether transaction data in the read and write sets of the target block is valid, and writing the read and write set corresponding to valid transaction data of the target block into the cache; and using a means of background execution to read from the cache the data corresponding to the target block and writing same into the state database. Thus, the efficiency and accuracy of writing into a state database are increased.

Description

状态数据库的写入方法、装置、电子设备及存储介质State database writing method, device, electronic equipment and storage medium
本申请要求于2020年1月9日提交中国专利局、申请号为CN202010024089.0、发明名称为“状态数据库的写入方法、数据处理装置及存储介质”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。This application claims the priority of the Chinese patent application filed with the Chinese Patent Office on January 9, 2020, the application number is CN202010024089.0, and the invention title is "state database writing method, data processing device and storage medium", all of which The content is incorporated in this application by reference.
技术领域Technical field
本申请涉及区块链技术领域,尤其涉及一种状态数据库的写入方法、装置、电子设备及存储介质。This application relates to the field of blockchain technology, and in particular to a method, device, electronic device, and storage medium for writing a state database.
背景技术Background technique
区块链完成交易打包,即完成共识出块后,产生了新的区块。随后需要将区块中的事务预执行得到预执行结果,以完成事务并更新数据库,即落账。区块链属于链式数据结构,每个区块指向上一个区块,因此,验证数据的有效性就需要顺序地对每个区块进行校验。为了保证交易中资产落账一致,区块中的事务要按照排序依次预执行,即同账户相关的交易需要保证顺序性。也就是说,对于一个新加入区块链网络的节点来说,需要先同步所有的历史区块数据并且进行逐个的有效性校验,才能开始同步最新的区块。The block chain completes the transaction packaging, that is, after the consensus is completed, a new block is generated. Afterwards, it is necessary to pre-execute the transaction in the block to obtain the pre-execution result to complete the transaction and update the database, that is, to settle the account. The blockchain belongs to a chained data structure, and each block points to the previous block. Therefore, to verify the validity of the data, each block needs to be verified sequentially. In order to ensure that the assets in the transaction are consistent, the transactions in the block must be pre-executed in order, that is, the transactions related to the account need to be sequenced. In other words, for a node that newly joins the blockchain network, it needs to synchronize all historical block data and verify the validity one by one before starting to synchronize the latest block.
技术问题technical problem
发明人意识到历史区块数据往往很大,因此,该同步过程需耗费很长时间,导致时间和机器性能的浪费。The inventor realizes that historical block data is often very large, and therefore, the synchronization process takes a long time, resulting in a waste of time and machine performance.
技术解决方案Technical solutions
本申请提供一种状态数据库的写入方法、装置、电子设备及计算机可读存储介质。This application provides a method, device, electronic device, and computer-readable storage medium for writing a state database.
本申请提供一种状态数据库的写入方法,所述方法包括:This application provides a method for writing a state database, and the method includes:
接收步骤,接收排序节点发送的若干个区块的模拟结果后,获取所述若干个区块的区块信息;In the receiving step, after receiving the simulation results of several blocks sent by the ordering node, obtain the block information of the several blocks;
第一写步骤,依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集;及The first writing step is to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Writing set; and
第二写步骤,以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中。The second writing step is to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
本申请还提供一种状态数据库的写入装置,所述装置包括:This application also provides a device for writing a state database, the device including:
接收模块,用于接收排序节点发送的若干个区块的模拟结果后,获取所述若干个区块的区块信息;The receiving module is used to obtain the block information of the several blocks after receiving the simulation results of the several blocks sent by the ordering node;
第一写模块,用于依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集;及The first write module is used to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write of the target block based on the data in the preset cache and the state database Whether the centralized transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache valid transactions in each block in the blockchain network Read and write set; and
第二写模块,用于以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中。The second writing module is configured to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
本申请还提供一种电子设备,该电子设备包括:存储器、处理器,所述存储器上存储有可在所述处理器上运行的状态数据库的写入程序,所述状态数据库的写入程序被所述处理器执行时实现如下步骤:The present application also provides an electronic device, the electronic device comprising: a memory, a processor, the memory stores a state database write program that can be run on the processor, and the state database write program is When the processor executes, the following steps are implemented:
接收步骤,接收排序节点发送的若干个区块的模拟结果后,获取所述若干个区块的区块信息;In the receiving step, after receiving the simulation results of several blocks sent by the ordering node, obtain the block information of the several blocks;
第一写步骤,依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集;及The first writing step is to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Writing set; and
第二写步骤,以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中。The second writing step is to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
本申请还提供一种计算机可读存储介质,所述计算机可读存储介质中包括状态数据库的写入程序,所述状态数据库的写入程序被处理器执行时实现如下步骤:The present application also provides a computer-readable storage medium that includes a state database writing program, and the state database writing program is executed by a processor to implement the following steps:
接收步骤,接收排序节点发送的若干个区块的模拟结果后,获取所述若干个区块的区块信息;In the receiving step, after receiving the simulation results of several blocks sent by the ordering node, obtain the block information of the several blocks;
第一写步骤,依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集;及The first writing step is to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Writing set; and
第二写步骤,以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中。The second writing step is to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
附图说明Description of the drawings
图1为本申请状态数据库的写入方法较佳实施例的流程图;FIG. 1 is a flowchart of a preferred embodiment of a method for writing into a state database of this application;
图2为本申请电子设备较佳实施例的示意图;FIG. 2 is a schematic diagram of a preferred embodiment of the electronic device of this application;
图3为本申请状态数据库的写入装置的模块示意图。Fig. 3 is a schematic diagram of modules of the writing device of the state database of the present application.
本申请目的的实现、功能特点及优点将结合实施例,参照附图做进一步说明。The realization, functional characteristics, and advantages of the purpose of this application will be further described in conjunction with the embodiments and with reference to the accompanying drawings.
本发明的实施方式Embodiments of the present invention
应当理解,此处所描述的具体实施例仅仅用以解释本申请,并不用于限定本申请。It should be understood that the specific embodiments described here are only used to explain the present application, and are not used to limit the present application.
本申请提供一种状态数据库的写入方法。参照图1所示,为本申请状态数据库的写入方法较佳实施例的流程图。该方法可以由一个电子设备执行,该电子设备可以由软件和/或硬件实现。This application provides a method for writing a state database. Referring to FIG. 1, it is a flowchart of a preferred embodiment of a method for writing a state database according to this application. The method can be executed by an electronic device, and the electronic device can be implemented by software and/or hardware.
在本实施例中,所述状态数据库的写入方法仅包括步骤S10-S30。In this embodiment, the method for writing the state database only includes steps S10-S30.
一种状态数据库的写入方法,应用于区块链网络中的交易节点,该方法包括:A method for writing a state database is applied to transaction nodes in a blockchain network, and the method includes:
步骤S10,接收排序节点发送的若干个区块的模拟结果后,获取所述若干个区块的区块信息。Step S10, after receiving the simulation results of several blocks sent by the ordering node, obtain block information of the several blocks.
在以下实施例中,以交易节点对应的电子设备为执行主体对本申请各实施例进行说明。所述区块链网络包括交易节点、提交节点、共识节点。基于区块链网络的交易过程大致如下:In the following embodiments, the electronic device corresponding to the transaction node is used as the execution subject to describe the embodiments of the present application. The blockchain network includes transaction nodes, submission nodes, and consensus nodes. The transaction process based on the blockchain network is roughly as follows:
1. 客户端创建交易提案并发送到交易节点(智能合约(chaindode)对应的背书节点,即智能合约指定的可以验证该交易的peer节点);1. The client creates a transaction proposal and sends it to the transaction node (the endorsement node corresponding to the smart contract (chaindode), that is, the peer node designated by the smart contract that can verify the transaction);
2. 交易节点执行智能合约,基于读取和写入的key生成读写操作集(fabric读写集);2. The transaction node executes the smart contract and generates a read and write operation set (fabric read and write set) based on the read and write keys;
3. 交易节点向客户端返回提案结果(交易节点执行智能合约的时候产生的模拟结果);3. The transaction node returns the proposal result (the simulation result generated when the transaction node executes the smart contract) to the client;
4. 客户端将交易数据提交到共识节点(Orderer),交易数据包括来自提案的读写集;4. The client submits the transaction data to the consensus node (Orderer), and the transaction data includes the read-write set from the proposal;
5. 共识节点将排序完的交易打包进区块中;5. The consensus node packs the sorted transaction into the block;
6. 区块将提案结果发送到提交节点(若提案结果为成功,将提案结果发给排序节点,排序节点会将这个交易广播给其他的交易节点);6. The block sends the proposal result to the submitting node (if the proposal result is successful, the proposal result is sent to the sorting node, and the sorting node will broadcast the transaction to other transaction nodes);
7. 提交节点进行如下操作:7. Submit the node to perform the following operations:
1)MVCC多版本控制(Multi-Version Concurrency Control 多版本并发控制),读取状态数据库(state db)中最新的值,验证交易的有效性;1) MVCC multi-version control (Multi-Version Concurrency Control), read the latest value in the state database (state db), and verify the validity of the transaction;
2)在文件系统上将新的区块写入区块链中(交易的数据打包成区块,区块写进区块链),将区块中有效的交易写入状态数据库中,写入数据库检查点。2) Write the new block to the block chain on the file system (the transaction data is packaged into blocks, and the block is written into the block chain), and the valid transactions in the block are written into the state database, and write Database checkpoint.
在第7步骤中,每个区块依次执行1、2步骤,性能比较低。In the seventh step, each block executes steps 1 and 2 in sequence, and the performance is relatively low.
例如,现在账本里面A=100元,交易P消费90元,模拟结果为A=10,该结果还未提交至排序节点时,交易Q消费20元,模拟结果为A=80,此时,将两个交易的模拟结果都提交至排序节点,执行MVCC时会发现交易P、Q都是基于A=100这个前提,因此会判定P、Q中的一个交易失败(其中,先提交至排序节点的交易成功,后提交的被设置为无效)。For example, now in the ledger, A=100 yuan, transaction P consumes 90 yuan, and the simulation result is A=10. When the result has not been submitted to the ordering node, transaction Q consumes 20 yuan, and the simulation result is A=80. At this time, The simulation results of the two transactions are submitted to the sorting node. When the MVCC is executed, it will be found that the transactions P and Q are based on the premise of A=100. Therefore, it will be determined that one of the transactions of P and Q has failed (among which, the first submitted to the sorting node The transaction is successful, and the submitted one is set as invalid).
改进前的步骤如下:输入一个区块,交易节点读取区块里面的读写集,将读写集里面的数据跟状态数据库里面最新的数据进行对比,判断读写集里面哪些数据是有效的,哪些是无效的。具体地,交易节点从排序节点获取交易信息,并去验证交易是否有效,通过获取读写集里面的值,跟本地的状态数据库进行对比,从而判断交易是否有效,这个过程称之为MVCC。然后将区块信息写入账本。然后,将上一步校验有效交易的读写集信息更新到状态数据库中。该区块的信息写完之后,写入检查点信息。然后,对下一个区块重复以上步骤。然而,以上步骤的问题在于,写状态数据库的过程是顺序的,且写状态数据库是一个IO密集型操作,若需要单个区块按顺序去写状态数据库,对性能的影响比较大。The steps before improvement are as follows: input a block, the transaction node reads the read-write set in the block, compares the data in the read-write set with the latest data in the state database, and judges which data in the read-write set is valid , Which are invalid. Specifically, the transaction node obtains transaction information from the ordering node and verifies whether the transaction is valid. By obtaining the value in the read-write set, compare it with the local state database to determine whether the transaction is valid. This process is called MVCC. Then write the block information into the ledger. Then, update the read-write set information of valid transactions in the previous step to the state database. After the information of the block is written, write the checkpoint information. Then, repeat the above steps for the next block. However, the problem with the above steps is that the process of writing the state database is sequential, and writing the state database is an IO-intensive operation. If a single block is required to write the state database in order, the performance impact will be relatively large.
当交易节点接收到排序节点发送的模拟结果(提案结果)后,按照各区块的区块编号的顺序分别获取各区块的区块信息。其中,各区块的区块信息包括:多个交易的交易信息、交易是否有效、交易对应的读写集、各交易对应的智能合约及区块链条。区块链网络中一条链对应一个状态数据库,所有区块对应同一个缓存,缓存中的数据以map形式保存。After the transaction node receives the simulation result (proposal result) sent by the ordering node, it obtains the block information of each block in the order of the block number of each block. Among them, the block information of each block includes: transaction information of multiple transactions, whether the transaction is valid, the read-write set corresponding to the transaction, the smart contract and the block chain corresponding to each transaction. In the blockchain network, one chain corresponds to a state database, all blocks correspond to the same cache, and the data in the cache is stored in the form of a map.
需要说明的是,在步骤S10之前,该方法还包括:预先确定并保存各区块的编号;每新增一个区块,在已有编号的最大值的基础上生成一个新的编号作为所述新增区块的编号。例如,按照业务需求为已有区块进行编号,得到区块1、2、3、…、n,各编号排序就是获取各区块的区块信息的顺序。在产生新的区块的过程中,新区块的编号即为n+1,依此类推。It should be noted that, before step S10, the method further includes: predetermining and saving the number of each block; for each new block, a new number is generated based on the maximum value of the existing number as the new number. Increase the number of the block. For example, the existing blocks are numbered according to business requirements, and blocks 1, 2, 3, ..., n are obtained. The order of each number is the order in which the block information of each block is obtained. In the process of generating a new block, the number of the new block is n+1, and so on.
步骤S20,依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集。Step S20: Read the read-write set of the target block from the block information of the several blocks in sequence, and determine the transaction data in the read-write set of the target block based on the data in the preset cache and the state database Whether it is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the read-write set of valid transactions in each block in the blockchain network .
现有技术中的流程是:如果按顺序有3个区块1、2、3。校验区块2中的交易是否有效,需要依赖区块1的结果,校验区块3中的交易是否有效,需要依赖区块1、区块2的结果,每次校验完一个区块之后,就会把结果写进状态数据库之中。可以理解为,上一次写完的状态数据库里面,是包含区块链里面最新的数据,处理新的区块的时候,去状态数据库查询即可。这个流程的问题在于,需要按顺序等每个区块把交易结果写进状态数据库中,也就是说,校验数据有效性会使得写状态数据库这一过程会非常慢。The process in the prior art is: if there are 3 blocks 1, 2, 3 in sequence. To verify whether the transaction in block 2 is valid, you need to rely on the result of block 1, and to verify whether the transaction in block 3 is valid, you need to rely on the results of block 1 and block 2, and verify one block at a time After that, the results are written into the state database. It can be understood that the state database that was written last time contains the latest data in the blockchain. When processing a new block, you can go to the state database to query. The problem with this process is that it needs to wait for each block in order to write the transaction results into the state database, that is, verifying the validity of the data will make the process of writing the state database very slow.
为了提高数据校验效率,新增一个用于保存所述区块链网络中各区块中有效交易的读写集的缓存(cache)。在本实施例中,所述基于所述缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,包括:In order to improve the efficiency of data verification, a new cache is added for storing the read-write set of valid transactions in each block in the blockchain network. In this embodiment, the judging whether the transaction data in the read-write set of the target block is valid based on the data in the cache and the state database includes:
确定所述目标区块对应的参考区块,从所述缓存中获取所述参考区块对应的数据,将所述目标区块的读写集与所述缓存中的所述参考区块对应的数据进行比对,若存在与所述目标区块的读写集对应的数据,则确定所述目标区块的读写集中的交易数据有效;若不存在与所述目标区块的读写集对应的数据,则从状态数据库中获取所述参考区块对应的数据,将所述目标区块的读写集与状态数据库中的所述参考区块对应的数据进行比对,若存在与所述目标区块的读写集对应的数据,则确定所述目标区块的读写集中的交易数据有效。Determine the reference block corresponding to the target block, obtain the data corresponding to the reference block from the cache, and compare the read/write set of the target block with the reference block in the cache The data is compared, and if there is data corresponding to the read-write set of the target block, it is determined that the transaction data in the read-write set of the target block is valid; if there is no read-write set with the target block Corresponding data, the data corresponding to the reference block is obtained from the state database, and the read-write set of the target block is compared with the data corresponding to the reference block in the state database. The data corresponding to the read-write set of the target block determines that the transaction data in the read-write set of the target block is valid.
需要说明的是,目标区块对应的参考区块基于区块编号排序确定。以区块1、2、3为例,区块1没有参考区块,区块2的参考区块为区块1,区块3的参考区块为区块1、区块2。例如,当交易节点检查完区块1中的读写集之后,将有效交易数据对应的读写集写入缓存中,在区块1正在抑或还没开始写状态数据库的时候,区块2先将区块信息中的读写集与缓存中区块1的数据进行对比,如果缓存中中没有区块1的数据,就与状态数据库中最新的数据进行对比,从而能正确判断出区块2的读写集中的交易数据是否有效。也就是说,检查一个目标区块的读写集,不单单对比状态数据库的数据,还要对比缓存中的数据。It should be noted that the reference block corresponding to the target block is determined based on the sequence of block numbers. Taking blocks 1, 2, and 3 as an example, block 1 has no reference block, the reference block of block 2 is block 1, and the reference block of block 3 is block 1 and block 2. For example, after the transaction node has checked the read-write set in block 1, it writes the read-write set corresponding to the valid transaction data into the cache. When block 1 is or has not yet begun to write the state database, block 2 first Compare the read and write set in the block information with the data in block 1 in the cache. If there is no data in block 1 in the cache, compare it with the latest data in the state database, so that block 2 can be correctly judged Whether the transaction data in the read-write set is valid. In other words, to check the read and write set of a target block, not only compare the data in the state database, but also compare the data in the cache.
在本实施例中,将各区块的区块信息中的读写集写入缓存的顺序是按照区块编号排序串行的。例如,区块1、2、3、…、n的顺序为区块1、区块1、区块3、…、区块n,那么,先写区块1的缓存,然后写区块2的缓存,…,依此类推。In this embodiment, the order of writing the read-write set in the block information of each block into the cache is serialized according to the block number. For example, if the order of block 1, 2, 3,..., n is block 1, block 1, block 3,..., block n, then write the cache of block 1 first, and then write the cache of block 2. Caching,..., and so on.
步骤S30,以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中。In step S30, the data corresponding to the target block is read from the cache in a background execution manner, and the data corresponding to the target block is written into the state database.
在本实施例中,将目标区块对应的数据写入状态数据库的操作时并发执行的,以充分利用计算机的性能。In this embodiment, the operation of writing the data corresponding to the target block into the state database is executed concurrently, so as to make full use of the performance of the computer.
每个区块都有自己的区块编号。将区块编号跟区块里面有效交易的读写集关联起来。利用区块编号从缓存中索引得到各目标区块对应的数据,即各目标区块中的有效交易数据对应的读写集。Each block has its own block number. Associate the block number with the read-write set of valid transactions in the block. The block number is used to index the data corresponding to each target block from the cache, that is, the read-write set corresponding to the valid transaction data in each target block.
在本实施例中,可以在将部分目标区块的有效交易数据对应的读写集写入缓存后开始进行写状态数据库的操作,也可以在将所有目标区块的有效交易数据对应的读写集写入缓存后开始进行写状态数据库的操作。因此,可以理解的是,步骤S20及步骤S30在写完第一个目标区块的缓存后可同时进行。In this embodiment, the read and write set corresponding to the valid transaction data of part of the target block can be written into the cache, and the operation of writing the state database can also be started after the valid transaction data of all target blocks is read and written. After the set is written into the cache, the operation of writing the state database is started. Therefore, it can be understood that step S20 and step S30 can be performed at the same time after writing the cache of the first target block.
为了充分利用计算机性能、提高写状态数据库的效率,在本实施例中,将第一个目标区块的有效交易数据对应的读写集写入缓存后,则开始在后台对第一个目标区块进行写状态数据库操作。例如,在将区块1的有效交易数据对应的读写集写入缓存后,判断区块2的读写集中交易数据是否有效的同时,在后台开始对区块1执行写状态数据库的操作。In order to make full use of computer performance and improve the efficiency of writing the state database, in this embodiment, after the read-write set corresponding to the valid transaction data of the first target block is written into the cache, the first target block is started in the background. The block performs write state database operations. For example, after the read-write set corresponding to the valid transaction data of block 1 is written into the cache, it is determined whether the transaction data of the read-write set of block 2 is valid, and at the same time, the operation of writing the state database to block 1 is started in the background.
需要说明的是,写缓存及写状态数据库均有交易节点执行,区别在于,写缓存及写状态数据库由交易节点的不同协程完成,例如,写缓存由交易节点创建的第一协程完成,写状态数据库由交易节点创建的第二协程完成,以实现后台写状态数据库这一操作。It should be noted that both the write cache and the write state database are executed by the transaction node. The difference is that the write cache and the write state database are completed by different coroutines of the transaction node. For example, the write cache is completed by the first coroutine created by the transaction node. The writing of the state database is completed by the second coroutine created by the transaction node to realize the operation of writing the state database in the background.
上述实施例提出的状态数据库的写入方法,通过新增一个缓存,将校验有效的区块的读写集写入缓存中,后续区块在校验数据过程中可从缓存中获取相关数据进行校验,无需等上一个区块完成写状态数据库即可处理下一个区块,鉴于写缓存的速度远远大于写状态数据库的速度,因此可提高后续区块数据校验的效率;同时,通过将写缓存与写状态数据库分离,并后台执行写状态数据库的过程,充分利用计算机性能,加快了写状态数据库的处理进度。In the method for writing the state database proposed in the above embodiment, by adding a new cache, the read and write set of valid blocks are written into the cache, and subsequent blocks can obtain relevant data from the cache during the process of verifying data. For verification, the next block can be processed without waiting for the previous block to complete writing to the state database. Since the write cache speed is much faster than the write state database, the efficiency of subsequent block data verification can be improved; at the same time, By separating the write cache from the write state database, and execute the process of writing the state database in the background, the computer performance is fully utilized and the processing progress of the write state database is accelerated.
鉴于区块数量较多,因此缓存中存储的各区块的有效读写集会占用一定的内存,为了充分利用存储空间,提高数据处理效率,在其他实施例中,所述步骤S30还包括:In view of the large number of blocks, the effective read and write set of each block stored in the cache will occupy a certain amount of memory. In order to make full use of the storage space and improve data processing efficiency, in other embodiments, the step S30 further includes:
当将所述目标区块对应的数据写入状态数据库后,从所述缓存中释放所述目标区块对应的数据。After the data corresponding to the target block is written into the state database, the data corresponding to the target block is released from the cache.
当一个区块的有效读写集写进状态数据库之后,就不需要缓存中的这份数据了,因此可以把缓存中不再需要的数据删除,以释放存储空间。After the effective read-write set of a block is written into the state database, the data in the cache is no longer needed, so data that is no longer needed in the cache can be deleted to free up storage space.
为了保证状态数据库中的数据与区块链中的数据一致,需要防止在写状态数据库的过程中有peer节点中途宕机的情况,因此,在将区块的读写集写入状态数据库后,需写入检查点,以在恢复写状态数据库时确定未写完的区块。In order to ensure that the data in the state database is consistent with the data in the blockchain, it is necessary to prevent the peer node from going down during the process of writing the state database. Therefore, after writing the block's read-write set to the state database, A checkpoint needs to be written to determine the uncompleted blocks when the write state database is restored.
然而,目前写检查点的步骤为:输入一个区块,检查区块数据,将区块的有效读写集写入状态数据库中,写检查点,然后对下一个区块重复执行相同的步骤。需要说明的是,区块并发的将各自的读写集写入状态数据库后,如果写检查点操作还是紧跟着写state数据库之后,此时的检查点的功能会失效。例如,由于并发写区块1、区块2、区块3的状态数据库,假如区块2的状态数据库写完之后,写入了检查点为2,此时peer挂掉宕机,重新启动peer时,由于检查点是2,程序就会从区块2开始恢复,恢复区块3及其以后的数据。无法发现实际上区块1的数据还没写入状态数据库,会导致装填数据库中的数据出现丢失,导致与其他节点的状态数据库的数据不一致的情况。However, the current steps for writing a checkpoint are: input a block, check the block data, write the valid read and write set of the block into the state database, write the checkpoint, and then repeat the same steps for the next block. It should be noted that after the blocks concurrently write their respective read and write sets to the state database, if the write checkpoint operation still follows the write state database, the checkpoint function at this time will become invalid. For example, because the state database of block 1, block 2, and block 3 is written concurrently, if the state database of block 2 is written and the checkpoint is written to 2, then the peer hangs down and restarts the peer When the checkpoint is 2, the program will start to restore from block 2, and restore the data in block 3 and beyond. It is impossible to find that the data of block 1 has not been written into the state database, which will cause the data in the filling database to be lost, resulting in inconsistencies with the data in the state database of other nodes.
因此,为了保证数据的一致性,在其他实施例中,所述状态数据库的写入方法还包括:Therefore, in order to ensure data consistency, in other embodiments, the writing method of the state database further includes:
步骤S40,当将所述目标区块对应的数据写入状态数据库后,向检查点协程发送写检查点请求,所述请求中包括目标区块对应的区块编号,并控制所述检查点协程基于所述区块编号及预设分析规则为所述目标区块写检查点。Step S40: After the data corresponding to the target block is written into the state database, a checkpoint write request is sent to the checkpoint coroutine, the request includes the block number corresponding to the target block, and the checkpoint is controlled The coroutine writes checkpoints for the target block based on the block number and preset analysis rules.
写检查点就是将检查点对应的区块编号写进状态数据库中,每写完一个区块,就写一次检查点,这样就清楚地知道状态数据库中保存着0~xx号区块的有效读写集。To write a checkpoint is to write the block number corresponding to the checkpoint into the state database. Each time a block is written, the checkpoint is written once, so that it is clear that the state database stores valid reads of blocks 0~xx Write set.
在本实施例中,写检查点的操作与写状态数据库的操作分离。交易节点为写检查点单独起个协程(第三协程),该协程可理解为交易节点新建的进程。In this embodiment, the operation of writing the checkpoint is separated from the operation of writing the state database. The transaction node sets up a separate coroutine (third coroutine) for writing checkpoints, which can be understood as a new process of the transaction node.
在本实施例中,所述检查点协程基于所述区块编号及预设分析规则为所述目标区块写检查点,包括:In this embodiment, the checkpoint coroutine writes checkpoints for the target block based on the block number and preset analysis rules, including:
a1.所述检查点协程接收到请求后,获取所述目标区块的区块编号,判断该编号是否为期望编号;a1. After receiving the request, the checkpoint coroutine obtains the block number of the target block, and determines whether the number is the expected number;
a2.若是,则为所述目标区块写检查点,并更新期望编号;a2. If yes, write a checkpoint for the target block and update the expected number;
a3.若否,则将该请求按照编号的顺序写入等待队列中,并判断等待队列中是否存在期望编号;若是,则执行步骤a2,为期望编号对应的区块写检查点,若否,执行步骤a1。a3. If not, write the request into the waiting queue in the order of numbers, and determine whether the expected number exists in the waiting queue; if so, perform step a2 to write a checkpoint for the block corresponding to the expected number, if not, Go to step a1.
当将目标区块的有效读写集写入状态数据库之后,向检查点协程发送一条携带区块编号的写检查点请求,检查点协程只有接收到顺序的编号(即期望编号)之后,才会真正写入检查点信息。由于本申请中是并发将各区块写入状态数据库,因此可能出现编号不连续的情况,当出现不连续的编号时,等待期望编号,再写入。每写完一个检查点会将上一个检查点覆盖,仅保留最新的值。After the effective read-write set of the target block is written into the state database, a write checkpoint request carrying the block number is sent to the checkpoint coroutine. The checkpoint coroutine only receives the sequential number (ie the expected number), Will actually write the checkpoint information. Since each block is written into the state database concurrently in this application, there may be discontinuous numbers. When there are discontinuous numbers, wait for the expected number before writing. Every time a checkpoint is written, the previous checkpoint will be overwritten, and only the latest value will be retained.
例如,区块0、区块1、区块5的有效读写集都写入状态数据库中,而区块2、区块3、区块4的有效读写集还未写入状态数据库中,因此,在为区块5写检查点时,需保证区块2、区块3、区块4都已经写入状态数据库中,故将编号为5的这一请求写入等待队列中,待编号为4的请求写完检查点后为编号为5的请求写检查点。For example, the effective read and write sets of block 0, block 1, and block 5 are all written into the state database, while the effective read and write sets of block 2, block 3, and block 4 have not been written into the state database. Therefore, when writing a checkpoint for block 5, it is necessary to ensure that block 2, block 3, and block 4 have been written into the state database. Therefore, the request numbered 5 is written into the waiting queue, and the number is to be numbered. After writing the checkpoint for the request of 4, write the checkpoint for the request of No. 5.
期望编号为上一次写完检查点的编号加1。例如,上一次写完编号为1的请求的检查点,则此时的期望编号为1+1,当收到区块编号为2的请求后,为区块2写检查点,若接收到的请求都不是2,将这些请求按编号顺序写入等待队列中,直到出现期望编号时再为期望编号对应的区块写检查点。即使此时peer宕机,也能正确去恢复区块2及之后的数据。The expected number is the number of the last checkpoint written plus one. For example, the last time the checkpoint of the request with number 1 is written, the expected number at this time is 1+1. When the request with block number 2 is received, the checkpoint is written for block 2. If the received None of the requests are 2, write these requests into the waiting queue in order of numbers, and write checkpoints for the blocks corresponding to the expected numbers until the expected numbers appear. Even if the peer is down at this time, the data in block 2 and beyond can be restored correctly.
上述实施例提出的状态数据库的写入方法,通过后台并发将各区块的有效读写集写入状态数据库中,并在写完状态数据库后,按照区块编号顺序依次为期望编号对应的区块写检查点,避免peer宕机后无法正确恢复数据的情况,提高写状态数据库的效率及准确性。The writing method of the state database proposed in the above embodiment writes the effective read-write set of each block into the state database through the background concurrently, and after the state database is written, the blocks corresponding to the expected numbers are sequentially listed in the order of the block numbers. Write checkpoints to avoid the situation that the peer cannot recover data correctly after the peer is down, and improve the efficiency and accuracy of writing the state database.
参照图2所示,为本申请电子设备1较佳实施例的示意图。Refer to FIG. 2, which is a schematic diagram of a preferred embodiment of the electronic device 1 of this application.
在本实施例中,电子设备1为区块链网络中交易节点对应的电子设备。电子设备1可以是服务器、智能手机、平板电脑、便携计算机、桌上型计算机等具有数据处理功能的终端设备,所述服务器可以是机架式服务器、刀片式服务器、塔式服务器或机柜式服务器。In this embodiment, the electronic device 1 is an electronic device corresponding to a transaction node in the blockchain network. The electronic device 1 may be a terminal device with data processing functions such as a server, a smart phone, a tablet computer, a portable computer, a desktop computer, etc. The server may be a rack server, a blade server, a tower server or a cabinet server .
该电子设备1包括存储器11、处理器12及网络接口13。The electronic device 1 includes a memory 11, a processor 12 and a network interface 13.
其中,存储器11至少包括一种类型的可读存储介质,所述可读存储介质包括闪存、硬盘、多媒体卡、卡型存储器(例如,SD或DX存储器等)、磁性存储器、磁盘、光盘等。存储器11在一些实施例中可以是所述电子设备1的内部存储单元,例如该电子设备1的硬盘。存储器11在另一些实施例中也可以是所述电子设备1的外部存储设备,例如该电子设备1上配备的插接式硬盘,智能存储卡(Smart Media Card,SMC),安全数字(Secure Digital,SD)卡,闪存卡(Flash Card)等。进一步地,存储器11还可以既包括该电子设备1的内部存储单元也包括外部存储设备。The memory 11 includes at least one type of readable storage medium, and the readable storage medium includes flash memory, hard disk, multimedia card, card-type memory (for example, SD or DX memory, etc.), magnetic memory, magnetic disk, optical disk, and the like. The memory 11 may be an internal storage unit of the electronic device 1 in some embodiments, such as a hard disk of the electronic device 1. In other embodiments, the memory 11 may also be an external storage device of the electronic device 1, such as a plug-in hard disk, a smart memory card (Smart Media Card, SMC), and a secure digital (Secure Digital) equipped on the electronic device 1. , SD) card, flash card (Flash Card), etc. Further, the memory 11 may also include both an internal storage unit of the electronic device 1 and an external storage device.
存储器11不仅可以用于存储安装于该电子设备1的应用软件及各类数据,例如,状态数据库的写入程序10等,还可以用于暂时地存储已经输出或者将要输出的数据。The memory 11 can be used not only to store application software and various data installed in the electronic device 1, for example, the write program 10 of the state database, etc., but also to temporarily store data that has been output or will be output.
处理器12在一些实施例中可以是一中央处理器(Central Processing Unit, CPU)、控制器、微控制器、微处理器或其他数据处理芯片,用于运行存储器11中存储的程序代码或处理数据,例如,状态数据库的写入程序10等。The processor 12 may be a central processing unit (Central Processing Unit) in some embodiments. Processing Unit (CPU), a controller, a microcontroller, a microprocessor, or other data processing chips are used to run the program code or processing data stored in the memory 11, for example, the write program 10 of the state database.
网络接口13可选的可以包括标准的有线接口、无线接口(如WI-FI接口)。电子设备1的组件11-13通过通信总线相互通信。The network interface 13 may optionally include a standard wired interface and a wireless interface (such as a WI-FI interface). The components 11-13 of the electronic device 1 communicate with each other via a communication bus.
图2仅示出了具有组件11-13的电子设备1,本领域技术人员可以理解的是,图2示出的结构并不构成对电子设备1的限定,可以包括比图示更少或者更多的部件,或者组合某些部件,或者不同的部件布置。FIG. 2 only shows the electronic device 1 with the components 11-13. Those skilled in the art can understand that the structure shown in FIG. 2 does not constitute a limitation on the electronic device 1, and may include fewer or more components than shown in the figure. Multiple components, or a combination of certain components, or different component arrangements.
可选地,该电子设备1还可以包括用户接口,用户接口可以包括显示器(Display)、输入单元比如键盘(Keyboard),可选的用户接口还可以包括标准的有线接口、无线接口。Optionally, the electronic device 1 may further include a user interface. The user interface may include a display (Display) and an input unit such as a keyboard (Keyboard). The optional user interface may also include a standard wired interface and a wireless interface.
可选地,在一些实施例中,显示器可以是LED显示器、液晶显示器、触控式液晶显示器以及有机发光二极管(Organic Light-Emitting Diode,OLED)触摸器等。其中,显示器也可以称为显示屏或显示单元,用于显示在电子设备1中处理的信息以及用于显示可视化的用户界面。Optionally, in some embodiments, the display may be an LED display, a liquid crystal display, a touch-sensitive liquid crystal display, an organic light-emitting diode (Organic Light-Emitting Diode, OLED) touch device, etc. Among them, the display may also be called a display screen or a display unit, which is used to display the information processed in the electronic device 1 and to display a visualized user interface.
参照图3所示,为本申请状态数据库的写入装置100较佳实施例的模块示意图,该实施例中,状态数据库的写入装置100可以仅包括模块110-130。Referring to FIG. 3, this is a schematic diagram of modules of a preferred embodiment of an apparatus 100 for writing a state database according to this application. In this embodiment, the apparatus 100 for writing a state database may only include modules 110-130.
接收模块110,用于接收排序节点发送的若干个区块的模拟结果后,获取所述若干个区块的区块信息;The receiving module 110 is configured to obtain the block information of the several blocks after receiving the simulation results of the several blocks sent by the ordering node;
当接收模块110接收到排序节点发送的模拟结果(提案结果)后,按照各区块的区块编号的顺序分别获取各区块的区块信息。其中,各区块的区块信息包括:多个交易的交易信息、交易是否有效、交易对应的读写集、各交易对应的智能合约及区块链条。区块链网络中一条链对应一个状态数据库,所有区块对应同一个缓存,缓存中的数据以map形式保存。After receiving the simulation result (proposal result) sent by the ordering node, the receiving module 110 obtains the block information of each block in the order of the block number of each block. Among them, the block information of each block includes: transaction information of multiple transactions, whether the transaction is valid, the read-write set corresponding to the transaction, the smart contract and the block chain corresponding to each transaction. In the blockchain network, one chain corresponds to a state database, all blocks correspond to the same cache, and the data in the cache is stored in the form of a map.
需要说明的是,各区块的编号是预先确定并存储的,且每新增一个区块,在已有编号的最大值的基础上生成一个新的编号作为所述新增区块的编号。例如,按照业务需求为已有区块进行编号,得到区块1、2、3、…、n,各编号排序就是获取各区块的区块信息的顺序。在产生新的区块的过程中,新区块的编号即为n+1,依此类推。。It should be noted that the number of each block is predetermined and stored, and each time a new block is added, a new number is generated based on the maximum value of the existing number as the number of the newly added block. For example, the existing blocks are numbered according to business requirements, and blocks 1, 2, 3, ..., n are obtained. The order of each number is the order in which the block information of each block is obtained. In the process of generating a new block, the number of the new block is n+1, and so on. .
第一写模块120,用于依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集。The first write module 120 is configured to read the read and write set of the target block from the block information of the several blocks in sequence, and determine the read of the target block based on the data in the preset cache and the state database Whether the transaction data in the write set is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the valid blocks in the blockchain network The read-write set of the transaction.
现有技术中的流程需要按顺序等每个区块把交易结果写进状态数据库中,也就是说,校验数据有效性会使得写状态数据库这一过程会非常慢。The process in the prior art needs to wait for each block to write the transaction result into the state database in order, that is, verifying the validity of the data will make the process of writing the state database very slow.
为了提高数据校验效率,在本实施例中,所述基于所述缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,包括:In order to improve the efficiency of data verification, in this embodiment, the judging whether the transaction data in the read-write set of the target block is valid based on the data in the cache and the state database includes:
确定所述目标区块对应的参考区块,从所述缓存中获取所述参考区块对应的数据,将所述目标区块的读写集与所述缓存中的所述参考区块对应的数据进行比对,若存在与所述目标区块的读写集对应的数据,则确定所述目标区块的读写集中的交易数据有效;若不存在与所述目标区块的读写集对应的数据,则从状态数据库中获取所述参考区块对应的数据,将所述目标区块的读写集与状态数据库中的所述参考区块对应的数据进行比对,若存在与所述目标区块的读写集对应的数据,则确定所述目标区块的读写集中的交易数据有效。Determine the reference block corresponding to the target block, obtain the data corresponding to the reference block from the cache, and compare the read/write set of the target block with the reference block in the cache The data is compared, and if there is data corresponding to the read-write set of the target block, it is determined that the transaction data in the read-write set of the target block is valid; if there is no read-write set with the target block Corresponding data, the data corresponding to the reference block is obtained from the state database, and the read-write set of the target block is compared with the data corresponding to the reference block in the state database. The data corresponding to the read-write set of the target block determines that the transaction data in the read-write set of the target block is valid.
需要说明的是,目标区块对应的参考区块基于区块编号排序确定。以区块1、2、3为例,区块1没有参考区块,区块2的参考区块为区块1,区块3的参考区块为区块1、区块2。例如,当交易节点检查完区块1中的读写集之后,将有效交易数据对应的读写集写入缓存中,在区块1正在抑或还没开始写状态数据库的时候,区块2先将区块信息中的读写集与缓存中区块1的数据进行对比,如果缓存中中没有区块1的数据,就与状态数据库中最新的数据进行对比,从而能正确判断出区块2的读写集中的交易数据是否有效。也就是说,检查一个目标区块的读写集,不单单对比状态数据库的数据,还要对比缓存中的数据。It should be noted that the reference block corresponding to the target block is determined based on the sequence of block numbers. Taking blocks 1, 2, and 3 as an example, block 1 has no reference block, the reference block of block 2 is block 1, and the reference block of block 3 is block 1 and block 2. For example, after the transaction node has checked the read-write set in block 1, it writes the read-write set corresponding to the valid transaction data into the cache. When block 1 is or has not yet begun to write the state database, block 2 first Compare the read and write set in the block information with the data in block 1 in the cache. If there is no data in block 1 in the cache, compare it with the latest data in the state database, so that block 2 can be correctly judged Whether the transaction data in the read-write set is valid. In other words, to check the read and write set of a target block, not only compare the data in the state database, but also compare the data in the cache.
在本实施例中,第一写模块120将各区块的区块信息中的读写集写入缓存的顺序是按照区块编号排序串行的。例如,区块1、2、3、…、n的顺序为区块1、区块1、区块3、…、区块n,那么,先写区块1的缓存,然后写区块2的缓存,…,依此类推。In this embodiment, the order in which the first write module 120 writes the read and write sets in the block information of each block into the cache is serialized according to the block number. For example, if the order of block 1, 2, 3,..., n is block 1, block 1, block 3,..., block n, then write the cache of block 1 first, and then write the cache of block 2. Caching,..., and so on.
第二写模块130,用于当第一写模块110将目标区块的有效交易数据对应的读写集写入所述缓存后,以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中。The second write module 130 is configured to read the target area from the cache in a background execution manner after the first write module 110 writes the read and write set corresponding to the valid transaction data of the target block into the cache Data corresponding to the block, and write the data corresponding to the target block into the state database.
在本实施例中,第二写模块130将目标区块对应的数据写入状态数据库的操作时并发执行的,以充分利用计算机的性能。In this embodiment, the operation of writing the data corresponding to the target block into the state database by the second writing module 130 is performed concurrently, so as to make full use of the performance of the computer.
每个区块都有自己的区块编号。将区块编号跟区块里面有效交易的读写集关联起来。利用区块编号从缓存中索引得到各目标区块对应的数据,即各目标区块中的有效交易数据对应的读写集。Each block has its own block number. Associate the block number with the read-write set of valid transactions in the block. The block number is used to index the data corresponding to each target block from the cache, that is, the read-write set corresponding to the valid transaction data in each target block.
在本实施例中,可以在将部分目标区块的有效交易数据对应的读写集写入缓存后开始进行写状态数据库的操作,也可以在将所有目标区块的有效交易数据对应的读写集写入缓存后开始进行写状态数据库的操作。因此,可以理解的是,写缓存及写状态数据库在写完第一个目标区块的缓存后可同时进行。In this embodiment, the read and write set corresponding to the valid transaction data of part of the target block can be written into the cache, and the operation of writing the state database can also be started after the valid transaction data of all target blocks is read and written. After the set is written into the cache, the operation of writing the state database is started. Therefore, it can be understood that the write cache and the write state database can be performed at the same time after the cache of the first target block is written.
为了充分利用计算机性能、提高写状态数据库的效率,在本实施例中,将第一个目标区块的有效交易数据对应的读写集写入缓存后,则开始在后台对第一个目标区块进行写状态数据库操作。例如,在将区块1的有效交易数据对应的读写集写入缓存后,判断区块2的读写集中交易数据是否有效的同时,在后台开始对区块1执行写状态数据库的操作。In order to make full use of computer performance and improve the efficiency of writing the state database, in this embodiment, after the read-write set corresponding to the valid transaction data of the first target block is written into the cache, the first target block is started in the background. The block performs write state database operations. For example, after the read-write set corresponding to the valid transaction data of block 1 is written into the cache, it is determined whether the transaction data of the read-write set of block 2 is valid, and at the same time, the operation of writing the state database to block 1 is started in the background.
上述实施例提出的电子设备1,通过新增一个缓存,将校验有效的区块的读写集写入缓存中,后续区块在校验数据过程中可从缓存中获取相关数据进行校验,无需等上一个区块完成写状态数据库即可处理下一个区块,鉴于写缓存的速度远远大于写状态数据库的速度,因此可提高后续区块数据校验的效率;同时,通过将写缓存与写状态数据库分离,并后台执行写状态数据库的过程,充分利用计算机性能,加快了写状态数据库的处理进度。The electronic device 1 proposed in the above embodiment adds a cache to write the read and write set of valid blocks into the cache, and subsequent blocks can obtain relevant data from the cache for verification during the process of verifying data. , The next block can be processed without waiting for the previous block to finish writing the state database. Since the write cache speed is much faster than the write state database, the efficiency of subsequent block data verification can be improved; at the same time, by writing The cache is separated from the write state database, and the process of writing the state database is executed in the background, making full use of the computer performance and speeding up the processing progress of the writing state database.
鉴于区块数量较多,因此缓存中存储的各区块的有效读写集会占用一定的内存,为了充分利用存储空间,提高数据处理效率,在其他实施例中,所述第二写模块130,还用于当将所述目标区块对应的数据写入状态数据库后,从所述缓存中释放所述目标区块对应的数据。In view of the large number of blocks, the effective read and write set of each block stored in the cache will occupy a certain amount of memory. In order to make full use of the storage space and improve the efficiency of data processing, in other embodiments, the second write module 130 also It is used to release the data corresponding to the target block from the cache after the data corresponding to the target block is written into the state database.
当一个区块的有效读写集写进状态数据库之后,就不需要缓存中的这份数据了,因此可以把缓存中不再需要的数据删除,以释放存储空间。After the effective read-write set of a block is written into the state database, the data in the cache is no longer needed, so data that is no longer needed in the cache can be deleted to free up storage space.
为了保证状态数据库中的数据与区块链中的数据一致,需要防止在写状态数据库的过程中有peer节点中途宕机的情况,因此,在将区块的读写集写入状态数据库后,需写入检查点,以在恢复写状态数据库时确定未写完的区块。In order to ensure that the data in the state database is consistent with the data in the blockchain, it is necessary to prevent the peer node from going down during the process of writing the state database. Therefore, after writing the block's read-write set to the state database, A checkpoint needs to be written to determine the uncompleted blocks when the write state database is restored.
然而,目前写检查点的步骤为:输入一个区块,检查区块数据,将区块的有效读写集写入状态数据库中,写检查点,然后对下一个区块重复执行相同的步骤。需要说明的是,区块并发的将各自的读写集写入状态数据库后,如果写检查点操作还是紧跟着写state数据库之后,此时的检查点的功能会失效。例如,由于并发写区块1、区块2、区块3的状态数据库,假如区块2的状态数据库写完之后,写入了检查点为2,此时peer挂掉宕机,重新启动peer时,由于检查点是2,程序就会从区块2开始恢复,恢复区块3及其以后的数据。无法发现实际上区块1的数据还没写入状态数据库,会导致装填数据库中的数据出现丢失,导致与其他节点的状态数据库的数据不一致的情况。However, the current steps for writing a checkpoint are: input a block, check the block data, write the valid read and write set of the block into the state database, write the checkpoint, and then repeat the same steps for the next block. It should be noted that after the blocks concurrently write their respective read and write sets to the state database, if the write checkpoint operation still follows the write state database, the checkpoint function at this time will become invalid. For example, because the state database of block 1, block 2, and block 3 is written concurrently, if the state database of block 2 is written and the checkpoint is written to 2, then the peer hangs down and restarts the peer When the checkpoint is 2, the program will start to restore from block 2, and restore the data in block 3 and beyond. It is impossible to find that the data of block 1 has not been written into the state database, which will cause the data in the filling database to be lost, resulting in inconsistencies with the data in the state database of other nodes.
因此,为了保证数据的一致性,在其他实施例中,所述状态数据库的写入装置100还包括:Therefore, in order to ensure data consistency, in other embodiments, the apparatus 100 for writing a state database further includes:
第三写模块140,用于接收第二写模块130发送的写检查点请求,所述请求中包括目标区块对应的区块编号,并基于所述区块编号及预设分析规则为所述目标区块写检查点。The third write module 140 is configured to receive a write checkpoint request sent by the second write module 130, where the request includes a block number corresponding to the target block, and is based on the block number and a preset analysis rule as the The target block write checkpoint.
在本实施例中,写检查点的操作与写状态数据库的操作分离。写检查点就是将检查点对应的区块编号写进状态数据库中,第二写模块130每写完一个区块,第三写模块140就写一次检查点,这样就清楚地知道状态数据库中保存着0~xx号区块的有效读写集。In this embodiment, the operation of writing the checkpoint is separated from the operation of writing the state database. To write a checkpoint is to write the block number corresponding to the checkpoint into the state database. Every time the second writing module 130 finishes writing a block, the third writing module 140 writes the checkpoint once, so that it is clear that the state is stored in the database. It is the effective read and write set of blocks 0~xx.
在本实施例中,所述基于所述区块编号及预设分析规则为所述目标区块写检查点,包括:In this embodiment, writing a checkpoint for the target block based on the block number and a preset analysis rule includes:
a1.所述检查点协程接收到请求后,获取所述目标区块的区块编号,判断该编号是否为期望编号;a1. After receiving the request, the checkpoint coroutine obtains the block number of the target block, and determines whether the number is the expected number;
a2.若是,则为所述目标区块写检查点,并更新期望编号;a2. If yes, write a checkpoint for the target block and update the expected number;
a3.若否,则将该请求按照编号的顺序写入等待队列中,并判断等待队列中是否存在期望编号;若是,则执行步骤a2,为期望编号对应的区块写检查点,若否,执行步骤a1。a3. If not, write the request into the waiting queue in the order of numbers, and determine whether the expected number exists in the waiting queue; if so, perform step a2 to write a checkpoint for the block corresponding to the expected number, if not, Go to step a1.
当第二写模块130将目标区块的有效读写集写入状态数据库之后,向第三写模块140发送一条携带区块编号的写检查点请求,第三写模块140只有接收到顺序的编号(即期望编号)之后,才会真正写入检查点信息。由于本申请中是并发将各区块写入状态数据库,因此可能出现编号不连续的情况,当出现不连续的编号时,等待期望编号,再写入。每写完一个检查点会将上一个检查点覆盖,仅保留最新的值。After the second writing module 130 writes the effective read-write set of the target block into the state database, it sends a write checkpoint request carrying the block number to the third writing module 140, and the third writing module 140 only receives the sequential number (That is, the expected number), the checkpoint information is actually written. Since each block is written into the state database concurrently in this application, there may be discontinuous numbers. When there are discontinuous numbers, wait for the expected number before writing. Every time a checkpoint is written, the previous checkpoint will be overwritten, and only the latest value will be retained.
例如,区块0、区块1、区块5的有效读写集都写入状态数据库中,而区块2、区块3、区块4的有效读写集还未写入状态数据库中,因此,在为区块5写检查点时,需保证区块2、区块3、区块4都已经写入状态数据库中,故将编号为5的这一请求写入等待队列中,待编号为4的请求写完检查点后为编号为5的请求写检查点。For example, the effective read and write sets of block 0, block 1, and block 5 are all written into the state database, while the effective read and write sets of block 2, block 3, and block 4 have not been written into the state database. Therefore, when writing a checkpoint for block 5, it is necessary to ensure that block 2, block 3, and block 4 have been written into the state database. Therefore, the request numbered 5 is written into the waiting queue, and the number is to be numbered. After writing the checkpoint for the request of 4, write the checkpoint for the request of No. 5.
期望编号为上一次写完检查点的编号加1。例如,上一次写完编号为1的请求的检查点,则此时的期望编号为1+1,当收到区块编号为2的请求后,为区块2写检查点,若接收到的请求都不是2,将这些请求按编号顺序写入等待队列中,直到出现期望编号时再为期望编号对应的区块写检查点。即使此时peer宕机,也能正确去恢复区块2及之后的数据。The expected number is the number of the last checkpoint written plus one. For example, the last time the checkpoint of the request with number 1 is written, the expected number at this time is 1+1. When the request with block number 2 is received, the checkpoint is written for block 2. If the received None of the requests are 2, write these requests into the waiting queue in order of numbers, and write checkpoints for the blocks corresponding to the expected numbers until the expected numbers appear. Even if the peer is down at this time, the data in block 2 and beyond can be restored correctly.
上述实施例提出的电子设备1,通过后台并发将各区块的有效读写集写入状态数据库中,并在写完状态数据库后,按照区块编号顺序依次为期望编号对应的区块写检查点,避免peer宕机后无法正确恢复数据的情况,提高写状态数据库的效率及准确性。The electronic device 1 proposed in the above embodiment concurrently writes the effective read-write set of each block into the state database through the background, and after writing the state database, write checkpoints for the blocks corresponding to the expected numbers in the order of the block numbers , To avoid the situation that the data cannot be recovered correctly after the peer is down, and to improve the efficiency and accuracy of writing the state database.
此外,本申请实施例还提出一种计算机可读存储介质,所述计算机可读存储介质可以是易失性,也可以是非易失性,所述计算机可读存储介质中包括状态数据库的写入程序10,所述状态数据库的写入程序10被处理器执行时实现如下操作:In addition, the embodiment of the present application also proposes a computer-readable storage medium. The computer-readable storage medium may be volatile or non-volatile. The computer-readable storage medium includes the writing of the state database. Program 10, when the state database writing program 10 is executed by the processor, the following operations are implemented:
第一写步骤:依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集;The first writing step: read the read-write set of the target block sequentially from the block information of the several blocks, and determine the read-write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Write set
第二写步骤:当将目标区块的有效交易数据对应的读写集写入所述缓存后,以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中;及The second writing step: after the read-write set corresponding to the valid transaction data of the target block is written into the cache, the data corresponding to the target block is read from the cache in a background execution manner, and the data corresponding to the target block is read The data corresponding to the target block is written into the state database; and
第三写步骤:接收写检查点请求,所述请求中包括目标区块对应的区块编号,并基于所述区块编号及预设分析规则为所述目标区块写检查点。The third writing step: receiving a request for writing a checkpoint, the request including a block number corresponding to the target block, and writing a checkpoint for the target block based on the block number and a preset analysis rule.
本申请之计算机可读存储介质的具体实施方式与上述状态数据库的写入方法的具体实施方式大致相同,在此不再赘述。The specific implementation of the computer-readable storage medium of the present application is substantially the same as the specific implementation of the above-mentioned state database writing method, and will not be repeated here.
上述本申请实施例序号仅仅为了描述,不代表实施例的优劣。The serial numbers of the foregoing embodiments of the present application are for description only, and do not represent the advantages and disadvantages of the embodiments.
需要说明的是,在本文中,术语“包括”、“包含”或者其任何其他变体意在涵盖非排他性的包含,从而使得包括一系列要素的过程、装置、物品或者方法不仅包括那些要素,而且还包括没有明确列出的其他要素,或者是还包括为这种过程、装置、物品或者方法所固有的要素。在没有更多限制的情况下,由语句“包括一个……”限定的要素,并不排除在包括该要素的过程、装置、物品或者方法中还存在另外的相同要素。It should be noted that in this article, the terms "include", "include" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, device, article or method including a series of elements not only includes those elements, It also includes other elements not explicitly listed, or elements inherent to the process, device, article, or method. If there are no more restrictions, the element defined by the sentence "including a..." does not exclude the existence of other identical elements in the process, device, article, or method that includes the element.
通过以上的实施方式的描述,本领域的技术人员可以清楚地了解到上述实施例方法可借助软件加必需的通用硬件平台的方式来实现,当然也可以通过硬件,但很多情况下前者是更佳的实施方式。基于这样的理解,本申请的技术方案本质上或者说对现有技术做出贡献的部分可以以软件产品的形式体现出来,该计算机软件产品存储在如上所述的一个存储介质(如ROM/RAM、磁碟、光盘)中,包括若干指令用以使得一台终端设备(可以是手机,计算机,服务器,或者网络设备等)执行本申请各个实施例所述的方法。Through the description of the above implementation manners, those skilled in the art can clearly understand that the above-mentioned embodiment method can be implemented by means of software plus the necessary general hardware platform, of course, it can also be implemented by hardware, but in many cases the former is better.的实施方式。 Based on this understanding, the technical solution of this application essentially or the part that contributes to the existing technology can be embodied in the form of a software product, and the computer software product is stored in a storage medium (such as ROM/RAM) as described above. , Magnetic disks, optical disks), including several instructions to make a terminal device (which can be a mobile phone, a computer, a server, or a network device, etc.) execute the methods described in the various embodiments of this application.
以上仅为本申请的优选实施例,并非因此限制本申请的专利范围,凡是利用本申请说明书及附图内容所作的等效结构或等效流程变换,或直接或间接运用在其它相关的技术领域,均同理包括在本申请的专利保护范围内。The above are only the preferred embodiments of the application, and do not limit the scope of the patent for this application. Any equivalent structure or equivalent process transformation made using the content of the description and drawings of the application, or directly or indirectly applied to other related technical fields , The same reason is included in the scope of patent protection of this application.

Claims (20)

  1. 一种状态数据库的写入方法,应用于区块链网络中交易节点的电子设备,所述区块链网络还包括排序节点,其中,所述方法包括:A method for writing a state database is applied to the electronic equipment of a transaction node in a blockchain network, the blockchain network further includes an ordering node, wherein the method includes:
    接收步骤,接收排序节点发送的若干个区块的模拟结果后,获取所述若干个区块的区块信息;In the receiving step, after receiving the simulation results of several blocks sent by the ordering node, obtain the block information of the several blocks;
    第一写步骤,依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集;及The first writing step is to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Writing set; and
    第二写步骤,以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中。The second writing step is to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
  2. 根据权利要求1所述的状态数据库的写入方法,其中,所述基于所述缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,包括:The method for writing a state database according to claim 1, wherein the judging whether the transaction data in the read-write set of the target block is valid based on the data in the cache and the state database comprises:
    确定所述目标区块对应的参考区块,从所述缓存中获取所述参考区块对应的数据,将所述目标区块的读写集与所述缓存中的所述参考区块对应的数据进行比对;Determine the reference block corresponding to the target block, obtain the data corresponding to the reference block from the cache, and compare the read/write set of the target block with the reference block in the cache Data comparison;
    若存在与所述目标区块的读写集对应的数据,则确定所述目标区块的读写集中的交易数据有效;及If there is data corresponding to the read-write set of the target block, it is determined that the transaction data in the read-write set of the target block is valid; and
    若不存在与所述目标区块的读写集对应的数据,则从状态数据库中获取所述参考区块对应的数据,将所述目标区块的读写集与状态数据库中的所述参考区块对应的数据进行比对,若存在与所述目标区块的读写集对应的数据,则确定所述目标区块的读写集中的交易数据有效。If there is no data corresponding to the read-write set of the target block, the data corresponding to the reference block is obtained from the state database, and the read-write set of the target block is compared with the reference in the state database. The data corresponding to the blocks are compared, and if there is data corresponding to the read-write set of the target block, it is determined that the transaction data in the read-write set of the target block is valid.
  3. 根据权利要求1所述的状态数据库的写入方法,其中,所述第二写步骤还包括:The method for writing a state database according to claim 1, wherein the second writing step further comprises:
    当将所述目标区块对应的数据写入所述状态数据库后,从所述缓存中释放所述目标区块对应的数据。After the data corresponding to the target block is written into the state database, the data corresponding to the target block is released from the cache.
  4. 根据权利要求1至3中任意一项所述的状态数据库的写入方法,其中,所述状态数据库的写入方法还包括:The method for writing a state database according to any one of claims 1 to 3, wherein the method for writing the state database further comprises:
    第三写步骤,当将所述目标区块对应的数据写入状态数据库后,向检查点协程发送写检查点请求,所述请求中包括目标区块对应的区块编号,并控制所述检查点协程基于所述区块编号及预设分析规则为所述目标区块写检查点。In the third writing step, after the data corresponding to the target block is written into the state database, a checkpoint write request is sent to the checkpoint coroutine, and the request includes the block number corresponding to the target block, and controls the The checkpoint coroutine writes checkpoints for the target block based on the block number and preset analysis rules.
  5. 根据权利要求4所述的状态数据库的写入方法,其中,所述检查点协程基于所述区块编号及预设分析规则为所述目标区块写检查点,包括:The method for writing a state database according to claim 4, wherein the checkpoint coroutine writes a checkpoint for the target block based on the block number and a preset analysis rule, comprising:
    a1.所述检查点协程接收到请求后,获取所述目标区块的区块编号,判断该编号是否为期望编号;a1. After receiving the request, the checkpoint coroutine obtains the block number of the target block, and determines whether the number is the expected number;
    a2.若是,则为所述目标区块写检查点,并更新期望编号;a2. If yes, write a checkpoint for the target block and update the expected number;
    a3.若否,则将该请求按照编号的顺序写入等待队列中,并判断等待队列中是否存在期望编号;若是,则执行步骤a2,为期望编号对应的区块写检查点,若否,执行步骤a1。a3. If not, write the request into the waiting queue in the order of numbers, and determine whether the expected number exists in the waiting queue; if so, perform step a2 to write a checkpoint for the block corresponding to the expected number, if not, Go to step a1.
  6. 根据权利要求2所述的状态数据库的写入方法,其中,在接收步骤之前,所述方法还包括:The method for writing a state database according to claim 2, wherein, before the receiving step, the method further comprises:
    预先确定并保存各区块的编号,每新增一个区块,在已有编号的最大值的基础上生成一个新的编号作为所述新增区块的编号。The number of each block is determined and saved in advance, and each time a new block is added, a new number is generated based on the maximum value of the existing number as the number of the newly added block.
  7. 根据权利要求6所述的状态数据库的写入方法,其中,所述目标区块对应的参考区块基于区块编码排序确定。7. The method for writing a state database according to claim 6, wherein the reference block corresponding to the target block is determined based on a block coding order.
  8. 一种状态数据库的写入装置,其中,所述装置包括:A writing device for a state database, wherein the device includes:
    接收模块,用于接收排序节点发送的若干个区块的模拟结果后,获取所述若干个区块的区块信息;The receiving module is used to obtain the block information of the several blocks after receiving the simulation results of the several blocks sent by the ordering node;
    第一写模块,用于依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集;及The first write module is used to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write of the target block based on the data in the preset cache and the state database Whether the centralized transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache valid transactions in each block in the blockchain network Read and write set; and
    第二写模块,用于以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中。The second writing module is configured to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
  9. 一种电子设备,其中,该电子设备包括:存储器、处理器,所述存储器上存储有可在所述处理器上运行的状态数据库的写入程序,所述状态数据库的写入程序被所述处理器执行时,可实现如下步骤:An electronic device, wherein the electronic device includes a memory and a processor, the memory stores a state database writing program that can be run on the processor, and the state database writing program is controlled by the state database. When the processor executes, the following steps can be implemented:
    接收步骤:接收排序节点发送的若干个区块的模拟结果后,获取所述若干个区块的区块信息;Receiving step: after receiving the simulation results of several blocks sent by the ordering node, obtain block information of the several blocks;
    第一写步骤:依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集;及The first writing step: read the read-write set of the target block sequentially from the block information of the several blocks, and determine the read-write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Writing set; and
    第二写步骤:以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中。The second writing step: read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
  10. 根据权利要求9所述的电子设备,其中,所述基于所述缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,包括:The electronic device according to claim 9, wherein the judging whether the transaction data in the read-write set of the target block is valid based on the data in the cache and the state database comprises:
    确定所述目标区块对应的参考区块,从所述缓存中获取所述参考区块对应的数据,将所述目标区块的读写集与所述缓存中的所述参考区块对应的数据进行比对;Determine the reference block corresponding to the target block, obtain the data corresponding to the reference block from the cache, and compare the read/write set of the target block with the reference block in the cache Data comparison;
    若存在与所述目标区块的读写集对应的数据,则确定所述目标区块的读写集中的交易数据有效;及If there is data corresponding to the read-write set of the target block, it is determined that the transaction data in the read-write set of the target block is valid; and
    若不存在与所述目标区块的读写集对应的数据,则从状态数据库中获取所述参考区块对应的数据,将所述目标区块的读写集与状态数据库中的所述参考区块对应的数据进行比对,若存在与所述目标区块的读写集对应的数据,则确定所述目标区块的读写集中的交易数据有效。If there is no data corresponding to the read-write set of the target block, the data corresponding to the reference block is obtained from the state database, and the read-write set of the target block is compared with the reference in the state database. The data corresponding to the blocks are compared, and if there is data corresponding to the read-write set of the target block, it is determined that the transaction data in the read-write set of the target block is valid.
  11. 根据权利要求9至10中任意一项所述的电子设备,其中,所述状态数据库的写入程序被所述处理器执行时,还实现如下步骤:The electronic device according to any one of claims 9 to 10, wherein when the state database writing program is executed by the processor, the following steps are further implemented:
    第三写步骤:接收写检查点请求,所述请求中包括目标区块对应的区块编号,并基于所述区块编号及预设分析规则为所述目标区块写检查点。The third writing step: receiving a request for writing a checkpoint, the request including a block number corresponding to the target block, and writing a checkpoint for the target block based on the block number and a preset analysis rule.
  12. 根据权利要求11所述的电子设备,其中,所述基于所述区块编号及预设分析规则为所述目标区块写检查点,包括:11. The electronic device of claim 11, wherein the writing a checkpoint for the target block based on the block number and a preset analysis rule comprises:
    a1.所述检查点协程接收到请求后,获取所述目标区块的区块编号,判断该编号是否为期望编号;a1. After receiving the request, the checkpoint coroutine obtains the block number of the target block, and determines whether the number is the expected number;
    a2.若是,则为所述目标区块写检查点,并更新期望编号;a2. If yes, write a checkpoint for the target block and update the expected number;
    a3.若否,则将该请求按照编号的顺序写入等待队列中,并判断等待队列中是否存在期望编号;若是,则执行步骤a2,为期望编号对应的区块写检查点,若否,执行步骤a1。a3. If not, write the request into the waiting queue in the order of numbers, and determine whether the expected number exists in the waiting queue; if so, perform step a2 to write a checkpoint for the block corresponding to the expected number, if not, Go to step a1.
  13. 根据权利要求9所述的电子设备,其中,所述第二写步骤还包括:The electronic device according to claim 9, wherein the second writing step further comprises:
    当将所述目标区块对应的数据写入所述状态数据库后,从所述缓存中释放所述目标区块对应的数据。After the data corresponding to the target block is written into the state database, the data corresponding to the target block is released from the cache.
  14. 根据权利要求10所述的电子设备,其中,在接收步骤之前,所述状态数据库的写入程序被所述处理器执行时还实现如下步骤:The electronic device according to claim 10, wherein, before the receiving step, the following steps are further implemented when the state database writing program is executed by the processor:
    预先确定并保存各区块的编号,每新增一个区块,在已有编号的最大值的基础上生成一个新的编号作为所述新增区块的编号。The number of each block is determined and saved in advance, and each time a new block is added, a new number is generated based on the maximum value of the existing number as the number of the newly added block.
  15. 根据权利要求14所述的电子设备,其中,所述目标区块对应的参考区块基于区块编码排序确定。15. The electronic device according to claim 14, wherein the reference block corresponding to the target block is determined based on a block coding order.
  16. 一种计算机可读存储介质,其中,所述计算机可读存储介质中包括状态数据库的写入程序,所述状态数据库的写入程序被处理器执行时,实现如下步骤:A computer-readable storage medium, wherein the computer-readable storage medium includes a state database writing program, and when the state database writing program is executed by a processor, the following steps are implemented:
    接收步骤,接收排序节点发送的若干个区块的模拟结果后,获取所述若干个区块的区块信息;In the receiving step, after receiving the simulation results of several blocks sent by the ordering node, obtain the block information of the several blocks;
    第一写步骤,依序从所述若干个区块的区块信息中读取目标区块的读写集,基于预设缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,并将所述目标区块的有效交易数据对应的读写集写入所述缓存中,其中,所述缓存用于缓存所述区块链网络中各区块中有效交易的读写集;及The first writing step is to sequentially read the read and write set of the target block from the block information of the several blocks, and determine the read and write set of the target block based on the data in the preset cache and the state database Whether the transaction data is valid, and write the read-write set corresponding to the valid transaction data of the target block into the cache, where the cache is used to cache the reads of valid transactions in each block in the blockchain network Writing set; and
    第二写步骤,以后台执行的方式从所述缓存中读取所述目标区块对应的数据,并将所述目标区块对应的数据写入所述状态数据库中。The second writing step is to read the data corresponding to the target block from the cache in a background execution manner, and write the data corresponding to the target block into the state database.
  17. 根据权利要求16所述的计算机可读存储介质,其中,所述基于所述缓存及状态数据库中的数据判断所述目标区块的读写集中的交易数据是否有效,包括:The computer-readable storage medium according to claim 16, wherein the judging whether the transaction data in the read-write set of the target block is valid based on the data in the cache and the state database comprises:
    确定所述目标区块对应的参考区块,从所述缓存中获取所述参考区块对应的数据,将所述目标区块的读写集与所述缓存中的所述参考区块对应的数据进行比对;Determine the reference block corresponding to the target block, obtain the data corresponding to the reference block from the cache, and compare the read/write set of the target block with the reference block in the cache Data comparison;
    若存在与所述目标区块的读写集对应的数据,则确定所述目标区块的读写集中的交易数据有效;及If there is data corresponding to the read-write set of the target block, it is determined that the transaction data in the read-write set of the target block is valid; and
    若不存在与所述目标区块的读写集对应的数据,则从状态数据库中获取所述参考区块对应的数据,将所述目标区块的读写集与状态数据库中的所述参考区块对应的数据进行比对,若存在与所述目标区块的读写集对应的数据,则确定所述目标区块的读写集中的交易数据有效。If there is no data corresponding to the read-write set of the target block, the data corresponding to the reference block is obtained from the state database, and the read-write set of the target block is compared with the reference in the state database. The data corresponding to the blocks are compared, and if there is data corresponding to the read-write set of the target block, it is determined that the transaction data in the read-write set of the target block is valid.
  18. 根据权利要求16所述的计算机可读存储介质,其中,所述第二写步骤还包括:The computer-readable storage medium according to claim 16, wherein the second writing step further comprises:
    当将所述目标区块对应的数据写入所述状态数据库后,从所述缓存中释放所述目标区块对应的数据。After the data corresponding to the target block is written into the state database, the data corresponding to the target block is released from the cache.
  19. 根据权利要求16至18中任意一项所述的计算机可读存储介质,其中,所述状态数据库的写入程序被处理器执行时还实现如下步骤:The computer-readable storage medium according to any one of claims 16 to 18, wherein the following steps are further implemented when the write program of the state database is executed by the processor:
    第三写步骤,当将所述目标区块对应的数据写入状态数据库后,向检查点协程发送写检查点请求,所述请求中包括目标区块对应的区块编号,并控制所述检查点协程基于所述区块编号及预设分析规则为所述目标区块写检查点。In the third writing step, after the data corresponding to the target block is written into the state database, a checkpoint write request is sent to the checkpoint coroutine, and the request includes the block number corresponding to the target block, and controls the The checkpoint coroutine writes checkpoints for the target block based on the block number and preset analysis rules.
  20. 根据权利要求19所述的计算机可读存储介质,其中,所述检查点协程基于所述区块编号及预设分析规则为所述目标区块写检查点,包括:18. The computer-readable storage medium of claim 19, wherein the checkpoint coroutine writes a checkpoint for the target block based on the block number and a preset analysis rule, comprising:
    a1.所述检查点协程接收到请求后,获取所述目标区块的区块编号,判断该编号是否为期望编号;a1. After receiving the request, the checkpoint coroutine obtains the block number of the target block, and determines whether the number is the expected number;
    a2.若是,则为所述目标区块写检查点,并更新期望编号;a2. If yes, write a checkpoint for the target block and update the expected number;
    a3.若否,则将该请求按照编号的顺序写入等待队列中,并判断等待队列中是否存在期望编号;若是,则执行步骤a2,为期望编号对应的区块写检查点,若否,执行步骤a1。a3. If not, write the request into the waiting queue in the order of numbers, and determine whether the expected number exists in the waiting queue; if so, perform step a2 to write a checkpoint for the block corresponding to the expected number, if not, Go to step a1.
PCT/CN2020/099087 2020-01-09 2020-06-30 Method and apparatus for writing into state database, electronic device, and storage medium WO2021139109A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN202010024089.0 2020-01-09
CN202010024089.0A CN111241061B (en) 2020-01-09 2020-01-09 Writing method of state database, data processing device and storage medium

Publications (1)

Publication Number Publication Date
WO2021139109A1 true WO2021139109A1 (en) 2021-07-15

Family

ID=70874416

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2020/099087 WO2021139109A1 (en) 2020-01-09 2020-06-30 Method and apparatus for writing into state database, electronic device, and storage medium

Country Status (2)

Country Link
CN (1) CN111241061B (en)
WO (1) WO2021139109A1 (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20220311595A1 (en) * 2021-03-24 2022-09-29 International Business Machines Corporation Reducing transaction aborts in execute-order-validate blockchain models
CN117520460A (en) * 2024-01-05 2024-02-06 成都安世赛斯特软件技术有限公司 Custom number generation management method and system

Families Citing this family (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111241061B (en) * 2020-01-09 2023-04-07 平安科技(深圳)有限公司 Writing method of state database, data processing device and storage medium
CN112037058B (en) * 2020-08-28 2024-03-26 平安科技(深圳)有限公司 Data verification method, device and storage medium
CN111984421B (en) * 2020-09-03 2022-09-16 深圳壹账通智能科技有限公司 Data processing method, device and storage medium
CN112559637B (en) * 2021-02-19 2021-06-18 腾讯科技(深圳)有限公司 Data processing method, device, equipment and medium based on distributed storage
CN112883068A (en) * 2021-04-30 2021-06-01 支付宝(杭州)信息技术有限公司 Block chain transaction execution method, block chain node and control device

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107992356A (en) * 2017-12-13 2018-05-04 上海壹账通金融科技有限公司 Block chain affairs block processes method, electronic device and readable storage medium storing program for executing
CN110222116A (en) * 2019-05-07 2019-09-10 北京奇艺世纪科技有限公司 A kind of control method, device and the storage medium of transaction data storage
WO2019191432A1 (en) * 2018-03-29 2019-10-03 Walmart Apollo, Llc System and method for supply chain verification using blockchain
CN111241061A (en) * 2020-01-09 2020-06-05 平安科技(深圳)有限公司 Writing method of state database, data processing device and storage medium

Family Cites Families (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108776897B (en) * 2018-06-05 2020-04-21 腾讯科技(深圳)有限公司 Data processing method, device, server and computer readable storage medium
CN109684337B (en) * 2018-12-29 2021-05-04 杭州趣链科技有限公司 Block chain state data storage and reading method based on multi-level cache
CN110597911B (en) * 2019-09-12 2021-08-17 腾讯科技(深圳)有限公司 Certificate processing method and device for block chain network, electronic equipment and storage medium

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107992356A (en) * 2017-12-13 2018-05-04 上海壹账通金融科技有限公司 Block chain affairs block processes method, electronic device and readable storage medium storing program for executing
WO2019191432A1 (en) * 2018-03-29 2019-10-03 Walmart Apollo, Llc System and method for supply chain verification using blockchain
CN110222116A (en) * 2019-05-07 2019-09-10 北京奇艺世纪科技有限公司 A kind of control method, device and the storage medium of transaction data storage
CN111241061A (en) * 2020-01-09 2020-06-05 平安科技(深圳)有限公司 Writing method of state database, data processing device and storage medium

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20220311595A1 (en) * 2021-03-24 2022-09-29 International Business Machines Corporation Reducing transaction aborts in execute-order-validate blockchain models
CN117520460A (en) * 2024-01-05 2024-02-06 成都安世赛斯特软件技术有限公司 Custom number generation management method and system
CN117520460B (en) * 2024-01-05 2024-04-02 成都安世赛斯特软件技术有限公司 Custom number generation management method and system

Also Published As

Publication number Publication date
CN111241061A (en) 2020-06-05
CN111241061B (en) 2023-04-07

Similar Documents

Publication Publication Date Title
WO2021139109A1 (en) Method and apparatus for writing into state database, electronic device, and storage medium
KR101805948B1 (en) Checkpoints for a file system
US20210049715A1 (en) Blockchain-based data procesing method, apparatus, and electronic device
US20140344311A1 (en) Systems and methods for asynchronous schema changes
CN110706101B (en) Method and apparatus for concurrently executing transactions in a blockchain
WO2020082576A1 (en) Device and method for processing block transaction, and storage medium
US11436212B2 (en) Concurrent transaction processing in a database system
WO2022048358A1 (en) Data processing method and device, and storage medium
US20200026427A1 (en) System and method for handling data storage on storage devices
US11360966B2 (en) Information processing system and method of controlling information processing system
WO2021189902A1 (en) Data storage method and apparatus based on cloud storage, and computer device and storage medium
US10997158B2 (en) Techniques for updating big data tables using snapshot isolation
JP6812826B2 (en) Storage method, storage device and storage program
US20230124036A1 (en) In-place garbage collection for state machine replication
WO2020225224A1 (en) Secure and transparent database transactions in untrusted environments using permissioned, minimally invasive blockchain technology
CN106326310B (en) Resource encryption updating method for mobile phone client software
CN110377614B (en) Order processing lock system under distributed environment
CN114675995A (en) Data backup method and device and electronic equipment
EP4136541B1 (en) Transactional support for non-relational database
CN113434359B (en) Data traceability system construction method and device, electronic device and readable storage medium
US20230195719A1 (en) Optimizations to read and write transactions for large values in distributed databases
US11704305B1 (en) Optimizations for long-lived statements in a database system
US11709808B1 (en) Schema evolution for the serialization of non-primary key columnar data into row-organized byte sequences
CN115827204A (en) Transaction scheduling method, transaction execution method, device and computer equipment
CN116010412A (en) Database full synchronization breakpoint continuous transmission method, system and storage medium

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 20911676

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 20911676

Country of ref document: EP

Kind code of ref document: A1