Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] peer: prevent last block height going backwards #392

Merged
merged 1 commit into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,15 @@ func (p *Peer) String() string {
// This function is safe for concurrent access.
func (p *Peer) UpdateLastBlockHeight(newHeight int32) {
p.statsMtx.Lock()
defer p.statsMtx.Unlock()

if newHeight <= p.lastBlock {
return
}

log.Tracef("Updating last block height of peer %v from %v to %v",
p.addr, p.lastBlock, newHeight)
p.lastBlock = newHeight
p.statsMtx.Unlock()
}

// UpdateLastAnnouncedBlock updates meta-data about the last block hash this
Expand Down
67 changes: 67 additions & 0 deletions peer/peer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,3 +917,70 @@ func TestDuplicateVersionMsg(t *testing.T) {
t.Fatal("peer did not disconnect")
}
}

// TestUpdateLastBlockHeight ensures the last block height is set properly
// during the initial version negotiation and is only allowed to advance to
// higher values via the associated update function.
func TestUpdateLastBlockHeight(t *testing.T) {
// Create a pair of peers that are connected to each other using a fake
// connection and the remote peer starting at height 100.
const remotePeerHeight = 100
verack := make(chan struct{})
peerCfg := peer.Config{
Listeners: peer.MessageListeners{
OnVerAck: func(p *peer.Peer, msg *wire.MsgVerAck) {
verack <- struct{}{}
},
},
UserAgentName: "peer",
UserAgentVersion: "1.0",
ChainParams: &chaincfg.MainNetParams,
Services: 0,
TstAllowSelfConnection: true,
}
remotePeerCfg := peerCfg
remotePeerCfg.NewestBlock = func() (*chainhash.Hash, int32, error) {
return &chainhash.Hash{}, remotePeerHeight, nil
}
inConn, outConn := pipe(
&conn{laddr: "10.0.0.1:9108", raddr: "10.0.0.2:9108"},
&conn{laddr: "10.0.0.2:9108", raddr: "10.0.0.1:9108"},
)
localPeer, err := peer.NewOutboundPeer(&peerCfg, inConn.laddr)
if err != nil {
t.Fatalf("NewOutboundPeer: unexpected err: %v\n", err)
}
localPeer.AssociateConnection(outConn)
inPeer := peer.NewInboundPeer(&remotePeerCfg)
inPeer.AssociateConnection(inConn)

// Wait for the veracks from the initial protocol version negotiation.
for i := 0; i < 2; i++ {
select {
case <-verack:
case <-time.After(time.Second):
t.Fatal("verack timeout")
}
}

// Ensure the latest block height starts at the value reported by the remote
// peer via its version message.
if height := localPeer.LastBlock(); height != remotePeerHeight {
t.Fatalf("wrong starting height - got %d, want %d", height,
remotePeerHeight)
}

// Ensure the latest block height is not allowed to go backwards.
localPeer.UpdateLastBlockHeight(remotePeerHeight - 1)
if height := localPeer.LastBlock(); height != remotePeerHeight {
t.Fatalf("height allowed to go backwards - got %d, want %d", height,
remotePeerHeight)
}

// Ensure the latest block height is allowed to advance.
localPeer.UpdateLastBlockHeight(remotePeerHeight + 1)
if height := localPeer.LastBlock(); height != remotePeerHeight+1 {
t.Fatalf("height not allowed to advance - got %d, want %d", height,
remotePeerHeight+1)
}
}