From 6f577fa3898d95115ea793ee83e9a6f2b9a9291b Mon Sep 17 00:00:00 2001 From: William Banfield Date: Tue, 17 May 2022 21:38:44 -0400 Subject: [PATCH] try commit and extcommit in blocksync response --- internal/blocksync/reactor.go | 41 +++++++++++++++++++---------------- types/block.go | 20 +++++++++++++++++ 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/internal/blocksync/reactor.go b/internal/blocksync/reactor.go index 8f8157829..803e5ca90 100644 --- a/internal/blocksync/reactor.go +++ b/internal/blocksync/reactor.go @@ -185,31 +185,34 @@ func (r *Reactor) OnStop() { // Otherwise, we'll respond saying we do not have it. func (r *Reactor) respondToPeer(ctx context.Context, msg *bcproto.BlockRequest, peerID types.NodeID, blockSyncCh *p2p.Channel) error { block := r.store.LoadBlock(msg.Height) - if block != nil { - extCommit := r.store.LoadBlockExtendedCommit(msg.Height) - if extCommit == nil { - return fmt.Errorf("found block in store without extended commit: %v", block) - } - blockProto, err := block.ToProto() - if err != nil { - return fmt.Errorf("failed to convert block to protobuf: %w", err) - } - + if block == nil { + r.logger.Info("peer requesting a block we do not have", "peer", peerID, "height", msg.Height) return blockSyncCh.Send(ctx, p2p.Envelope{ - To: peerID, - Message: &bcproto.BlockResponse{ - Block: blockProto, - ExtCommit: extCommit.ToProto(), - }, + To: peerID, + Message: &bcproto.NoBlockResponse{Height: msg.Height}, }) } - - r.logger.Info("peer requesting a block we do not have", "peer", peerID, "height", msg.Height) + extCommit := r.store.LoadBlockExtendedCommit(msg.Height) + if extCommit == nil { + c := r.store.LoadBlockCommit(msg.Height) + extCommit = c.WrappedExtendedCommit() + } + if extCommit == nil { + return fmt.Errorf("found block in store with no commit and no extended commit: %v", block) + } + blockProto, err := block.ToProto() + if err != nil { + return fmt.Errorf("failed to convert block to protobuf: %w", err) + } return blockSyncCh.Send(ctx, p2p.Envelope{ - To: peerID, - Message: &bcproto.NoBlockResponse{Height: msg.Height}, + To: peerID, + Message: &bcproto.BlockResponse{ + Block: blockProto, + ExtCommit: extCommit.ToProto(), + }, }) + } // handleMessage handles an Envelope sent from a peer on a specific p2p Channel. diff --git a/types/block.go b/types/block.go index 133401576..aedd90341 100644 --- a/types/block.go +++ b/types/block.go @@ -917,6 +917,26 @@ func (commit *Commit) Hash() tmbytes.HexBytes { return commit.hash } +// WrappedExtendedCommit wraps a commit as an ExtendedCommit. +// The VoteExtension fields of the resulting value will by nil. +// Wrapping a Commit as an ExtendedCommit is useful when an API +// requires an ExtendedCommit wire type but does not +// need the VoteExtension data. +func (commit *Commit) WrappedExtendedCommit() *ExtendedCommit { + cs := make([]ExtendedCommitSig, len(commit.Signatures)) + for idx, s := range commit.Signatures { + cs[idx] = ExtendedCommitSig{ + CommitSig: s, + } + } + return &ExtendedCommit{ + Height: commit.Height, + Round: commit.Round, + BlockID: commit.BlockID, + ExtendedSignatures: cs, + } +} + // StringIndented returns a string representation of the commit. func (commit *Commit) StringIndented(indent string) string { if commit == nil {