From f83c4bf75d78916074130757598e35084d5d5ebe Mon Sep 17 00:00:00 2001 From: ThunderBolt-OS Date: Wed, 11 Dec 2024 00:18:11 +0530 Subject: [PATCH 1/3] refactor: move network validation to GetFullNodeAPIV1Curio --- build/params.go | 19 +++++++++++++++++++ cmd/curio/run.go | 11 ++++++++++- deps/apiinfo.go | 43 +++++++++++++++++++++++++++++++------------ 3 files changed, 60 insertions(+), 13 deletions(-) diff --git a/build/params.go b/build/params.go index 22badfaca..d38e814a2 100644 --- a/build/params.go +++ b/build/params.go @@ -10,3 +10,22 @@ func SetAddressNetwork(n address.Network) { } const TicketRandomnessLookback = abi.ChainEpoch(1) + +const ( + NetworkMainnet = "mainnet" + NetworkCalibnet = "calibnet" + NetworkTestnet = "testnet" +) + +func CurrentNetwork() string { + switch BuildType { + case BuildMainnet: + return NetworkMainnet + case BuildCalibnet: + return NetworkCalibnet + case Build2k: + return NetworkTestnet + default: + return "unknown" + } +} diff --git a/cmd/curio/run.go b/cmd/curio/run.go index 9a359cddf..c2443450f 100644 --- a/cmd/curio/run.go +++ b/cmd/curio/run.go @@ -20,6 +20,9 @@ import ( "github.com/filecoin-project/lotus/lib/ulimit" "github.com/filecoin-project/lotus/metrics" + "github.com/filecoin-project/lotus/api/v0api" + "github.com/filecoin-project/lotus/build" + "github.com/filecoin-project/lotus/cli/lcli" ) type stackTracer interface { @@ -135,7 +138,13 @@ var runCmd = &cli.Command{ return xerrors.Errorf("starting market RPCs: %w", err) } - err = rpc.ListenAndServe(ctx, dependencies, shutdownChan) // Monitor for shutdown. + api, closer, err := lcli.GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer closer() + + err = rpc.ListenAndServe(ctx, dependencies, shutdownChan) if err != nil { return err } diff --git a/deps/apiinfo.go b/deps/apiinfo.go index e40b420f4..78d1fb825 100644 --- a/deps/apiinfo.go +++ b/deps/apiinfo.go @@ -22,6 +22,7 @@ import ( lapi "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/chain/types" cliutil "github.com/filecoin-project/lotus/cli/util" + "github.com/filecoin-project/lotus/build" ) var clog = logging.Logger("curio/chain") @@ -30,20 +31,20 @@ func GetFullNodeAPIV1Curio(ctx *cli.Context, ainfoCfg []string) (api.Chain, json if tn, ok := ctx.App.Metadata["testnode-full"]; ok { return tn.(api.Chain), func() {}, nil } + + if len(ainfoCfg) == 0 { + return nil, nil, xerrors.Errorf("could not get API info: none configured. \nConsider getting base.toml with './curio config get base >/tmp/base.toml' \nthen adding \n[APIs] \n ChainApiInfo = [\" result_from lotus auth api-info --perm=admin \"]\n and updating it with './curio config set /tmp/base.toml'") + } + var httpHeads []httpHead version := "v1" - { - if len(ainfoCfg) == 0 { - return nil, nil, xerrors.Errorf("could not get API info: none configured. \nConsider getting base.toml with './curio config get base >/tmp/base.toml' \nthen adding \n[APIs] \n ChainApiInfo = [\" result_from lotus auth api-info --perm=admin \"]\n and updating it with './curio config set /tmp/base.toml'") - } - for _, i := range ainfoCfg { - ainfo := cliutil.ParseApiInfo(i) - addr, err := ainfo.DialArgs(version) - if err != nil { - return nil, nil, xerrors.Errorf("could not get DialArgs: %w", err) - } - httpHeads = append(httpHeads, httpHead{addr: addr, header: ainfo.AuthHeader()}) + for _, i := range ainfoCfg { + ainfo := cliutil.ParseApiInfo(i) + addr, err := ainfo.DialArgs(version) + if err != nil { + return nil, nil, xerrors.Errorf("could not get DialArgs: %w", err) } + httpHeads = append(httpHeads, httpHead{addr: addr, header: ainfo.AuthHeader()}) } if cliutil.IsVeryVerbose { @@ -53,18 +54,36 @@ func GetFullNodeAPIV1Curio(ctx *cli.Context, ainfoCfg []string) (api.Chain, json var fullNodes []api.Chain var closers []jsonrpc.ClientCloser + // Check network compatibility for each node for _, head := range httpHeads { v1api, closer, err := newChainNodeRPCV1(ctx.Context, head.addr, head.header) if err != nil { clog.Warnf("Not able to establish connection to node with addr: %s, Reason: %s", head.addr, err.Error()) continue } + + // Validate network match + networkName, err := v1api.StateNetworkName(ctx.Context) + if err != nil { + clog.Warnf("Failed to get network name from node %s: %s", head.addr, err.Error()) + closer() + continue + } + + // Compare with binary's network using BuildTypeString() + if string(networkName) != build.BuildTypeString() { + clog.Warnf("Network mismatch for node %s: binary built for %s but node is on %s", + head.addr, build.BuildTypeString(), networkName) + closer() + continue + } + fullNodes = append(fullNodes, v1api) closers = append(closers, closer) } if len(fullNodes) == 0 { - return nil, nil, xerrors.Errorf("failed to establish connection with all nodes") + return nil, nil, xerrors.Errorf("no compatible nodes found - all nodes had network mismatches or connection errors") } finalCloser := func() { From 1008da264450332f5176aa9f60734b0bfa03cbdb Mon Sep 17 00:00:00 2001 From: ThunderBolt-OS Date: Sun, 15 Dec 2024 04:37:28 +0530 Subject: [PATCH 2/3] chore: address review feedback --- build/params.go | 21 +-------------------- cmd/curio/run.go | 4 ---- deps/apiinfo.go | 2 +- 3 files changed, 2 insertions(+), 25 deletions(-) diff --git a/build/params.go b/build/params.go index d38e814a2..df9ebd059 100644 --- a/build/params.go +++ b/build/params.go @@ -9,23 +9,4 @@ func SetAddressNetwork(n address.Network) { address.CurrentNetwork = n } -const TicketRandomnessLookback = abi.ChainEpoch(1) - -const ( - NetworkMainnet = "mainnet" - NetworkCalibnet = "calibnet" - NetworkTestnet = "testnet" -) - -func CurrentNetwork() string { - switch BuildType { - case BuildMainnet: - return NetworkMainnet - case BuildCalibnet: - return NetworkCalibnet - case Build2k: - return NetworkTestnet - default: - return "unknown" - } -} +const TicketRandomnessLookback = abi.ChainEpoch(1) \ No newline at end of file diff --git a/cmd/curio/run.go b/cmd/curio/run.go index c2443450f..9d7c1a31c 100644 --- a/cmd/curio/run.go +++ b/cmd/curio/run.go @@ -20,9 +20,6 @@ import ( "github.com/filecoin-project/lotus/lib/ulimit" "github.com/filecoin-project/lotus/metrics" - "github.com/filecoin-project/lotus/api/v0api" - "github.com/filecoin-project/lotus/build" - "github.com/filecoin-project/lotus/cli/lcli" ) type stackTracer interface { @@ -138,7 +135,6 @@ var runCmd = &cli.Command{ return xerrors.Errorf("starting market RPCs: %w", err) } - api, closer, err := lcli.GetFullNodeAPI(cctx) if err != nil { return err } diff --git a/deps/apiinfo.go b/deps/apiinfo.go index 78d1fb825..52affb261 100644 --- a/deps/apiinfo.go +++ b/deps/apiinfo.go @@ -83,7 +83,7 @@ func GetFullNodeAPIV1Curio(ctx *cli.Context, ainfoCfg []string) (api.Chain, json } if len(fullNodes) == 0 { - return nil, nil, xerrors.Errorf("no compatible nodes found - all nodes had network mismatches or connection errors") + return nil, nil, xerrors.Errorf("failed to establish connection with all nodes") } finalCloser := func() { From e507b74f4a6fe2d74a0cbfeb90cbd4814d82e1a2 Mon Sep 17 00:00:00 2001 From: ThunderBolt-OS Date: Mon, 16 Dec 2024 18:43:37 +0530 Subject: [PATCH 3/3] chore: remove unused network validation code --- cmd/curio/run.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/cmd/curio/run.go b/cmd/curio/run.go index 9d7c1a31c..ef34b8ac3 100644 --- a/cmd/curio/run.go +++ b/cmd/curio/run.go @@ -135,11 +135,6 @@ var runCmd = &cli.Command{ return xerrors.Errorf("starting market RPCs: %w", err) } - if err != nil { - return err - } - defer closer() - err = rpc.ListenAndServe(ctx, dependencies, shutdownChan) if err != nil { return err