github.com/annchain/OG@v0.0.9/consensus/dkg/dummy_test.go (about)

     1  package dkg
     2  
     3  import (
     4  	"github.com/sirupsen/logrus"
     5  )
     6  
     7  type dummyDkgPeerCommunicator struct {
     8  	Myid    int
     9  	Peers   []chan *DkgMessageEvent
    10  	pipeIn  chan *DkgMessageEvent
    11  	pipeOut chan *DkgMessageEvent
    12  }
    13  
    14  func NewDummyDkgPeerCommunicator(myid int, incoming chan *DkgMessageEvent, peers []chan *DkgMessageEvent) *dummyDkgPeerCommunicator {
    15  	d := &dummyDkgPeerCommunicator{
    16  		Peers:   peers,
    17  		Myid:    myid,
    18  		pipeIn:  incoming,
    19  		pipeOut: make(chan *DkgMessageEvent, 10000), // must be big enough to avoid blocking issue
    20  	}
    21  	return d
    22  }
    23  
    24  func (d *dummyDkgPeerCommunicator) Broadcast(msg DkgMessage, peers []DkgPeer) {
    25  	for _, peer := range peers {
    26  		logrus.WithField("peer", peer.Id).WithField("me", d.Myid).Debug("broadcasting message")
    27  		go func(peer DkgPeer) {
    28  			//<-ffchan.NewTimeoutSenderShort(d.Peers[peer.Id], msg, "dkg").C
    29  			d.Peers[peer.Id] <- &DkgMessageEvent{
    30  				Message: msg,
    31  				Peer: DkgPeer{
    32  					Id: d.Myid,
    33  				},
    34  			}
    35  			logrus.WithField("type", msg.GetType()).Info("broadcast")
    36  			//d.PeerPipeIns[peer.Id] <- msg
    37  		}(peer)
    38  	}
    39  }
    40  
    41  func (d *dummyDkgPeerCommunicator) Unicast(msg DkgMessage, peer DkgPeer) {
    42  	go func(peerId int) {
    43  		//<-ffchan.NewTimeoutSenderShort(d.Peers[peer.Id], msg, "dkg").C
    44  		d.Peers[peer.Id] <- &DkgMessageEvent{
    45  			Message: msg,
    46  			Peer: DkgPeer{
    47  				Id: d.Myid,
    48  			},
    49  		}
    50  		logrus.WithField("type", msg.GetType()).Info("unicast")
    51  		//d.PeerPipeIns[peerId] <- msg
    52  	}(peer.Id)
    53  }
    54  
    55  func (d *dummyDkgPeerCommunicator) GetPipeOut() chan *DkgMessageEvent {
    56  	return d.pipeOut
    57  }
    58  
    59  func (d *dummyDkgPeerCommunicator) GetPipeIn() chan *DkgMessageEvent {
    60  	return d.pipeIn
    61  }
    62  
    63  func (d *dummyDkgPeerCommunicator) Run() {
    64  	go func() {
    65  		for {
    66  			v := <-d.pipeIn
    67  			//<-ffchan.NewTimeoutSenderShort(d.pipeOut, v, "pc").C
    68  			d.pipeOut <- v
    69  		}
    70  	}()
    71  }