Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dot/peerset): Implement peer scoring #1791

Merged
merged 30 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
af2dc58
Implement peerState and PSM (Peer Set Manager) and add test cases.
arijitAD Sep 22, 2021
c789a48
Fix test failures.
arijitAD Sep 28, 2021
9cb2014
Test case fix and added inbound check
arijitAD Sep 29, 2021
897927a
Fix failing test.
arijitAD Oct 7, 2021
951e489
Self review.
arijitAD Oct 7, 2021
6c87932
Address comments.
arijitAD Oct 12, 2021
1e1ffa4
Fix deep source error.
arijitAD Oct 13, 2021
ec22171
Fix failing unit test.
arijitAD Oct 14, 2021
21a5248
Fix deepsource errors.
arijitAD Oct 18, 2021
ad1967f
Address comments and add unit test.
arijitAD Oct 19, 2021
3509cf9
Merge branch 'development' into peer-scoring
arijitAD Oct 20, 2021
b953984
Address comments.
arijitAD Oct 21, 2021
9656e2e
Remove channel from sorted peers
arijitAD Oct 22, 2021
1545212
Minor changes.
arijitAD Oct 22, 2021
68bde70
Remove channel from sorted peers
arijitAD Oct 22, 2021
00d4975
Merge branch 'development' into peer-scoring
arijitAD Oct 22, 2021
dd64bc0
Address comments
arijitAD Oct 25, 2021
900d173
Merge branch 'development' into peer-scoring
arijitAD Oct 25, 2021
e7c300f
Merge branch 'development' into peer-scoring
arijitAD Oct 26, 2021
7960728
Merge remote-tracking branch 'origin/development' into peer-scoring
arijitAD Oct 27, 2021
f7b4615
Fix failing tests.
arijitAD Oct 28, 2021
15764ee
Merge branch 'development' into peer-scoring
arijitAD Oct 28, 2021
cc22fd3
Address comments
arijitAD Oct 28, 2021
382a978
Merge branch 'development' into peer-scoring
arijitAD Oct 28, 2021
413c120
Address comments.
arijitAD Nov 1, 2021
d5048d3
Merge branch 'development' into peer-scoring
arijitAD Nov 1, 2021
fd03454
Merge remote-tracking branch 'origin/development' into peer-scoring
arijitAD Nov 1, 2021
ba5c418
Merge remote-tracking branch 'origin/development' into peer-scoring
arijitAD Nov 2, 2021
1af2755
Merge branch 'development' into peer-scoring
arijitAD Nov 2, 2021
78e7680
Merge remote-tracking branch 'origin/development' into peer-scoring
arijitAD Nov 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions chain/dev/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package dev

import (
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
log "github.com/ChainSafe/log15"

"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
)

var (
Expand Down Expand Up @@ -72,7 +73,7 @@ var (
// NetworkConfig

// DefaultNetworkPort network port
DefaultNetworkPort = uint32(7001)
DefaultNetworkPort = uint16(7001)
// DefaultNetworkBootnodes network bootnodes
DefaultNetworkBootnodes = []string(nil)
// DefaultNoBootstrap disables bootstrap
Expand Down
5 changes: 3 additions & 2 deletions chain/gssmr/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package gssmr
import (
"time"

"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
log "github.com/ChainSafe/log15"

"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
)

var (
Expand Down Expand Up @@ -74,7 +75,7 @@ var (
// NetworkConfig

// DefaultNetworkPort network port
DefaultNetworkPort = uint32(7001)
DefaultNetworkPort = uint16(7001)
// DefaultNetworkBootnodes network bootnodes
DefaultNetworkBootnodes = []string(nil)
// DefaultNoBootstrap disables bootstrap
Expand Down
5 changes: 3 additions & 2 deletions chain/kusama/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package kusama

import (
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
log "github.com/ChainSafe/log15"

"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
)

var (
Expand Down Expand Up @@ -68,7 +69,7 @@ var (
// NetworkConfig

// DefaultNetworkPort network port
DefaultNetworkPort = uint32(7001)
DefaultNetworkPort = uint16(7001)
// DefaultNetworkBootnodes network bootnodes
DefaultNetworkBootnodes = []string(nil)
// DefaultNoBootstrap disables bootstrap
Expand Down
5 changes: 3 additions & 2 deletions chain/polkadot/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
package polkadot

import (
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
log "github.com/ChainSafe/log15"

"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
)

var (
Expand Down Expand Up @@ -69,7 +70,7 @@ var (
// NetworkConfig

// DefaultNetworkPort network port
DefaultNetworkPort = uint32(7001)
DefaultNetworkPort = uint16(7001)
// DefaultNetworkBootnodes network bootnodes
DefaultNetworkBootnodes = []string(nil)
// DefaultNoBootstrap disables bootstrap
Expand Down
2 changes: 1 addition & 1 deletion cmd/gossamer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ func setDotNetworkConfig(ctx *cli.Context, tomlCfg ctoml.NetworkConfig, cfg *dot

// check --port flag and update node configuration
if port := ctx.GlobalUint(PortFlag.Name); port != 0 {
cfg.Port = uint32(port)
cfg.Port = uint16(port)
}

// check --bootnodes flag and update node configuration
Expand Down
5 changes: 3 additions & 2 deletions dot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import (
"encoding/json"
"time"

log "github.com/ChainSafe/log15"

"github.com/ChainSafe/gossamer/chain/dev"
"github.com/ChainSafe/gossamer/chain/gssmr"
"github.com/ChainSafe/gossamer/chain/kusama"
"github.com/ChainSafe/gossamer/chain/polkadot"
"github.com/ChainSafe/gossamer/dot/state/pruner"
"github.com/ChainSafe/gossamer/dot/types"
log "github.com/ChainSafe/log15"
)

// TODO: update config to have toml rules and perhaps un-export some fields, since we don't want to expose all
Expand Down Expand Up @@ -83,7 +84,7 @@ type AccountConfig struct {

// NetworkConfig is to marshal/unmarshal toml network config vars
type NetworkConfig struct {
Port uint32
Port uint16
Bootnodes []string
ProtocolID string
NoBootstrap bool
Expand Down
2 changes: 1 addition & 1 deletion dot/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type AccountConfig struct {

// NetworkConfig is to marshal/unmarshal toml network config vars
type NetworkConfig struct {
Port uint32 `toml:"port,omitempty"`
Port uint16 `toml:"port,omitempty"`
Bootnodes []string `toml:"bootnodes,omitempty"`
ProtocolID string `toml:"protocol,omitempty"`
NoBootstrap bool `toml:"nobootstrap,omitempty"`
Expand Down
4 changes: 4 additions & 0 deletions dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import (
"math/big"
"sync"

"github.com/libp2p/go-libp2p-core/peer"

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/runtime"
Expand Down Expand Up @@ -86,6 +89,7 @@ type TransactionState interface {
type Network interface {
GossipMessage(network.NotificationsMessage)
IsSynced() bool
ReportPeer(change peerset.ReputationChange, p peer.ID)
}

// EpochState is the interface for state.EpochState
Expand Down
19 changes: 18 additions & 1 deletion dot/core/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
package core

import (
"errors"

"github.com/libp2p/go-libp2p-core/peer"

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/peerset"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/transaction"
)

// HandleTransactionMessage validates each transaction in the message and
// adds valid transactions to the transaction queue of the BABE session
// returns boolean for transaction propagation, true - transactions should be propagated
func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (bool, error) {
func (s *Service) HandleTransactionMessage(peerID peer.ID, msg *network.TransactionMessage) (bool, error) {
logger.Debug("received TransactionMessage")

if !s.net.IsSynced() {
Expand Down Expand Up @@ -64,6 +70,12 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (boo
externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, tx...))
val, err := rt.ValidateTransaction(externalExt)
if err != nil {
if errors.Is(err, runtime.ErrInvalidTransaction) {
s.net.ReportPeer(peerset.ReputationChange{
Value: peerset.BadTransactionValue,
Reason: peerset.BadTransactionReason,
}, peerID)
}
logger.Debug("failed to validate transaction", "err", err)
return nil
}
Expand All @@ -88,6 +100,11 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) (boo
}
}

s.net.ReportPeer(peerset.ReputationChange{
Value: peerset.GoodTransactionValue,
Reason: peerset.GoodTransactionReason,
}, peerID)

msg.Extrinsics = toPropagate
return len(msg.Extrinsics) > 0, nil
}
Expand Down
17 changes: 10 additions & 7 deletions dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import (
"testing"
"time"

"github.com/centrifuge/go-substrate-rpc-client/v3/signature"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v3/types"
"github.com/stretchr/testify/require"

"github.com/ChainSafe/gossamer/dot/core/mocks"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
Expand All @@ -31,11 +35,6 @@ import (
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/pkg/scale"

"github.com/centrifuge/go-substrate-rpc-client/v3/signature"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v3/types"

"github.com/stretchr/testify/require"
)

func createExtrinsic(t *testing.T, rt runtime.Instance, genHash common.Hash, nonce uint64) types.Extrinsic {
Expand Down Expand Up @@ -128,6 +127,10 @@ func TestService_HandleBlockProduced(t *testing.T) {
}

func TestService_HandleTransactionMessage(t *testing.T) {
t.Parallel()

const peer1 = "testPeer1"
qdm12 marked this conversation as resolved.
Show resolved Hide resolved

kp, err := sr25519.GenerateKeypair()
require.NoError(t, err)

Expand Down Expand Up @@ -158,7 +161,7 @@ func TestService_HandleTransactionMessage(t *testing.T) {

extBytes := createExtrinsic(t, rt, genHash, 0)
msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}}
b, err := s.HandleTransactionMessage(msg)
b, err := s.HandleTransactionMessage(peer1, msg)
require.NoError(t, err)
require.True(t, b)

Expand All @@ -168,7 +171,7 @@ func TestService_HandleTransactionMessage(t *testing.T) {

extBytes = []byte(`bogus extrinsic`)
msg = &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}}
b, err = s.HandleTransactionMessage(msg)
b, err = s.HandleTransactionMessage(peer1, msg)
require.NoError(t, err)
require.False(t, b)
}
9 changes: 9 additions & 0 deletions dot/core/mocks/Network.go

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

14 changes: 5 additions & 9 deletions dot/core/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import (
"path/filepath"
"testing"

log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

coremocks "github.com/ChainSafe/gossamer/dot/core/mocks"
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
Expand All @@ -32,9 +35,6 @@ import (
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/utils"
log "github.com/ChainSafe/log15"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

// NewTestService creates a new test core service
Expand Down Expand Up @@ -127,6 +127,7 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
net := new(coremocks.Network)
net.On("GossipMessage", mock.AnythingOfType("*network.TransactionMessage"))
net.On("IsSynced").Return(true)
net.On("ReportPeer", mock.AnythingOfType("peerset.ReputationChange"), mock.AnythingOfType("peer.ID"))
cfg.Network = net
}

Expand All @@ -148,10 +149,5 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
s, err := NewService(cfg)
require.NoError(t, err)

if net, ok := cfg.Network.(*network.Service); ok {
net.SetTransactionHandler(s)
_ = net.Stop()
}

return s
}
4 changes: 2 additions & 2 deletions dot/network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const (
DefaultBasePath = "~/.gossamer/gssmr"

// DefaultPort the default value for Config.Port
DefaultPort = uint32(7000)
DefaultPort = uint16(7000)

// DefaultRandSeed the default value for Config.RandSeed (0 = non-deterministic)
DefaultRandSeed = int64(0)
Expand Down Expand Up @@ -74,7 +74,7 @@ type Config struct {
TransactionHandler TransactionHandler

// Port the network port used for listening
Port uint32
Port uint16
// RandSeed the seed used to generate the network p2p identity (0 = non-deterministic random seed)
RandSeed int64
// Bootnodes the peer addresses used for bootstrapping
Expand Down
Loading