I'm curious about how Bitcoin nodes sync their time. There are delays within data transmission so if a node mines a block, how does the network determine whether it is the first?
Nodes determine which block is "first" based on whichever block they receive first.
Or in other words, the timestamp on a block is not used to determine which block a node will add to their blockchain; nodes simply add the first valid block they receive to their blockchain.
As a result, if two blocks are mined at the same time, it's possible that different nodes on the network will have different blocks at the top of their blockchain. To illustrate:
Node1 Node2 ----- ----- [x] [y] [c] [c] [b] [b] [a] [a]
[x]was mined "first"
[y]was mined just after block
- Due to network delays,
Node2ends up receiving block
- Therefore, Node2 adds
[y]to the top of their chain (but they also keep a copy of block
So at this point in time, there is a disagreement about the state of the blockchain on the network. This happens.
This situation is resolved when a new block gets mined.
For example, if
Node1 mines a new block on to their chain:
Node1 ----- [z] [x] [c] [b] [a]
This new block
[z] creates the new longest chain on the network. So when
Node2 receives block
[z], they will adopt the new longest chain:
Node1 Node2 ----- ----- [z] [z] [x] [x][y] [c] [c] [b] [b] [a] [a]
...and the network is back in agreement about the state of the blockchain.
The order of blocks (or which block is "first") is not determined by the timestamp on blocks relative to any network time.
Instead, nodes will add the first block they receive, and are free to disagree on which block is at the top of the chain. The order is resolved when a new block is mined, and all nodes adopt the longest known chain.