forked from TrueCloudLab/neoneo-go
network: use local timestamp to decide when to ping
We don't and we won't have synchronized clocks in the network so the only timestamp that we can compare our local time with is the one made ourselves. What this ping mechanism is used for is to recover from missing the block broadcast, thus it's appropriate for it to trigger after X seconds of the local time since the last block received. Relates to #430.
This commit is contained in:
parent
a8252ecc05
commit
62092c703d
2 changed files with 14 additions and 9 deletions
|
@ -68,6 +68,8 @@ type (
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
|
|
||||||
connected *atomic.Bool
|
connected *atomic.Bool
|
||||||
|
// Time of the last block receival.
|
||||||
|
lastBlockTS *atomic.Int64
|
||||||
|
|
||||||
log *zap.Logger
|
log *zap.Logger
|
||||||
}
|
}
|
||||||
|
@ -101,6 +103,7 @@ func NewServer(config ServerConfig, chain core.Blockchainer, log *zap.Logger) *S
|
||||||
unregister: make(chan peerDrop),
|
unregister: make(chan peerDrop),
|
||||||
peers: make(map[Peer]bool),
|
peers: make(map[Peer]bool),
|
||||||
connected: atomic.NewBool(false),
|
connected: atomic.NewBool(false),
|
||||||
|
lastBlockTS: atomic.NewInt64(0),
|
||||||
log: log,
|
log: log,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -359,6 +362,7 @@ func (s *Server) handleHeadersCmd(p Peer, headers *payload.Headers) {
|
||||||
|
|
||||||
// handleBlockCmd processes the received block received from its peer.
|
// handleBlockCmd processes the received block received from its peer.
|
||||||
func (s *Server) handleBlockCmd(p Peer, block *block.Block) error {
|
func (s *Server) handleBlockCmd(p Peer, block *block.Block) error {
|
||||||
|
s.lastBlockTS.Store(time.Now().UTC().Unix())
|
||||||
return s.bQueue.putBlock(block)
|
return s.bQueue.putBlock(block)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -658,6 +662,12 @@ func (s *Server) handleNewPayload(p *consensus.Payload) {
|
||||||
s.relayInventoryCmd(CMDInv, payload.ConsensusType, p.Hash())
|
s.relayInventoryCmd(CMDInv, payload.ConsensusType, p.Hash())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getLastBlockTime returns unix timestamp for the moment when the last block
|
||||||
|
// was received.
|
||||||
|
func (s *Server) getLastBlockTime() int64 {
|
||||||
|
return s.lastBlockTS.Load()
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) requestTx(hashes ...util.Uint256) {
|
func (s *Server) requestTx(hashes ...util.Uint256) {
|
||||||
if len(hashes) == 0 {
|
if len(hashes) == 0 {
|
||||||
return
|
return
|
||||||
|
|
|
@ -208,15 +208,10 @@ func (p *TCPPeer) StartProtocol() {
|
||||||
if p.LastBlockIndex() > p.server.chain.BlockHeight() {
|
if p.LastBlockIndex() > p.server.chain.BlockHeight() {
|
||||||
err = p.server.requestBlocks(p)
|
err = p.server.requestBlocks(p)
|
||||||
} else {
|
} else {
|
||||||
block, errGetBlock := p.server.chain.GetBlock(p.server.chain.CurrentBlockHash())
|
diff := time.Now().UTC().Unix() - p.server.getLastBlockTime()
|
||||||
if errGetBlock != nil {
|
if diff > int64(p.server.PingInterval/time.Second) {
|
||||||
err = errGetBlock
|
p.UpdatePingSent(p.GetPingSent() + 1)
|
||||||
} else {
|
err = p.EnqueueMessage(NewMessage(p.server.Net, CMDPing, payload.NewPing(p.server.id, p.server.chain.HeaderHeight())))
|
||||||
diff := uint32(time.Now().UTC().Unix()) - block.Timestamp
|
|
||||||
if diff > uint32(p.server.PingInterval/time.Second) {
|
|
||||||
p.UpdatePingSent(p.GetPingSent() + 1)
|
|
||||||
err = p.EnqueueMessage(NewMessage(p.server.Net, CMDPing, payload.NewPing(p.server.id, p.server.chain.HeaderHeight())))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
Loading…
Reference in a new issue