Disclosure of Invention
The embodiment of the application provides a transaction packaging method based on a block chain, which is used for improving consensus efficiency while preventing replay.
The embodiment of the application provides a transaction packaging method based on a block chain, which comprises the following steps:
carrying out duplicate removal processing on a plurality of transaction records in the transaction set list;
packaging the rest transaction records in the transaction set list to generate a plurality of candidate blocks;
placing the plurality of candidate blocks into a candidate block queue;
at the beginning of the consensus process, a given candidate block is pulled from the candidate block queue to initiate consensus.
In one embodiment, the deduplication processing of multiple transaction records in the transaction collection list includes:
and carrying out double-flower prevention detection on each transaction record, and removing the transaction records which fail to pass the double-flower prevention detection.
In one embodiment, the performing the anti-double-blossom detection on each transaction record and removing the transaction record which fails the anti-double-blossom detection includes:
locking each transaction record in sequence, judging whether the hash value of the locked transaction record is the same as the hash values of the other transaction records, and if so, removing the locked transaction record.
In an embodiment, the packaging the remaining transaction records in the transaction set list to generate a plurality of candidate blocks includes:
and arranging the rest transaction records in the transaction set list according to the time sequence, and packing the transaction records in sequence to generate a plurality of candidate blocks.
In one embodiment, the pulling of the specified candidate block from the candidate block queue initiates consensus, comprising:
and pulling the current first candidate block from the candidate block queue to initiate consensus.
In one embodiment, the pulling of the specified candidate block from the candidate block queue initiates consensus, comprising:
pulling the candidate block with the earliest current timestamp from the candidate block queue to initiate consensus.
In an embodiment, after the pulling of the specified candidate block from the candidate block queue initiates consensus, the method further comprises:
removing the designated candidate block in the candidate block queue;
after the designated candidate block is successfully identified, judging whether a transaction record with the same hash value exists in a transaction list contained in the current first candidate block in the candidate block queue and the transaction list contained in the designated candidate block;
and if so, removing the transaction records with the same hash value in the first candidate block.
In one embodiment, before the deduplication processing is performed on the plurality of transaction records in the transaction collection list, the method further includes:
receiving a transaction record sent by a client, and adding the transaction record into a queue to be detected;
and starting an asynchronous thread, and pulling a plurality of transaction records in the queue to be detected to form the transaction set list through the asynchronous thread.
In one embodiment, the de-duplication processing and the packing of the candidate tiles are performed iteratively by different asynchronous threads.
The embodiment of the application provides a trade packing apparatus based on block chain, includes:
the duplicate removal processing module is used for carrying out duplicate removal processing on a plurality of transaction records in the transaction set list;
the packing candidate module is used for packing the rest transaction records in the transaction set list to generate a plurality of candidate blocks;
the queue adding module is used for placing the candidate blocks into a candidate block queue;
and the consensus initiating module is used for pulling the specified candidate block from the candidate block queue to initiate consensus when a consensus process starts.
An embodiment of the present application provides an electronic device, which includes:
a processor;
a memory for storing processor-executable instructions;
wherein the processor is configured to execute the above block chain based transaction packaging method.
The embodiment of the present application provides a computer-readable storage medium, wherein the storage medium stores a computer program, and the computer program is executable by a processor to implement the above block chain based transaction packaging method.
According to the technical scheme provided by the embodiment of the application, before the consensus process is started, the transaction records are packaged in advance in the anti-replay stage, so that in the packaging stage of the consensus process, the specified candidate blocks can be directly pulled from the candidate block queue to send the consensus, and the transaction records do not need to be pulled from the transaction pool to be packaged, so that the consensus efficiency is improved, and the consensus time is shortened.
Detailed Description
The technical solutions in the embodiments of the present application will be described below with reference to the drawings in the embodiments of the present application.
Like reference numbers and letters refer to like items in the following figures, and thus, once an item is defined in one figure, it need not be further defined and explained in subsequent figures. Meanwhile, in the description of the present application, the terms "first", "second", and the like are used only for distinguishing the description, and are not to be construed as indicating or implying relative importance.
Fig. 1 is a schematic application scenario diagram of a block chain-based transaction packaging method according to an embodiment of the present application. As shown in fig. 1, the application scenario includes a plurality ofclients 11 and a plurality ofblockchain nodes 12.Blockchain nodes 12 refer to computers, handsets, mining machines, servers, etc. in a blockchain network. Theclient 11 may be a computer, smart phone, etc. running a software application. The software application may be a shopping-type software application, a money transfer-type software application, or the like. Theclient 11 and theblock chain node 12, and the plurality ofblock chain nodes 12 are connected through a network. A P2P (peer-to-peer) network architecture is adopted betweenblockchain nodes 12. I.e., eachblockchain node 12 is peer-to-peer with each other, the nodes collectively provide service, and there is no special node. In one embodiment, theblockchain nodes 12 may be members of a federation chain, and the consensus mechanism may employ a consensus algorithm such as equity certification or PBTF (pragmatine fault tolerance), the consensus process of which is controlled by a preselected node.
In the prior art, when ablock chain node 12 receives a transaction record, transaction anti-double-blossom detection is firstly carried out, the detected transaction record is stored in a transaction pool, and then the transaction record is pulled from the transaction pool to be packaged at the stage of consensus starting, so that a block is generated. The packaging of the transaction at the beginning of the consensus takes more time, which affects the consensus progress.
In the embodiment of the application, theblockchain node 12 firstly performs deduplication processing on a plurality of transaction records in the transaction set list; then packaging the rest transaction records in the transaction set list to generate a plurality of candidate blocks; putting a plurality of candidate blocks into a candidate block queue; then, in the packing stage of the consensus process, a specified candidate block is pulled from the candidate block queue to initiate consensus. Therefore, in the consensus stage, the packed candidate blocks are directly pulled to initiate consensus, the blocks do not need to be packed, and the consensus efficiency is improved.
Fig. 2 is a schematic frame diagram of an electronic device provided in an embodiment of the present application. Theelectronic device 200 may be ablockchain node 12, theelectronic device 200 comprising: aprocessor 220; amemory 210 for storing instructions executable byprocessor 220; wherein, theprocessor 220 is configured to execute the transaction packaging method based on the blockchain provided by the embodiment of the application.
TheMemory 210 may be implemented by any type of volatile or non-volatile Memory device or combination thereof, such as Static Random Access Memory (SRAM), Electrically Erasable Programmable Read-Only Memory (EEPROM), Erasable Programmable Read-Only Memory (EPROM), Programmable Read-Only Memory (PROM), Read-Only Memory (ROM), magnetic Memory, flash Memory, magnetic disk or optical disk. The memory also stores a plurality of modules which are respectively executed by the processor to complete the following steps of the transaction packaging method based on the block chain.
Embodiments of the present application also provide a computer-readable storage medium, which stores a computer program executable by theprocessor 220 to implement the following method for packaging transactions based on blockchains.
Fig. 3 is a schematic flowchart of a transaction packaging method based on a blockchain according to an embodiment of the present disclosure. As shown in fig. 3, the method includes the following steps S310 to S340.
Step S310: and carrying out deduplication processing on the plurality of transaction records in the transaction collection list.
The transaction record may include the transaction amount, transaction account, etc., transaction time, etc. The deduplication processing refers to removing duplicate transaction records, for example, if transaction record x and transaction record y are duplicate, then transaction record x or transaction record y is removed, and the same transaction record only remains one.
The Transaction set List (List < Transaction > pendingTxns) may contain a large number of outstanding Transaction records. The blockchain node can start an asynchronous thread to perform deduplication processing on multiple transaction records in the transaction collection list.
In one embodiment, the deduplication process may be to perform anti-double-blossom detection on each transaction record and remove transaction records that fail the anti-double-blossom detection.
For example, if the same transaction record exists twice, it is equivalent to paying twice. The double-flower prevention detection refers to detecting whether the same transaction record exists, thereby avoiding repeated expenses. For example, the duplicate prevention detection for the transaction record x may be to determine whether a certain transaction record exists in the rest of transaction records (except the transaction record x) and is the same as the transaction record x, but if the transaction record x does not exist, the duplicate prevention detection for the transaction record x passes. Conversely, if present, transaction record A fails the anti-double blossom detection, and may be considered to be the same transaction record present.
In one embodiment, each transaction record may be locked in sequence, and whether the hash value of the locked transaction record is the same as the hash values of the other transaction records is determined, and if so, the locked transaction record is removed.
Wherein, the rest transaction records are the transaction records in the transaction aggregate list except the locked transaction records. And a large number of transaction records exist in the transaction set list, the transaction set list can be traversed, global locks are added to the transaction records one by one through the exclusive lock component, and whether the hash value of the locked transaction record is the same as the hash values of the other transaction records is judged. And if the hash value of a certain transaction record in the transaction records is the same as that of the locked transaction record, removing the locked transaction record. If the hash values of the other transaction records are different from the hash value of the locked transaction record, the global lock can be released, and the next transaction record is continuously locked until the judgment of all transaction records is completed. And ensuring that the hash values of the rest transaction records in the transaction set list are different, namely no repeated transaction records exist.
In an embodiment, before the step S310, when the tile chain node receives a transaction record sent by the client, the transaction record may be added to a pending checkqueue, and then an asynchronous thread is started, and the multiple transaction records in the pending queue are pulled by the asynchronous thread to form the transaction set list.
Asynchronous threads are those threads that can be executed synchronously (i.e., simultaneously) without waiting for another step to complete. The asynchronous thread may repeatedly perform the step of pulling all transaction records from the queue to be detected, with all the pulled transaction records forming a transaction set list.
In an embodiment, a first asynchronous thread may be configured, and "pull all transactions from a queue to be detected to form a transaction set list" is repeatedly executed, "configure a second asynchronous thread, and" repeatedly execute "deduplication processing (obtain a global lock, then traverse all transaction records of the transaction set list, perform double-flower prevention detection one by one, and remove transaction records in the transaction set list that do not pass double-flower prevention detection. A third asynchronous thread is configured to repeatedly perform the "transaction packing process" (i.e., steps S320 and S330 below). Through a plurality of asynchronous threads, a transaction set list is synchronously formed, and the transaction processing effect can be improved through deduplication processing and packaging processing.
Step S320: and packaging the rest transaction records in the transaction set list to generate a plurality of candidate blocks.
The remaining transaction records in the transaction aggregate list may be considered to have had duplicate transaction records removed. The candidate block is a block generated by packing the remaining transaction records in the transaction set list, and is distinguished from the common block, and the block generated by packing is called a candidate block.
A large number of transaction records exist in the transaction set list, and the data volume of each block is limited, so that a plurality of candidate blocks can be generated in a packaging mode. In an embodiment, the remaining transaction records in the transaction set list may be arranged according to a time sequence, and a plurality of candidate blocks may be generated by sequentially packing.
So that transaction records that are close in time can be packed into one candidate block. For example, after sorting the remaining transaction records in chronological order, every 10 minutes of transaction records may be packed to generate a candidate block. For example, 9: transaction records from 00-9:10 are packed to generate a first candidate block, transaction records from 9:10-9:20 are packed to generate a second candidate block, and so on.
According to the requirement, the rest transaction records can be packaged to generate a plurality of candidate blocks according to the size of the preset candidate blocks.
Step S330: placing the plurality of candidate blocks into a candidate block queue.
All the packed candidate blocks may be stored in a queue, which may be referred to as a candidate block queue for differentiation.
Step S340: at the beginning of the consensus process, a given candidate block is pulled from the candidate block queue to initiate consensus.
The candidate block queue may be a first-in-first-out mode, and the given candidate block may be the first candidate block in the candidate block queue. In an embodiment, each candidate block may be arranged in order according to the packing order. The candidate block packed first may be the first candidate block in the candidate block queue, and the specified candidate block may be the first candidate block in the candidate block queue, i.e., the oldest candidate block packed. As the candidate block queue changes, the first candidate block also changes over time. In the packaging stage of the consensus process, the block chain node can directly pull the current first candidate block from the candidate block queue to initiate consensus, so that a transaction list does not need to be pulled from a transaction pool to perform transaction packaging, and then serialization is performed.
In one embodiment, the designated candidate block may be the candidate block with the earliest current timestamp in the candidate block queue. The candidate block with the earliest current timestamp refers to the candidate block with the earliest timestamp in the candidate block queue at the current time. As the candidate block queue changes, the candidate block with the earliest current timestamp may also change. In an embodiment, the timestamp earliest candidate block may be the earliest packed candidate block. In one embodiment, in the packing stage of the consensus process, the block link node may directly pull the candidate block with the earliest current timestamp from the candidate block queue to initiate consensus, so that the transaction packing is performed without pulling the transaction list from the transaction pool, and then serialization is performed.
According to the technical scheme provided by the embodiment of the application, before the consensus process is started, the transaction records are packaged in advance in the anti-replay stage, so that in the packaging stage of the consensus process, the specified candidate blocks can be directly pulled from the candidate block queue to send the consensus, and the transaction records do not need to be pulled from the transaction pool to be packaged, so that the consensus efficiency is improved, and the consensus time is shortened.
In an embodiment, in order to ensure that the double flowers are protected against the hundred percent, as shown in fig. 4, after the step S340, the method provided in the embodiment of the present application further includes:
step S350: removing the designated candidate block in the candidate block queue.
For example, after a consensus is initiated for a given candidate block (e.g., the first candidate block), the first candidate block in the candidate block queue may be removed.
Step S360: and at the submission stage when the designated candidate block is successfully identified, judging whether a transaction list contained in the current first candidate block in the candidate block queue and the transaction list contained in the designated candidate block have transaction records with the same hash value.
Assuming that the order of the candidate block is candidate block a, candidate block B, candidate block C … …, and so on, when the candidate block is designated as candidate block a, then the current first candidate block after candidate block a is removed is candidate block B. After the candidate block a is successfully identified, the candidate block a is to be uplinked (i.e., committed) after the previous block with successful identification. After the candidate block a is successfully identified, the next candidate block initiating the identification is the candidate block B, so to further avoid the replay, it can be determined whether the transaction list of the candidate block B and the transaction list of the candidate block a have the same transaction record with the hash value. The hash value may be a unique identifier of the transaction record, and the hash values are the same and may be considered to be the same transaction record.
Step S370: and if so, removing the transaction records with the same hash value in the first candidate block.
If the transaction list of candidate blob B and the transaction list of candidate blob a have transaction records with the same hash value, then to avoid replay, the transaction records in candidate blob B with the same hash value may be removed. Therefore, after the candidate block B is linked up, the same transaction record does not exist with the candidate block a, so that the transaction is ensured not to be repeated. On the contrary, if there is no transaction record with the same hash value between the transaction list of the candidate block B and the transaction list of the candidate block a, the candidate block a is directly uplink-linked without performing deduplication on the candidate block B.
The following is an embodiment of the apparatus of the present application, which can be used to implement the above embodiment of the transaction packing method based on blockchain of the present application. For details not disclosed in the embodiments of the apparatus of the present application, please refer to the embodiments of the transaction packing method based on blockchain of the present application.
Fig. 5 is a block diagram of a transaction packaging apparatus based on a blockchain according to an embodiment of the present disclosure. As shown in fig. 5, the apparatus includes: adeduplication processing module 510, a packingcandidate module 520, ajoin queue module 530, and aconsensus initiation module 540.
Adeduplication processing module 510, configured to perform deduplication processing on multiple transaction records in the transaction aggregation list;
a packingcandidate module 520, configured to pack the remaining transaction records in the transaction set list to generate a plurality of candidate blocks;
aqueue adding module 530, configured to place the candidate blocks into a candidate block queue;
aconsensus initiating module 540, configured to pull the specified candidate block from the candidate block queue to initiate consensus when the consensus process starts.
The implementation process of the functions and actions of each module in the above device is specifically described in the implementation process of the corresponding step in the above transaction packing method based on the block chain, and is not described herein again.
In the embodiments provided in the present application, the disclosed apparatus and method can be implemented in other ways. The apparatus embodiments described above are merely illustrative, and for example, the flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of apparatus, methods and computer program products according to various embodiments of the present application. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). In some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
In addition, functional modules in the embodiments of the present application may be integrated together to form an independent part, or each module may exist separately, or two or more modules may be integrated to form an independent part.
The functions, if implemented in the form of software functional modules and sold or used as a stand-alone product, may be stored in a computer readable storage medium. Based on such understanding, the technical solution of the present application or portions thereof that substantially contribute to the prior art may be embodied in the form of a software product stored in a storage medium and including instructions for causing a computer device (which may be a personal computer, a server, or a network device) to execute all or part of the steps of the method according to the embodiments of the present application. And the aforementioned storage medium includes: a U-disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and other various media capable of storing program codes.