NTP clock server (Beidou satellite time service) synchronous block chain distributed system


NTP clock server (Beidou satellite time service) synchronous block chain distributed system

NTP clock server (Beidou satellite time service) synchronous block chain distributed system

Beijing Zhun electronic technology official micro enterprise — ahjzsz

Distributed system is defined by Tanenbaum, “distributed system is a group of independent computers, which appear as a single, coherent system of users in” distributed system – Principles and examples “.

Blockchain tries to realize decentralized new data storage and organization structure by building global distributed system.

First of all, the reasons for positioning to distributed systems are mainly scalability, location and availability. Blockchain is no exception. Geographic scalability, forming a global value storage network / information protection area, including tamper proof / zero downtime availability under a decentralized architecture. All of these will be implemented in block with distributed system.

0. Contents

10. Blockchain and distributed system

1. Introduction (overview of synchronization and overall process)


2–1。 Physical clock (clock and clock offset)

2–2。 Clock synchronization algorithm (Network Time Protocol (NTP) / Berkeley algorithm)

3. Logic clock

3–1。 Lamport’s logical clock (fully ordered multicast)

3–2。 Vector clock (causal order multicast)

4. Exclusive control

4–1。 Centralized algorithm

4–2。 Decentralized algorithm

4–3。 Distributed algorithm

5. Election algorithm

5–1。 bully algorithm

5–2。 Ring algorithm

6. Block chain and synchronization as distributed system

6–1。 Block chain and clock synchronization (block chain and physical / logical clock)

6–2。 Control block algorithm in exclusive and exclusive control chain

6–3。 Block chain and leader election algorithm (Leader Selection Algorithm in pow · POS · BFT)

1. Introduction (overview of synchronization and overall process)

Unlike centralized systems, it is not easy to agree on time in a distributed system.

In the former case, the absolute order relationship can be determined based on the global shared clock, but in the latter case, due to the existence of clock value error and corresponding time, it is difficult to share the absolute time.

However, the order of absolute time is not absolutely necessary. If the relative order is fixed, it is usually enough.

In this article, synchronization between nodes is explained in the following order.

How does clock synchronization happen?

Relative sorting method using logical clock and vector clock

On the elimination control algorithm of consistency in distributed system

Leader election algorithm in distributed system

2. Clock synchronization

2 – 1. Physical clock

Clock and clock skew

Most computers have hold timeCircuitThis device is called a clock. It’s frequency basedVibrationThe vibration can be clearly defined by the type of crystal, the method of cutting and the amount of pressure at which tension is applied to precisely machined quartz.

Although this frequency is fairly stable, there is no guarantee that all crystals in different computers will run at exactly the same frequency. The resulting difference in synchronization time is called clock skew.

In this case, especially in real-time systems, how to synchronize multiple clocks with real-time clocks is a problem.

Time in the real world was originally based on the average solar second, but now the time of 9192631770 cesium-133 transitions is defined as one second, and the international atomic time and universal coordinated time (UTC) are defined. In order to provide UTC to those who need accurate time, WWV is used and the time is provided with an accuracy of ± 10 ms.

2 – 2. Clock synchronization algorithm

However, most machines do not have WWV receivers. Therefore, each machine needs time tracking and management algorithms so that all machines can synchronize time.

Incidentally, the error used to determine if resynchronization is required, the real-time clock offset, is measured as follows.

H is defined as the number of interrupts per second caused by crystal vibrations counted by each machine, and C is represented as the value of the clock. Let CP (T) be the clock value of the machine, when UTC time is t.

If P is defined as the maximum drift rate that defines the amount of clock skew allowed, it is assumed that it operates in the following range.

1-p 《= dC/dt 《= 1+p

That is, after at seconds from the start of the previous synchronization, the two clocks are separated by up to 2p Δ t.

When it is guaranteed that there is no deviation greater than & during operation execution, the software must be resynchronized at least every & / 2p.

Network time protocol (NTP)

It is common in many protocolstiThe method first proposed by an, 1989 is a method of communication with client server. Because the time server has a WWV receiver or has an accurate clock, it can provide the current time. When communicating with the server, it is important to delay reporting the delay in message propagation, but by estimating the delay, errors can be minimized here. Currently, NTP is known to achieve accuracy in the range of 1 to 50 ms.

Berkeley algorithm

In many algorithms such as NTP, the time server is passive and only answers queries. On the other hand, in Berkeley algorithm, the time server receives the time held by each participating node and changes its own time based on the average value. When the time value does not have to be related to the real world, it is easy to reach an agreement at the same current time, and it is effective for this algorithm.

3. Logic clock

So far, although we have described a method of synchronizing the clock with absolute time based on the actual clock as a reference, only relative synchronization is usually performed. Here, the concept of logical clock is used to determine the relative order.

3 – 1. Lamport’s logical clock

To synchronize the logical clock, Lamport defines a relationship called happen before. The expression a → B means “a occurs before B”, which means that the event occurs first, and then all processes agree that event B will occur. Before   –   the relationship can be observed directly in the following two situations.

If a and B are events in the same process and a appears before B, then a → B is true.

If the message of a → B is received by another process, then the message is sent by another process B. A message cannot be received before it is sent, even if the message also requires a limited non-zero time.

If a → B and B → C, then a → C can be proved. If events x, y occur in different processes that do not exchange messages, then neither x → y nor y → x is true, and these events are considered concurrent. (the previous relationship is unknown.)

Using the logical clock, the relative time is measured by allocating a consistent time C (a) of all processes for each event a. If these time values are a → B, they are corrected by adding positive values to the time such that C (a) C (b). By assigning a time value as shown in the figure below, you can master the previous relationships.


In Lamport’s logic clock, if a → B, then C (a) C (b) can be proved, but if C (a) C (b), then a → B is not necessarily true. In other words, a → B is a necessary condition of C (a) C (b), and is not a sufficient condition. The logic clock of Lamport has been improved. It is a vector clock, which can meet the necessary and sufficient conditions.

Fully ordered multicast

For more information, see the article, “distributed system consistency.”

In many cases, it is necessary to perform fully ordered multicast between duplicate copies. In other words, all messages need to be delivered to each recipient in the same order. Lamport’s logical clock can be used to implement fully ordered multicast in a fully distributed system.

When a process receives a message, it places it in the local queue in order based on the timestamp. The recipient multicast confirms to another process. If you adjust the local clock according to Lamport’s algorithm, all processes actually have the same copy of the local queue. Only when the message is at the head of the queue and acknowledged by all other processes, can a process deliver messages from the queue to the running application, so all messages are delivered everywhere in the same order. In other words, a fully ordered multicast has been established.

3 – 2. Vector clock

Using vector clock, we can master the causality that cannot be mastered by Lamport logic clock. Assuming that the vector clock of event a is VC (a), perform the following steps to make a → B a necessary and sufficient condition for VC (a) VC (b).

Before sending a message over the network, the nodePiAdd 1 to the vector clock VCI [i], or operate some internal events.

2. If the processing PI sends the message M to PJ, PI sets the vector timestamp TS (m) of M equal to VCI after executing the previous step.

3. When the message M is received, the process PJ performs step 1, distributes the message to the application, and then updates each k of its own vector clock as follows: VCJ [k] ← Max {VCJ [k], TS (m) [k]}.


Causality multicast

By using the vector clock, the causal ordered multicast which is slightly weaker than the fully ordered multicast described above can be realized.

By comparing the value of the vector clock and mastering the relationship that happened before, for a specific event x, other events can be classified as past events, concurrent events and future events. For example, in the above figure, when event D is used as a reference point, past events are a, B, C, I, concurrent events are j, l, m, and future events are f, G, H.

At this point, it is assumed that causal ordered multicast is a sequence of past events and causal events, in which all causal relationships occur in order to be consistent in all processes, but the order of concurrent events is irrelevant. In this way, unlike Lamport’s logical clock, vector clock can be used to master causality.

4. Exclusive control

The concurrent operation and cooperative operation between multiple processes are the basic of distributed system. However, in order to ensure the exclusive access to resources, so that the same resources are not in inconsistent state when multiple processes access the same resources at the same time, distributed exclusive algorithm is needed.

Distributed exclusive control algorithms can be divided into the following two types.

Token based solutions

Permission based approach

In token based schemes, starva is easy to avoidTIOn (not allowed to access resources for a long time) and dielock(multiple processes wait for each other’s progress). A typical example is token ring algorithm. However, when the process of holding token stops abnormally and the token is lost, it is necessary to generate only a new token, which is a serious disadvantage.

Many other decentralized exclusive control algorithms use permission based methods, and there are many different ways to obtain permissions, which we will explain in detail.

4 – 1. Centralized algorithm

By simulating the function of single processor system, we can easily realize the single access of exclusive control in distributed system. In the centralized algorithm, a process is designated as a coordinator, and when a process accesses a shared resource, a request message is sent to the coordinator to obtain permission. If the shared resource is not accessed by other processes, the coordinator returns a permission response, and after receiving the reply, the requested process executes the process.

It is easy to see that the algorithm guarantees exclusive access to resources, but it has the serious disadvantage of single point failure. Although this may be a performance bottleneck in a large system, the advantages of this simplicity can still make up for these shortcomings.

4 – 2. Decentralized algorithm

Suppose that each item will be repeated N times. In the decentralized algorithm, when a process accesses resources, it needs to approve most M ﹣ n / 2. If a majority approval is obtained, the process is licensed and can be processed.

Although the scheme solves the single point failure problem of the centralized algorithm, if there are too many nodes trying to access, there is another problem, that is, no node can get enough votes to obtain sufficient performance.

4 – 3. Distributed algorithm

In this algorithm, it is assumed that the order of all events on the system can be defined as a completely ordered relationship. As a basis, the logical clock of Lamport described in the previous chapter is used, and no messages are assumed to be lost.

When a process attempts to access a shared resource, it creates a message containing the resource name, its own process number, and the current logical clock, and sends it to all other processes. When the request message is received, the following operations are performed according to its own state.

1. If the recipient does not access the resource and does not attempt to access the resource, the recipient will return an OK message to the sender.

2. If the recipient is already accessing the resource, please do not reply and execute the queuing request.

3. If the recipient is trying to access a resource but has not yet completed, compare the timestamp in the input message with that in the message sent to other processes, and take the lower one as the winner. If the received message has a small timestamp, the recipient returns an OK message. If its own message has a small timestamp, the receiver will not queue the input message.

Obviously, if it doesn’t conflict like process 1 or 2, the algorithm will work. Even in the case of a conflict, only the only condition that a process can access is established.

Like the centralized algorithm, the algorithm can guarantee exclusive control without deadlock or starvation. In addition, there is no single point of failure. However, the single point fault is replaced by the fault n location feature. It can be solved by replying or rejecting permissions and introducing timeouts, but there are other problems, such as the need for multicast primitives. Unfortunately, no distributed algorithm has been designed and is still under study.

When comparing the algorithms, it becomes as follows.


5. Leader election algorithm

Many distributed algorithms require a special process, which has the role of leader as coordinator or initiator. Which process is the leader, and whether the only process can be a leader is an important question, which researchers have been working on for decades.

5 – 1. Bullying algorithm

When the coordinator fails and any process P notices the situation, P activates the election according to the following procedure.

·P sends elec to all processes with a higher value than itselfTIOn message.

·If no one answers, P will win the election and become the coordinator.

·If the answer comes from a process with a value higher than P, it is replaced. P’s work is over.

With this algorithm, the coordinator can be uniquely determined. However, the algorithm needs a lot of message and data traffic, which can be said to be redundant. As an alternative, there is a ring algorithm.

5 – 2. Ring algorithm

Different from the general ring algorithm, this algorithm does not use token. Find any process that the coordinator does not work. Construct an elec with its own process numberTIOn message and sends it to its successor (the next node in the ring). If the successor fails, skip. If there is no node with a higher value than you, your message will still be returned to your own process number, so it will be designated as coordinator.

In this algorithm, leader election with reduced number of messages is performed, but the algorithm with less data traffic can also be implemented by setting the message destination to two adjacent nodes.

6. Block chain and synchronization as distributed system

Therefore, in the block chain as one of the distributed systems, how does the synchronization between processes occur?

6 – 1. Blockchain and clock synchronization

Clock and logic block

First, consider whether the physical clock in the blockchain can be used to master the absolute time relationship. As mentioned in Chapter 2, each node participating in the network does not always maintain the correct physical clock, and there should be clock skew. becauseBitcoinThe average generation time of blockchain is 10 minutes, so it is considered that even a certain degree of large clock deviation is acceptable. However, when nodes are scattered around the world, it is difficult to synchronize the physical clocks, and there may be nodes that camouflage clocks. It is difficult to resynchronize the correct time between nodes by introducing network time protocol (NTP).

Blockchain and logical clock

Therefore, it is practical to prepare a logical clock instead of a physical clock. In fact, by adding time marks to the block, a mechanism very similar to the Lamport logic clock can be prepared.

As described in bitcoin: Satoshi Nakamoto, a peer-to-peer e-cash system, each node that writes to a block as a miner has its own role as a timestamp server. Each timestamp is chained by including the previous timestamp in its hash. However, there is no guarantee that these nodes will maintain the correct physical clock. The value of the timestamp, that is, the order and time of each transaction is relatively ambiguous.

Due to this ambiguity of the clock, it is possible to make double payments. However, in the bitcoin blockchain, only the longest chain is legal, and incorrect transactions are discarded after secondary verification. Therefore, the sequence of blocks is uniquely determined over time. As each timestamp increases, the previous timestamp is enhanced.

In short, the order consistency of transactions is not accurate under the fuzzy timestamp in the blockchain. However, with the simple mechanism of chain connection, the pre occurrence relationship of each transaction is established over time. In addition, there is an incentive structure so that miners transfer to good and inconsistent orders of transactions do not occur.

The relationship between clocks can be more clearly defined as the relationship between clocks before the implementation of Lamport.

For most transactions, there is no causal relationship, so if you introduce the vector clock and adopt the concept of causal ordering, you can greatly relax the constraints on the order relationship. However, in a blockchain, since the structure itself shares the order of all blocks by default, the total sort (relative to blocks after a period of time) is maintained.

6 – 2. Blockchain and exclusive control algorithm

Even in the blockchain as a distributed system, it is necessary to exclude control. In the blockchain network, each node operates asynchronously in parallel. At this point, the information of the blockchain itself to be shared should not be inconsistent.

Exclusive control algorithm in pow · POS

As described in Chapter 4, distributed exclusive control algorithms can be divided into the following two types.

·Solution based on token

·Rights based solutions

POW and POS are permission based, in which, it can be said that it is similar to the distributed algorithm mechanism. So when do you get access to resources? Yes, just when you find a random number.

In pow, a valid new block write operation can only be performed if a random number with a hash value followed by 0 followed by N is found. And broadcast it to all miners.

Usually, when a node finds a nonce and creates a block earlier than itself, the minor synchronizes the information and moves to search for the next nonce value. This is because if you search for the next nonce value using rules that are considered legal for the longest chain, they can make more profit. Although POS gives priority to providing resource access for people with large coin holdings, the basic exclusion control algorithm structure is similar to the distributed algorithm.

However, strictly speaking, exclusion control is not enforced. This is to synchronize and form a consensus in common time for 10 minutes until the next block. When two or more nodes find random values at the same time, the write operation is performed in an exclusive state. At this point, since only the longest chain is considered legal, the information in the blockchain network is consistent with the passage of time. One of the problems with forks is that strict exclusive control is not enforced and the final result is not confirmed.

BFT type exclusive control algorithm

On the other hand, through the BFT type, the permission based decentralized algorithm performs exclusive control. This algorithm solves the bifurcation and termination problem, which is similar to the distributed algorithm in pow.

In the BFT type, only one node named proposer, orderer, etc. can generate new blocks. When you create a block, you can collect votes from all participating nodes, and you can only create a new block with more than 2 / 3 consent. At this point, it is necessary to agree that more than two-thirds of the reasons, rather than most, are dealing with Byzantine failures, details of which are described in the article “fault tolerance in distributed systems.”.

In BFT type algorithm, unlike pow, only one node can gain exclusive access to the blockchain, so fork and finality will not be determined immediately. However, the property that anyone can participate in the network as a miner is often lost.

6 – 3. Blockchain and leader selection algorithm

Pow, POS and leader selection algorithm

The leader selection algorithm on blockchain is similar to the exclusive control algorithm. In bitcoin, the algorithm used to elect the leader, that is, the node of the newly created block is pow.

POW allows the addition of a block as a good leader, providing nodes with computational complexity and nonce discovery for bitcoin networks. Every miner who becomes a leader will try to contribute to the bitcoin network, because it is easier to synchronize early to first discover the current node and start searching for the current value of the next block, which is more likely to be rewarded. Although there is a problem that the chain is completely branched by hard fork, a very simple incentive structure is prepared based on game theory to realize synchronization as a distributed system in block chain network.

In the case of Ethereum, due to the short block generation time, there is a tendency for more branching. In this regard, by adopting the concept of unkle block, we have implemented a structure that rewards some illegal chains.

The introduction of future POS in the future allows the priority to generate blocks of nodes with large coin retention as guides. This is an algorithm to solve / improve the problem that the necessary power in POW becomes huge and vulnerable to 51% attack. This is an election algorithm based on game theory. If a node holds a large number of coins, it will not take malicious actions such as destroying the network.

BFT and leader selection algorithm

The problem with BFT type algorithms is how to choose the leader who votes for block generation as a proposer or orderer.

In the hyperledger adopted by pbft, the original reliable organization will be registered as orderer. However, this is a centralized leader selection method, which is obviously different from the distributed system.

stayTeIn the ndermint protocol, leaders are selected in a circular manner to make recommendations by alternating with different verifiers. In this case, leader candidate is based on POS, and can be said to be one of the algorithms that can realize Leader Selection in distributed system.