From fb35b474ad9321624ba06be135825a469b0ea684 Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Tue, 14 Apr 2020 10:48:40 +0200 Subject: [PATCH] blockchain/v2: allow setting nil switch, for CustomReactors() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes an issue reported in https://github.com/tendermint/tendermint/issues/4595#issuecomment-612667441. Not sure if this is sufficient to fully remove the reactor, but it fixes the immediate problem. ______ For contributor use: - [x] Wrote tests - [x] ~Updated CHANGELOG_PENDING.md~ - [x] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [x] ~Updated relevant documentation (`docs/`) and code comments~ - [x] Re-reviewed `Files changed` in the Github PR explorer --- blockchain/v2/reactor.go | 10 +++++----- blockchain/v2/reactor_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/blockchain/v2/reactor.go b/blockchain/v2/reactor.go index 88ec6268d..ff89ee94c 100644 --- a/blockchain/v2/reactor.go +++ b/blockchain/v2/reactor.go @@ -187,12 +187,12 @@ func NewBlockchainReactor( // SetSwitch implements Reactor interface. func (r *BlockchainReactor) SetSwitch(sw *p2p.Switch) { - if sw == nil { - panic("set nil switch") - } - r.Switch = sw - r.io = newSwitchIo(sw) + if sw != nil { + r.io = newSwitchIo(sw) + } else { + r.io = nil + } } func (r *BlockchainReactor) setMaxPeerHeight(height int64) { diff --git a/blockchain/v2/reactor_test.go b/blockchain/v2/reactor_test.go index 0b5f0b388..10b1d23df 100644 --- a/blockchain/v2/reactor_test.go +++ b/blockchain/v2/reactor_test.go @@ -411,6 +411,22 @@ func TestReactorHelperMode(t *testing.T) { } } +func TestReactorSetSwitchNil(t *testing.T) { + config := cfg.ResetTestRoot("blockchain_reactor_v2_test") + defer os.RemoveAll(config.RootDir) + genDoc, privVals := randGenesisDoc(config.ChainID(), 1, false, 30) + + reactor := newTestReactor(testReactorParams{ + logger: log.TestingLogger(), + genDoc: genDoc, + privVals: privVals, + }) + reactor.SetSwitch(nil) + + assert.Nil(t, reactor.Switch) + assert.Nil(t, reactor.io) +} + //---------------------------------------------- // utility funcs