Commit da2644c9 authored by Andreas Bigger's avatar Andreas Bigger

start tests

parent 05ec8aa0
......@@ -25,6 +25,15 @@ var (
Required: false,
EnvVar: p2pEnv("NO_DISCOVERY"),
}
P2PScoring = cli.StringFlag{
Name: "p2p.scoring",
Usage: "Sets the scoring strategy for the P2P stack. " +
"Can be one of: none, light, full." +
"Custom scoring strategies can be defined in the config file.",
Required: false,
Value: "none",
EnvVar: p2pEnv("SCORING"),
}
P2PPrivPath = cli.StringFlag{
Name: "p2p.priv.path",
Usage: "Read the hex-encoded 32-byte private key for the peer ID from this txt file. Created if not already exists." +
......
......@@ -66,7 +66,6 @@ type Metricer interface {
RecordSequencerSealingTime(duration time.Duration)
Document() []metrics.DocumentedMetric
// P2P Metrics
RecordGossipEvent(evType int32)
RecordPeerScoring(peerID peer.ID, score float64)
}
......
......@@ -24,7 +24,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
)
func NewConfig(ctx *cli.Context) (*p2p.Config, error) {
func NewConfig(ctx *cli.Context, blockTime uint64) (*p2p.Config, error) {
conf := &p2p.Config{}
if ctx.GlobalBool(flags.DisableP2P.Name) {
......@@ -54,6 +54,10 @@ func NewConfig(ctx *cli.Context) (*p2p.Config, error) {
return nil, fmt.Errorf("failed to load p2p gossip options: %w", err)
}
if err := loadScoringOpts(conf, ctx, blockTime); err != nil {
return nil, fmt.Errorf("failed to load p2p scoring options: %w", err)
}
conf.ConnGater = p2p.DefaultConnGater
conf.ConnMngr = p2p.DefaultConnManager
......@@ -73,11 +77,21 @@ func validatePort(p uint) (uint16, error) {
return uint16(p), nil
}
// TODO: Load peer scoring options
// func loadScoringOpts(conf *p2p.Config, ctx *cli.Context) error {
// conf.ScoringParams = p2p.DefaultScoringParams
// return nil
// }
// loadScoringOpts loads the scoring options from the CLI context.
func loadScoringOpts(conf *p2p.Config, ctx *cli.Context, blockTime uint64) error {
scoringLevel := ctx.GlobalString(flags.P2PScoring.Name)
if scoringLevel == "" {
scoringLevel = "none"
}
peerScoreParams, err := p2p.GetPeerScoreParams(scoringLevel, blockTime)
if err != nil {
return err
}
conf.PeerScoring = peerScoreParams
return nil
}
func loadListenOpts(conf *p2p.Config, ctx *cli.Context) error {
listenIP := ctx.GlobalString(flags.ListenIP.Name)
......
......@@ -19,6 +19,7 @@ import (
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/net/conngater"
cmgr "github.com/libp2p/go-libp2p/p2p/net/connmgr"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/ethereum-optimism/optimism/op-node/rollup"
)
......@@ -49,8 +50,8 @@ type Config struct {
DisableP2P bool
NoDiscovery bool
// P2P Scoring Configurations
ScoringConfig *ScoringConfig
// Pubsub P2P Scoring Configurations
PeerScoring pubsub.PeerScoreParams
ListenIP net.IP
ListenTCPPort uint16
......@@ -98,11 +99,6 @@ type Config struct {
ConnMngr func(conf *Config) (connmgr.ConnManager, error)
}
type ScoringConfig struct {
// Scoring Level
EnablePeerScoring bool
}
type ConnectionGater interface {
connmgr.ConnectionGater
......@@ -146,6 +142,10 @@ func (conf *Config) Disabled() bool {
return conf.DisableP2P
}
func (conf *Config) Scoring() *pubsub.PeerScoreParams {
return &conf.PeerScoring
}
const maxMeshParam = 1000
func (conf *Config) Check() error {
......
......@@ -51,6 +51,7 @@ var MessageDomainInvalidSnappy = [4]byte{0, 0, 0, 0}
var MessageDomainValidSnappy = [4]byte{1, 0, 0, 0}
type GossipSetupConfigurables interface {
Scoring() *pubsub.PeerScoreParams
ConfigureGossip(params *pubsub.GossipSubParams) []pubsub.Option
}
......@@ -152,11 +153,6 @@ func NewGossipSub(p2pCtx context.Context, h host.Host, g ConnectionGater, cfg *r
return nil, err
}
params := BuildGlobalGossipParams(cfg)
// TODO: make this configurable behind a cli flag - disabled or default
peerScoreParams, err := GetPeerScoreParams("default", cfg)
if err != nil {
return nil, err
}
peerScoreThresholds := NewPeerScoreThresholds()
scorer := NewScorer(g, h.Peerstore(), m, log)
gossipOpts := []pubsub.Option{
......@@ -173,7 +169,7 @@ func NewGossipSub(p2pCtx context.Context, h host.Host, g ConnectionGater, cfg *r
pubsub.WithBlacklist(denyList),
pubsub.WithGossipSubParams(params),
pubsub.WithEventTracer(&gossipTracer{m: m}),
pubsub.WithPeerScore(&peerScoreParams, &peerScoreThresholds),
pubsub.WithPeerScore(gossipConf.Scoring(), &peerScoreThresholds),
pubsub.WithPeerScoreInspect(scorer.SnapshotHook(), peerScoreInspectFrequency),
}
gossipOpts = append(gossipOpts, gossipConf.ConfigureGossip(&params)...)
......
......@@ -76,7 +76,7 @@ func (n *NodeP2P) init(resourcesCtx context.Context, rollupCfg *rollup.Config, l
// notify of any new connections/streams/etc.
n.host.Network().Notify(NewNetworkNotifier(log, metrics))
// note: the IDDelta functionality was removed from libP2P, and no longer needs to be explicitly disabled.
n.gs, err = NewGossipSub(resourcesCtx, n.host, rollupCfg, setup, metrics)
n.gs, err = NewGossipSub(resourcesCtx, n.host, n.gater, rollupCfg, setup, metrics, log)
if err != nil {
return fmt.Errorf("failed to start gossipsub router: %w", err)
}
......
......@@ -5,7 +5,6 @@ import (
"math"
"time"
"github.com/ethereum-optimism/optimism/op-node/rollup"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/peer"
)
......@@ -19,12 +18,12 @@ func ScoreDecay(duration time.Duration, slot time.Duration) float64 {
return math.Pow(DecayToZero, 1/float64(numOfTimes))
}
// DefaultPeerScoreParams is a default instantiation of [pubsub.PeerScoreParams].
// LightPeerScoreParams is an instantiation of [pubsub.PeerScoreParams] with light penalties.
// See [PeerScoreParams] for detailed documentation.
//
// [PeerScoreParams]: https://pkg.go.dev/github.com/libp2p/go-libp2p-pubsub@v0.8.1#PeerScoreParams
var DefaultPeerScoreParams = func(cfg *rollup.Config) pubsub.PeerScoreParams {
slot := time.Duration(cfg.BlockTime) * time.Second
var LightPeerScoreParams = func(blockTime uint64) pubsub.PeerScoreParams {
slot := time.Duration(blockTime) * time.Second
if slot == 0 {
slot = 2 * time.Second
}
......@@ -56,8 +55,8 @@ var DefaultPeerScoreParams = func(cfg *rollup.Config) pubsub.PeerScoreParams {
// See [PeerScoreParams] for detailed documentation.
//
// [PeerScoreParams]: https://pkg.go.dev/github.com/libp2p/go-libp2p-pubsub@v0.8.1#PeerScoreParams
var DisabledPeerScoreParams = func(cfg *rollup.Config) pubsub.PeerScoreParams {
slot := time.Duration(cfg.BlockTime) * time.Second
var DisabledPeerScoreParams = func(blockTime uint64) pubsub.PeerScoreParams {
slot := time.Duration(blockTime) * time.Second
// We initialize an "epoch" as 6 blocks suggesting 6 blocks,
// each taking ~ 2 seconds, is 12 seconds
epoch := 6 * slot
......@@ -84,9 +83,9 @@ var DisabledPeerScoreParams = func(cfg *rollup.Config) pubsub.PeerScoreParams {
}
// PeerScoreParamsByName is a map of name to function that returns a [pubsub.PeerScoreParams] based on the provided [rollup.Config].
var PeerScoreParamsByName = map[string](func(cfg *rollup.Config) pubsub.PeerScoreParams){
"default": DefaultPeerScoreParams,
"disabled": DisabledPeerScoreParams,
var PeerScoreParamsByName = map[string](func(blockTime uint64) pubsub.PeerScoreParams){
"light": LightPeerScoreParams,
"none": DisabledPeerScoreParams,
}
// AvailablePeerScoreParams returns a list of available peer score params.
......@@ -101,13 +100,13 @@ func AvailablePeerScoreParams() []string {
}
// GetPeerScoreParams returns the [pubsub.PeerScoreParams] for the given name.
func GetPeerScoreParams(name string, cfg *rollup.Config) (pubsub.PeerScoreParams, error) {
func GetPeerScoreParams(name string, blockTime uint64) (pubsub.PeerScoreParams, error) {
params, ok := PeerScoreParamsByName[name]
if !ok {
return pubsub.PeerScoreParams{}, fmt.Errorf("invalid params %s", name)
}
return params(cfg), nil
return params(blockTime), nil
}
// NewPeerScoreThresholds returns a default [pubsub.PeerScoreThresholds].
......
package p2p
import (
"sort"
"testing"
"github.com/stretchr/testify/suite"
)
// PeerParamsTestSuite tests peer parameterization.
type PeerParamsTestSuite struct {
suite.Suite
}
// SetupTest sets up the test suite.
func (testSuite *PeerParamsTestSuite) SetupTest() {
// TODO:
}
// TestPeerParams runs the PeerParamsTestSuite.
func TestPeerParams(t *testing.T) {
suite.Run(t, new(PeerParamsTestSuite))
}
// TestAvailablePeerScoreParams validates the available peer score parameters.
func (testSuite *PeerParamsTestSuite) TestAvailablePeerScoreParams() {
available := AvailablePeerScoreParams()
sort.Strings(available)
expected := []string{"light", "none"}
testSuite.Equal(expected, available)
}
......@@ -64,6 +64,10 @@ func (p *Prepared) ConfigureGossip(params *pubsub.GossipSubParams) []pubsub.Opti
return nil
}
func (p *Prepared) Scoring() *pubsub.PeerScoreParams {
return nil
}
func (p *Prepared) Disabled() bool {
return false
}
......@@ -46,7 +46,7 @@ func NewConfig(ctx *cli.Context, log log.Logger) (*node.Config, error) {
return nil, fmt.Errorf("failed to load p2p signer: %w", err)
}
p2pConfig, err := p2pcli.NewConfig(ctx)
p2pConfig, err := p2pcli.NewConfig(ctx, rollupConfig.BlockTime)
if err != nil {
return nil, fmt.Errorf("failed to load p2p config: %w", err)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment