Skip to content

Commit

Permalink
fix: return error when AddChannel fails (#500)
Browse files Browse the repository at this point in the history
* Return error

* add no lint tags

* Add error handling

* Remove logging that never happens

* Add no lint tags

* Add logging
  • Loading branch information
ulbqb authored Nov 22, 2022
1 parent 15a55a2 commit 2828a22
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 9 deletions.
5 changes: 4 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ func CustomReactors(reactors map[string]p2p.Reactor) Option {
for _, chDesc := range reactor.GetChannels() {
if !ni.HasChannel(chDesc.ID) {
ni.Channels = append(ni.Channels, chDesc.ID)
n.transport.AddChannel(chDesc.ID)
err := n.transport.AddChannel(chDesc.ID)
if err != nil {
n.Logger.Debug("AddChannel failed", "err", err)
}
}
}
n.nodeInfo = ni
Expand Down
66 changes: 66 additions & 0 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/line/ostracon/p2p"
"github.com/line/ostracon/p2p/conn"
p2pmock "github.com/line/ostracon/p2p/mock"
p2pmocks "github.com/line/ostracon/p2p/mocks"
"github.com/line/ostracon/privval"
"github.com/line/ostracon/proxy"
sm "github.com/line/ostracon/state"
Expand Down Expand Up @@ -578,3 +579,68 @@ func state(nVals int, height int64) (sm.State, dbm.DB, []types.PrivValidator) {
}
return s, stateDB, privVals
}

func TestNodeInvalidNodeInfoCustomReactors(t *testing.T) {
config := cfg.ResetTestRoot("node_new_node_custom_reactors_test")
defer os.RemoveAll(config.RootDir)

cr := p2pmock.NewReactor()
cr.Channels = []*conn.ChannelDescriptor{
{
ID: byte(0x31),
Priority: 5,
SendQueueCapacity: 100,
RecvMessageCapacity: 100,
},
}
customBlockchainReactor := p2pmock.NewReactor()

nodeKey, err := p2p.LoadOrGenNodeKey(config.NodeKeyFile())
require.NoError(t, err)

pvKey, _ := privval.LoadOrGenFilePV(config.PrivValidatorKeyFile(), config.PrivValidatorStateFile(),
config.PrivValidatorKeyType())
_, err = NewInvalidNode(config,
pvKey,
nodeKey,
proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()),
DefaultGenesisDocProviderFunc(config),
DefaultDBProvider,
DefaultMetricsProvider(config.Instrumentation),
log.TestingLogger(),
CustomReactors(map[string]p2p.Reactor{"FOO": cr, "BLOCKCHAIN": customBlockchainReactor}),
)
require.NoError(t, err)
}

func NewInvalidNode(config *cfg.Config,
privValidator types.PrivValidator,
nodeKey *p2p.NodeKey,
clientCreator proxy.ClientCreator,
genesisDocProvider GenesisDocProvider,
dbProvider DBProvider,
metricsProvider MetricsProvider,
logger log.Logger,
options ...Option) (*Node, error) {
n, err := NewNode(config,
privValidator,
nodeKey,
clientCreator,
genesisDocProvider,
dbProvider,
metricsProvider,
logger,
)
if err != nil {
return nil, err
}

transport, _ := createTransport(config, &p2pmocks.NodeInfo{}, nodeKey, n.proxyApp)
n.transport = transport

for _, option := range options {
option(n)
}

return n, nil
}
93 changes: 93 additions & 0 deletions p2p/mocks/node_info.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion p2p/mocks/peer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions p2p/node_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/line/ostracon/version"
)

//go:generate mockery --case underscore --name NodeInfo

const (
maxNodeInfoSize = 10240 // 10KB
maxNumChannels = 16 // plenty of room for upgrades, for now
Expand Down
15 changes: 9 additions & 6 deletions p2p/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,16 @@ func (mt *MultiplexTransport) Listen(addr NetAddress) error {
// NOTE: NodeInfo must be of type DefaultNodeInfo else channels won't be updated
// This is a bit messy at the moment but is cleaned up in the following version
// when NodeInfo changes from an interface to a concrete type
func (mt *MultiplexTransport) AddChannel(chID byte) {
if ni, ok := mt.nodeInfo.(DefaultNodeInfo); ok {
if !ni.HasChannel(chID) {
ni.Channels = append(ni.Channels, chID)
}
mt.nodeInfo = ni
func (mt *MultiplexTransport) AddChannel(chID byte) error {
ni, ok := mt.nodeInfo.(DefaultNodeInfo)
if !ok {
return fmt.Errorf("nodeInfo type: %T is not supported", mt.nodeInfo)
}
if !ni.HasChannel(chID) {
ni.Channels = append(ni.Channels, chID)
}
mt.nodeInfo = ni
return nil
}

func (mt *MultiplexTransport) acceptPeers() {
Expand Down
4 changes: 3 additions & 1 deletion p2p/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/line/ostracon/libs/protoio"
"github.com/line/ostracon/p2p/conn"
tmp2p "github.com/line/ostracon/proto/ostracon/p2p"
"github.com/stretchr/testify/require"
)

var defaultNodeName = "host_peer"
Expand Down Expand Up @@ -630,7 +631,8 @@ func TestTransportAddChannel(t *testing.T) {
)
testChannel := byte(0x01)

mt.AddChannel(testChannel)
err := mt.AddChannel(testChannel)
require.NoError(t, err)
if !mt.nodeInfo.(DefaultNodeInfo).HasChannel(testChannel) {
t.Errorf("missing added channel %v. Got %v", testChannel, mt.nodeInfo.(DefaultNodeInfo).Channels)
}
Expand Down

0 comments on commit 2828a22

Please sign in to comment.