diff --git a/cmd/node/appchain.go b/cmd/node/appchain.go index f640527..e909820 100644 --- a/cmd/node/appchain.go +++ b/cmd/node/appchain.go @@ -9,7 +9,6 @@ import ( "os" "path/filepath" "strconv" - "strings" "time" cosmossdk_io_math "cosmossdk.io/math" @@ -23,28 +22,28 @@ import ( "github.com/rs/zerolog/log" ) -func getCosmosClient(config AppChainConfig) (*cosmosclient.Client, error) { - // create a cosmos client instance +func getAlloraClient(config AppChainConfig) (*cosmosclient.Client, error) { + // create a allora client instance ctx := context.Background() userHomeDir, _ := os.UserHomeDir() - cosmosClientHome := filepath.Join(userHomeDir, ".allorad") - if config.CosmosHomeDir != "" { - cosmosClientHome = config.CosmosHomeDir + alloraClientHome := filepath.Join(userHomeDir, ".allorad") + if config.AlloraHomeDir != "" { + alloraClientHome = config.AlloraHomeDir } // Check that the given home folder exist - if _, err := os.Stat(cosmosClientHome); errors.Is(err, os.ErrNotExist) { - log.Warn().Err(err).Msg("could not get home directory for cosmos client, creating...") - err = os.MkdirAll(cosmosClientHome, 0755) + if _, err := os.Stat(alloraClientHome); errors.Is(err, os.ErrNotExist) { + log.Warn().Err(err).Msg("could not get home directory for allora client, creating...") + err = os.MkdirAll(alloraClientHome, 0755) if err != nil { - log.Warn().Err(err).Str("directory", cosmosClientHome).Msg("Cannot create cosmos client home directory") + log.Warn().Err(err).Str("directory", alloraClientHome).Msg("Cannot create allora client home directory") config.SubmitTx = false return nil, err } - log.Info().Err(err).Str("directory", cosmosClientHome).Msg("cosmos client home directory created") + log.Info().Err(err).Str("directory", alloraClientHome).Msg("allora client home directory created") } - client, err := cosmosclient.New(ctx, cosmosclient.WithNodeAddress(config.NodeRPCAddress), cosmosclient.WithAddressPrefix(config.AddressPrefix), cosmosclient.WithHome(cosmosClientHome)) + client, err := cosmosclient.New(ctx, cosmosclient.WithNodeAddress(config.NodeRPCAddress), cosmosclient.WithAddressPrefix(config.AddressPrefix), cosmosclient.WithHome(alloraClientHome)) if err != nil { log.Warn().Err(err).Msg("unable to create an allora blockchain client") config.SubmitTx = false @@ -56,7 +55,7 @@ func getCosmosClient(config AppChainConfig) (*cosmosclient.Client, error) { // create a new appchain client that we can use func NewAppChain(config AppChainConfig, log zerolog.Logger) (*AppChain, error) { config.SubmitTx = true - client, err := getCosmosClient(config) + client, err := getAlloraClient(config) if err != nil { config.SubmitTx = false return nil, err @@ -85,7 +84,7 @@ func NewAppChain(config AppChainConfig, log zerolog.Logger) (*AppChain, error) { } } } else { - log.Warn().Msg("no cosmos account was loaded") + log.Warn().Msg("no allora account was loaded") return nil, nil } @@ -111,9 +110,8 @@ func NewAppChain(config AppChainConfig, log zerolog.Logger) (*AppChain, error) { QueryClient: queryClient, Config: config, } - if !queryIsNodeRegistered(*appchain) { - registerWithBlockchain(appchain) - } + + registerWithBlockchain(appchain) return appchain, nil } @@ -122,51 +120,58 @@ func NewAppChain(config AppChainConfig, log zerolog.Logger) (*AppChain, error) { func registerWithBlockchain(appchain *AppChain) { ctx := context.Background() - var msg sdktypes.Msg + isReputer := false if appchain.Config.NodeRole == blockless.HeadNode { - msg = &types.MsgRegisterReputer{ + isReputer = true + } + + // Check if address is already registered in a topic + res, err := appchain.QueryClient.GetRegisteredTopicsIds(ctx, &types.QueryRegisteredTopicsIdsRequest{ + Address: appchain.ReputerAddress, + IsReputer: isReputer, + }) + if err != nil { + appchain.Logger.Fatal().Err(err).Msg("could not check if the node is already registered") + } + var msg sdktypes.Msg + if len(res.TopicsIds) > 0 { + for _, topicId := range res.TopicsIds { + if topicId == appchain.Config.TopicId { + appchain.Logger.Info().Msg("node is already registered") + return + } + } + + // If registered in other topic, don't need an initial stake + msg = &types.MsgAddNewRegistration{ Creator: appchain.ReputerAddress, LibP2PKey: appchain.Config.LibP2PKey, MultiAddress: appchain.Config.MultiAddress, - InitialStake: cosmossdk_io_math.NewUint(1), TopicId: appchain.Config.TopicId, + Owner: appchain.ReputerAddress, + IsReputer: isReputer, } } else { - msg = &types.MsgRegisterWorker{ + // If not registered in any topic, need an initial stake + msg = &types.MsgRegister{ Creator: appchain.ReputerAddress, - Owner: appchain.ReputerAddress, // we need to allow a pass in of a claim address LibP2PKey: appchain.Config.LibP2PKey, MultiAddress: appchain.Config.MultiAddress, InitialStake: cosmossdk_io_math.NewUint(1), - TopicId: appchain.Config.TopicId, + TopicsIds: []uint64{appchain.Config.TopicId}, + Owner: appchain.ReputerAddress, + IsReputer: isReputer, } } txResp, err := appchain.Client.BroadcastTx(ctx, appchain.ReputerAccount, msg) if err != nil { - if strings.Contains(fmt.Sprint(err), types.Err_ErrWorkerAlreadyRegistered.String()) || strings.Contains(fmt.Sprint(err), types.Err_ErrReputerAlreadyRegistered.String()) { - appchain.Logger.Info().Err(err).Msg("node is already registered") - } else { - appchain.Logger.Fatal().Err(err).Msg("could not register the node with the allora blockchain") - } + appchain.Logger.Fatal().Err(err).Msg(fmt.Sprintf("could not register the node with the Allora blockchain in topic %d", appchain.Config.TopicId)) } else { - appchain.Logger.Info().Str("txhash", txResp.TxHash).Msg("successfully registered node with Allora blockchain") + appchain.Logger.Info().Str("txhash", txResp.TxHash).Msg(fmt.Sprintf("successfully registered node with Allora blockchain in topic %d", appchain.Config.TopicId)) } } -func queryIsNodeRegistered(appchain AppChain) bool { - ctx := context.Background() - queryResp, err := appchain.QueryClient.GetWorkerNodeRegistration(ctx, &types.QueryRegisteredWorkerNodesRequest{ - NodeId: appchain.ReputerAddress + appchain.Config.StringSeperator + appchain.Config.LibP2PKey, - }) - - if err != nil { - appchain.Logger.Fatal().Err(err).Msg("node could not be registered with blockchain") - } - - return (len(queryResp.Nodes) >= 1) -} - // Retry function with a constant number of retries. func (ap *AppChain) SendInferencesWithRetry(ctx context.Context, req *types.MsgProcessInferences, MaxRetries int, Delay int) (*cosmosclient.Response, error) { var txResp *cosmosclient.Response diff --git a/cmd/node/flags.go b/cmd/node/flags.go index fdc8ff9..4b30a5f 100644 --- a/cmd/node/flags.go +++ b/cmd/node/flags.go @@ -54,8 +54,8 @@ func parseFlags() *alloraCfg { pflag.Float64Var(&cfg.CPUPercentage, "cpu-percentage-limit", 1.0, "amount of CPU time allowed for Blockless Functions in the 0-1 range, 1 being unlimited") pflag.Int64Var(&cfg.MemoryMaxKB, "memory-limit", 0, "memory limit (kB) for Blockless Functions") - // Cosmos L1 configuration - pflag.StringVarP(&cfg.AppChainConfig.CosmosHomeDir, "allora-chain-home-dir", "", "", "The Home folder of the client, use the user home if not set") + // Allora L1 configuration + pflag.StringVarP(&cfg.AppChainConfig.AlloraHomeDir, "allora-chain-home-dir", "", "", "The Home folder of the client, use the user home if not set") pflag.StringVarP(&cfg.AppChainConfig.AddressKeyName, "allora-chain-key-name", "", "", "The name of a key stored in the Allora Blockchain Wallet") pflag.StringVarP(&cfg.AppChainConfig.AddressRestoreMnemonic, "allora-chain-restore-mnemonic", "", "", "The restore mnemonic for an Allora Blockchain Wallet") pflag.StringVarP(&cfg.AppChainConfig.AddressAccountPassphrase, "allora-chain-account-password", "", "", "The password for an Allora Blockchain Wallet Key") diff --git a/cmd/node/main.go b/cmd/node/main.go index 4fae8cd..dfef9f7 100644 --- a/cmd/node/main.go +++ b/cmd/node/main.go @@ -37,7 +37,7 @@ func main() { func connectToAlloraBlockchain(cfg AppChainConfig, log zerolog.Logger) (*AppChain, error) { appchain, err := NewAppChain(cfg, log) - if err != nil { + if err != nil || appchain == nil { log.Warn().Err(err).Msg("error connecting to allora blockchain") return nil, err } diff --git a/cmd/node/types.go b/cmd/node/types.go index 0612201..5bb76a6 100644 --- a/cmd/node/types.go +++ b/cmd/node/types.go @@ -26,12 +26,12 @@ type AppChain struct { type AppChainConfig struct { NodeRPCAddress string // rpc node to attach to - AddressPrefix string // prefix for the cosmos addresses + AddressPrefix string // prefix for the allora addresses AddressKeyName string // load a address by key from the keystore AddressRestoreMnemonic string AddressAccountPassphrase string - CosmosHomeDir string // home directory for the cosmos keystore - StringSeperator string // string seperator used for key identifiers in cosmos + AlloraHomeDir string // home directory for the allora keystore + StringSeperator string // string seperator used for key identifiers in allora LibP2PKey string // the libp2p key used to sign offchain communications SubmitTx bool // do we need to commit these to the chain, might be a reason not to MultiAddress string diff --git a/go.mod b/go.mod index b4b5b03..e5ec6b9 100644 --- a/go.mod +++ b/go.mod @@ -6,12 +6,11 @@ toolchain go1.21.5 require ( cosmossdk.io/math v1.2.0 - github.com/allora-network/allora-chain v0.0.0-20240208180735-24a88913f37e + github.com/allora-network/allora-chain v0.0.0-20240222130242-ea5a963913cc github.com/cockroachdb/pebble v1.0.0 github.com/cosmos/cosmos-sdk v0.50.3 github.com/ignite/cli/v28 v28.1.1 github.com/labstack/echo/v4 v4.11.4 - github.com/libp2p/go-libp2p v0.32.2 github.com/multiformats/go-multiaddr v0.12.2 github.com/spf13/pflag v1.0.5 ) @@ -146,6 +145,7 @@ require ( github.com/libp2p/go-buffer-pool v0.1.0 // indirect github.com/libp2p/go-cidranger v1.1.0 // indirect github.com/libp2p/go-flow-metrics v0.1.0 // indirect + github.com/libp2p/go-libp2p v0.32.2 // indirect github.com/libp2p/go-libp2p-asn-util v0.4.1 // indirect github.com/libp2p/go-libp2p-consensus v0.0.1 // indirect github.com/libp2p/go-libp2p-gostream v0.6.0 // indirect diff --git a/go.sum b/go.sum index c1f5523..7357ac4 100644 --- a/go.sum +++ b/go.sum @@ -57,8 +57,8 @@ github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuy github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= -github.com/allora-network/allora-chain v0.0.0-20240208180735-24a88913f37e h1:NBwqDe7zqW4gbtjsoofZkhvw7rcji1aQWBgvotPA4Do= -github.com/allora-network/allora-chain v0.0.0-20240208180735-24a88913f37e/go.mod h1:n/uJmsa0D44UH5bDt+9w8tClIw5wj9P78x35sslefIQ= +github.com/allora-network/allora-chain v0.0.0-20240222130242-ea5a963913cc h1:CkV7PkQOz5hzzBJL9wdDiBgZmsRBoNgPlFs7ucfV2dM= +github.com/allora-network/allora-chain v0.0.0-20240222130242-ea5a963913cc/go.mod h1:n/uJmsa0D44UH5bDt+9w8tClIw5wj9P78x35sslefIQ= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=