try commit and extcommit in blocksync response

This commit is contained in:
William Banfield
2022-05-17 21:38:44 -04:00
parent 4ceead171b
commit 6f577fa389
2 changed files with 42 additions and 19 deletions
+22 -19
View File
@@ -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.
+20
View File
@@ -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 {