diff --git a/_previous/cmd/kwil-admin/cmds/node/cmd.go b/_previous/cmd/kwil-admin/cmds/node/cmd.go deleted file mode 100644 index db0b7f91f..000000000 --- a/_previous/cmd/kwil-admin/cmds/node/cmd.go +++ /dev/null @@ -1,25 +0,0 @@ -package node - -import ( - "github.com/spf13/cobra" -) - -const nodeExplain = "The `node` command is used to get information about a running Kwil node." - -var nodeCmd = &cobra.Command{ - Use: "node", - Short: nodeExplain, - Long: nodeExplain, -} - -func NewNodeCmd() *cobra.Command { - nodeCmd.AddCommand( - dumpCfgCmd(), - versionCmd(), - statusCmd(), - peersCmd(), - genAuthKeyCmd(), - ) - - return nodeCmd -} diff --git a/app/node/build.go b/app/node/build.go index 59e4d8e89..9ef219e03 100644 --- a/app/node/build.go +++ b/app/node/build.go @@ -117,7 +117,7 @@ func buildServer(ctx context.Context, d *coreDependencies) *server { // key because it is used to sign transactions and provide an Identity for // account information (nonce and balance). txSigner := &auth.EthPersonalSigner{Key: *d.privKey.(*crypto.Secp256k1PrivateKey)} - jsonAdminSvc := adminsvc.NewService(db, node, bp, nil, txSigner, d.cfg, + jsonAdminSvc := adminsvc.NewService(db, node, bp, vs, nil, txSigner, d.cfg, d.genesisCfg.ChainID, adminServerLogger) jsonRPCAdminServer = buildJRPCAdminServer(d) jsonRPCAdminServer.RegisterSvc(jsonAdminSvc) diff --git a/app/root.go b/app/root.go index 1127c20a0..e2d4c856a 100644 --- a/app/root.go +++ b/app/root.go @@ -8,8 +8,10 @@ import ( "github.com/kwilteam/kwil-db/app/key" "github.com/kwilteam/kwil-db/app/node" "github.com/kwilteam/kwil-db/app/node/conf" + "github.com/kwilteam/kwil-db/app/rpc" "github.com/kwilteam/kwil-db/app/setup" "github.com/kwilteam/kwil-db/app/shared/bind" + "github.com/kwilteam/kwil-db/app/validator" "github.com/kwilteam/kwil-db/version" "github.com/spf13/cobra" @@ -53,7 +55,8 @@ func RootCmd() *cobra.Command { // There is a virtual "node" command grouping, but no actual "node" command yet. cmd.AddCommand(node.StartCmd()) cmd.AddCommand(node.PrintConfigCmd()) - + cmd.AddCommand(rpc.NewAdminCmd()) + cmd.AddCommand(validator.NewValidatorsCmd()) cmd.AddCommand(setup.SetupCmd()) cmd.AddCommand(key.KeyCmd()) diff --git a/app/rpc/admin.go b/app/rpc/admin.go new file mode 100644 index 000000000..a58f0ccb5 --- /dev/null +++ b/app/rpc/admin.go @@ -0,0 +1,163 @@ +package rpc + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + + adminclient "github.com/kwilteam/kwil-db/node/admin" + "github.com/spf13/cobra" +) + +const ( + kwildRootTODO = "~/.kwild" + adminCertName = "admin.cert" +) + +// BindRPCFlags binds the RPC flags to the given command. +// This includes an rpcserver flag, and the TLS flags. +// These flags can be used to create an admin service client. +// The flags will be bound to all subcommands of the given command. +func BindRPCFlags(cmd *cobra.Command) { + cmd.PersistentFlags().StringP("rpcserver", "s", "/tmp/kwild.socket", "admin RPC server address (either unix or tcp)") + + cmd.PersistentFlags().String("authrpc-cert", "", "kwild's TLS certificate, required for HTTPS server") + cmd.PersistentFlags().String("pass", "", "admin server password (alternative to mTLS with tlskey/tlscert). May be set in ~/.kwil-admin/rpc-admin-pass instead.") + cmd.PersistentFlags().String("tlskey", "auth.key", "kwil-admin's TLS key file to establish a mTLS (authenticated) connection") + cmd.PersistentFlags().String("tlscert", "auth.cert", "kwil-admin's TLS certificate file for server to authenticate us") +} + +// GetRPCServerFlag returns the RPC flag from the given command. +func GetRPCServerFlag(cmd *cobra.Command) (string, error) { + return cmd.Flags().GetString("rpcserver") +} + +// AdminSvcClient will return an admin service client based on the flags. +// The flags should be bound using the BindRPCFlags function. +func AdminSvcClient(ctx context.Context, cmd *cobra.Command) (*adminclient.AdminClient, error) { + adminOpts := []adminclient.Opt{} + + rpcServer, err := GetRPCServerFlag(cmd) + if err != nil { + return nil, err + } + + // get the tls files + // if one is specified, all must be specified + // if none are specified, then we do not use tls + if cmd.Flags().Changed("authrpc-cert") || cmd.Flags().Changed("tlskey") || cmd.Flags().Changed("tlscert") { + kwildTLSCertFile, clientTLSKeyFile, clientTLSCertFile, err := getTLSFlags(cmd) + if err != nil { + return nil, err + } + + adminOpts = append(adminOpts, adminclient.WithTLS(kwildTLSCertFile, clientTLSKeyFile, clientTLSCertFile)) + } + + if pass, err := cmd.Flags().GetString("pass"); err != nil { + return nil, err + } else if pass != "" { + adminOpts = append(adminOpts, adminclient.WithPass(pass)) + } + + return adminclient.NewClient(ctx, rpcServer, adminOpts...) +} + +// getTLSFlags returns the TLS flags from the given command. +func getTLSFlags(cmd *cobra.Command) (kwildTLSCertFile, clientTLSKeyFile, clientTLSCertFile string, err error) { + kwildTLSCertFile, err = cmd.Flags().GetString("authrpc-cert") + if err != nil { + return "", "", "", err + } + + clientTLSKeyFile, err = cmd.Flags().GetString("tlskey") + if err != nil { + return "", "", "", err + } + + clientTLSCertFile, err = cmd.Flags().GetString("tlscert") + if err != nil { + return "", "", "", err + } + + cert := nodeCert{ + KwildTLSCertFile: kwildTLSCertFile, + ClientTLSKeyFile: clientTLSKeyFile, + ClientTLSCertFile: clientTLSCertFile, + } + + return cert.tlsFiles() +} + +// nodeCert is the struct that holds the TLS certificate and key files for +// kwil-admin to use when connecting to the remote node. +type nodeCert struct { + KwildTLSCertFile string + ClientTLSKeyFile string // default: auth.key + ClientTLSCertFile string // default: auth.cert +} + +// tlsFiles loads the remote nodes TLS certificate, which the client uses to +// authenticate the server during the connection handshake, and our TLS key +// pair, which allows the server to authenticate the client. The server's TLS +// certificate would be obtained from the node machine and specified with the +// --authrpc-cert flag, this will search for it in known paths. The client key +// pair (ours) should first be generated with the `node gen-auth-key` command. +func (nc *nodeCert) tlsFiles() (nodeCert, ourKey, ourCert string, err error) { + // Look for kwild's TLS certificate in: + // 1. any path provided via --authrpc-cert + // 2. ~/.kwil-admin/kwild.cert + // 3. ~/.kwild/rpc.cert + nodeCert = nc.KwildTLSCertFile + if nodeCert != "" { // --authrpc-cert + nodeCert = fullPath(nodeCert) + } else { // search the two fallback paths + nodeCert = filepath.Join(kwildRootTODO, adminCertName) + } + if nodeCert == "" || !fileExists(nodeCert) { + err = fmt.Errorf("kwild cert file not found, checked %v", nodeCert) + return + } + ourKey, ourCert = fullPath(nc.ClientTLSKeyFile), fullPath(nc.ClientTLSCertFile) + if ourKey == "" || ourCert == "" { + err = errors.New("our TLS key/cert not found") + return // leave the existence check until we load the files + } + + return +} + +// fullPath gets the full path to a file, searching in the following order: +// 1. the path itself, if it is absolute +// 2. the current directory +// 3. ~/.kwild +func fullPath(path string) string { + // If an absolute path is specified, do nothing. + if filepath.IsAbs(path) { + return path + } + + // First check relative to the current directory. + fullPath, err := filepath.Abs(path) + if err != nil { + return "" + } + if fileExists(fullPath) { + return fullPath + } + + // Check for the file name in root dir + fullPath = filepath.Join(kwildRootTODO, path) + if fileExists(fullPath) { + return fullPath + } + + return "" +} + +func fileExists(path string) bool { + _, err := os.Stat(path) + return err == nil +} diff --git a/app/rpc/cmd.go b/app/rpc/cmd.go new file mode 100644 index 000000000..5afd39275 --- /dev/null +++ b/app/rpc/cmd.go @@ -0,0 +1,30 @@ +package rpc + +import ( + "github.com/spf13/cobra" + + "github.com/kwilteam/kwil-db/app/shared/display" +) + +const adminExplain = "The `admin` command is used to get information about a running Kwil node." + +func NewAdminCmd() *cobra.Command { + adminCmd := &cobra.Command{ + Use: "admin", + Short: "commands for admin RPCs", + Long: adminExplain, + } + + adminCmd.AddCommand( + dumpCfgCmd(), + versionCmd(), + statusCmd(), + peersCmd(), + genAuthKeyCmd(), + ) + + BindRPCFlags(adminCmd) + display.BindOutputFormatFlag(adminCmd) + + return adminCmd +} diff --git a/_previous/cmd/kwil-admin/cmds/node/dump-cfg.go b/app/rpc/dump-cfg.go similarity index 67% rename from _previous/cmd/kwil-admin/cmds/node/dump-cfg.go rename to app/rpc/dump-cfg.go index 2cef55813..b9d015ec5 100644 --- a/_previous/cmd/kwil-admin/cmds/node/dump-cfg.go +++ b/app/rpc/dump-cfg.go @@ -1,12 +1,12 @@ -package node +package rpc import ( "context" "encoding/json" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" - "github.com/pelletier/go-toml/v2" + "github.com/kwilteam/kwil-db/app/shared/display" + "github.com/kwilteam/kwil-db/config" + "github.com/spf13/cobra" ) @@ -25,7 +25,7 @@ func dumpCfgCmd() *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - client, err := common.GetAdminSvcClient(ctx, cmd) + client, err := AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } @@ -35,31 +35,38 @@ func dumpCfgCmd() *cobra.Command { return display.PrintErr(cmd, err) } - cfg := make(map[string]interface{}) - err = json.Unmarshal(bts, &cfg) + var cfg config.Config + err = cfg.FromTOML(bts) if err != nil { return display.PrintErr(cmd, err) } - return display.PrintCmd(cmd, &cfgMsg{cfg: cfg}) + return display.PrintCmd(cmd, &cfgMsg{toml: bts, cfg: &cfg}) }, } - common.BindRPCFlags(cmd) + BindRPCFlags(cmd) return cmd } type cfgMsg struct { - cfg map[string]interface{} + toml []byte + cfg *config.Config } var _ display.MsgFormatter = (*cfgMsg)(nil) func (c *cfgMsg) MarshalJSON() ([]byte, error) { - return json.Marshal(c.cfg) + return json.Marshal(struct { + OK bool `json:"ok"` + TOML string `json:"toml"` + }{ + OK: true, + TOML: string(c.toml), + }) } func (c *cfgMsg) MarshalText() ([]byte, error) { - return toml.Marshal(c.cfg) + return c.cfg.ToTOML() } diff --git a/_previous/cmd/kwil-admin/cmds/node/gen-auth-key.go b/app/rpc/gen-auth-key.go similarity index 88% rename from _previous/cmd/kwil-admin/cmds/node/gen-auth-key.go rename to app/rpc/gen-auth-key.go index 4ea038782..4be16eba9 100644 --- a/_previous/cmd/kwil-admin/cmds/node/gen-auth-key.go +++ b/app/rpc/gen-auth-key.go @@ -1,12 +1,12 @@ -package node +package rpc import ( "fmt" "os" "path/filepath" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" + "github.com/kwilteam/kwil-db/app/shared/bind" + "github.com/kwilteam/kwil-db/app/shared/display" "github.com/kwilteam/kwil-db/core/rpc/transport" "github.com/spf13/cobra" ) @@ -31,7 +31,7 @@ func genAuthKeyCmd() *cobra.Command { Example: genAuthKeyExample, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { - rootDir := common.DefaultKwilAdminRoot() + rootDir, _ := bind.RootDir(cmd) if !filepath.IsAbs(keyFile) { keyFile = filepath.Join(rootDir, keyFile) @@ -63,8 +63,3 @@ func genAuthKeyCmd() *cobra.Command { return cmd } - -func fileExists(path string) bool { - _, err := os.Stat(path) - return !os.IsNotExist(err) -} diff --git a/_previous/cmd/kwil-admin/cmds/node/peers.go b/app/rpc/peers.go similarity index 86% rename from _previous/cmd/kwil-admin/cmds/node/peers.go rename to app/rpc/peers.go index f535e8e18..3aba22834 100644 --- a/_previous/cmd/kwil-admin/cmds/node/peers.go +++ b/app/rpc/peers.go @@ -1,11 +1,10 @@ -package node +package rpc import ( "context" "encoding/json" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" + "github.com/kwilteam/kwil-db/app/shared/display" types "github.com/kwilteam/kwil-db/core/types/admin" "github.com/spf13/cobra" ) @@ -26,7 +25,7 @@ func peersCmd() *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - client, err := common.GetAdminSvcClient(ctx, cmd) + client, err := AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } @@ -40,7 +39,7 @@ func peersCmd() *cobra.Command { }, } - common.BindRPCFlags(cmd) + BindRPCFlags(cmd) return cmd } diff --git a/_previous/cmd/kwil-admin/cmds/node/status.go b/app/rpc/status.go similarity index 86% rename from _previous/cmd/kwil-admin/cmds/node/status.go rename to app/rpc/status.go index 7e47d7404..5beedd36f 100644 --- a/_previous/cmd/kwil-admin/cmds/node/status.go +++ b/app/rpc/status.go @@ -1,11 +1,10 @@ -package node +package rpc import ( "context" "encoding/json" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" + "github.com/kwilteam/kwil-db/app/shared/display" types "github.com/kwilteam/kwil-db/core/types/admin" "github.com/spf13/cobra" ) @@ -26,7 +25,7 @@ func statusCmd() *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - client, err := common.GetAdminSvcClient(ctx, cmd) + client, err := AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } @@ -40,7 +39,7 @@ func statusCmd() *cobra.Command { }, } - common.BindRPCFlags(cmd) + BindRPCFlags(cmd) return cmd } diff --git a/_previous/cmd/kwil-admin/cmds/node/version.go b/app/rpc/version.go similarity index 80% rename from _previous/cmd/kwil-admin/cmds/node/version.go rename to app/rpc/version.go index 268c1887c..a009a7b05 100644 --- a/_previous/cmd/kwil-admin/cmds/node/version.go +++ b/app/rpc/version.go @@ -1,10 +1,9 @@ -package node +package rpc import ( "context" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" + "github.com/kwilteam/kwil-db/app/shared/display" "github.com/spf13/cobra" ) @@ -24,7 +23,7 @@ func versionCmd() *cobra.Command { Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, _ []string) error { ctx := context.Background() - client, err := common.GetAdminSvcClient(ctx, cmd) + client, err := AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } @@ -38,7 +37,7 @@ func versionCmd() *cobra.Command { }, } - common.BindRPCFlags(cmd) + BindRPCFlags(cmd) return cmd } diff --git a/_previous/cmd/kwil-admin/cmds/validators/approve.go b/app/validator/approve.go similarity index 87% rename from _previous/cmd/kwil-admin/cmds/validators/approve.go rename to app/validator/approve.go index 9aeaa0daf..cd30159a9 100644 --- a/_previous/cmd/kwil-admin/cmds/validators/approve.go +++ b/app/validator/approve.go @@ -1,12 +1,13 @@ -package validators +package validator import ( "context" "encoding/hex" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" "github.com/spf13/cobra" + + "github.com/kwilteam/kwil-db/app/rpc" + "github.com/kwilteam/kwil-db/app/shared/display" ) var ( @@ -26,7 +27,7 @@ func approveCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - clt, err := common.GetAdminSvcClient(ctx, cmd) + clt, err := rpc.AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } diff --git a/_previous/cmd/kwil-admin/cmds/validators/cmd.go b/app/validator/cmd.go similarity index 57% rename from _previous/cmd/kwil-admin/cmds/validators/cmd.go rename to app/validator/cmd.go index 5d400f1e1..56203a60a 100644 --- a/_previous/cmd/kwil-admin/cmds/validators/cmd.go +++ b/app/validator/cmd.go @@ -1,20 +1,20 @@ -package validators +package validator import ( - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" "github.com/spf13/cobra" + + "github.com/kwilteam/kwil-db/app/rpc" ) -const validatorsShort = "The `validators` command provides functions for creating and broadcasting validator-related transactions." const validatorsLong = "The `validators` command provides functions for creating and broadcasting validator-related transactions (join/approve/leave), and retrieving information on the current validators and join requests." -var validatorsCmd = &cobra.Command{ - Use: "validators", - Short: validatorsShort, - Long: validatorsLong, -} - func NewValidatorsCmd() *cobra.Command { + validatorsCmd := &cobra.Command{ + Use: "validators", + Short: "validator related actions", + Long: validatorsLong, + } + validatorsCmd.AddCommand( joinCmd(), joinStatusCmd(), @@ -25,7 +25,7 @@ func NewValidatorsCmd() *cobra.Command { listJoinRequestsCmd(), ) - common.BindRPCFlags(validatorsCmd) + rpc.BindRPCFlags(validatorsCmd) return validatorsCmd } diff --git a/_previous/cmd/kwil-admin/cmds/validators/join-status.go b/app/validator/join-status.go similarity index 94% rename from _previous/cmd/kwil-admin/cmds/validators/join-status.go rename to app/validator/join-status.go index f563d5bcc..b1ce9d25b 100644 --- a/_previous/cmd/kwil-admin/cmds/validators/join-status.go +++ b/app/validator/join-status.go @@ -1,4 +1,4 @@ -package validators +package validator import ( "bytes" @@ -9,11 +9,12 @@ import ( "fmt" "math" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" + "github.com/spf13/cobra" + + "github.com/kwilteam/kwil-db/app/rpc" + "github.com/kwilteam/kwil-db/app/shared/display" "github.com/kwilteam/kwil-db/core/rpc/client" "github.com/kwilteam/kwil-db/core/types" - "github.com/spf13/cobra" ) var ( @@ -32,7 +33,7 @@ func joinStatusCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - clt, err := common.GetAdminSvcClient(ctx, cmd) + clt, err := rpc.AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } diff --git a/_previous/cmd/kwil-admin/cmds/validators/join.go b/app/validator/join.go similarity index 86% rename from _previous/cmd/kwil-admin/cmds/validators/join.go rename to app/validator/join.go index 678be7187..5fd6626a9 100644 --- a/_previous/cmd/kwil-admin/cmds/validators/join.go +++ b/app/validator/join.go @@ -1,11 +1,12 @@ -package validators +package validator import ( "context" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" "github.com/spf13/cobra" + + "github.com/kwilteam/kwil-db/app/rpc" + "github.com/kwilteam/kwil-db/app/shared/display" ) var ( @@ -25,7 +26,7 @@ func joinCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - clt, err := common.GetAdminSvcClient(ctx, cmd) + clt, err := rpc.AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } diff --git a/_previous/cmd/kwil-admin/cmds/validators/leave.go b/app/validator/leave.go similarity index 81% rename from _previous/cmd/kwil-admin/cmds/validators/leave.go rename to app/validator/leave.go index a3a704d2c..a2f26c79d 100644 --- a/_previous/cmd/kwil-admin/cmds/validators/leave.go +++ b/app/validator/leave.go @@ -1,11 +1,12 @@ -package validators +package validator import ( "context" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" "github.com/spf13/cobra" + + "github.com/kwilteam/kwil-db/app/rpc" + "github.com/kwilteam/kwil-db/app/shared/display" ) var ( @@ -25,7 +26,7 @@ func leaveCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - clt, err := common.GetAdminSvcClient(ctx, cmd) + clt, err := rpc.AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } diff --git a/_previous/cmd/kwil-admin/cmds/validators/list-join-requests.go b/app/validator/list-join-requests.go similarity index 93% rename from _previous/cmd/kwil-admin/cmds/validators/list-join-requests.go rename to app/validator/list-join-requests.go index e7954c06d..34035aecd 100644 --- a/_previous/cmd/kwil-admin/cmds/validators/list-join-requests.go +++ b/app/validator/list-join-requests.go @@ -1,4 +1,4 @@ -package validators +package validator import ( "bytes" @@ -8,10 +8,11 @@ import ( "fmt" "math" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" - "github.com/kwilteam/kwil-db/core/types" "github.com/spf13/cobra" + + "github.com/kwilteam/kwil-db/app/rpc" + "github.com/kwilteam/kwil-db/app/shared/display" + "github.com/kwilteam/kwil-db/core/types" ) var ( @@ -33,7 +34,7 @@ func listJoinRequestsCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - clt, err := common.GetAdminSvcClient(ctx, cmd) + clt, err := rpc.AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } diff --git a/_previous/cmd/kwil-admin/cmds/validators/list.go b/app/validator/list.go similarity index 90% rename from _previous/cmd/kwil-admin/cmds/validators/list.go rename to app/validator/list.go index cb17f100b..0274755c3 100644 --- a/_previous/cmd/kwil-admin/cmds/validators/list.go +++ b/app/validator/list.go @@ -1,4 +1,4 @@ -package validators +package validator import ( "bytes" @@ -6,10 +6,11 @@ import ( "encoding/json" "fmt" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" - "github.com/kwilteam/kwil-db/core/types" "github.com/spf13/cobra" + + "github.com/kwilteam/kwil-db/app/rpc" + "github.com/kwilteam/kwil-db/app/shared/display" + "github.com/kwilteam/kwil-db/core/types" ) var ( @@ -30,7 +31,7 @@ func listCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - clt, err := common.GetAdminSvcClient(ctx, cmd) + clt, err := rpc.AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } diff --git a/_previous/cmd/kwil-admin/cmds/validators/remove.go b/app/validator/remove.go similarity index 86% rename from _previous/cmd/kwil-admin/cmds/validators/remove.go rename to app/validator/remove.go index 77d143c9f..349481c83 100644 --- a/_previous/cmd/kwil-admin/cmds/validators/remove.go +++ b/app/validator/remove.go @@ -1,12 +1,13 @@ -package validators +package validator import ( "context" "encoding/hex" - "github.com/kwilteam/kwil-db/cmd/common/display" - "github.com/kwilteam/kwil-db/cmd/kwil-admin/cmds/common" "github.com/spf13/cobra" + + "github.com/kwilteam/kwil-db/app/rpc" + "github.com/kwilteam/kwil-db/app/shared/display" ) var ( @@ -26,7 +27,7 @@ func removeCmd() *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { ctx := context.Background() - clt, err := common.GetAdminSvcClient(ctx, cmd) + clt, err := rpc.AdminSvcClient(ctx, cmd) if err != nil { return display.PrintErr(cmd, err) } diff --git a/common/common.go b/common/common.go index af1bc2889..dc2fcfe55 100644 --- a/common/common.go +++ b/common/common.go @@ -125,7 +125,7 @@ type Accounts interface { type Validators interface { // GetValidatorPower retrieves the power of the given validator. If // the validator does not exist, it will return 0. - GetValidatorPower(ctx context.Context, tx sql.Executor, validator []byte) (int64, error) + GetValidatorPower(ctx context.Context, validator []byte) (int64, error) // GetValidators retrieves all validators. GetValidators() []*types.Validator // SetValidatorPower sets the power of a validator. If the target diff --git a/config/config.go b/config/config.go index e3afe42fe..e5f848b64 100644 --- a/config/config.go +++ b/config/config.go @@ -269,6 +269,10 @@ func (nc Config) ToTOML() ([]byte, error) { return toml.Marshal(nc) } +func (nc *Config) FromTOML(b []byte) error { + return toml.Unmarshal(b, &nc) +} + func (nc *Config) SaveAs(filename string) error { bts, err := nc.ToTOML() if err != nil { diff --git a/node/node.go b/node/node.go index 109fb9651..3f901db6f 100644 --- a/node/node.go +++ b/node/node.go @@ -433,13 +433,27 @@ func (n *Node) doStatesync(ctx context.Context) error { } func (n *Node) Peers(context.Context) ([]*adminTypes.PeerInfo, error) { - return []*adminTypes.PeerInfo{ // TODO - { + peers := n.pm.ConnectedPeers() + var peersInfo []*adminTypes.PeerInfo + for _, peer := range peers { + conns := n.host.Network().ConnsToPeer(peer.ID) + if len(conns) == 0 { // should be at least one + continue + } + + var addr string + if len(peer.Addrs) > 0 { + host, port, _ := maHostPort(peer.Addrs[0]) + addr = net.JoinHostPort(host, port) + } + + peersInfo = append(peersInfo, &adminTypes.PeerInfo{ NodeInfo: &adminTypes.NodeInfo{}, - Inbound: false, - RemoteAddr: "", - }, - }, nil + Inbound: conns[0].Stat().Direction == network.DirInbound, + RemoteAddr: addr, + }) + } + return peersInfo, nil } func (n *Node) Status(ctx context.Context) (*adminTypes.Status, error) { @@ -611,21 +625,26 @@ func newHost(ip string, port uint64, privKey crypto.PrivateKey) (host.Host, erro return h, nil } +func maHostPort(addr multiaddr.Multiaddr) (host, port, protocol string) { + port, _ = addr.ValueForProtocol(multiaddr.P_TCP) + protocol = "ip4" + host, _ = addr.ValueForProtocol(multiaddr.P_IP4) + if host == "" { + host, _ = addr.ValueForProtocol(multiaddr.P_IP6) + protocol = "ip6" + } + return +} + func hostPort(host host.Host) ([]string, []int, []string) { var addrStr []string var ports []int var protocols []string // ip4 or ip6 for _, addr := range host.Addrs() { // host.Network().ListenAddresses() - ps, _ := addr.ValueForProtocol(multiaddr.P_TCP) - port, _ := strconv.Atoi(ps) + host, portStr, protocol := maHostPort(addr) + port, _ := strconv.Atoi(portStr) ports = append(ports, port) - protocol := "ip4" - as, _ := addr.ValueForProtocol(multiaddr.P_IP4) - if as == "" { - as, _ = addr.ValueForProtocol(multiaddr.P_IP6) - protocol = "ip6" - } - addrStr = append(addrStr, as) + addrStr = append(addrStr, host) protocols = append(protocols, protocol) } diff --git a/node/services/jsonrpc/adminsvc/service.go b/node/services/jsonrpc/adminsvc/service.go index c0a1b1718..489de4f20 100644 --- a/node/services/jsonrpc/adminsvc/service.go +++ b/node/services/jsonrpc/adminsvc/service.go @@ -12,11 +12,11 @@ import ( jsonrpc "github.com/kwilteam/kwil-db/core/rpc/json" adminjson "github.com/kwilteam/kwil-db/core/rpc/json/admin" userjson "github.com/kwilteam/kwil-db/core/rpc/json/user" - coretypes "github.com/kwilteam/kwil-db/core/types" ktypes "github.com/kwilteam/kwil-db/core/types" types "github.com/kwilteam/kwil-db/core/types/admin" "github.com/kwilteam/kwil-db/extensions/resolutions" rpcserver "github.com/kwilteam/kwil-db/node/services/jsonrpc" + nodetypes "github.com/kwilteam/kwil-db/node/types" "github.com/kwilteam/kwil-db/node/types/sql" "github.com/kwilteam/kwil-db/node/voting" "github.com/kwilteam/kwil-db/version" @@ -27,7 +27,7 @@ import ( type Node interface { Status(context.Context) (*types.Status, error) Peers(context.Context) ([]*types.PeerInfo, error) - BroadcastTx(ctx context.Context, tx *coretypes.Transaction, sync uint8) (*coretypes.ResultBroadcastTx, error) + BroadcastTx(ctx context.Context, tx *ktypes.Transaction, sync uint8) (*ktypes.ResultBroadcastTx, error) } type P2P interface { @@ -46,13 +46,13 @@ type App interface { // If unconfirmed is true, the account found in the mempool is returned. // Otherwise, the account found in the blockchain is returned. AccountInfo(ctx context.Context, db sql.DB, identifier []byte, unconfirmed bool) (balance *big.Int, nonce int64, err error) - Price(ctx context.Context, db sql.DB, tx *coretypes.Transaction) (*big.Int, error) + Price(ctx context.Context, db sql.DB, tx *ktypes.Transaction) (*big.Int, error) } type Validators interface { SetValidatorPower(ctx context.Context, tx sql.Executor, pubKey []byte, power int64) error - GetValidatorPower(ctx context.Context, tx sql.Executor, pubKey []byte) (int64, error) - GetValidators() []*coretypes.Validator + GetValidatorPower(ctx context.Context, pubKey []byte) (int64, error) + GetValidators() []*ktypes.Validator } type Service struct { @@ -144,7 +144,7 @@ func (svc *Service) HealthMethod(ctx context.Context, _ *userjson.HealthRequest) Role: status.Validator.Role, NumValidators: len(vals.Validators), }, nil - // slices.ContainsFunc(vals.Validators, func(v *coretypes.Validator) bool { return bytes.Equal(v.PubKey, status.Validator.PubKey) }) + // slices.ContainsFunc(vals.Validators, func(v *ktypes.Validator) bool { return bytes.Equal(v.PubKey, status.Validator.PubKey) }) } func (svc *Service) Methods() map[jsonrpc.Method]rpcserver.MethodDef { @@ -222,12 +222,13 @@ func (svc *Service) Handlers() map[jsonrpc.Method]rpcserver.MethodHandler { // NewService constructs a new Service. func NewService(db sql.DelayedReadTxMaker, blockchain Node, app App, - p2p P2P, txSigner auth.Signer, cfg *config.Config, + vs Validators, p2p P2P, txSigner auth.Signer, cfg *config.Config, chainID string, logger log.Logger) *Service { return &Service{ blockchain: blockchain, p2p: p2p, app: app, + voting: vs, signer: txSigner, chainID: chainID, cfg: cfg, @@ -252,13 +253,19 @@ func (svc *Service) Status(ctx context.Context, req *adminjson.StatusRequest) (* return nil, jsonrpc.NewError(jsonrpc.ErrorNodeInternal, "node status unavailable", nil) } + var power int64 + switch status.Validator.Role { + case nodetypes.RoleLeader.String(), nodetypes.RoleValidator.String(): + power, _ = svc.voting.GetValidatorPower(ctx, status.Validator.PubKey) + } + return &adminjson.StatusResponse{ Node: status.Node, Sync: convertSyncInfo(status.Sync), Validator: &adminjson.Validator{ // TODO: weed out the type dups Role: status.Validator.Role, PubKey: status.Validator.PubKey, - // Power: status.Validator.Power, + Power: power, }, }, nil } @@ -282,7 +289,7 @@ func (svc *Service) Peers(ctx context.Context, _ *adminjson.PeersRequest) (*admi } // sendTx makes a transaction and sends it to the local node. -func (svc *Service) sendTx(ctx context.Context, payload coretypes.Payload) (*userjson.BroadcastResponse, *jsonrpc.Error) { +func (svc *Service) sendTx(ctx context.Context, payload ktypes.Payload) (*userjson.BroadcastResponse, *jsonrpc.Error) { readTx := svc.db.BeginDelayedReadTx() defer readTx.Rollback(ctx) @@ -292,7 +299,7 @@ func (svc *Service) sendTx(ctx context.Context, payload coretypes.Payload) (*use return nil, jsonrpc.NewError(jsonrpc.ErrorAccountInternal, "account info error", nil) } - tx, err := coretypes.CreateTransaction(payload, svc.chainID, uint64(nonce+1)) + tx, err := ktypes.CreateTransaction(payload, svc.chainID, uint64(nonce+1)) if err != nil { return nil, jsonrpc.NewError(jsonrpc.ErrorInternal, "unable to create transaction", nil) } @@ -318,7 +325,7 @@ func (svc *Service) sendTx(ctx context.Context, payload coretypes.Payload) (*use code, txHash := res.Code, res.Hash - if txCode := coretypes.TxCode(code); txCode != coretypes.CodeOk { + if txCode := ktypes.TxCode(code); txCode != ktypes.CodeOk { errData := &userjson.BroadcastError{ TxCode: uint32(txCode), // e.g. invalid nonce, wrong chain, etc. Hash: txHash.String(), @@ -336,19 +343,19 @@ func (svc *Service) sendTx(ctx context.Context, payload coretypes.Payload) (*use } func (svc *Service) Approve(ctx context.Context, req *adminjson.ApproveRequest) (*userjson.BroadcastResponse, *jsonrpc.Error) { - return svc.sendTx(ctx, &coretypes.ValidatorApprove{ + return svc.sendTx(ctx, &ktypes.ValidatorApprove{ Candidate: req.PubKey, }) } func (svc *Service) Join(ctx context.Context, req *adminjson.JoinRequest) (*userjson.BroadcastResponse, *jsonrpc.Error) { - return svc.sendTx(ctx, &coretypes.ValidatorJoin{ + return svc.sendTx(ctx, &ktypes.ValidatorJoin{ Power: 1, }) } func (svc *Service) Remove(ctx context.Context, req *adminjson.RemoveRequest) (*userjson.BroadcastResponse, *jsonrpc.Error) { - return svc.sendTx(ctx, &coretypes.ValidatorRemove{ + return svc.sendTx(ctx, &ktypes.ValidatorRemove{ Validator: req.PubKey, }) } @@ -385,7 +392,7 @@ func (svc *Service) JoinStatus(ctx context.Context, req *adminjson.JoinStatusReq } func (svc *Service) Leave(ctx context.Context, req *adminjson.LeaveRequest) (*userjson.BroadcastResponse, *jsonrpc.Error) { - return svc.sendTx(ctx, &coretypes.ValidatorLeave{}) + return svc.sendTx(ctx, &ktypes.ValidatorLeave{}) } func (svc *Service) ListValidators(ctx context.Context, req *adminjson.ListValidatorsRequest) (*adminjson.ListValidatorsResponse, *jsonrpc.Error) { @@ -396,7 +403,7 @@ func (svc *Service) ListValidators(ctx context.Context, req *adminjson.ListValid pbValidators[i] = &adminjson.Validator{ Role: vi.Role, PubKey: vi.PubKey, - // Power: vi.Power, + Power: vi.Power, } } @@ -507,8 +514,8 @@ func (svc *Service) ListPeers(ctx context.Context, req *adminjson.PeersRequest) } func (svc *Service) CreateResolution(ctx context.Context, req *adminjson.CreateResolutionRequest) (*userjson.BroadcastResponse, *jsonrpc.Error) { - res := &coretypes.CreateResolution{ - Resolution: &coretypes.VotableEvent{ + res := &ktypes.CreateResolution{ + Resolution: &ktypes.VotableEvent{ Type: req.ResolutionType, Body: req.Resolution, }, @@ -518,7 +525,7 @@ func (svc *Service) CreateResolution(ctx context.Context, req *adminjson.CreateR } func (svc *Service) ApproveResolution(ctx context.Context, req *adminjson.ApproveResolutionRequest) (*userjson.BroadcastResponse, *jsonrpc.Error) { - res := &coretypes.ApproveResolution{ + res := &ktypes.ApproveResolution{ ResolutionID: req.ResolutionID, } @@ -527,7 +534,7 @@ func (svc *Service) ApproveResolution(ctx context.Context, req *adminjson.Approv /* disabled until the tx route is tested func (svc *Service) DeleteResolution(ctx context.Context, req *adminjson.DeleteResolutionRequest) (*userjson.BroadcastResponse, *jsonrpc.Error) { - res := &coretypes.DeleteResolution{ + res := &ktypes.DeleteResolution{ ResolutionID: req.ResolutionID, } @@ -553,7 +560,7 @@ func (svc *Service) ResolutionStatus(ctx context.Context, req *adminjson.Resolut // } return &adminjson.ResolutionStatusResponse{ - Status: &coretypes.PendingResolution{ + Status: &ktypes.PendingResolution{ ResolutionID: req.ResolutionID, ExpiresAt: resolution.ExpirationHeight, Board: nil, // resolution.Voters ??? diff --git a/node/services/jsonrpc/server.go b/node/services/jsonrpc/server.go index 844a5ea92..28bb82b6e 100644 --- a/node/services/jsonrpc/server.go +++ b/node/services/jsonrpc/server.go @@ -280,6 +280,8 @@ func NewServer(addr string, log log.Logger, opts ...Opt) (*Server, error) { var h http.Handler h = http.HandlerFunc(s.handlerJSONRPCV1) // last, after middleware below h = http.MaxBytesHandler(h, int64(cfg.reqSzLimit)) + // h = middleware.Logger(h) + h = middleware.Recoverer(h) // amazingly, exceeding the server's write timeout does not cancel request // contexts: https://github.com/golang/go/issues/59602 // So, we add a timeout to the Request's context. @@ -292,7 +294,8 @@ func NewServer(addr string, log log.Logger, opts ...Opt) (*Server, error) { h = compMW(h) h = reqCounter(h, metrics[reqCounterName]) h = realIPHandler(h, cfg.proxyCount) // for effective rate limiting - h = recoverer(h, log) // first, wrap with defer and call next ^ + + // h = recoverer(h, log) // first, wrap with defer and call next ^ mux.Handle(pathRPCV1, h) // do not add method! We need to handle OPTIONS for CORS, but only POST in JSON-RPC @@ -384,7 +387,7 @@ func jsonRPCTimeoutHandler(h http.Handler, timeout time.Duration, logger log.Log // downstream and we don't have the request ID. resp := jsonrpc.NewErrorResponse(-1, jsonrpc.NewError(jsonrpc.ErrorTimeout, "RPC timeout", nil)) respMsg, _ := json.Marshal(resp) - h = http.TimeoutHandler(h, timeout, string(respMsg)) + h = http.TimeoutHandler(h, timeout, string(respMsg)) // https://github.com/golang/go/issues/27375 // Log total request handling time (including transfer). return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -443,7 +446,7 @@ func recoverer(h http.Handler, log log.Logger) http.Handler { } debugStack := debug.Stack() - log.Errorf("panic:\n%v", string(debugStack)) + log.Errorf("panic: %v\n%v", rvr, string(debugStack)) w.WriteHeader(http.StatusInternalServerError) } diff --git a/node/services/jsonrpc/usersvc/service.go b/node/services/jsonrpc/usersvc/service.go index 8113c9cab..1b11b6568 100644 --- a/node/services/jsonrpc/usersvc/service.go +++ b/node/services/jsonrpc/usersvc/service.go @@ -52,7 +52,7 @@ type NodeApp interface { type Validators interface { SetValidatorPower(ctx context.Context, tx sql.Executor, pubKey []byte, power int64) error - GetValidatorPower(ctx context.Context, tx sql.Executor, pubKey []byte) (int64, error) + GetValidatorPower(ctx context.Context, pubKey []byte) (int64, error) GetValidators() []*types.Validator } diff --git a/node/statesync_test.go b/node/statesync_test.go index 065ebd33e..52209fb3a 100644 --- a/node/statesync_test.go +++ b/node/statesync_test.go @@ -207,7 +207,7 @@ func TestStateSyncService(t *testing.T) { // bootstrap the ss3 with the trusted providers for _, addr := range addrs { i, err := connectPeer(ctx, addr, h3) - assert.NoError(t, err) + require.NoError(t, err) ss3.trustedProviders = append(ss3.trustedProviders, i) } diff --git a/node/txapp/interfaces.go b/node/txapp/interfaces.go index 9c2ce8338..2afab55a5 100644 --- a/node/txapp/interfaces.go +++ b/node/txapp/interfaces.go @@ -21,7 +21,7 @@ type Accounts interface { type Validators interface { SetValidatorPower(ctx context.Context, tx sql.Executor, pubKey []byte, power int64) error - GetValidatorPower(ctx context.Context, tx sql.Executor, pubKey []byte) (int64, error) + GetValidatorPower(ctx context.Context, pubKey []byte) (int64, error) GetValidators() []*types.Validator Commit() error } diff --git a/node/txapp/mempool.go b/node/txapp/mempool.go index a5fa2d69f..3cd23e38a 100644 --- a/node/txapp/mempool.go +++ b/node/txapp/mempool.go @@ -120,7 +120,7 @@ func (m *mempool) applyTransaction(ctx *common.TxContext, tx *types.Transaction, // seems like maybe this should go in the switch statement below, // but I put it here to avoid extra db call for account info if tx.Body.PayloadType == types.PayloadTypeValidatorVoteIDs { - power, err := m.validatorMgr.GetValidatorPower(ctx.Ctx, dbTx, tx.Sender) + power, err := m.validatorMgr.GetValidatorPower(ctx.Ctx, tx.Sender) if err != nil { return err } diff --git a/node/txapp/routes.go b/node/txapp/routes.go index 0b428effb..13be8743d 100644 --- a/node/txapp/routes.go +++ b/node/txapp/routes.go @@ -456,7 +456,7 @@ func (d *validatorJoinRoute) PreTx(ctx *common.TxContext, svc *common.Service, t func (d *validatorJoinRoute) InTx(ctx *common.TxContext, app *common.App, tx *types.Transaction) (types.TxCode, error) { // ensure this candidate is not already a validator - power, err := app.Validators.GetValidatorPower(ctx.Ctx, app.DB, tx.Sender) + power, err := app.Validators.GetValidatorPower(ctx.Ctx, tx.Sender) if err != nil { return types.CodeUnknownError, err } @@ -547,7 +547,7 @@ func (d *validatorApproveRoute) InTx(ctx *common.TxContext, app *common.App, tx } // ensure that sender is a validator - power, err := app.Validators.GetValidatorPower(ctx.Ctx, app.DB, tx.Sender) + power, err := app.Validators.GetValidatorPower(ctx.Ctx, tx.Sender) if err != nil { return types.CodeUnknownError, err } @@ -609,7 +609,7 @@ func (d *validatorRemoveRoute) InTx(ctx *common.TxContext, app *common.App, tx * } // ensure the sender is a validator - power, err := app.Validators.GetValidatorPower(ctx.Ctx, app.DB, tx.Sender) + power, err := app.Validators.GetValidatorPower(ctx.Ctx, tx.Sender) if err != nil { return types.CodeUnknownError, err } @@ -618,7 +618,7 @@ func (d *validatorRemoveRoute) InTx(ctx *common.TxContext, app *common.App, tx * } // ensure that the target is a validator - power, err = app.Validators.GetValidatorPower(ctx.Ctx, app.DB, d.target) + power, err = app.Validators.GetValidatorPower(ctx.Ctx, d.target) if err != nil { return types.CodeUnknownError, err } @@ -673,7 +673,7 @@ func (d *validatorLeaveRoute) PreTx(ctx *common.TxContext, svc *common.Service, } func (d *validatorLeaveRoute) InTx(ctx *common.TxContext, app *common.App, tx *types.Transaction) (types.TxCode, error) { - power, err := app.Validators.GetValidatorPower(ctx.Ctx, app.DB, tx.Sender) + power, err := app.Validators.GetValidatorPower(ctx.Ctx, tx.Sender) if err != nil { return types.CodeUnknownError, err } @@ -719,7 +719,7 @@ func (d *validatorVoteIDsRoute) PreTx(ctx *common.TxContext, svc *common.Service func (d *validatorVoteIDsRoute) InTx(ctx *common.TxContext, app *common.App, tx *types.Transaction) (types.TxCode, error) { // if the caller has 0 power, they are not a validator, and should not be able to vote - power, err := app.Validators.GetValidatorPower(ctx.Ctx, app.DB, tx.Sender) + power, err := app.Validators.GetValidatorPower(ctx.Ctx, tx.Sender) if err != nil { return types.CodeUnknownError, err } @@ -913,7 +913,7 @@ func (d *createResolutionRoute) PreTx(ctx *common.TxContext, svc *common.Service func (d *createResolutionRoute) InTx(ctx *common.TxContext, app *common.App, tx *types.Transaction) (types.TxCode, error) { // ensure the sender is a validator // only validators can create resolutions - power, err := app.Validators.GetValidatorPower(ctx.Ctx, app.DB, tx.Sender) + power, err := app.Validators.GetValidatorPower(ctx.Ctx, tx.Sender) if err != nil { return types.CodeUnknownError, err } @@ -969,7 +969,7 @@ func (d *approveResolutionRoute) PreTx(ctx *common.TxContext, svc *common.Servic func (d *approveResolutionRoute) InTx(ctx *common.TxContext, app *common.App, tx *types.Transaction) (types.TxCode, error) { // ensure the sender is a validator - power, err := app.Validators.GetValidatorPower(ctx.Ctx, app.DB, tx.Sender) + power, err := app.Validators.GetValidatorPower(ctx.Ctx, tx.Sender) if err != nil { return types.CodeUnknownError, err } diff --git a/node/txapp/routes_test.go b/node/txapp/routes_test.go index 242084ce3..780c41f08 100644 --- a/node/txapp/routes_test.go +++ b/node/txapp/routes_test.go @@ -335,7 +335,7 @@ func (v *mockValidator) GetValidators() []*types.Validator { return nil } -func (v *mockValidator) GetValidatorPower(_ context.Context, _ sql.Executor, pubKey []byte) (int64, error) { +func (v *mockValidator) GetValidatorPower(_ context.Context, pubKey []byte) (int64, error) { return v.getVoterFn() } diff --git a/node/voting/vote_test.go b/node/voting/vote_test.go index d4001cb13..0d54b6ee5 100644 --- a/node/voting/vote_test.go +++ b/node/voting/vote_test.go @@ -103,7 +103,7 @@ func Test_Voting(t *testing.T) { voters = v.GetValidators() require.Len(t, voters, 2) - voterAPower, err := v.GetValidatorPower(ctx, db, []byte("a")) + voterAPower, err := v.GetValidatorPower(ctx, []byte("a")) require.NoError(t, err) require.Equal(t, int64(100), voterAPower) diff --git a/node/voting/voting.go b/node/voting/voting.go index d6ba56bed..3a3415698 100644 --- a/node/voting/voting.go +++ b/node/voting/voting.go @@ -584,7 +584,7 @@ func (v *VoteStore) SetValidatorPower(ctx context.Context, db sql.Executor, reci // GetValidatorPower gets the power of a voter. // If the voter does not exist, it will return 0. -func (v *VoteStore) GetValidatorPower(ctx context.Context, db sql.Executor, identifier []byte) (power int64, err error) { +func (v *VoteStore) GetValidatorPower(ctx context.Context, identifier []byte) (power int64, err error) { v.mtx.Lock() defer v.mtx.Unlock()