diff --git a/blockchain/v0/reactor.go b/blockchain/v0/reactor.go index 2054bf946..0ac278041 100644 --- a/blockchain/v0/reactor.go +++ b/blockchain/v0/reactor.go @@ -234,7 +234,7 @@ func (bcR *BlockchainReactor) ReceiveEnvelope(e p2p.Envelope) { } func (bcR *BlockchainReactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - var msg *bcproto.Message + msg := &bcproto.Message{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err) diff --git a/blockchain/v0/reactor_test.go b/blockchain/v0/reactor_test.go index dd23d015f..f6fcf9ec3 100644 --- a/blockchain/v0/reactor_test.go +++ b/blockchain/v0/reactor_test.go @@ -7,6 +7,7 @@ import ( "testing" "time" + "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -17,6 +18,7 @@ import ( "github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/mempool/mock" "github.com/tendermint/tendermint/p2p" + bcproto "github.com/tendermint/tendermint/proto/tendermint/blockchain" "github.com/tendermint/tendermint/proxy" sm "github.com/tendermint/tendermint/state" "github.com/tendermint/tendermint/store" @@ -192,6 +194,25 @@ func TestNoBlockResponse(t *testing.T) { } } +func TestLegacyReactorReceiveBasic(t *testing.T) { + config = cfg.ResetTestRoot("blockchain_reactor_test") + defer os.RemoveAll(config.RootDir) + genDoc, privVals := randGenesisDoc(1, false, 30) + reactor := newBlockchainReactor(log.TestingLogger(), genDoc, privVals, 10).reactor + peer := p2p.CreateRandomPeer(false) + + reactor.InitPeer(peer) + reactor.AddPeer(peer) + m := &bcproto.StatusRequest{} + wm := m.Wrap() + msg, err := proto.Marshal(wm) + assert.NoError(t, err) + + assert.NotPanics(t, func() { + reactor.Receive(BlockchainChannel, peer, msg) + }) +} + // NOTE: This is too hard to test without // an easy way to add test peer to switch // or without significant refactoring of the module. diff --git a/blockchain/v1/reactor.go b/blockchain/v1/reactor.go index 94ab3ef2a..f6fb93ab8 100644 --- a/blockchain/v1/reactor.go +++ b/blockchain/v1/reactor.go @@ -312,7 +312,7 @@ func (bcR *BlockchainReactor) ReceiveEnvelope(e p2p.Envelope) { } func (bcR *BlockchainReactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - var msg *bcproto.Message + msg := &bcproto.Message{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err) diff --git a/blockchain/v1/reactor_test.go b/blockchain/v1/reactor_test.go index 32b170913..4147a1be5 100644 --- a/blockchain/v1/reactor_test.go +++ b/blockchain/v1/reactor_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -18,6 +19,7 @@ import ( "github.com/tendermint/tendermint/libs/log" "github.com/tendermint/tendermint/mempool/mock" "github.com/tendermint/tendermint/p2p" + bcproto "github.com/tendermint/tendermint/proto/tendermint/blockchain" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/tendermint/tendermint/proxy" sm "github.com/tendermint/tendermint/state" @@ -349,6 +351,25 @@ outerFor: assert.True(t, lastReactorPair.bcR.Switch.Peers().Size() < len(reactorPairs)-1) } +func TestLegacyReactorReceiveBasic(t *testing.T) { + config = cfg.ResetTestRoot("blockchain_reactor_test") + defer os.RemoveAll(config.RootDir) + genDoc, privVals := randGenesisDoc(1, false, 30) + reactor := newBlockchainReactor(t, log.TestingLogger(), genDoc, privVals, 10) + peer := p2p.CreateRandomPeer(false) + + reactor.InitPeer(peer) + reactor.AddPeer(peer) + m := &bcproto.StatusRequest{} + wm := m.Wrap() + msg, err := proto.Marshal(wm) + assert.NoError(t, err) + + assert.NotPanics(t, func() { + reactor.Receive(BlockchainChannel, peer, msg) + }) +} + //---------------------------------------------- // utility funcs diff --git a/blockchain/v2/reactor.go b/blockchain/v2/reactor.go index 4b3296dfd..f42897485 100644 --- a/blockchain/v2/reactor.go +++ b/blockchain/v2/reactor.go @@ -518,7 +518,7 @@ func (r *BlockchainReactor) ReceiveEnvelope(e p2p.Envelope) { } func (r *BlockchainReactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - var msg *bcproto.Message + msg := &bcproto.Message{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err) diff --git a/blockchain/v2/reactor_test.go b/blockchain/v2/reactor_test.go index 9681ab263..2bd21a267 100644 --- a/blockchain/v2/reactor_test.go +++ b/blockchain/v2/reactor_test.go @@ -415,6 +415,34 @@ func TestReactorHelperMode(t *testing.T) { } } +func TestLegacyReactorReceiveBasic(t *testing.T) { + config := cfg.ResetTestRoot("blockchain_reactor_v2_test") + defer os.RemoveAll(config.RootDir) + genDoc, privVals := randGenesisDoc(config.ChainID(), 1, false, 30) + params := testReactorParams{ + logger: log.TestingLogger(), + genDoc: genDoc, + privVals: privVals, + startHeight: 20, + mockA: true, + } + reactor := newTestReactor(params) + mockSwitch := &mockSwitchIo{switchedToConsensus: false} + reactor.io = mockSwitch + peer := p2p.CreateRandomPeer(false) + + reactor.InitPeer(peer) + reactor.AddPeer(peer) + m := &bcproto.StatusRequest{} + wm := m.Wrap() + msg, err := proto.Marshal(wm) + assert.NoError(t, err) + + assert.NotPanics(t, func() { + reactor.Receive(BlockchainChannel, peer, msg) + }) +} + func TestReactorSetSwitchNil(t *testing.T) { config := cfg.ResetTestRoot("blockchain_reactor_v2_test") defer os.RemoveAll(config.RootDir) diff --git a/consensus/reactor.go b/consensus/reactor.go index 2b99d9aa0..e052f197a 100644 --- a/consensus/reactor.go +++ b/consensus/reactor.go @@ -393,7 +393,7 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } func (conR *Reactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - var msg *tmcons.Message + msg := &tmcons.Message{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err) diff --git a/consensus/reactor_test.go b/consensus/reactor_test.go index d2721a7dc..851070ba6 100644 --- a/consensus/reactor_test.go +++ b/consensus/reactor_test.go @@ -11,6 +11,7 @@ import ( "testing" "time" + "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -255,7 +256,7 @@ func TestReactorCreatesBlockWhenEmptyBlocksFalse(t *testing.T) { }, css) } -func TestReactorReceiveDoesNotPanicIfAddPeerHasntBeenCalledYet(t *testing.T) { +func TestLegacyReactorReceiveBasicIfAddPeerHasntBeenCalledYet(t *testing.T) { N := 1 css, cleanup := randConsensusNet(N, "consensus_reactor_test", newMockTickerFunc(true), newCounter) defer cleanup() @@ -281,6 +282,35 @@ func TestReactorReceiveDoesNotPanicIfAddPeerHasntBeenCalledYet(t *testing.T) { }) } +func TestLegacyReactorReceiveBasic(t *testing.T) { + N := 1 + css, cleanup := randConsensusNet(N, "consensus_reactor_test", newMockTickerFunc(true), newCounter) + defer cleanup() + reactors, _, eventBuses := startConsensusNet(t, css, N) + defer stopConsensusNet(log.TestingLogger(), reactors, eventBuses) + + var ( + reactor = reactors[0] + peer = p2pmock.NewPeer(nil) + ) + + reactor.InitPeer(peer) + v := &tmcons.HasVote{ + Height: 1, + Round: 1, + Index: 1, + Type: tmproto.PrevoteType, + } + w := v.Wrap() + msg, err := proto.Marshal(w) + assert.NoError(t, err) + + assert.NotPanics(t, func() { + reactor.Receive(StateChannel, peer, msg) + reactor.AddPeer(peer) + }) +} + func TestReactorReceivePanicsIfInitPeerHasntBeenCalledYet(t *testing.T) { N := 1 css, cleanup := randConsensusNet(N, "consensus_reactor_test", newMockTickerFunc(true), newCounter) diff --git a/evidence/reactor.go b/evidence/reactor.go index baf4ceb53..d0e843fc1 100644 --- a/evidence/reactor.go +++ b/evidence/reactor.go @@ -93,7 +93,7 @@ func (evR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } func (evR *Reactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - var msg *tmproto.EvidenceList + msg := &tmproto.EvidenceList{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err) diff --git a/evidence/reactor_test.go b/evidence/reactor_test.go index b7e86abc1..8dbd58dcb 100644 --- a/evidence/reactor_test.go +++ b/evidence/reactor_test.go @@ -9,6 +9,7 @@ import ( "github.com/fortytw2/leaktest" "github.com/go-kit/log/term" + "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" @@ -369,6 +370,33 @@ func exampleVote(t byte) *types.Vote { ValidatorIndex: 56789, } } +func TestLegacyReactorReceiveBasic(t *testing.T) { + config := cfg.TestConfig() + N := 1 + + stateDBs := make([]sm.Store, N) + val := types.NewMockPV() + stateDBs[0] = initializeValidatorState(val, 1) + + reactors, _ := makeAndConnectReactorsAndPools(config, stateDBs) + + var ( + reactor = reactors[0] + peer = &p2pmocks.Peer{} + ) + quitChan := make(<-chan struct{}) + peer.On("Quit").Return(quitChan) + + reactor.InitPeer(peer) + reactor.AddPeer(peer) + e := &tmproto.EvidenceList{} + msg, err := proto.Marshal(e) + assert.NoError(t, err) + + assert.NotPanics(t, func() { + reactor.Receive(evidence.EvidenceChannel, peer, msg) + }) +} //nolint:lll //ignore line length for tests func TestEvidenceVectors(t *testing.T) { diff --git a/mempool/v0/reactor.go b/mempool/v0/reactor.go index 3ac2e010e..52ed18514 100644 --- a/mempool/v0/reactor.go +++ b/mempool/v0/reactor.go @@ -191,7 +191,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } func (memR *Reactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - var msg *protomem.Message + msg := &protomem.Message{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err) diff --git a/mempool/v0/reactor_test.go b/mempool/v0/reactor_test.go index bd4ee3f65..c629b0aeb 100644 --- a/mempool/v0/reactor_test.go +++ b/mempool/v0/reactor_test.go @@ -10,6 +10,7 @@ import ( "github.com/fortytw2/leaktest" "github.com/go-kit/log/term" + "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -293,6 +294,31 @@ func TestDontExhaustMaxActiveIDs(t *testing.T) { } } +func TestLegacyReactorReceiveBasic(t *testing.T) { + config := cfg.TestConfig() + const N = 1 + reactors := makeAndConnectReactors(config, N) + var ( + reactor = reactors[0] + peer = mock.NewPeer(nil) + ) + defer func() { + err := reactor.Stop() + assert.NoError(t, err) + }() + + reactor.InitPeer(peer) + reactor.AddPeer(peer) + m := &memproto.Txs{} + wm := m.Wrap() + msg, err := proto.Marshal(wm) + assert.NoError(t, err) + + assert.NotPanics(t, func() { + reactor.Receive(mempool.MempoolChannel, peer, msg) + }) +} + // mempoolLogger is a TestingLogger which uses a different // color for each validator ("validator" key must exist). func mempoolLogger() log.Logger { diff --git a/mempool/v1/reactor.go b/mempool/v1/reactor.go index d16b4bd41..00fe65e79 100644 --- a/mempool/v1/reactor.go +++ b/mempool/v1/reactor.go @@ -190,7 +190,7 @@ func (memR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } func (memR *Reactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - var msg *protomem.Message + msg := &protomem.Message{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err) diff --git a/mempool/v1/reactor_test.go b/mempool/v1/reactor_test.go index a91122016..74f9f469f 100644 --- a/mempool/v1/reactor_test.go +++ b/mempool/v1/reactor_test.go @@ -8,10 +8,12 @@ import ( "time" "github.com/go-kit/log/term" + "github.com/gogo/protobuf/proto" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/abci/example/kvstore" + "github.com/tendermint/tendermint/p2p/mock" cfg "github.com/tendermint/tendermint/config" @@ -93,6 +95,35 @@ func TestMempoolVectors(t *testing.T) { } } +func TestLegacyReactorReceiveBasic(t *testing.T) { + config := cfg.TestConfig() + // if there were more than two reactors, the order of transactions could not be + // asserted in waitForTxsOnReactors (due to transactions gossiping). If we + // replace Connect2Switches (full mesh) with a func, which connects first + // reactor to others and nothing else, this test should also pass with >2 reactors. + const N = 1 + reactors := makeAndConnectReactors(config, N) + var ( + reactor = reactors[0] + peer = mock.NewPeer(nil) + ) + defer func() { + err := reactor.Stop() + assert.NoError(t, err) + }() + + reactor.InitPeer(peer) + reactor.AddPeer(peer) + m := &memproto.Txs{} + wm := m.Wrap() + msg, err := proto.Marshal(wm) + assert.NoError(t, err) + + assert.NotPanics(t, func() { + reactor.Receive(mempool.MempoolChannel, peer, msg) + }) +} + func makeAndConnectReactors(config *cfg.Config, n int) []*Reactor { reactors := make([]*Reactor, n) logger := mempoolLogger() diff --git a/p2p/pex/pex_reactor.go b/p2p/pex/pex_reactor.go index ff5f98f01..bcc8c4cce 100644 --- a/p2p/pex/pex_reactor.go +++ b/p2p/pex/pex_reactor.go @@ -301,7 +301,7 @@ func (r *Reactor) ReceiveEnvelope(e p2p.Envelope) { } func (r *Reactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - var msg *tmp2p.Message + msg := &tmp2p.Message{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err) diff --git a/p2p/pex/pex_reactor_test.go b/p2p/pex/pex_reactor_test.go index a2f0a2a71..b39587ebd 100644 --- a/p2p/pex/pex_reactor_test.go +++ b/p2p/pex/pex_reactor_test.go @@ -498,6 +498,22 @@ func TestPEXReactorDoesNotAddPrivatePeersToAddrBook(t *testing.T) { assert.Equal(t, size, book.Size()) } +func TestLegacyReactorReceiveBasic(t *testing.T) { + pexR, _ := createReactor(&ReactorConfig{}) + peer := p2p.CreateRandomPeer(false) + + pexR.InitPeer(peer) + pexR.AddPeer(peer) + m := &tmp2p.PexAddrs{} + wm := m.Wrap() + msg, err := proto.Marshal(wm) + assert.NoError(t, err) + + assert.NotPanics(t, func() { + pexR.Receive(PexChannel, peer, msg) + }) +} + func TestPEXReactorDialPeer(t *testing.T) { pexR, book := createReactor(&ReactorConfig{}) defer teardownReactor(book) diff --git a/p2p/switch_test.go b/p2p/switch_test.go index 4e10a61c5..5695d03dd 100644 --- a/p2p/switch_test.go +++ b/p2p/switch_test.go @@ -80,7 +80,7 @@ func (tr *TestReactor) ReceiveEnvelope(e Envelope) { } func (tr *TestReactor) Receive(chID byte, peer Peer, msgBytes []byte) { - var msg *p2pproto.Message + msg := &p2pproto.Message{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err) diff --git a/statesync/reactor.go b/statesync/reactor.go index cedd9204d..835b6313b 100644 --- a/statesync/reactor.go +++ b/statesync/reactor.go @@ -226,7 +226,7 @@ func (r *Reactor) ReceiveEnvelope(e p2p.Envelope) { } func (r *Reactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - var msg *ssproto.Message + msg := &ssproto.Message{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err) diff --git a/statesync/reactor_test.go b/statesync/reactor_test.go index da3336396..3dc089fce 100644 --- a/statesync/reactor_test.go +++ b/statesync/reactor_test.go @@ -183,3 +183,21 @@ func TestReactor_Receive_SnapshotsRequest(t *testing.T) { }) } } + +func TestLegacyReactorReceiveBasic(t *testing.T) { + cfg := config.DefaultStateSyncConfig() + conn := &proxymocks.AppConnSnapshot{} + reactor := NewReactor(*cfg, conn, nil, "") + peer := p2p.CreateRandomPeer(false) + + reactor.InitPeer(peer) + reactor.AddPeer(peer) + m := &ssproto.ChunkRequest{Height: 1, Format: 1, Index: 1} + wm := m.Wrap() + msg, err := proto.Marshal(wm) + assert.NoError(t, err) + + assert.NotPanics(t, func() { + reactor.Receive(ChunkChannel, peer, msg) + }) +} diff --git a/test/maverick/consensus/reactor.go b/test/maverick/consensus/reactor.go index 6e730721c..31c6db16e 100644 --- a/test/maverick/consensus/reactor.go +++ b/test/maverick/consensus/reactor.go @@ -396,7 +396,7 @@ func (conR *Reactor) ReceiveEnvelope(e p2p.Envelope) { } func (conR *Reactor) Receive(chID byte, peer p2p.Peer, msgBytes []byte) { - var msg *tmcons.Message + msg := &tmcons.Message{} err := proto.Unmarshal(msgBytes, msg) if err != nil { panic(err)