Skip to content

Commit

Permalink
refactor: make rpcapi names enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
niklr committed Apr 1, 2022
1 parent 13f5fcd commit fb34302
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 64 deletions.
13 changes: 7 additions & 6 deletions cmd/nodemanager/full_node_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/vitelabs/go-vite/v2/common"
"github.com/vitelabs/go-vite/v2/log15"
"github.com/vitelabs/go-vite/v2/node"
nodeconfig "github.com/vitelabs/go-vite/v2/node/config"
)

var defaultNodeConfigFileName = "node_config.json"
Expand All @@ -34,8 +35,8 @@ func (maker FullNodeMaker) MakeNode(ctx *cli.Context) (*node.Node, error) {
return node, nil
}

func (maker FullNodeMaker) MakeNodeConfig(ctx *cli.Context) (*node.Config, error) {
cfg := node.DefaultNodeConfig
func (maker FullNodeMaker) MakeNodeConfig(ctx *cli.Context) (*nodeconfig.Config, error) {
cfg := nodeconfig.DefaultNodeConfig
log.Info(fmt.Sprintf("DefaultNodeconfig: %v", cfg))

// 1: Load config file.
Expand All @@ -62,7 +63,7 @@ func (maker FullNodeMaker) MakeNodeConfig(ctx *cli.Context) (*node.Config, error
}

// SetNodeConfig applies node-related command line flags to the config.
func mappingNodeConfig(ctx *cli.Context, cfg *node.Config) {
func mappingNodeConfig(ctx *cli.Context, cfg *nodeconfig.Config) {

//Global Config
if dataDir := ctx.GlobalString(utils.DataDirFlag.Name); len(dataDir) > 0 {
Expand Down Expand Up @@ -165,7 +166,7 @@ func mappingNodeConfig(ctx *cli.Context, cfg *node.Config) {
}
}

func overrideNodeConfigs(ctx *cli.Context, cfg *node.Config) {
func overrideNodeConfigs(ctx *cli.Context, cfg *nodeconfig.Config) {

if len(cfg.DataDir) == 0 || cfg.DataDir == "" {
cfg.DataDir = common.DefaultDataDir()
Expand Down Expand Up @@ -216,7 +217,7 @@ func overrideNodeConfigs(ctx *cli.Context, cfg *node.Config) {
}
}

func loadNodeConfigFromFile(ctx *cli.Context, cfg *node.Config) error {
func loadNodeConfigFromFile(ctx *cli.Context, cfg *nodeconfig.Config) error {

configFile := ctx.GlobalString(utils.ConfigFileFlag.Name)
if configFile == "" {
Expand All @@ -236,7 +237,7 @@ func IsExist(f string) bool {
return err == nil || os.IsExist(err)
}

func makeRunLogFile(cfg *node.Config) {
func makeRunLogFile(cfg *nodeconfig.Config) {
defaultHandler := common.LogHandler(cfg.RunLogDir(), "", "vite.log", cfg.LogLevel)
errorHandler := common.LogHandler(cfg.RunLogDir(), "error", "vite.error.log", log15.LvlError.String())

Expand Down
7 changes: 4 additions & 3 deletions cmd/nodemanager/local_node_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/vitelabs/go-vite/v2/common"
"github.com/vitelabs/go-vite/v2/node"
nodeconfig "github.com/vitelabs/go-vite/v2/node/config"
)

type LocalNodeMaker struct {
Expand All @@ -29,8 +30,8 @@ func (maker LocalNodeMaker) MakeNode(ctx *cli.Context) (*node.Node, error) {
return node, nil
}

func (maker LocalNodeMaker) MakeNodeConfig(ctx *cli.Context) (*node.Config, error) {
cfg := &node.DefaultNodeConfig
func (maker LocalNodeMaker) MakeNodeConfig(ctx *cli.Context) (*nodeconfig.Config, error) {
cfg := &nodeconfig.DefaultNodeConfig
log.Info(fmt.Sprintf("DefaultNodeconfig: %v", cfg))

// 1: Load config file.
Expand Down Expand Up @@ -59,7 +60,7 @@ func (maker LocalNodeMaker) MakeNodeConfig(ctx *cli.Context) (*node.Config, erro
return cfg, nil
}

func makeLocalNodeCfg(ctx *cli.Context, cfg *node.Config) {
func makeLocalNodeCfg(ctx *cli.Context, cfg *nodeconfig.Config) {
// single mode
cfg.Single = true
// no miner
Expand Down
3 changes: 2 additions & 1 deletion cmd/nodemanager/node_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"gopkg.in/urfave/cli.v1"

"github.com/vitelabs/go-vite/v2/node"
nodeconfig "github.com/vitelabs/go-vite/v2/node/config"
)

type NodeMaker interface {
Expand All @@ -12,5 +13,5 @@ type NodeMaker interface {
MakeNode(ctx *cli.Context) (*node.Node, error)

//create NodeConfig
MakeNodeConfig(ctx *cli.Context) (*node.Config, error)
MakeNodeConfig(ctx *cli.Context) (*nodeconfig.Config, error)
}
7 changes: 7 additions & 0 deletions common/types/enum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

type Enum interface {
name() string
ordinal() int
values() *[]string
}
30 changes: 15 additions & 15 deletions node/config.go → node/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package node
package nodeconfig

import (
"encoding/hex"
Expand Down Expand Up @@ -117,25 +117,25 @@ type Config struct {
InfluxDBHostTag *string `json:"InfluxDBHostTag"`
}

func (c *Config) makeWalletConfig() *config.Wallet {
func (c *Config) MakeWalletConfig() *config.Wallet {
return &config.Wallet{DataDir: c.KeyStoreDir}
}

func (c *Config) makeViteConfig() *config.Config {
func (c *Config) MakeViteConfig() *config.Config {
return &config.Config{
Chain: c.makeChainConfig(),
Producer: c.makeMinerConfig(),
Chain: c.MakeChainConfig(),
Producer: c.MakeMinerConfig(),
DataDir: c.DataDir,
Net: c.makeNetConfig(),
Vm: c.makeVmConfig(),
Subscribe: c.makeSubscribeConfig(),
NodeReward: c.makeRewardConfig(),
Net: c.MakeNetConfig(),
Vm: c.MakeVmConfig(),
Subscribe: c.MakeSubscribeConfig(),
NodeReward: c.MakeRewardConfig(),
Genesis: config.MakeGenesisConfig(c.GenesisFile),
LogLevel: c.LogLevel,
}
}

func (c *Config) makeNetConfig() *config.Net {
func (c *Config) MakeNetConfig() *config.Net {
datadir := filepath.Join(c.DataDir, config.DefaultNetDirName)

return &config.Net{
Expand Down Expand Up @@ -167,14 +167,14 @@ func (c *Config) makeNetConfig() *config.Net {
}
}

func (c *Config) makeRewardConfig() *config.NodeReward {
func (c *Config) MakeRewardConfig() *config.NodeReward {
return &config.NodeReward{
RewardAddr: c.RewardAddr,
Name: c.Identity,
}
}

func (c *Config) makeVmConfig() *config.Vm {
func (c *Config) MakeVmConfig() *config.Vm {
return &config.Vm{
IsVmTest: c.VMTestEnabled,
IsUseVmTestParam: c.VMTestParamEnabled,
Expand All @@ -183,12 +183,12 @@ func (c *Config) makeVmConfig() *config.Vm {
}
}

func (c *Config) makeSubscribeConfig() *config.Subscribe {
func (c *Config) MakeSubscribeConfig() *config.Subscribe {
return &config.Subscribe{
IsSubscribe: c.SubscribeEnabled,
}
}
func (c *Config) makeMinerConfig() *config.Producer {
func (c *Config) MakeMinerConfig() *config.Producer {
cfg := &config.Producer{
Producer: c.MinerEnabled,
Coinbase: c.CoinBase,
Expand All @@ -202,7 +202,7 @@ func (c *Config) makeMinerConfig() *config.Producer {
return cfg
}

func (c *Config) makeChainConfig() *config.Chain {
func (c *Config) MakeChainConfig() *config.Chain {

// is open ledger gc
ledgerGc := true
Expand Down
6 changes: 3 additions & 3 deletions node/config_test.go → node/config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package node
package nodeconfig

import (
"encoding/json"
Expand All @@ -24,7 +24,7 @@ func TestChainConfig(t *testing.T) {
t.Fatal(err)
}

configchain := config.makeChainConfig()
configchain := config.MakeChainConfig()
if len(configchain.VmLogWhiteList) != 1 {
t.Fatal("length must be 1")
}
Expand All @@ -40,7 +40,7 @@ func TestChainConfig(t *testing.T) {
t.Fatal(err)
}

configchainB := configB.makeChainConfig()
configchainB := configB.MakeChainConfig()

if configchainB.VmLogAll != true {
t.Fatal("VmLogAll must be true ")
Expand Down
2 changes: 1 addition & 1 deletion node/defaults.go → node/config/defaults.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package node
package nodeconfig

import (
"os"
Expand Down
11 changes: 6 additions & 5 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/vitelabs/go-vite/v2/common/config"
"github.com/vitelabs/go-vite/v2/log15"
"github.com/vitelabs/go-vite/v2/monitor"
nodeconfig "github.com/vitelabs/go-vite/v2/node/config"
"github.com/vitelabs/go-vite/v2/pow"
"github.com/vitelabs/go-vite/v2/pow/remote"
"github.com/vitelabs/go-vite/v2/rpc"
Expand All @@ -31,7 +32,7 @@ var (

// Node is chain container that manages p2p、rpc、vite modules
type Node struct {
config *Config
config *nodeconfig.Config

//wallet
walletConfig *config.Wallet
Expand Down Expand Up @@ -66,11 +67,11 @@ type Node struct {
instanceDirLock flock.Releaser // prevents concurrent use of instance directory
}

func New(conf *Config) (*Node, error) {
func New(conf *nodeconfig.Config) (*Node, error) {
return &Node{
config: conf,
walletConfig: conf.makeWalletConfig(),
viteConfig: conf.makeViteConfig(),
walletConfig: conf.MakeWalletConfig(),
viteConfig: conf.MakeViteConfig(),
ipcEndpoint: conf.IPCEndpoint(),
httpEndpoint: conf.HTTPEndpoint(),
wsEndpoint: conf.WSEndpoint(),
Expand Down Expand Up @@ -224,7 +225,7 @@ func (node *Node) Vite() *vite.Vite {
return node.viteServer
}

func (node *Node) Config() *Config {
func (node *Node) Config() *nodeconfig.Config {
return node.config
}

Expand Down
25 changes: 24 additions & 1 deletion rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ package rpc
import (
"context"
"fmt"
"github.com/vitelabs/go-vite/v2/rpcapi/api"
"reflect"
"runtime"
"strings"
"sync"
"sync/atomic"

"github.com/vitelabs/go-vite/v2/rpcapi/api"

mapset "github.com/deckarep/golang-set"
log "github.com/vitelabs/go-vite/v2/log15"
)
Expand Down Expand Up @@ -74,6 +75,28 @@ func (s *RPCService) Modules() map[string]string {
return modules
}

// Methods returns the list of RPC services with their method names
func (s *Server) Methods() map[string][]string {
modules := make(map[string][]string)
for svcName, svc := range s.services {
for name := range svc.callbacks {
modules[svcName] = append(modules[svcName], name)
}
}
return modules
}

// Subscriptions returns the list of RPC services with their subscription names
func (s *Server) Subscriptions() map[string][]string {
modules := make(map[string][]string)
for svcName, svc := range s.services {
for name := range svc.subscriptions {
modules[svcName] = append(modules[svcName], name)
}
}
return modules
}

// RegisterName will create chain service for the given rcvr type under the given name. When no methods on the given rcvr
// match the criteria to be either chain RPC method or chain subscription an error is returned. Otherwise chain new service is
// created and added to the service collection this server instance serves.
Expand Down
Loading

0 comments on commit fb34302

Please sign in to comment.