github.com/annchain/OG@v0.0.9/plugin/annsensus/communicator.go (about)

     1  package annsensus
     2  
     3  import (
     4  	"github.com/annchain/OG/arefactor/common/utilfuncs"
     5  	"github.com/annchain/OG/communication"
     6  	"github.com/annchain/OG/consensus/annsensus"
     7  	"github.com/annchain/OG/message"
     8  )
     9  
    10  type ProxyAnnsensusPeerCommunicator struct {
    11  	AnnsensusMessageAdapter AnnsensusMessageAdapter // either TrustfulBftAdapter or PlainBftAdapter
    12  	GeneralOutgoing         communication.GeneralPeerCommunicatorOutgoing
    13  	pipe                    chan *annsensus.AnnsensusMessageEvent
    14  }
    15  
    16  func (p *ProxyAnnsensusPeerCommunicator) InitDefault() {
    17  	p.pipe = make(chan *annsensus.AnnsensusMessageEvent)
    18  }
    19  
    20  func (p ProxyAnnsensusPeerCommunicator) Broadcast(msg annsensus.AnnsensusMessage, peers []annsensus.AnnsensusPeer) {
    21  	if p.pipe == nil {
    22  		panic("not initialized.")
    23  	}
    24  
    25  	ogMessage, err := p.AnnsensusMessageAdapter.AdaptAnnsensusMessage(msg)
    26  	utilfuncs.PanicIfError(err, "Adapter for annsensus should never fail")
    27  
    28  	ogPeers := make([]message.GeneralPeer, len(peers))
    29  	for i, peer := range peers {
    30  		adaptedValue, err := p.AnnsensusMessageAdapter.AdaptAnnsensusPeer(peer)
    31  		utilfuncs.PanicIfError(err, "Adapter for annsensus peer should never fail")
    32  		ogPeers[i] = adaptedValue
    33  	}
    34  	p.GeneralOutgoing.Multicast(ogMessage, ogPeers)
    35  }
    36  
    37  func (p ProxyAnnsensusPeerCommunicator) Unicast(msg annsensus.AnnsensusMessage, peer annsensus.AnnsensusPeer) {
    38  	if p.pipe == nil {
    39  		panic("not initialized.")
    40  	}
    41  
    42  	ogMessage, err := p.AnnsensusMessageAdapter.AdaptAnnsensusMessage(msg)
    43  	utilfuncs.PanicIfError(err, "Adapter for annsensus should never fail")
    44  	ogPeer, err := p.AnnsensusMessageAdapter.AdaptAnnsensusPeer(peer)
    45  	utilfuncs.PanicIfError(err, "Adapter for annsensus peer should never fail")
    46  	p.GeneralOutgoing.Unicast(ogMessage, ogPeer)
    47  }
    48  
    49  func (p ProxyAnnsensusPeerCommunicator) GetPipeIn() chan *annsensus.AnnsensusMessageEvent {
    50  	if p.pipe == nil {
    51  		panic("not initialized.")
    52  	}
    53  	return p.pipe
    54  }
    55  
    56  func (p ProxyAnnsensusPeerCommunicator) GetPipeOut() chan *annsensus.AnnsensusMessageEvent {
    57  	if p.pipe == nil {
    58  		panic("not initialized.")
    59  	}
    60  	return p.pipe
    61  }