接口描述
fabric/core/deliverservice/deliveryclient.go
// DeliverService used to communicate with orderers to obtain
// new block and send the to the committer service
type DeliverService interface {
// StartDeliverForChannel dynamically starts delivery of new blocks from ordering service
// to channel peers.
StartDeliverForChannel(chainID string, ledgerInfo blocksprovider.LedgerInfo) error
// StopDeliverForChannel dynamically stops delivery of new blocks from ordering service
// to channel peers.
StopDeliverForChannel(chainID string) error
// Stop terminates delivery service and closes the connection
Stop()
}
根据描述可以很明显的看出来是与order服务进行收发block的接口,接下来阅读以下主要代码,三个函数只有 StartDeliverForChannel是重点,其余两个都不是重点,所以我们主要就搞懂这个就可以了,既然是收发代码,就是找到收发逻辑就可以了。
接口实现
// StartDeliverForChannel starts blocks delivery for channel
// initializes the grpc stream for given chainID, creates blocks provider instance
// that spawns in go routine to read new blocks starting from the position provided by ledger
// info instance.
func (d *deliverServiceImpl) StartDeliverForChannel(chainID string, ledgerInfo blocksprovider.LedgerInfo) error {
d.lock.Lock()
defer d.lock.Unlock()
if d.stopping {
errMsg := fmt.Sprintf("Delivery service is stopping cannot join a new channel %s", chainID)
logger.Errorf(errMsg)
return errors.New(errMsg)
}
if _, exist := d.clients[chainID]; exist {
errMsg := fmt.Sprintf("Delivery service - block provider already exists for %s found, can't start delivery", chainID)
logger.Errorf(errMsg)
return errors.New(errMsg)
} else {
abc, err := d.clientsFactory.Create()
if err != nil {
logger.Errorf("Unable to initialize atomic broadcast, due to %s", err)
return err
}
logger.Debug("This peer will pass blocks from orderer service to other peers")
d.clients[chainID] = blocksprovider.NewBlocksProvider(chainID, abc, d.gossip)
if err := d.clients[chainID].RequestBlocks(ledgerInfo); err == nil {
// Start reading blocks from ordering service in case this peer is a leader for specified chain
go d.clients[chainID].DeliverBlocks()
}
}
return nil
}
通过代码可以看出来 blocksprovider.NewBlocksProvider 产生了真正的连接,在这个连接上会先 RequestBlocks,然后 DeliverBlocks ,我们只要搞清楚 与 谁建立的连接,RequestBlocks,DeliverBlocks 的逻辑后就弄清楚了 这个service的主要功能了
连接的建立
func NewDeliverService(gossip blocksprovider.GossipServiceAdapter, endpoints []string) (DeliverService, error) {
indices := rand.Perm(len(endpoints))
for _, idx := range indices {
logger.Infof("Creating delivery service to get blocks from the ordering service, %s", endpoints[idx])
dialOpts := []grpc.DialOption{grpc.WithInsecure(), grpc.WithTimeout(3 * time.Second), grpc.WithBlock()}
if comm.TLSEnabled() {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(comm.InitTLSForPeer()))
} else {
dialOpts = append(dialOpts, grpc.WithInsecure())
}
conn, err := grpc.Dial(endpoints[idx], dialOpts...)
if err != nil {
logger.Errorf("Cannot dial to %s, because of %s", endpoints[idx], err)
continue
}
return NewFactoryDeliverService(gossip, &blocksDelivererFactoryImpl{conn}, conn), nil
}
return nil, fmt.Errorf("Wasn't able to connect to any of ordering service endpoints %s", endpoints)
}
可以看到连接是建立到了,endpoints,通过追踪代码可以发现就是 order 节点的地址,这里需要注意一点, 传过来的 endpoints 是一个数组,但是只随机的与某一个节点建立连接。
现在我们知道了是与order通信,由于fabric是基于grpc的,所以我们还要找出来与order的哪个服务进行通信,blocksDelivererFactoryImpl 这个struct定义了与哪个服务通信,我们一起看下
fabric/core/deliverservice/deliveryclient.go
type blocksDelivererFactoryImpl struct {
conn *grpc.ClientConn
}
// Create a factory method which is capable to instantiate new BlocksDeliverer
func (factory *blocksDelivererFactoryImpl) Create() (blocksprovider.BlocksDeliverer, error) {
var abc orderer.AtomicBroadcast_DeliverClient
var err error
abc, err = orderer.NewAtomicBroadcastClient(factory.conn).Deliver(context.TODO())
if err != nil {
return nil, err
}
return abc, nil
}
明显是AtomicBroadcast 服务的 Deliver 接口通信
RequestBlocks
fabric/core/deliverservice/blocksprovider/blocksprovider.go
func (b *blocksProviderImpl) RequestBlocks(ledgerInfoProvider LedgerInfo) error {
height, err := ledgerInfoProvider.LedgerHeight()
if err != nil {
logger.Errorf("Can't get legder height from committer [%s]", err)
return err
}
if height > 0 {
logger.Debugf("Starting deliver with block [%d]", height)
if err := b.seekLatestFromCommitter(height); err != nil {
return err
}
} else {
logger.Debug("Starting deliver with olders block")
if err := b.seekOldest(); err != nil {
return err
}
}
return nil
}
func (b *blocksProviderImpl) seekLatestFromCommitter(height uint64) error {
seekInfo := &orderer.SeekInfo{
Start: &orderer.SeekPosition{Type: &orderer.SeekPosition_Specified{Specified: &orderer.SeekSpecified{Number: height}}},
Stop: &orderer.SeekPosition{Type: &orderer.SeekPosition_Specified{Specified: &orderer.SeekSpecified{Number: math.MaxUint64}}},
Behavior: orderer.SeekInfo_BLOCK_UNTIL_READY,
}
//TODO- epoch and msgVersion may need to be obtained for nowfollowing usage in orderer/configupdate/configupdate.go
msgVersion := int32(0)
epoch := uint64(0)
env, err := utils.CreateSignedEnvelope(common.HeaderType_CONFIG_UPDATE, b.chainID, localmsp.NewSigner(), seekInfo, msgVersion, epoch)
if err != nil {
return err
}
return b.client.Send(env)
}
明显 可以看出来只是在请求 start – stop 之间的block
DeliverBlocks
func (b *blocksProviderImpl) DeliverBlocks() {
for !b.isDone() {
msg, err := b.client.Recv() // 接收Request请求的代码
if err != nil {
logger.Warningf("Receive error: %s", err.Error())
return
}
switch t := msg.Type.(type) {
case *orderer.DeliverResponse_Status:
if t.Status == common.Status_SUCCESS {
logger.Warning("ERROR! Received success for a seek that should never complete")
return
}
logger.Warning("Got error ", t)
case *orderer.DeliverResponse_Block:
seqNum := t.Block.Header.Number
numberOfPeers := len(b.gossip.PeersOfChannel(gossipcommon.ChainID(b.chainID)))
// Create payload with a block received
payload := createPayload(seqNum, t.Block)
// Use payload to create gossip message
gossipMsg := createGossipMsg(b.chainID, payload)
logger.Debugf("Adding payload locally, buffer seqNum = [%d], peers number [%d]", seqNum, numberOfPeers)
// Add payload to local state payloads buffer
b.gossip.AddPayload(b.chainID, payload)
// Gossip messages with other nodes
logger.Debugf("Gossiping block [%d], peers number [%d]", seqNum, numberOfPeers)
b.gossip.Gossip(gossipMsg) // gossip 发送
default:
logger.Warning("Received unknown: ", t)
return
}
}
}
可以看出来,request 来的 block,在这边都通过 gossip 广播出去了
总结
这个这个代码很清晰了,DeliverService 就是从 order 请求 block,然后广播出去。 当然在整个fabric中的业务作用目前看不出来,还需要深入分析