From b21a8a11128e301a7ce7763551d25cde17695f15 Mon Sep 17 00:00:00 2001 From: Kishan Sagathiya Date: Mon, 8 Aug 2022 21:02:03 +0800 Subject: [PATCH] Added checks to avoid decoding messages as handshakes (#2681) Sometimes, we end up decoding a message as a handshake and IsHandshake is not useful after decoding has happened. IsHandshake in Handshake implementations has been improved to check if the value of `Roles` is valid. If we decode bytes of handshake to handshake Roles would have a valid value (1,2, 4) otherwise it would have some random value. --- chain/dev/defaults.go | 3 ++- chain/gssmr/defaults.go | 3 ++- chain/kusama/defaults.go | 3 ++- chain/polkadot/defaults.go | 3 ++- cmd/gossamer/config.go | 25 ++++++++++++------------- cmd/gossamer/config_test.go | 3 ++- cmd/gossamer/export.go | 2 +- dot/config.go | 5 +++-- dot/config_test.go | 9 +++++---- dot/network/block_announce.go | 9 ++++++++- dot/network/block_announce_test.go | 1 + dot/network/config.go | 5 +++-- dot/network/host_test.go | 3 +-- dot/network/notifications.go | 2 ++ dot/network/service.go | 2 +- dot/node_integration_test.go | 13 ++++++------- dot/node_test.go | 6 +++--- dot/rpc/modules/api.go | 2 +- dot/rpc/modules/mocks/network_api.go | 8 ++++---- dot/rpc/modules/system.go | 6 +++--- dot/rpc/modules/system_test.go | 10 +++++----- dot/services.go | 2 +- dot/services_integration_test.go | 13 +++++++------ dot/types/roles.go | 15 --------------- lib/common/network.go | 22 ++++++++++++++++++++-- lib/runtime/types.go | 2 +- lib/runtime/wasmer/instance.go | 2 +- lib/runtime/wasmer/test_helpers.go | 4 ++-- tests/rpc/system_integration_test.go | 4 ++-- 29 files changed, 103 insertions(+), 84 deletions(-) delete mode 100644 dot/types/roles.go diff --git a/chain/dev/defaults.go b/chain/dev/defaults.go index eb6a172117..152999106a 100644 --- a/chain/dev/defaults.go +++ b/chain/dev/defaults.go @@ -5,6 +5,7 @@ package dev import ( "github.com/ChainSafe/gossamer/internal/log" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/runtime/wasmer" ) @@ -52,7 +53,7 @@ var ( // DefaultAuthority is true if the node is a block producer and a grandpa authority DefaultAuthority = true // DefaultRoles Default node roles - DefaultRoles = byte(4) // authority node (see Table D.2) + DefaultRoles = common.AuthorityRole // authority node (see Table D.2) // DefaultBabeAuthority is true if the node is a block producer (overwrites previous settings) DefaultBabeAuthority = true // DefaultGrandpaAuthority is true if the node is a grandpa authority (overwrites previous settings) diff --git a/chain/gssmr/defaults.go b/chain/gssmr/defaults.go index 061b8cd478..2f5b7d9e15 100644 --- a/chain/gssmr/defaults.go +++ b/chain/gssmr/defaults.go @@ -7,6 +7,7 @@ import ( "time" "github.com/ChainSafe/gossamer/internal/log" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/runtime/wasmer" ) @@ -54,7 +55,7 @@ var ( // DefaultAuthority is true if the node is a block producer and a grandpa authority DefaultAuthority = true // DefaultRoles Default node roles - DefaultRoles = byte(4) // authority node (see Table D.2) + DefaultRoles = common.AuthorityRole // authority node (see Table D.2) // DefaultBabeAuthority is true if the node is a block producer (overwrites previous settings) DefaultBabeAuthority = true // DefaultGrandpaAuthority is true if the node is a grandpa authority (overwrites previous settings) diff --git a/chain/kusama/defaults.go b/chain/kusama/defaults.go index f5c7f2f52c..a55caecd5b 100644 --- a/chain/kusama/defaults.go +++ b/chain/kusama/defaults.go @@ -5,6 +5,7 @@ package kusama import ( "github.com/ChainSafe/gossamer/internal/log" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/runtime/wasmer" ) @@ -52,7 +53,7 @@ var ( // DefaultAuthority true if BABE block producer DefaultAuthority = false // DefaultRoles Default node roles - DefaultRoles = byte(1) // full node (see Table D.2) + DefaultRoles = common.FullNodeRole // full node (see Table D.2) // DefaultWasmInterpreter is the name of the wasm interpreter to use by default DefaultWasmInterpreter = wasmer.Name diff --git a/chain/polkadot/defaults.go b/chain/polkadot/defaults.go index 4ae826c5e1..be9394f088 100644 --- a/chain/polkadot/defaults.go +++ b/chain/polkadot/defaults.go @@ -5,6 +5,7 @@ package polkadot import ( "github.com/ChainSafe/gossamer/internal/log" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/runtime/wasmer" ) @@ -49,7 +50,7 @@ var ( // DefaultAuthority is true if the node is a block producer and a grandpa authority DefaultAuthority = true // DefaultRoles Default node roles - DefaultRoles = byte(1) // authority node (see Table D.2) + DefaultRoles = common.FullNodeRole // authority node (see Table D.2) // DefaultBabeAuthority is true if the node is a block producer (overwrites previous settings) DefaultBabeAuthority = true // DefaultGrandpaAuthority is true if the node is a grandpa authority (overwrites previous settings) diff --git a/cmd/gossamer/config.go b/cmd/gossamer/config.go index 228eda271e..764db13b60 100644 --- a/cmd/gossamer/config.go +++ b/cmd/gossamer/config.go @@ -16,7 +16,6 @@ import ( ctoml "github.com/ChainSafe/gossamer/dot/config/toml" "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/state/pruner" - "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/internal/log" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" @@ -572,9 +571,9 @@ func setDotAccountConfig(ctx *cli.Context, tomlCfg ctoml.AccountConfig, cfg *dot // setDotCoreConfig sets dot.CoreConfig using flag values from the cli context func setDotCoreConfig(ctx *cli.Context, tomlCfg ctoml.CoreConfig, cfg *dot.CoreConfig) { - cfg.Roles = tomlCfg.Roles - cfg.BabeAuthority = tomlCfg.Roles == types.AuthorityRole - cfg.GrandpaAuthority = tomlCfg.Roles == types.AuthorityRole + cfg.Roles = common.Roles(tomlCfg.Roles) + cfg.BabeAuthority = common.Roles(tomlCfg.Roles) == common.AuthorityRole + cfg.GrandpaAuthority = common.Roles(tomlCfg.Roles) == common.AuthorityRole cfg.GrandpaInterval = time.Second * time.Duration(tomlCfg.GrandpaInterval) cfg.BABELead = tomlCfg.BABELead @@ -586,14 +585,14 @@ func setDotCoreConfig(ctx *cli.Context, tomlCfg ctoml.CoreConfig, cfg *dot.CoreC if roles := ctx.GlobalString(RolesFlag.Name); roles != "" { // convert string to byte n, err := strconv.Atoi(roles) - b := byte(n) + b := common.Roles(n) if err != nil { logger.Errorf("failed to convert Roles to byte: %s", err) - } else if b == types.AuthorityRole { + } else if b == common.AuthorityRole { // if roles byte is 4, act as an authority (see Table D.2) logger.Debug("authority enabled (roles=4)") cfg.Roles = b - } else if b > types.AuthorityRole { + } else if b > common.AuthorityRole { // if roles byte is greater than 4, invalid roles byte (see Table D.2) logger.Errorf("invalid roles option provided, authority disabled (roles=%d)", b) } else { @@ -605,15 +604,15 @@ func setDotCoreConfig(ctx *cli.Context, tomlCfg ctoml.CoreConfig, cfg *dot.CoreC // to turn on BABE but not grandpa, cfg.Roles must be set to 4 // and cfg.GrandpaAuthority must be set to false - if cfg.Roles == types.AuthorityRole && !tomlCfg.BabeAuthority { + if cfg.Roles == common.AuthorityRole && !tomlCfg.BabeAuthority { cfg.BabeAuthority = false } - if cfg.Roles == types.AuthorityRole && !tomlCfg.GrandpaAuthority { + if cfg.Roles == common.AuthorityRole && !tomlCfg.GrandpaAuthority { cfg.GrandpaAuthority = false } - if cfg.Roles != types.AuthorityRole { + if cfg.Roles != common.AuthorityRole { cfg.BabeAuthority = false cfg.GrandpaAuthority = false } @@ -805,9 +804,9 @@ func setSystemInfoConfig(ctx *cli.Context, cfg *dot.Config) { func updateDotConfigFromGenesisJSONRaw(tomlCfg ctoml.Config, cfg *dot.Config) { cfg.Account.Key = tomlCfg.Account.Key cfg.Account.Unlock = tomlCfg.Account.Unlock - cfg.Core.Roles = tomlCfg.Core.Roles - cfg.Core.BabeAuthority = tomlCfg.Core.Roles == types.AuthorityRole - cfg.Core.GrandpaAuthority = tomlCfg.Core.Roles == types.AuthorityRole + cfg.Core.Roles = common.Roles(tomlCfg.Core.Roles) + cfg.Core.BabeAuthority = common.Roles(tomlCfg.Core.Roles) == common.AuthorityRole + cfg.Core.GrandpaAuthority = common.Roles(tomlCfg.Core.Roles) == common.AuthorityRole // use default genesis file if genesis configuration not provided, for example, // if we load a toml configuration file without a defined genesis init value or diff --git a/cmd/gossamer/config_test.go b/cmd/gossamer/config_test.go index 0a744d5369..e033766e57 100644 --- a/cmd/gossamer/config_test.go +++ b/cmd/gossamer/config_test.go @@ -21,6 +21,7 @@ import ( "github.com/ChainSafe/gossamer/dot/state" "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/internal/log" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/runtime" "github.com/ChainSafe/gossamer/lib/utils" @@ -974,7 +975,7 @@ func TestGlobalNodeName_WhenNodeAlreadyHasStoredName(t *testing.T) { err = os.WriteFile(genPath, genData, os.ModePerm) require.NoError(t, err) - cfg.Core.Roles = types.FullNodeRole + cfg.Core.Roles = common.FullNodeRole cfg.Core.BabeAuthority = false cfg.Core.GrandpaAuthority = false cfg.Init.Genesis = genPath diff --git a/cmd/gossamer/export.go b/cmd/gossamer/export.go index d511889877..1819374e4a 100644 --- a/cmd/gossamer/export.go +++ b/cmd/gossamer/export.go @@ -101,7 +101,7 @@ func dotConfigToToml(dcfg *dot.Config) *ctoml.Config { } cfg.Core = ctoml.CoreConfig{ - Roles: dcfg.Core.Roles, + Roles: byte(dcfg.Core.Roles), BabeAuthority: dcfg.Core.BabeAuthority, GrandpaAuthority: dcfg.Core.GrandpaAuthority, GrandpaInterval: uint32(dcfg.Core.GrandpaInterval / time.Second), diff --git a/dot/config.go b/dot/config.go index a9e1b633df..f170975fd8 100644 --- a/dot/config.go +++ b/dot/config.go @@ -16,6 +16,7 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/internal/log" "github.com/ChainSafe/gossamer/internal/pprof" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" ) @@ -106,7 +107,7 @@ type NetworkConfig struct { // CoreConfig is to marshal/unmarshal toml core config vars type CoreConfig struct { - Roles byte + Roles common.Roles BabeAuthority bool BABELead bool GrandpaAuthority bool @@ -163,7 +164,7 @@ type StateConfig struct { // networkServiceEnabled returns true if the network service is enabled func networkServiceEnabled(cfg *Config) bool { - return cfg.Core.Roles != byte(0) + return cfg.Core.Roles != common.NoNetworkRole } // PprofConfig is the configuration for the pprof HTTP server. diff --git a/dot/config_test.go b/dot/config_test.go index 81a3e644e5..901deaa2bb 100644 --- a/dot/config_test.go +++ b/dot/config_test.go @@ -9,6 +9,7 @@ import ( "github.com/ChainSafe/gossamer/internal/log" "github.com/ChainSafe/gossamer/internal/pprof" + "github.com/ChainSafe/gossamer/lib/common" "github.com/stretchr/testify/assert" ) @@ -50,7 +51,7 @@ func TestConfig(t *testing.T) { Key: "alice", }, Core: CoreConfig{ - Roles: byte(4), + Roles: common.AuthorityRole, BabeAuthority: true, BABELead: true, GrandpaAuthority: true, @@ -109,7 +110,7 @@ func TestConfig(t *testing.T) { }, Account: AccountConfig{}, Core: CoreConfig{ - Roles: byte(4), + Roles: common.AuthorityRole, BabeAuthority: true, GrandpaAuthority: true, WasmInterpreter: "wasmer", @@ -170,7 +171,7 @@ func TestConfig(t *testing.T) { }, Account: AccountConfig{}, Core: CoreConfig{ - Roles: byte(1), + Roles: common.FullNodeRole, WasmInterpreter: "wasmer", GrandpaInterval: 0, }, @@ -227,7 +228,7 @@ func TestConfig(t *testing.T) { }, Init: InitConfig{Genesis: "./chain/polkadot/genesis.json"}, Core: CoreConfig{ - Roles: byte(1), + Roles: common.FullNodeRole, WasmInterpreter: "wasmer", }, Network: NetworkConfig{ diff --git a/dot/network/block_announce.go b/dot/network/block_announce.go index 033ded1e4b..a6457c9064 100644 --- a/dot/network/block_announce.go +++ b/dot/network/block_announce.go @@ -15,6 +15,7 @@ import ( "github.com/libp2p/go-libp2p-core/peer" ) +var errInvalidRole = errors.New("invalid role") var ( _ NotificationsMessage = &BlockAnnounceMessage{} _ NotificationsMessage = &BlockAnnounceHandshake{} @@ -103,7 +104,7 @@ func decodeBlockAnnounceMessage(in []byte) (NotificationsMessage, error) { // BlockAnnounceHandshake is exchanged by nodes that are beginning the BlockAnnounce protocol type BlockAnnounceHandshake struct { - Roles byte + Roles common.Roles BestBlockNumber uint32 BestBlockHash common.Hash GenesisHash common.Hash @@ -173,6 +174,12 @@ func (s *Service) validateBlockAnnounceHandshake(from peer.ID, hs Handshake) err return errors.New("invalid handshake type") } + switch bhs.Roles { + case common.FullNodeRole, common.LightClientRole, common.AuthorityRole: + default: + return fmt.Errorf("%w: %d", errInvalidRole, bhs.Roles) + } + if !bhs.GenesisHash.Equal(s.blockState.GenesisHash()) { s.host.cm.peerSetHandler.ReportPeer(peerset.ReputationChange{ Value: peerset.GenesisMismatch, diff --git a/dot/network/block_announce_test.go b/dot/network/block_announce_test.go index 3d9fd07314..1c5abf3a1f 100644 --- a/dot/network/block_announce_test.go +++ b/dot/network/block_announce_test.go @@ -165,6 +165,7 @@ func TestValidateBlockAnnounceHandshake(t *testing.T) { nodeA.notificationsProtocols[BlockAnnounceMsgType].peersData.setInboundHandshakeData(testPeerID, &handshakeData{}) err := nodeA.validateBlockAnnounceHandshake(testPeerID, &BlockAnnounceHandshake{ + Roles: common.FullNodeRole, BestBlockNumber: 100, GenesisHash: nodeA.blockState.GenesisHash(), }) diff --git a/dot/network/config.go b/dot/network/config.go index 8b849467a0..267a96bb15 100644 --- a/dot/network/config.go +++ b/dot/network/config.go @@ -13,6 +13,7 @@ import ( "github.com/ChainSafe/gossamer/dot/telemetry" "github.com/ChainSafe/gossamer/internal/log" "github.com/ChainSafe/gossamer/internal/metrics" + "github.com/ChainSafe/gossamer/lib/common" ) const ( @@ -32,7 +33,7 @@ const ( DefaultProtocolID = "/gossamer/gssmr/0" // DefaultRoles the default value for Config.Roles (0 = no network, 1 = full node) - DefaultRoles = byte(1) + DefaultRoles = common.FullNodeRole // DefaultMinPeerCount is the default minimum peer count DefaultMinPeerCount = 5 @@ -58,7 +59,7 @@ type Config struct { // BasePath the data directory for the node BasePath string // Roles a bitmap value that represents the different roles for the sender node (see Table D.2) - Roles byte + Roles common.Roles // Service interfaces BlockState BlockState diff --git a/dot/network/host_test.go b/dot/network/host_test.go index 4cb5bfab8e..f56018ba42 100644 --- a/dot/network/host_test.go +++ b/dot/network/host_test.go @@ -330,12 +330,11 @@ func TestStreamCloseMetadataCleanup(t *testing.T) { require.NoError(t, err) const ( - roles byte = 4 bestBlockNumber uint32 = 77 ) testHandshake := &BlockAnnounceHandshake{ - Roles: roles, + Roles: common.AuthorityRole, BestBlockNumber: bestBlockNumber, BestBlockHash: common.Hash{1}, GenesisHash: nodeB.blockState.GenesisHash(), diff --git a/dot/network/notifications.go b/dot/network/notifications.go index 84955a5e63..97179b3f3f 100644 --- a/dot/network/notifications.go +++ b/dot/network/notifications.go @@ -154,6 +154,8 @@ func (s *Service) createNotificationsMessageHandler( hs, ok := msg.(Handshake) if !ok { + // NOTE: As long as, Handshake interface and NotificationMessage interfaces are same, + // this error would never happen. return errMessageIsNotHandshake } diff --git a/dot/network/service.go b/dot/network/service.go index 5c7bf0c769..f19c8ac818 100644 --- a/dot/network/service.go +++ b/dot/network/service.go @@ -616,7 +616,7 @@ func (s *Service) RemoveReservedPeers(addrs ...string) error { } // NodeRoles Returns the roles the node is running as. -func (s *Service) NodeRoles() byte { +func (s *Service) NodeRoles() common.Roles { return s.cfg.Roles } diff --git a/dot/node_integration_test.go b/dot/node_integration_test.go index dfa18c2b69..1abb204ccd 100644 --- a/dot/node_integration_test.go +++ b/dot/node_integration_test.go @@ -15,7 +15,6 @@ import ( "github.com/ChainSafe/gossamer/dot/core" "github.com/ChainSafe/gossamer/dot/state" - "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/lib/babe" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/crypto/sr25519" @@ -92,7 +91,7 @@ func TestNewNodeIntegration(t *testing.T) { err = keystore.LoadKeystore("alice", ks.Babe) require.NoError(t, err) - cfg.Core.Roles = types.FullNodeRole + cfg.Core.Roles = common.FullNodeRole node, err := NewNode(cfg, ks) require.NoError(t, err) @@ -121,7 +120,7 @@ func TestNewNode_Authority(t *testing.T) { require.NoError(t, err) require.Equal(t, 1, ks.Babe.Size()) - cfg.Core.Roles = types.AuthorityRole + cfg.Core.Roles = common.AuthorityRole node, err := NewNode(cfg, ks) require.NoError(t, err) @@ -150,7 +149,7 @@ func TestStartStopNode(t *testing.T) { err = keystore.LoadKeystore("alice", ks.Babe) require.NoError(t, err) - cfg.Core.Roles = types.FullNodeRole + cfg.Core.Roles = common.FullNodeRole node, err := NewNode(cfg, ks) require.NoError(t, err) @@ -168,7 +167,7 @@ func TestInitNode_LoadStorageRoot(t *testing.T) { genPath := newTestGenesisAndRuntime(t) - cfg.Core.Roles = types.FullNodeRole + cfg.Core.Roles = common.FullNodeRole cfg.Core.BabeAuthority = false cfg.Core.GrandpaAuthority = false cfg.Init.Genesis = genPath @@ -217,7 +216,7 @@ func TestInitNode_LoadBalances(t *testing.T) { genPath := newTestGenesisAndRuntime(t) - cfg.Core.Roles = types.FullNodeRole + cfg.Core.Roles = common.FullNodeRole cfg.Core.BabeAuthority = false cfg.Core.GrandpaAuthority = false cfg.Init.Genesis = genPath @@ -256,7 +255,7 @@ func TestNode_PersistGlobalName_WhenInitialize(t *testing.T) { cfg := NewTestConfig(t) cfg.Global.Name = globalName - cfg.Core.Roles = types.FullNodeRole + cfg.Core.Roles = common.FullNodeRole cfg.Core.BabeAuthority = false cfg.Core.GrandpaAuthority = false cfg.Init.Genesis = newTestGenesisAndRuntime(t) diff --git a/dot/node_test.go b/dot/node_test.go index 9855fec79a..bf5bef60c8 100644 --- a/dot/node_test.go +++ b/dot/node_test.go @@ -139,14 +139,14 @@ func TestNewNode(t *testing.T) { Init: InitConfig{Genesis: genFile}, Account: AccountConfig{Key: "alice"}, Core: CoreConfig{ - Roles: types.FullNodeRole, + Roles: common.FullNodeRole, WasmInterpreter: wasmer.Name, }, } dotConfig.Init = InitConfig{Genesis: genFile} dotConfig.Account = AccountConfig{Key: "alice"} - dotConfig.Core.Roles = types.FullNodeRole + dotConfig.Core.Roles = common.FullNodeRole dotConfig.Core.WasmInterpreter = wasmer.Name dotConfig.Global.Name = "TestNode" @@ -307,7 +307,7 @@ func initKeystore(t *testing.T, cfg *Config) (*keystore.GlobalKeystore, error) { require.NoError(t, err) // if authority node, should have at least 1 key in keystore - if cfg.Core.Roles == types.AuthorityRole && (ks.Babe.Size() == 0 || ks.Gran.Size() == 0) { + if cfg.Core.Roles == common.AuthorityRole && (ks.Babe.Size() == 0 || ks.Gran.Size() == 0) { return nil, ErrNoKeysProvided } diff --git a/dot/rpc/modules/api.go b/dot/rpc/modules/api.go index 68f49a4e0d..5b06a8cba3 100644 --- a/dot/rpc/modules/api.go +++ b/dot/rpc/modules/api.go @@ -60,7 +60,7 @@ type NetworkAPI interface { Health() common.Health NetworkState() common.NetworkState Peers() []common.PeerInfo - NodeRoles() byte + NodeRoles() common.Roles Stop() error Start() error IsStopped() bool diff --git a/dot/rpc/modules/mocks/network_api.go b/dot/rpc/modules/mocks/network_api.go index 52a90b48f8..9fd809eac3 100644 --- a/dot/rpc/modules/mocks/network_api.go +++ b/dot/rpc/modules/mocks/network_api.go @@ -75,14 +75,14 @@ func (_m *NetworkAPI) NetworkState() common.NetworkState { } // NodeRoles provides a mock function with given fields: -func (_m *NetworkAPI) NodeRoles() byte { +func (_m *NetworkAPI) NodeRoles() common.Roles { ret := _m.Called() - var r0 byte - if rf, ok := ret.Get(0).(func() byte); ok { + var r0 common.Roles + if rf, ok := ret.Get(0).(func() common.Roles); ok { r0 = rf() } else { - r0 = ret.Get(0).(byte) + r0 = ret.Get(0).(common.Roles) } return r0 diff --git a/dot/rpc/modules/system.go b/dot/rpc/modules/system.go index cc091113fc..0dc99b0a53 100644 --- a/dot/rpc/modules/system.go +++ b/dot/rpc/modules/system.go @@ -141,11 +141,11 @@ func (sm *SystemModule) NodeRoles(r *http.Request, req *EmptyRequest, res *[]int role := sm.networkAPI.NodeRoles() switch role { - case 1: + case common.FullNodeRole: resultArray = append(resultArray, "Full") - case 2: + case common.LightClientRole: resultArray = append(resultArray, "LightClient") - case 4: + case common.AuthorityRole: resultArray = append(resultArray, "Authority") default: resultArray = append(resultArray, "UnknownRole") diff --git a/dot/rpc/modules/system_test.go b/dot/rpc/modules/system_test.go index 0bff5c7cf0..1d2b65f148 100644 --- a/dot/rpc/modules/system_test.go +++ b/dot/rpc/modules/system_test.go @@ -131,16 +131,16 @@ func TestSystemModule_PeersTest(t *testing.T) { func TestSystemModule_NodeRolesTest(t *testing.T) { mockNetworkAPI1 := new(mocks.NetworkAPI) - mockNetworkAPI1.On("NodeRoles").Return(byte(1), nil) + mockNetworkAPI1.On("NodeRoles").Return(common.FullNodeRole, nil) mockNetworkAPI2 := new(mocks.NetworkAPI) - mockNetworkAPI2.On("NodeRoles").Return(byte(2), nil) + mockNetworkAPI2.On("NodeRoles").Return(common.LightClientRole, nil) mockNetworkAPI3 := new(mocks.NetworkAPI) - mockNetworkAPI3.On("NodeRoles").Return(byte(4), nil) + mockNetworkAPI3.On("NodeRoles").Return(common.AuthorityRole, nil) mockNetworkAPI4 := new(mocks.NetworkAPI) - mockNetworkAPI4.On("NodeRoles").Return(byte(21), nil) + mockNetworkAPI4.On("NodeRoles").Return(common.Roles(21), nil) type args struct { r *http.Request @@ -183,7 +183,7 @@ func TestSystemModule_NodeRolesTest(t *testing.T) { args: args{ req: &EmptyRequest{}, }, - exp: []interface{}{"UnknownRole", []interface{}{uint8(21)}}, + exp: []interface{}{"UnknownRole", []interface{}{common.Roles(21)}}, }, } for _, tt := range tests { diff --git a/dot/services.go b/dot/services.go index a9bc446d3f..1f18a10102 100644 --- a/dot/services.go +++ b/dot/services.go @@ -223,7 +223,7 @@ func (nodeBuilder) createCoreService(cfg *Config, ks *keystore.GlobalKeystore, st *state.Service, net *network.Service, dh *digest.Handler) ( *core.Service, error) { logger.Debug("creating core service" + - asAuthority(cfg.Core.Roles == types.AuthorityRole) + + asAuthority(cfg.Core.Roles == common.AuthorityRole) + "...") genesisData, err := st.Base.LoadGenesisData() diff --git a/dot/services_integration_test.go b/dot/services_integration_test.go index ee55a8ba46..62a54f9bc7 100644 --- a/dot/services_integration_test.go +++ b/dot/services_integration_test.go @@ -16,6 +16,7 @@ import ( "github.com/ChainSafe/gossamer/dot/types" "github.com/ChainSafe/gossamer/internal/log" "github.com/ChainSafe/gossamer/internal/pprof" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/genesis" "github.com/ChainSafe/gossamer/lib/grandpa" "github.com/ChainSafe/gossamer/lib/keystore" @@ -96,7 +97,7 @@ func TestCreateCoreService(t *testing.T) { genFile := NewTestGenesisRawFile(t, cfg) - cfg.Core.Roles = types.FullNodeRole + cfg.Core.Roles = common.FullNodeRole cfg.Core.BabeAuthority = false cfg.Core.GrandpaAuthority = false cfg.Init.Genesis = genFile @@ -193,7 +194,7 @@ func TestCreateRPCService(t *testing.T) { genFile := NewTestGenesisRawFile(t, cfg) - cfg.Core.Roles = types.FullNodeRole + cfg.Core.Roles = common.FullNodeRole cfg.Core.BabeAuthority = false cfg.Core.GrandpaAuthority = false cfg.Init.Genesis = genFile @@ -242,7 +243,7 @@ func TestCreateBABEService_Integration(t *testing.T) { genFile := NewTestGenesisRawFile(t, cfg) - cfg.Core.Roles = types.FullNodeRole + cfg.Core.Roles = common.FullNodeRole cfg.Init.Genesis = genFile err := InitNode(cfg) @@ -277,7 +278,7 @@ func TestCreateGrandpaService(t *testing.T) { genFile := NewTestGenesisRawFile(t, cfg) - cfg.Core.Roles = types.AuthorityRole + cfg.Core.Roles = common.AuthorityRole cfg.Init.Genesis = genFile err := InitNode(cfg) @@ -344,7 +345,7 @@ func TestNewWebSocketServer(t *testing.T) { genFile := NewTestGenesisRawFile(t, cfg) - cfg.Core.Roles = types.FullNodeRole + cfg.Core.Roles = common.FullNodeRole cfg.Core.BabeAuthority = false cfg.Core.GrandpaAuthority = false cfg.Init.Genesis = genFile @@ -438,7 +439,7 @@ func Test_createDigestHandler(t *testing.T) { genFile := NewTestGenesisRawFile(t, cfg) - cfg.Core.Roles = types.AuthorityRole + cfg.Core.Roles = common.AuthorityRole cfg.Init.Genesis = genFile err := InitNode(cfg) diff --git a/dot/types/roles.go b/dot/types/roles.go deleted file mode 100644 index fe44c416c8..0000000000 --- a/dot/types/roles.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2021 ChainSafe Systems (ON) -// SPDX-License-Identifier: LGPL-3.0-only - -package types - -const ( - // NoNetworkRole runs a node without networking - NoNetworkRole = byte(0) - // FullNodeRole runs a full node - FullNodeRole = byte(1) - // LightClientRole runs a light client - LightClientRole = byte(2) - // AuthorityRole runs the node as a block-producing and finalising node - AuthorityRole = byte(4) -) diff --git a/lib/common/network.go b/lib/common/network.go index 4385687ff3..c5eac535d2 100644 --- a/lib/common/network.go +++ b/lib/common/network.go @@ -3,7 +3,9 @@ package common -import ma "github.com/multiformats/go-multiaddr" +import ( + ma "github.com/multiformats/go-multiaddr" +) // Health is network information about host needed for the rpc server type Health struct { @@ -21,7 +23,23 @@ type NetworkState struct { // PeerInfo is network information about peers needed for the rpc server type PeerInfo struct { PeerID string - Roles byte + Roles Roles BestHash Hash BestNumber uint64 } + +// Roles is the type of node. +type Roles byte + +const ( + // NoNetworkRole runs a node without networking + NoNetworkRole Roles = 0 + // FullNodeRole allow you to read the current state of the chain and to submit and validate + // extrinsics directly on the network without relying on a centralised infrastructure provider. + FullNodeRole Roles = 1 + // LightClientRole node has only the runtime and the current state, but does not store past + // blocks and so cannot read historical data without requesting it from a node that has it. + LightClientRole Roles = 2 + // AuthorityRole runs the node as a block-producing and finalising node + AuthorityRole Roles = 4 +) diff --git a/lib/runtime/types.go b/lib/runtime/types.go index 312be14058..14f4483974 100644 --- a/lib/runtime/types.go +++ b/lib/runtime/types.go @@ -52,7 +52,7 @@ type InstanceConfig struct { Storage Storage Keystore *keystore.GlobalKeystore LogLvl log.Level - Role byte + Role common.Roles NodeStorage NodeStorage Network BasicNetwork Transaction TransactionState diff --git a/lib/runtime/wasmer/instance.go b/lib/runtime/wasmer/instance.go index b26cf4d255..a4eb82b35e 100644 --- a/lib/runtime/wasmer/instance.go +++ b/lib/runtime/wasmer/instance.go @@ -94,7 +94,7 @@ func NewInstance(code []byte, cfg runtime.InstanceConfig) (instance *Instance, e Storage: cfg.Storage, Allocator: allocator, Keystore: cfg.Keystore, - Validator: cfg.Role == byte(4), + Validator: cfg.Role == common.AuthorityRole, NodeStorage: cfg.NodeStorage, Network: cfg.Network, Transaction: cfg.Transaction, diff --git a/lib/runtime/wasmer/test_helpers.go b/lib/runtime/wasmer/test_helpers.go index 95839014e0..d0ee3fd737 100644 --- a/lib/runtime/wasmer/test_helpers.go +++ b/lib/runtime/wasmer/test_helpers.go @@ -31,7 +31,7 @@ func NewTestInstance(t *testing.T, targetRuntime string) *Instance { func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie) *Instance { t.Helper() - cfg := setupConfig(t, tt, DefaultTestLogLvl, 0) + cfg := setupConfig(t, tt, DefaultTestLogLvl, common.NoNetworkRole) runtimeFilepath, err := runtime.GetRuntime(context.Background(), targetRuntime) require.NoError(t, err) @@ -41,7 +41,7 @@ func NewTestInstanceWithTrie(t *testing.T, targetRuntime string, tt *trie.Trie) return r } -func setupConfig(t *testing.T, tt *trie.Trie, lvl log.Level, role byte) runtime.InstanceConfig { +func setupConfig(t *testing.T, tt *trie.Trie, lvl log.Level, role common.Roles) runtime.InstanceConfig { t.Helper() s := storage.NewTrieState(tt) diff --git a/tests/rpc/system_integration_test.go b/tests/rpc/system_integration_test.go index 7964cfa8e5..0f413f7b0b 100644 --- a/tests/rpc/system_integration_test.go +++ b/tests/rpc/system_integration_test.go @@ -10,7 +10,7 @@ import ( "github.com/ChainSafe/gossamer/dot/config/toml" "github.com/ChainSafe/gossamer/dot/rpc/modules" - "github.com/ChainSafe/gossamer/dot/types" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/tests/utils" "github.com/ChainSafe/gossamer/tests/utils/node" "github.com/ChainSafe/gossamer/tests/utils/rpc" @@ -31,7 +31,7 @@ func TestStableNetworkRPC(t *testing.T) { //nolint:tparallel Modules: []string{"system", "author", "chain"}, }, Core: toml.CoreConfig{ - Roles: types.FullNodeRole, + Roles: byte(common.FullNodeRole), }, }