diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-04-12 11:35:37 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-12 11:35:37 +0800 |
commit | 24c0cecbbf7ba84754ccc02d37c9540ce317976c (patch) | |
tree | 81031ec19affa0a0b962af627301963b077ea194 /core/consensus_test.go | |
parent | 1107fedc61b1b042216948d9fea78e7747f7599f (diff) | |
download | dexon-consensus-24c0cecbbf7ba84754ccc02d37c9540ce317976c.tar dexon-consensus-24c0cecbbf7ba84754ccc02d37c9540ce317976c.tar.gz dexon-consensus-24c0cecbbf7ba84754ccc02d37c9540ce317976c.tar.bz2 dexon-consensus-24c0cecbbf7ba84754ccc02d37c9540ce317976c.tar.lz dexon-consensus-24c0cecbbf7ba84754ccc02d37c9540ce317976c.tar.xz dexon-consensus-24c0cecbbf7ba84754ccc02d37c9540ce317976c.tar.zst dexon-consensus-24c0cecbbf7ba84754ccc02d37c9540ce317976c.zip |
core: add report bad peer interface to network (#559)
Diffstat (limited to 'core/consensus_test.go')
-rw-r--r-- | core/consensus_test.go | 58 |
1 files changed, 45 insertions, 13 deletions
diff --git a/core/consensus_test.go b/core/consensus_test.go index f677d00..5f61028 100644 --- a/core/consensus_test.go +++ b/core/consensus_test.go @@ -18,6 +18,7 @@ package core import ( + "context" "encoding/json" "testing" "time" @@ -70,7 +71,7 @@ func (n *network) BroadcastAgreementResult( // SendDKGPrivateShare sends PrivateShare to a DKG participant. func (n *network) SendDKGPrivateShare( recv crypto.PublicKey, prvShare *typesDKG.PrivateShare) { - n.conn.send(types.NewNodeID(recv), prvShare) + n.conn.send(n.nID, types.NewNodeID(recv), prvShare) } // BroadcastDKGPrivateShare broadcasts PrivateShare to all DKG participants. @@ -87,8 +88,13 @@ func (n *network) BroadcastDKGPartialSignature( } // ReceiveChan returns a channel to receive messages from DEXON network. -func (n *network) ReceiveChan() <-chan interface{} { - return make(chan interface{}) +func (n *network) ReceiveChan() <-chan types.Msg { + return make(chan types.Msg) +} + +// ReportBadPeer reports that a peer is sending bad message. +func (n *network) ReportBadPeerChan() chan<- interface{} { + return n.conn.s.sink } func (nc *networkConnection) broadcast(from types.NodeID, msg interface{}) { @@ -96,11 +102,11 @@ func (nc *networkConnection) broadcast(from types.NodeID, msg interface{}) { if nID == from { continue } - nc.send(nID, msg) + nc.send(from, nID, msg) } } -func (nc *networkConnection) send(to types.NodeID, msg interface{}) { +func (nc *networkConnection) send(from, to types.NodeID, msg interface{}) { ch, exist := nc.cons[to] if !exist { return @@ -122,12 +128,15 @@ func (nc *networkConnection) send(to types.NodeID, msg interface{}) { } msgCopy = valCopy } - ch <- msgCopy + ch <- types.Msg{ + PeerID: from, + Payload: msgCopy, + } } type networkConnection struct { s *ConsensusTestSuite - cons map[types.NodeID]chan interface{} + cons map[types.NodeID]chan types.Msg } func (nc *networkConnection) newNetwork(nID types.NodeID) *network { @@ -138,14 +147,19 @@ func (nc *networkConnection) newNetwork(nID types.NodeID) *network { } func (nc *networkConnection) setCon(nID types.NodeID, con *Consensus) { - ch := make(chan interface{}, 1000) + ch := make(chan types.Msg, 1000) go func() { for { - msg := <-ch + var msg types.Msg + select { + case msg = <-ch: + case <-nc.s.ctx.Done(): + return + } var err error // Testify package does not support concurrent call. // Use panic() to detact error. - switch val := msg.(type) { + switch val := msg.Payload.(type) { case *types.Block: err = con.preProcessBlock(val) case *types.Vote: @@ -167,13 +181,31 @@ func (nc *networkConnection) setCon(nID types.NodeID, con *Consensus) { type ConsensusTestSuite struct { suite.Suite - conn *networkConnection + ctx context.Context + ctxCancel context.CancelFunc + conn *networkConnection + sink chan interface{} +} + +func (s *ConsensusTestSuite) SetupTest() { + s.ctx, s.ctxCancel = context.WithCancel(context.Background()) + s.sink = make(chan interface{}, 1000) + go func() { + select { + case <-s.sink: + case <-s.ctx.Done(): + return + } + }() +} +func (s *ConsensusTestSuite) TearDownTest() { + s.ctxCancel() } func (s *ConsensusTestSuite) newNetworkConnection() *networkConnection { return &networkConnection{ s: s, - cons: make(map[types.NodeID]chan interface{}), + cons: make(map[types.NodeID]chan types.Msg), } } @@ -384,7 +416,7 @@ func (s *ConsensusTestSuite) TestInitialHeightEventTriggered() { network, prvKey, []*types.Block(nil), - []interface{}(nil), + []types.Msg{}, &common.NullLogger{}, ) s.Require().NoError(err) |