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(core)!: separating core.ip into core.grpc.ip and core.rpc.ip #3153

Closed
wants to merge 3 commits into from
Closed
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
21 changes: 15 additions & 6 deletions nodebuilder/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ var MetricsEnabled bool

// Config combines all configuration fields for managing the relationship with a Core node.
type Config struct {
IP string
RPCIP string
GRPCIP string
RPCPort string
GRPCPort string
}
Expand All @@ -20,7 +21,8 @@ type Config struct {
// node's connection to a Celestia-Core endpoint.
func DefaultConfig() Config {
return Config{
IP: "",
RPCIP: "",
GRPCIP: "",
RPCPort: "26657",
GRPCPort: "9090",
}
Expand All @@ -32,11 +34,18 @@ func (cfg *Config) Validate() error {
return nil
}

ip, err := utils.ValidateAddr(cfg.IP)
rpcIP, err := utils.ValidateAddr(cfg.RPCIP)
if err != nil {
return err
return fmt.Errorf("nodebuilder/core: invalid rpc ip: %s", err.Error())
}
cfg.IP = ip
cfg.RPCIP = rpcIP

grpcIP, err := utils.ValidateAddr(cfg.GRPCIP)
if err != nil {
return fmt.Errorf("nodebuilder/core: invalid grpc ip: %s", err.Error())
}
cfg.GRPCIP = grpcIP

_, err = strconv.Atoi(cfg.RPCPort)
if err != nil {
return fmt.Errorf("nodebuilder/core: invalid rpc port: %s", err.Error())
Expand All @@ -51,5 +60,5 @@ func (cfg *Config) Validate() error {
// IsEndpointConfigured returns whether a core endpoint has been set
// on the config (true if set).
func (cfg *Config) IsEndpointConfigured() bool {
return cfg.IP != ""
return cfg.RPCIP != "" && cfg.GRPCIP != ""
}
2 changes: 1 addition & 1 deletion nodebuilder/core/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import (
)

func remote(cfg Config) (core.Client, error) {
return core.NewRemote(cfg.IP, cfg.RPCPort)
return core.NewRemote(cfg.RPCIP, cfg.RPCPort)
}
86 changes: 63 additions & 23 deletions nodebuilder/core/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,93 @@
)

var (
coreFlag = "core.ip"
coreRPCFlag = "core.rpc.port"
coreGRPCFlag = "core.grpc.port"
ipFlag = "core.ip"
rpcIPFlag = "core.rpc.ip"
grpcIPFlag = "core.grpc.ip"
rpcPortFlag = "core.rpc.port"
grpcPortFlag = "core.grpc.port"
)

// Flags gives a set of hardcoded Core flags.
func Flags() *flag.FlagSet {
flags := &flag.FlagSet{}

flags.String(
coreFlag,
ipFlag,
"",
"Indicates node to connect to the given core node. "+
"Indicates node to connect to the given core node's RPC and gRPC. "+
"NOTE: If this flag is set, the core.rpc.ip and core.grpc.ip flags cannot be set. "+
"Example: <ip>, 127.0.0.1. <dns>, subdomain.domain.tld "+

Check failure on line 27 in nodebuilder/core/flags.go

View workflow job for this annotation

GitHub Actions / go-ci / Lint

string `Example: <ip>, 127.0.0.1. <dns>, subdomain.domain.tld ` has 3 occurrences, make it a constant (goconst)
"Assumes RPC port 26657 and gRPC port 9090 as default unless otherwise specified.",

Check failure on line 28 in nodebuilder/core/flags.go

View workflow job for this annotation

GitHub Actions / go-ci / Lint

string `Assumes RPC port 26657 and gRPC port 9090 as default unless otherwise specified.` has 3 occurrences, make it a constant (goconst)
)
flags.String(
coreRPCFlag,
rpcIPFlag,
"",
"Indicates node to connect to the given core node's RPC. "+
"NOTE: If this flag is set, the core.ip flag cannot be set. "+
"Example: <ip>, 127.0.0.1. <dns>, subdomain.domain.tld "+
"Assumes RPC port 26657 and gRPC port 9090 as default unless otherwise specified.",
)
flags.String(
grpcIPFlag,
"",
"Indicates node to connect to the given core node's gRPC. "+
"NOTE: If this flag is set, the core.ip flag cannot be set. "+
"Example: <ip>, 127.0.0.1. <dns>, subdomain.domain.tld "+
"Assumes RPC port 26657 and gRPC port 9090 as default unless otherwise specified.",
)
flags.String(
rpcPortFlag,
"26657",
"Set a custom RPC port for the core node connection. The --core.ip flag must also be provided.",
"Set a custom RPC port for the core node connection. The --core.rpc.ip or --core.ip flag must also be provided.",
)
flags.String(
coreGRPCFlag,
grpcPortFlag,
"9090",
"Set a custom gRPC port for the core node connection. The --core.ip flag must also be provided.",
"Set a custom gRPC port for the core node connection. The --core.grpc.ip or --core.ip flag must also be provided.",
)
return flags
}

// ParseFlags parses Core flags from the given cmd and saves them to the passed config.
func ParseFlags(
cmd *cobra.Command,
cfg *Config,
) error {
coreIP := cmd.Flag(coreFlag).Value.String()
if coreIP == "" {
if cmd.Flag(coreGRPCFlag).Changed || cmd.Flag(coreRPCFlag).Changed {
return fmt.Errorf("cannot specify RPC/gRPC ports without specifying an IP address for --core.ip")
func ParseFlags(cmd *cobra.Command, cfg *Config) error {
coreIP := cmd.Flag(ipFlag).Value.String()
coreRPCIP, coreGRPCIP := cmd.Flag(rpcIPFlag).Value.String(), cmd.Flag(grpcIPFlag).Value.String()

// Check if core.ip is specified along with core.rpc.ip or core.grpc.ip
if coreIP != "" && (cmd.Flag(rpcIPFlag).Changed || cmd.Flag(grpcIPFlag).Changed) {
return fmt.Errorf("cannot specify core.ip and core.rpc.ip or core.grpc.ip flags together")
}

// Validate IP addresses and port settings
if coreIP == "" { // No core.ip specified
if coreRPCIP == "" {
if cmd.Flag(rpcPortFlag).Changed {
return fmt.Errorf("cannot specify RPC ports without specifying an IP address for --core.rpc.ip")
}
if cmd.Flag(grpcIPFlag).Changed {
return fmt.Errorf("setting gRPC IP requires also specifying an RPC IP. If they are identical use --core.ip instead")
}
}
if coreGRPCIP == "" {
if cmd.Flag(grpcPortFlag).Changed {
return fmt.Errorf("cannot specify gRPC ports without specifying an IP address for --core.grpc.ip")
}
if cmd.Flag(rpcIPFlag).Changed {
return fmt.Errorf("setting RPC IP requires also specifying a gRPC IP. If they are identical use --core.ip instead")
}
}
return nil
}

rpc := cmd.Flag(coreRPCFlag).Value.String()
grpc := cmd.Flag(coreGRPCFlag).Value.String()
// Assign IP addresses
if coreIP != "" {
cfg.RPCIP, cfg.GRPCIP = coreIP, coreIP
} else {
cfg.RPCIP, cfg.GRPCIP = coreRPCIP, coreGRPCIP
}

// Assign ports
cfg.RPCPort, cfg.GRPCPort = cmd.Flag(rpcPortFlag).Value.String(), cmd.Flag(grpcPortFlag).Value.String()

cfg.IP = coreIP
cfg.RPCPort = rpc
cfg.GRPCPort = grpc
return cfg.Validate()
}
2 changes: 1 addition & 1 deletion nodebuilder/state/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func coreAccessor(
sync *sync.Syncer[*header.ExtendedHeader],
fraudServ libfraud.Service[*header.ExtendedHeader],
) (*state.CoreAccessor, Module, *modfraud.ServiceBreaker[*state.CoreAccessor, *header.ExtendedHeader]) {
ca := state.NewCoreAccessor(signer, sync, corecfg.IP, corecfg.RPCPort, corecfg.GRPCPort)
ca := state.NewCoreAccessor(signer, sync, corecfg.RPCIP, corecfg.GRPCIP, corecfg.RPCPort, corecfg.GRPCPort)

return ca, ca, &modfraud.ServiceBreaker[*state.CoreAccessor, *header.ExtendedHeader]{
Service: ca,
Expand Down
3 changes: 2 additions & 1 deletion nodebuilder/tests/swamp/swamp.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ func (s *Swamp) DefaultTestConfig(tp node.Type) *nodebuilder.Config {
ip, port, err := net.SplitHostPort(s.cfg.AppConfig.GRPC.Address)
require.NoError(s.t, err)

cfg.Core.IP = ip
cfg.Core.RPCIP = ip
cfg.Core.GRPCIP = ip
cfg.Core.GRPCPort = port
return cfg
}
Expand Down
13 changes: 8 additions & 5 deletions state/core_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ type CoreAccessor struct {
prt *merkle.ProofRuntime

coreConn *grpc.ClientConn
coreIP string
rpcIP string
grpcIP string
rpcPort string
grpcPort string

Expand All @@ -79,7 +80,8 @@ type CoreAccessor struct {
func NewCoreAccessor(
signer *apptypes.KeyringSigner,
getter libhead.Head[*header.ExtendedHeader],
coreIP,
rpcIP,
grpcIP,
rpcPort string,
grpcPort string,
) *CoreAccessor {
Expand All @@ -90,7 +92,8 @@ func NewCoreAccessor(
return &CoreAccessor{
signer: signer,
getter: getter,
coreIP: coreIP,
rpcIP: rpcIP,
grpcIP: grpcIP,
rpcPort: rpcPort,
grpcPort: grpcPort,
prt: prt,
Expand All @@ -104,7 +107,7 @@ func (ca *CoreAccessor) Start(ctx context.Context) error {
ca.ctx, ca.cancel = context.WithCancel(context.Background())

// dial given celestia-core endpoint
endpoint := fmt.Sprintf("%s:%s", ca.coreIP, ca.grpcPort)
endpoint := fmt.Sprintf("%s:%s", ca.grpcIP, ca.grpcPort)
client, err := grpc.DialContext(
ctx,
endpoint,
Expand All @@ -121,7 +124,7 @@ func (ca *CoreAccessor) Start(ctx context.Context) error {
stakingCli := stakingtypes.NewQueryClient(ca.coreConn)
ca.stakingCli = stakingCli
// create ABCI query client
cli, err := http.New(fmt.Sprintf("http://%s:%s", ca.coreIP, ca.rpcPort), "/websocket")
cli, err := http.New(fmt.Sprintf("http://%s:%s", ca.rpcIP, ca.rpcPort), "/websocket")
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion state/core_access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestSubmitPayForBlob(t *testing.T) {
defer cancel()

signer := blobtypes.NewKeyringSigner(cctx.Keyring, accounts[0], cctx.ChainID)
ca := NewCoreAccessor(signer, nil, "127.0.0.1", extractPort(rpcAddr), extractPort(grpcAddr))
coreIP := "127.0.0.1"
ca := NewCoreAccessor(signer, nil, coreIP, coreIP, extractPort(rpcAddr), extractPort(grpcAddr))
// start the accessor
err := ca.Start(ctx)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion state/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *IntegrationTestSuite) SetupSuite() {
s.accounts = cfg.Accounts

signer := blobtypes.NewKeyringSigner(s.cctx.Keyring, s.accounts[0], s.cctx.ChainID)
accessor := NewCoreAccessor(signer, localHeader{s.cctx.Client}, "", "", "")
accessor := NewCoreAccessor(signer, localHeader{s.cctx.Client}, "", "", "", "")
setClients(accessor, s.cctx.GRPCClient, s.cctx.Client)
s.accessor = accessor

Expand Down
Loading