Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Made MaxBytesPerSecond configurable for standalone nodes #879

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
3 changes: 1 addition & 2 deletions cmd/mesh-bootstrap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ type Config struct {
// we reach PeerCountLow. Defaults to 110.
PeerCountHigh int `envvar:"PEER_COUNT_HIGH" default:"110"`
// MaxBytesPerSecond is the maximum number of bytes per second that a peer is
// allowed to send before failing the bandwidth check. Defaults to 1 MiB, which
// is roughly 100x expected usage based on real world measurements.
// allowed to send before failing the bandwidth check. Defaults to 1 MiB.
MaxBytesPerSecond float64 `envvar:"MAX_BYTES_PER_SECOND" default:"1048576"`
}

Expand Down
4 changes: 4 additions & 0 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ type Config struct {
// settable in browsers and cannot be set via environment variable. If
// provided, EthereumRPCURL will be ignored.
EthereumRPCClient ethclient.RPCClient `envvar:"-"`
// MaxBytesPerSecond is the maximum number of bytes per second that a peer is
// allowed to send before failing the bandwidth check. Defaults to 1 MiB.
MaxBytesPerSecond float64 `envvar:"MAX_BYTES_PER_SECOND" default:"1048576"`
}

type App struct {
Expand Down Expand Up @@ -622,6 +625,7 @@ func (app *App) Start() error {
BootstrapList: bootstrapList,
DataDir: filepath.Join(app.config.DataDir, "p2p"),
CustomMessageValidator: app.orderFilter.ValidatePubSubMessage,
MaxBytesPerSecond: app.config.MaxBytesPerSecond,
}
app.node, err = p2p.New(innerCtx, nodeConfig)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion p2p/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ type Config struct {
// according to this custom validator, which will be run in addition to the
// default validators.
CustomMessageValidator pubsub.Validator
// MaxBytesPerSecond is the maximum number of bytes per second that a peer is
// allowed to send before failing the bandwidth check. Defaults to 1 MiB.
MaxBytesPerSecond float64
}

func getPeerstoreDir(datadir string) string {
Expand Down Expand Up @@ -193,6 +196,9 @@ func New(ctx context.Context, config Config) (*Node, error) {
if config.PerPeerPubSubMessageBurst == 0 {
config.PerPeerPubSubMessageBurst = defaultPerPeerPubSubMessageBurst
}
if config.MaxBytesPerSecond == 0 {
config.MaxBytesPerSecond = defaultMaxBytesPerSecond
jalextowle marked this conversation as resolved.
Show resolved Hide resolved
}

// We need to declare the newDHT function ahead of time so we can use it in
// the libp2p.Routing option.
Expand Down Expand Up @@ -268,7 +274,7 @@ func New(ctx context.Context, config Config) (*Node, error) {
Host: basicHost,
Filters: filters,
BandwidthCounter: bandwidthCounter,
MaxBytesPerSecond: defaultMaxBytesPerSecond,
MaxBytesPerSecond: config.MaxBytesPerSecond,
LogBandwidthUsageStats: true,
})

Expand Down
4 changes: 4 additions & 0 deletions packages/mesh-browser-lite/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ export interface Config {
// Offers the ability to use your own web3 provider for all Ethereum RPC
// requests instead of the default.
web3Provider?: SupportedProvider;
// The maximum number of bytes per second that a peer is allowed to send before
// failing the bandwidth check. Defaults to 1 MiB.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you double check to make sure there aren't any other outdated comments?

Suggested change
// failing the bandwidth check. Defaults to 1 MiB.
// failing the bandwidth check. Defaults to 5 MiB.

maxBytesPerSecond?: number;
}

export interface ContractAddresses {
Expand Down Expand Up @@ -252,6 +255,7 @@ export interface WrapperConfig {
maxOrdersInStorage?: number;
customOrderFilter?: string; // json-encoded string instead of Object
web3Provider?: ZeroExProvider; // Standardized ZeroExProvider instead the more permissive SupportedProvider interface
maxBytesPerSecond?: number;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/mesh-browser/go/browserutil/browserutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func ConvertConfig(jsConfig js.Value) (core.Config, error) {
EnableEthereumRPCRateLimiting: true,
MaxOrdersInStorage: 100000,
CustomOrderFilter: orderfilter.DefaultCustomOrderSchema,
MaxBytesPerSecond: 1048576,
}

// Required config options
Expand Down Expand Up @@ -86,6 +87,9 @@ func ConvertConfig(jsConfig js.Value) (core.Config, error) {
if web3Provider := jsConfig.Get("web3Provider"); !jsutil.IsNullOrUndefined(web3Provider) {
config.EthereumRPCClient = providerwrapper.NewRPCClient(web3Provider)
}
if maxBytesPerSecond := jsConfig.Get("maxBytesPerSecond"); !jsutil.IsNullOrUndefined(maxBytesPerSecond) {
config.MaxBytesPerSecond = maxBytesPerSecond.Float()
}

return config, nil
}