Disclosure of Invention
In order to improve the replay attack prevention capability of a distributed public accounting system, the application provides a replay attack prevention method for alliance chain transaction under an account system.
The application provides a method for preventing replay attack of alliance chain transaction under an account system, which comprises the following steps:
acquiring a transaction request sent by a client;
acquiring the client time of the transaction request, and if the client time is earlier than a first reference time point T1, rejecting the transaction request;
judging whether the client time is earlier than or equal to a second reference time point T2, and if the client time is earlier than or equal to a second reference time point T2, comparing the transaction corresponding to the transaction request with all transactions in a transaction cache pool P;
if the transaction corresponding to the transaction request exists in the transaction cache pool P, rejecting the transaction request; and if the transaction corresponding to the transaction request does not exist in the transaction cache pool P, packaging the transaction corresponding to the transaction request into the current block, and caching the transaction corresponding to the transaction request into the transaction cache pool P.
Optionally, after the step of obtaining the client time of the transaction request, the method further includes:
if the time of the client is later than or equal to a first reference time point T1, judging that the transaction signature corresponding to the transaction request is correct, and if the transaction signature corresponding to the transaction request is correct, putting the transaction corresponding to the transaction request into a transaction pool; and if the transaction signature corresponding to the transaction request is incorrect, rejecting the transaction request.
Optionally, the step of determining whether the client time is earlier than or equal to a second reference time point T2 further includes:
if the client time is later than a second reference time point T2, continuously keeping the transaction request corresponding to the client time in a transaction pool;
and when the client time is earlier than or equal to the updated second reference time point T2, judging whether the client time is later than or equal to the updated first reference time point T1, and if the client time is later than or equal to the updated first reference time point T1, comparing the transaction corresponding to the corresponding transaction request with all transactions in the transaction cache pool P.
Optionally, n blocks are taken forward by the height m of the current block, and the construction time of the m-n blocks is selected as the first reference time point T1.
Optionally, the method for preventing replay attack of federation chain transaction under an account system further includes:
and acquiring block packing time corresponding to all transaction requests in the transaction cache pool P, and removing the transaction requests with the block packing time being earlier than or equal to a first reference time point T1 from the transaction cache pool P.
Optionally, the step of packaging the transaction request into the current block further includes:
acquiring the packing time of a previous block, and if the packing time of the previous block is less than or equal to the local time of a packing node of the current block, setting the packing time of the current block as the local time of the packing node of the current block;
and if the packing time of the previous block is greater than the local time of the packing node of the current block, calculating the average block-out time delta K of K blocks before the current block, and delaying the average block-out time delta K in the packing time of the previous block to be used as the packing time of the current block.
Optionally, the step of calculating the average block-out time Δ K of K blocks before the current block specifically includes:
acquiring K block outlet time corresponding to K blocks before the current block, and judging whether the block outlet interval time between any two continuous blocks in the K blocks is greater than the default block outlet interval time;
and if the interval time between two continuous blocks is greater than the default block time interval, replacing the interval time between two continuous blocks by the default block time interval, and calculating the average block output time delta K of the K blocks by the replaced block output interval time.
Optionally, the method includes:
the clock between each node is regularly calibrated, and the clock synchronization between each node is kept;
after the step of packaging the transaction request into a current block, further comprising:
and after receiving the current block generated by the current block packaging node, the replica node compares the construction time of the current block with the local time of the replica node, and if the interval between the construction time of the current block and the local time of the replica node is greater than a first preset threshold, the replica node does not vote for the current block.
Optionally, the method for preventing replay attack of federation chain transaction under an account system further includes:
after the node is down, acquiring a currently confirmed block of the node, searching n blocks from the currently confirmed block forward, and caching transaction requests corresponding to the n blocks into a transaction cache pool P.
Optionally, after the step of obtaining the client time of the transaction request, the method further includes:
and comparing the client time of the transaction request with the local time of the node, and if the interval between the client time of the transaction request and the local time of the node is greater than a second preset time, rejecting the transaction request.
Optionally, in the step of packaging the transaction request into a block, the building time of the block is strictly increased.
According to the technical scheme, the replay attack defense method for the alliance chain transaction under the account system comprises the following steps: acquiring a transaction request sent by a client; acquiring the client time of the transaction request, and if the client time is earlier than a first reference time point T1, rejecting the transaction request; judging whether the client time is earlier than or equal to a second reference time point T2, and if the client time is earlier than or equal to a second reference time point T2, comparing the transaction corresponding to the transaction request with all transactions in a transaction cache pool P; if the transaction corresponding to the transaction request exists in the transaction cache pool P, rejecting the transaction request; and if the transaction corresponding to the transaction request does not exist in the transaction cache pool P, packaging the transaction corresponding to the transaction request into the current block, and caching the transaction corresponding to the transaction request into the transaction cache pool P.
In the practical application process, by designing the sliding time window, only all packaged transactions after the first reference time point T1 need to be recorded, and in the case of a relatively short time window, the transaction records may be cached in the memory, and in the case of a relatively long time window, the transaction records may be considered to be stored in the database. Due to the fact that the stored transaction scale is small, the partial transactions can be placed in the cache under the condition that the time window is not particularly long, the transaction judging speed is extremely high, and the Tps of the system cannot be influenced. And the packaging execution of the transaction is not strictly limited in sequence, and the failure of one transaction does not influence the normal packaging execution of other transactions. And the size of the time window can be dynamically adjusted according to the Tps size of the system, so that various scenes are met. Because only n blocks ahead of the current block need to be cached into the transaction cache pool, the recovery of the transaction cache pool under abnormal conditions such as node downtime is very simple, and the performance of the alliance chain system cannot be influenced.
Detailed Description
The technical solutions in the embodiments of the present application will be described clearly and completely with reference to the drawings in the embodiments of the present application, and it is obvious that the described embodiments are only a part of the embodiments of the present application, and not all of the embodiments. All other embodiments, which can be derived by a person skilled in the art from the embodiments of the present application without making any creative effort, belong to the protection scope of the present application:
fig. 1 is a schematic overall flow chart of a replay attack prevention method for federation chain transaction under an account system according to an embodiment of the present application. The replay attack prevention method for the alliance chain transaction under the account system comprises the steps S101 to S104.
Step S101: and acquiring a transaction request sent by a client.
Step S102: and acquiring the client time of the transaction request, and rejecting the transaction request if the client time is earlier than a first reference time point T1.
Step S103: judging whether the client time is earlier than or equal to a second reference time point T2, and if the client time is earlier than or equal to a second reference time point T2, comparing the transaction corresponding to the transaction request with all transactions in a transaction cache pool P; .
Specifically, the first reference time point T1 is obtained by taking n blocks from the height m of the current block, and selecting the construction time of the m-n block as the first reference time point T1, wherein the second reference time point T2 is the current time point of the node, i.e., the height m of the current block. In the prior art, for whether a transaction is legal in time uplink, a network clock of each node is generally referred to, but in an actual application process, due to reasons such as network delay, if the network clock of each node is selected to calculate the reference time, a time interval instead of an accurate time point is selected as the reference time, so that a transaction request, for example, a transaction request is a transaction of a transaction client time in the time interval corresponding to the reference time, but due to the fact that the network clocks of the nodes are not consistent, the judgment of the same transaction request and different nodes is not consistent.
Referring to fig. 2, a schematic diagram of a determination principle of the first reference time point T1 provided for the embodiment of the present application, for normally identified nodes, each node locally has the same one blockchain copy, and the building time of a block that has landed is determined. Therefore, with the tile height as the reference standard, taking n tiles forward from the height m of the current tile, and selecting the time of the tile construction as the first reference time point T1, it is necessary to buffer the transactions in n-1 tiles forward from the current tile in the transaction buffer pool P. Along with the consensus, the height m of the block to be packed can be continuously increased, and every time m is increased by 1, a new time interval is corresponded.
If the time of the client is later than or equal to a first reference time point T1, judging that the transaction signature corresponding to the transaction request is correct, and if the transaction signature corresponding to the transaction request is correct, putting the transaction corresponding to the transaction request into a transaction pool; and if the transaction signature corresponding to the transaction request is incorrect, rejecting the transaction request.
And the transaction node acquires the transaction request from the transaction pool, judges whether the client time of the transaction request is earlier than or equal to a second reference time point T2 or not, and compares the transaction corresponding to the transaction request with all transactions in the transaction cache pool P if the client time is earlier than or equal to the second reference time point T2.
And if the client time is later than a second reference time point T2, continuously keeping the transaction request corresponding to the client time in a transaction pool.
The method comprises the steps that blocks are continuously generated along with the time, when the high end m of the current block changes once, the corresponding first reference time point T1 and the second reference time point T2 are updated, when the time of a client is earlier than or equal to the updated second reference time point T2, whether the time of the client is later than or equal to the updated first reference time point T1 is judged, if the time of the client is later than or equal to the updated first reference time point T1, transactions corresponding to corresponding transaction requests are compared with all transactions in a transaction cache pool P, and if the transactions corresponding to the transaction requests exist in the transaction cache pool P, the transactions corresponding to the transaction requests need to be removed from the transaction cache pool. It should be noted that the system does not create a new block, the first reference time point T1 is updated once, and correspondingly, the transaction requests smaller than the first reference time point T1 are removed from the transaction pool.
Step S104: if the transaction corresponding to the transaction request exists in the transaction cache pool P, rejecting the transaction request; and if the transaction corresponding to the transaction request does not exist in the transaction cache pool P, packaging the transaction request into the current block, and caching the transaction corresponding to the transaction request into the transaction cache pool P.
In the model corresponding to the method for preventing a replay attack of a transaction provided in the embodiment of the present application, a transaction cache pool P needs to be established for each channel, and the client time of the transaction cache pool P for caching the transaction is at the first reference time point T1, and the transaction is successfully packaged. The first reference time point T1 is used to determine whether a transaction is within the valid receiving time, and all transactions with transaction client time earlier than T1 are determined to be expired transactions.
The specific process is as follows: when the packaging block of the current block needs to package a transaction, firstly, whether the client time of the transaction request is within the valid time is judged, if the client time CT of the transaction request is earlier than T1, the transaction corresponding to the transaction request is considered to exceed the packaging valid time, and the packaging of the transaction is refused. If the client time of the transaction request is within the valid time, that is, CT is later than T1, it is determined whether the transaction corresponding to the transaction request exists in the transaction cache pool P, if so, it indicates that the transaction request has been repeatedly executed after referring to the time point T, packaging of the transaction corresponding to the transaction request is rejected, and if not, packaging of the transaction corresponding to the transaction request into the block is successful.
Further, in this embodiment of the present application, since the height m of the to-be-packaged chunk is continuously increased as the consensus proceeds, and m is increased by 1 every time, so that the first reference time point T1 varies with the height of the to-be-packaged chunk (the current chunk), in order to ensure that the transaction cached in the transaction cache pool P is always within the validity period, in this embodiment of the present application, the method for preventing replay attack on federation chain transaction under the account system further includes: and acquiring block packing time corresponding to all transaction requests in the transaction cache pool P, and removing the transaction requests with the block packing time being earlier than or equal to a first reference time point T1 from the transaction cache pool P.
Specifically, as the block height increases, each block increase means that the oldest (the block with the smallest height in the cache) block is invalidated, so that the transaction corresponding to the invalidated block is deleted from the transaction cache pool.
In the embodiment of the present application, the transactions cached in the transaction cache pool P should be all transactions executed after the first reference time point T1, that is, all transactions requested after the first reference time point T1 should not be packed in m-n blocks and the previous blocks, that is, it is required to ensure that the first reference time point T1 for establishing the transaction cache pool P can strictly divide the time zone, and the first reference time point T1 at different stages is strictly increased in time, so as to avoid that the first reference time point T1 at the next time is earlier than the first reference time point T1 at the previous time, and thus two conditions are required to be satisfied, where: the building time of a block is strictly increasing; and a second condition: the client time of the transaction request for all transactions packed within a block must be less than the build time for that block.
In this embodiment of the present application, in order to meet the first condition, it is required that the block-out time of each consensus node must be strictly increased, and in an actual application process, when a system has multiple node consensus, because clocks of the nodes are not completely synchronized, if a time interval for node block-out is small, the block-out time may not be strictly increased, and in order to ensure that the block-out time of a node is strictly increased under the condition that multiple node consensus exists, in this embodiment of the present application, the step of packaging the transaction request into the current block further includes steps S201 to S202.
Step S201, obtaining the packing time of the previous block, and if the packing time of the previous block is less than or equal to the local time of the current block packing node, setting the packing time of the current block as the local time of the current block packing node.
Step S202, if the packing time of the previous block is greater than the local time of the current block packing node, calculating an average block-out time Δ K of K blocks before the current block, and delaying the average block-out time Δ K in the packing time of the previous block as the packing time of the current block.
Referring to fig. 3, a schematic diagram of an obtaining principle of the average block output time Δ K of K blocks before the current block provided by the embodiment of the present application is shown, where the average block output time Δ K of K blocks before the current block is obtained by steps S301 to S302.
Step S301, obtaining K block output times corresponding to K blocks before the current block, and determining whether a block output interval time between any two consecutive blocks in the K blocks is greater than an default block output interval time.
Step S302, if the interval time between two consecutive blocks is greater than the default block time interval, replacing the interval time between two consecutive blocks with the default block time interval, and calculating the average block output time Δ K of the K blocks with the replaced block output interval time.
It should be noted that once a block is written into a block, the block construction time belongs to an attribute that cannot be modified in the block, and here, replacing the interval time between two consecutive blocks with a default block time interval does not mean modifying the time of the original block, but only using the default block time interval to calculate the average block time.
Specifically, in order to ensure that the block construction time of the previous block of the current block is not particularly off-spectrum, that is, the error between the local clock of the leader packing node and the clock of other nodes cannot be too large, the following two measures may be taken:
the first measure is as follows: the clock between each node is regularly calibrated, the clock synchronization between each node is maintained, specifically, when the operation and maintenance level redeploys the system, a tool is used for maintaining the clock synchronization between the nodes, the current physical clock synchronization can be accurate to the second level, and therefore, in the practical application process, the second level synchronization can completely meet the design requirement.
And step two: after the step of packaging the transaction request into a current block, further comprising:
after receiving the current block generated by the current block packaging node, the replica node compares the construction time of the current block with the local time of the replica node, and if the interval between the construction time of the current block and the local time of the replica node is greater than a first preset threshold, the replica node does not vote for the current block
Specifically, the size of the first preset threshold may be set according to actual design requirements, in this embodiment of the present application, the first preset threshold is taken for 2 minutes, if the interval between the building time of the current block and the local time of the replica node exceeds the first preset threshold, the block is not voted for, because the node at this time is likely to be a byzantine node, and through the set first preset threshold, the byzantine attack of the malicious node is effectively performed.
For the transaction meeting the first preset threshold, the leader node takes an effective transaction- > leader node from the transaction pool to construct a block, the block construction time- > leader node is set to send the block to the replica node- > the replica node to vote- > the next leader node collects the vote, if the vote exceeds 2/3, the block is identified, the block identification time- > block persistence is set, and the block persistence time is set.
Specifically, in the embodiment of the present application, in order to reduce the error of the average block-out time Δ K of the first K blocks of the current block, it is considered that the node is not transactedThere are no blocks, and if a transaction is sent in two of the K blocks with a long time between them, then the value of ak will be large, and if there are multiple such cases, a larger error will result. As shown in fig. 3, if the block-out time (t) of a certain block is calculatedk-1) And the block-out time (t) of the last blockk-2) Inter block-out time (t) in betweenk-2-tk-1) If the default block output time interval is greater than the default block output time interval, the default block output time interval is used as the block output time interval between the block and the previous block, and it should be noted that, in this embodiment of the present application, the default block output time interval is set to 2 seconds.
For the second condition, because the Leader node needs to perform a judgment when packaging the transaction, only the transaction of which the client time is earlier than the current time can be successfully packaged, and the building time of the block is certainly later than the packaging time, thereby ensuring that the second condition is met.
In the embodiment of the present application, the method for preventing replay attack of federation chain transaction under an account system further includes: and after the node is down, acquiring the current confirmed block of the node, searching n blocks from the current confirmed block forward, and performing the operation. The transaction requests corresponding to the n blocks are cached in the transaction cache pool P, and for the solution of recording all historical transactions, data loss caused by node downtime or physical damage can cause the data to be unusable, and it takes a very long time to reestablish all transaction records. According to the scheme, when the node is down or the data cache data is lost due to physical damage of the hard disk, the transaction cache pool P can be recovered quickly.
In order to prevent the client from tampering the time at will, in the embodiment of the present application. After the step of obtaining the client time of the transaction request, the method further includes: and comparing the client time of the transaction request with the local time of the node, and if the interval between the client time of the transaction request and the local time of the node is greater than a second preset time, rejecting the transaction request. Specifically, in the embodiment of the present application, the second preset time is set to 10 minutes, for example, the local time of the node is 15:00, and the client time of the transaction request that can be received is 14:50 to 15: 10.
According to the replay attack prevention method for the alliance chain transaction under the account system, when a node receives a new transaction request, if the client time of the transaction request is earlier than the first reference time point T1, the fact that the transaction corresponding to the transaction request is packaged in blocks before the first reference time point T1 is shown, but the transaction of the blocks is determined, so that the transaction request is considered to exceed the packaging validity period of the transaction, and the transaction request is rejected. If the client time of the transaction request is later than the first reference time point T1 and the client time of the transaction request is earlier than or equal to the second reference time point T2, the transaction cache pool P is queried for the presence of the transaction, and if the transaction is present, the transaction is declared and packaged, and the transaction request is discarded. If the transaction request does not exist, the transaction corresponding to the transaction request is a valid transaction, the packaging is performed in the latest block, and all transaction requests before the first reference time point T1 by the transaction client time are rejected, because the transaction before the first reference time point T1 is not cached, it cannot be determined whether the transaction has been packaged before the first reference time point T1. Through actual tests in a Newstand new generation alliance chain, under the condition that the block output interval is 2s, the transaction request number cached in the transaction cache pool P is 150, all transactions can be guaranteed to be normally packaged, and the condition that the transactions are rejected because the transaction exceeds the validity period hardly occurs. Certainly, with the advancement of the blockchain technology, the Tps (system throughput) of the federation chain will reach the level of one hundred thousand or even millions in the future, and at this time, the technical solution is still applicable as long as the cache height of the block is increased, for the increase of Tps, the parameter n may be adjusted to perform dynamic adaptation, and other methods have no adjustment capability.
According to the replay attack prevention method for the alliance chain transaction under the account system, through the design of the sliding time window, all packaged transactions only need to be recorded after the first reference time point T1, the transaction records can be cached in the memory under the condition that the time window is short, and the transaction records can be considered to be stored in the database under the condition that the time window is long. Due to the fact that the stored transaction scale is small, the partial transactions can be placed in the cache under the condition that the time window is not particularly long, the transaction judging speed is extremely high, and the Tps of the system cannot be influenced. And the packaging execution of the transaction is not strictly limited in sequence, and the failure of one transaction does not influence the normal packaging execution of other transactions. And the size of the time window can be dynamically adjusted according to the Tps size of the system, so that various scenes are met. As only n blocks from the current block to the transaction cache pool need to be cached, the recovery of the transaction cache pool under abnormal conditions such as node downtime is very simple, and the performance of the alliance chain system cannot be influenced.