-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BFT Block Puller: Refactor gossip out
Signed-off-by: Yoav Tock <tock@il.ibm.com> Change-Id: I359e8402954b6db3a7a2df7a29d60fb04cfda63c
- Loading branch information
Showing
10 changed files
with
387 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package deliverservice | ||
|
||
import ( | ||
"github.com/golang/protobuf/proto" | ||
"github.com/hyperledger/fabric-protos-go/common" | ||
"github.com/hyperledger/fabric-protos-go/gossip" | ||
"github.com/hyperledger/fabric/common/flogging" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
// GossipServiceAdapter serves to provide basic functionality | ||
// required from gossip service by delivery service | ||
// | ||
//go:generate counterfeiter -o fake/gossip_service_adapter.go --fake-name GossipServiceAdapter . GossipServiceAdapter | ||
type GossipServiceAdapter interface { | ||
// AddPayload adds payload to the local state sync buffer | ||
AddPayload(chainID string, payload *gossip.Payload) error | ||
|
||
// Gossip the message across the peers | ||
Gossip(msg *gossip.GossipMessage) | ||
} | ||
|
||
type GossipBlockHandler struct { | ||
gossip GossipServiceAdapter | ||
blockGossipDisabled bool | ||
logger *flogging.FabricLogger | ||
} | ||
|
||
func (h *GossipBlockHandler) HandleBlock(channelID string, block *common.Block) error { | ||
marshaledBlock, err := proto.Marshal(block) | ||
if err != nil { | ||
return errors.WithMessage(err, "block from orderer could not be re-marshaled") | ||
} | ||
|
||
// Create payload with a block received | ||
blockNum := block.GetHeader().GetNumber() | ||
payload := &gossip.Payload{ | ||
Data: marshaledBlock, | ||
SeqNum: blockNum, | ||
} | ||
|
||
// Use payload to create gossip message | ||
gossipMsg := &gossip.GossipMessage{ | ||
Nonce: 0, | ||
Tag: gossip.GossipMessage_CHAN_AND_ORG, | ||
Channel: []byte(channelID), | ||
Content: &gossip.GossipMessage_DataMsg{ | ||
DataMsg: &gossip.DataMessage{ | ||
Payload: payload, | ||
}, | ||
}, | ||
} | ||
|
||
h.logger.Debugf("Adding payload to local buffer, blockNum = [%d]", blockNum) | ||
// Add payload to local state payloads buffer | ||
if err := h.gossip.AddPayload(channelID, payload); err != nil { | ||
h.logger.Warningf("Block [%d] received from ordering service wasn't added to payload buffer: %v", blockNum, err) | ||
return errors.WithMessage(err, "could not add block as payload") | ||
} | ||
if h.blockGossipDisabled { | ||
return nil | ||
} | ||
// Gossip messages with other nodes | ||
h.logger.Debugf("Gossiping block [%d]", blockNum) | ||
h.gossip.Gossip(gossipMsg) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package deliverservice | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric-protos-go/common" | ||
"github.com/hyperledger/fabric/common/flogging" | ||
"github.com/hyperledger/fabric/internal/pkg/peer/blocksprovider/fake" | ||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGossipBlockHandler_HandleBlock(t *testing.T) { | ||
h := GossipBlockHandler{ | ||
blockGossipDisabled: false, | ||
logger: flogging.MustGetLogger("test.GossipBlockHandler"), | ||
} | ||
|
||
t.Run("error: cannot marshal block", func(t *testing.T) { | ||
h.gossip = &fake.GossipServiceAdapter{} | ||
err := h.HandleBlock("testchannel", nil) | ||
require.EqualError(t, err, "block from orderer could not be re-marshaled: proto: Marshal called with nil") | ||
}) | ||
|
||
t.Run("error: cannot add payload", func(t *testing.T) { | ||
fakeGossip := &fake.GossipServiceAdapter{} | ||
fakeGossip.AddPayloadReturns(errors.New("oops")) | ||
h.gossip = fakeGossip | ||
err := h.HandleBlock("testchannel", &common.Block{Header: &common.BlockHeader{Number: 8}}) | ||
require.EqualError(t, err, "could not add block as payload: oops") | ||
}) | ||
|
||
t.Run("valid: gossip", func(t *testing.T) { | ||
fakeGossip := &fake.GossipServiceAdapter{} | ||
h.gossip = fakeGossip | ||
err := h.HandleBlock("testchannel", &common.Block{Header: &common.BlockHeader{Number: 8}}) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, fakeGossip.GossipCallCount()) | ||
}) | ||
|
||
t.Run("valid: no gossip", func(t *testing.T) { | ||
fakeGossip := &fake.GossipServiceAdapter{} | ||
h.gossip = fakeGossip | ||
h.blockGossipDisabled = true | ||
err := h.HandleBlock("testchannel", &common.Block{Header: &common.BlockHeader{Number: 8}}) | ||
require.NoError(t, err) | ||
require.Equal(t, 0, fakeGossip.GossipCallCount()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.