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

Make max read size configurable #273

Merged
merged 4 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 9 additions & 4 deletions cmd/horcrux/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const (
flagGRPCTimeout = "grpc-timeout"
flagOverwrite = "overwrite"
flagBare = "bare"
flagGRPCAddress = "flagGRPCAddress"
flagGRPCAddress = "gprc-address"
flagMaxReadSize = "max-read-size"
)

func configCmd() *cobra.Command {
Expand Down Expand Up @@ -70,6 +71,7 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
}
debugAddr, _ := cmdFlags.GetString(flagDebugAddr)
grpcAddr, _ := cmdFlags.GetString(flagGRPCAddress)
maxReadSize, _ := cmdFlags.GetInt(flagMaxReadSize)
if signMode == string(signer.SignModeThreshold) {
// Threshold Mode Config
cosignersFlag, _ := cmdFlags.GetStringSlice(flagCosigner)
Expand All @@ -90,9 +92,10 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
GRPCTimeout: grpcTimeout,
RaftTimeout: raftTimeout,
},
ChainNodes: cn,
DebugAddr: debugAddr,
GRPCAddr: grpcAddr,
ChainNodes: cn,
DebugAddr: debugAddr,
GRPCAddr: grpcAddr,
MaxReadSize: maxReadSize,
}

if !bare {
Expand All @@ -107,6 +110,7 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
PrivValKeyDir: keyDir,
ChainNodes: cn,
DebugAddr: debugAddr,
MaxReadSize: maxReadSize,
}
if !bare {
if err = cfg.ValidateSingleSignerConfig(); err != nil {
Expand Down Expand Up @@ -162,5 +166,6 @@ for threshold signer mode, --cosigner flags and --threshold flag are required.
"allows initialization without providing any flags. If flags are provided, will not perform final validation",
)
f.StringP(flagGRPCAddress, "g", "", "GRPC address if listener should be enabled")
f.Int(flagMaxReadSize, 1024*1024, "max read size for remote signer connection")
return cmd
}
2 changes: 2 additions & 0 deletions cmd/horcrux/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ chainNodes:
- privValAddr: tcp://10.168.0.2:1234
debugAddr: ""
grpcAddr: ""
maxReadSize: 1048576
`,
},
{
Expand All @@ -64,6 +65,7 @@ chainNodes:
- privValAddr: tcp://10.168.0.2:1234
debugAddr: ""
grpcAddr: ""
maxReadSize: 1048576
`,
},
{
Expand Down
1 change: 1 addition & 0 deletions cmd/horcrux/cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func migrateCmd() *cobra.Command {
}

config.Config.SignMode = signMode
config.Config.MaxReadSize = 1024 * 1024

if err := config.WriteConfigFile(); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion cmd/horcrux/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func startCmd() *cobra.Command {

go EnableDebugAndMetrics(cmd.Context(), out)

services, err = signer.StartRemoteSigners(services, logger, val, config.Config.Nodes())
services, err = signer.StartRemoteSigners(services, logger, val, config.Config.Nodes(), config.Config.MaxReadSize)
if err != nil {
return fmt.Errorf("failed to start remote signer(s): %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/horcrux/cmd/testdata/config-migrated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ chainNodes:
- privValAddr: tcp://127.0.0.1:3456
debugAddr: ""
grpcAddr: ""
maxReadSize: 1048576
1 change: 1 addition & 0 deletions signer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Config struct {
ChainNodes ChainNodes `yaml:"chainNodes"`
DebugAddr string `yaml:"debugAddr"`
GRPCAddr string `yaml:"grpcAddr"`
MaxReadSize int `yaml:"maxReadSize"`
}

func (c *Config) Nodes() (out []string) {
Expand Down
2 changes: 2 additions & 0 deletions signer/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ func TestRuntimeConfigWriteConfigFile(t *testing.T) {
PrivValAddr: "tcp://127.0.0.1:3456",
},
},
MaxReadSize: 1024 * 1024,
},
}

Expand All @@ -404,6 +405,7 @@ chainNodes:
- privValAddr: tcp://127.0.0.1:3456
debugAddr: ""
grpcAddr: ""
maxReadSize: 1048576
`, string(configYamlBz))
}

Expand Down
8 changes: 5 additions & 3 deletions signer/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
)

// ReadMsg reads a message from an io.Reader
func ReadMsg(reader io.Reader) (msg cometprotoprivval.Message, err error) {
const maxRemoteSignerMsgSize = 1024 * 10
protoReader := protoio.NewDelimitedReader(reader, maxRemoteSignerMsgSize)
func ReadMsg(reader io.Reader, maxReadSize int) (msg cometprotoprivval.Message, err error) {
if maxReadSize <= 0 {
maxReadSize = 1024 * 1024 // 1MB
}
protoReader := protoio.NewDelimitedReader(reader, maxReadSize)
_, err = protoReader.ReadMsg(&msg)
return msg, err
}
Expand Down
17 changes: 11 additions & 6 deletions signer/remote_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type ReconnRemoteSigner struct {
privVal PrivValidator

dialer net.Dialer

maxReadSize int
}

// NewReconnRemoteSigner return a ReconnRemoteSigner that will dial using the given
Expand All @@ -49,12 +51,14 @@ func NewReconnRemoteSigner(
logger cometlog.Logger,
privVal PrivValidator,
dialer net.Dialer,
maxReadSize int,
) *ReconnRemoteSigner {
rs := &ReconnRemoteSigner{
address: address,
privVal: privVal,
dialer: dialer,
privKey: cometcryptoed25519.GenPrivKey(),
address: address,
privVal: privVal,
dialer: dialer,
privKey: cometcryptoed25519.GenPrivKey(),
maxReadSize: maxReadSize,
}

rs.BaseService = *cometservice.NewBaseService(logger, "RemoteSigner", rs)
Expand Down Expand Up @@ -136,7 +140,7 @@ func (rs *ReconnRemoteSigner) loop(ctx context.Context) {
return
}

req, err := ReadMsg(conn)
req, err := ReadMsg(conn, rs.maxReadSize)
if err != nil {
rs.Logger.Error(
"Failed to read message from connection",
Expand Down Expand Up @@ -288,6 +292,7 @@ func StartRemoteSigners(
logger cometlog.Logger,
privVal PrivValidator,
nodes []string,
maxReadSize int,
) ([]cometservice.Service, error) {
var err error
go StartMetrics()
Expand All @@ -296,7 +301,7 @@ func StartRemoteSigners(
// A long timeout such as 30 seconds would cause the sentry to fail in loops
// Use a short timeout and dial often to connect within 3 second window
dialer := net.Dialer{Timeout: 2 * time.Second}
s := NewReconnRemoteSigner(node, logger, privVal, dialer)
s := NewReconnRemoteSigner(node, logger, privVal, dialer, maxReadSize)

err = s.Start()
if err != nil {
Expand Down
Loading