Skip to content

Commit fb34302

Browse files
committed
refactor: make rpcapi names enumerable
1 parent 13f5fcd commit fb34302

File tree

12 files changed

+281
-64
lines changed

12 files changed

+281
-64
lines changed

cmd/nodemanager/full_node_maker.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/vitelabs/go-vite/v2/common"
1212
"github.com/vitelabs/go-vite/v2/log15"
1313
"github.com/vitelabs/go-vite/v2/node"
14+
nodeconfig "github.com/vitelabs/go-vite/v2/node/config"
1415
)
1516

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

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

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

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

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

168-
func overrideNodeConfigs(ctx *cli.Context, cfg *node.Config) {
169+
func overrideNodeConfigs(ctx *cli.Context, cfg *nodeconfig.Config) {
169170

170171
if len(cfg.DataDir) == 0 || cfg.DataDir == "" {
171172
cfg.DataDir = common.DefaultDataDir()
@@ -216,7 +217,7 @@ func overrideNodeConfigs(ctx *cli.Context, cfg *node.Config) {
216217
}
217218
}
218219

219-
func loadNodeConfigFromFile(ctx *cli.Context, cfg *node.Config) error {
220+
func loadNodeConfigFromFile(ctx *cli.Context, cfg *nodeconfig.Config) error {
220221

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

239-
func makeRunLogFile(cfg *node.Config) {
240+
func makeRunLogFile(cfg *nodeconfig.Config) {
240241
defaultHandler := common.LogHandler(cfg.RunLogDir(), "", "vite.log", cfg.LogLevel)
241242
errorHandler := common.LogHandler(cfg.RunLogDir(), "error", "vite.error.log", log15.LvlError.String())
242243

cmd/nodemanager/local_node_maker.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/vitelabs/go-vite/v2/common"
99
"github.com/vitelabs/go-vite/v2/node"
10+
nodeconfig "github.com/vitelabs/go-vite/v2/node/config"
1011
)
1112

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

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

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

62-
func makeLocalNodeCfg(ctx *cli.Context, cfg *node.Config) {
63+
func makeLocalNodeCfg(ctx *cli.Context, cfg *nodeconfig.Config) {
6364
// single mode
6465
cfg.Single = true
6566
// no miner

cmd/nodemanager/node_maker.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"gopkg.in/urfave/cli.v1"
55

66
"github.com/vitelabs/go-vite/v2/node"
7+
nodeconfig "github.com/vitelabs/go-vite/v2/node/config"
78
)
89

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

1415
//create NodeConfig
15-
MakeNodeConfig(ctx *cli.Context) (*node.Config, error)
16+
MakeNodeConfig(ctx *cli.Context) (*nodeconfig.Config, error)
1617
}

common/types/enum.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package types
2+
3+
type Enum interface {
4+
name() string
5+
ordinal() int
6+
values() *[]string
7+
}

node/config.go renamed to node/config/config.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package node
1+
package nodeconfig
22

33
import (
44
"encoding/hex"
@@ -117,25 +117,25 @@ type Config struct {
117117
InfluxDBHostTag *string `json:"InfluxDBHostTag"`
118118
}
119119

120-
func (c *Config) makeWalletConfig() *config.Wallet {
120+
func (c *Config) MakeWalletConfig() *config.Wallet {
121121
return &config.Wallet{DataDir: c.KeyStoreDir}
122122
}
123123

124-
func (c *Config) makeViteConfig() *config.Config {
124+
func (c *Config) MakeViteConfig() *config.Config {
125125
return &config.Config{
126-
Chain: c.makeChainConfig(),
127-
Producer: c.makeMinerConfig(),
126+
Chain: c.MakeChainConfig(),
127+
Producer: c.MakeMinerConfig(),
128128
DataDir: c.DataDir,
129-
Net: c.makeNetConfig(),
130-
Vm: c.makeVmConfig(),
131-
Subscribe: c.makeSubscribeConfig(),
132-
NodeReward: c.makeRewardConfig(),
129+
Net: c.MakeNetConfig(),
130+
Vm: c.MakeVmConfig(),
131+
Subscribe: c.MakeSubscribeConfig(),
132+
NodeReward: c.MakeRewardConfig(),
133133
Genesis: config.MakeGenesisConfig(c.GenesisFile),
134134
LogLevel: c.LogLevel,
135135
}
136136
}
137137

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

141141
return &config.Net{
@@ -167,14 +167,14 @@ func (c *Config) makeNetConfig() *config.Net {
167167
}
168168
}
169169

170-
func (c *Config) makeRewardConfig() *config.NodeReward {
170+
func (c *Config) MakeRewardConfig() *config.NodeReward {
171171
return &config.NodeReward{
172172
RewardAddr: c.RewardAddr,
173173
Name: c.Identity,
174174
}
175175
}
176176

177-
func (c *Config) makeVmConfig() *config.Vm {
177+
func (c *Config) MakeVmConfig() *config.Vm {
178178
return &config.Vm{
179179
IsVmTest: c.VMTestEnabled,
180180
IsUseVmTestParam: c.VMTestParamEnabled,
@@ -183,12 +183,12 @@ func (c *Config) makeVmConfig() *config.Vm {
183183
}
184184
}
185185

186-
func (c *Config) makeSubscribeConfig() *config.Subscribe {
186+
func (c *Config) MakeSubscribeConfig() *config.Subscribe {
187187
return &config.Subscribe{
188188
IsSubscribe: c.SubscribeEnabled,
189189
}
190190
}
191-
func (c *Config) makeMinerConfig() *config.Producer {
191+
func (c *Config) MakeMinerConfig() *config.Producer {
192192
cfg := &config.Producer{
193193
Producer: c.MinerEnabled,
194194
Coinbase: c.CoinBase,
@@ -202,7 +202,7 @@ func (c *Config) makeMinerConfig() *config.Producer {
202202
return cfg
203203
}
204204

205-
func (c *Config) makeChainConfig() *config.Chain {
205+
func (c *Config) MakeChainConfig() *config.Chain {
206206

207207
// is open ledger gc
208208
ledgerGc := true

node/config_test.go renamed to node/config/config_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package node
1+
package nodeconfig
22

33
import (
44
"encoding/json"
@@ -24,7 +24,7 @@ func TestChainConfig(t *testing.T) {
2424
t.Fatal(err)
2525
}
2626

27-
configchain := config.makeChainConfig()
27+
configchain := config.MakeChainConfig()
2828
if len(configchain.VmLogWhiteList) != 1 {
2929
t.Fatal("length must be 1")
3030
}
@@ -40,7 +40,7 @@ func TestChainConfig(t *testing.T) {
4040
t.Fatal(err)
4141
}
4242

43-
configchainB := configB.makeChainConfig()
43+
configchainB := configB.MakeChainConfig()
4444

4545
if configchainB.VmLogAll != true {
4646
t.Fatal("VmLogAll must be true ")

node/defaults.go renamed to node/config/defaults.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package node
1+
package nodeconfig
22

33
import (
44
"os"

node/node.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/vitelabs/go-vite/v2/common/config"
1818
"github.com/vitelabs/go-vite/v2/log15"
1919
"github.com/vitelabs/go-vite/v2/monitor"
20+
nodeconfig "github.com/vitelabs/go-vite/v2/node/config"
2021
"github.com/vitelabs/go-vite/v2/pow"
2122
"github.com/vitelabs/go-vite/v2/pow/remote"
2223
"github.com/vitelabs/go-vite/v2/rpc"
@@ -31,7 +32,7 @@ var (
3132

3233
// Node is chain container that manages p2p、rpc、vite modules
3334
type Node struct {
34-
config *Config
35+
config *nodeconfig.Config
3536

3637
//wallet
3738
walletConfig *config.Wallet
@@ -66,11 +67,11 @@ type Node struct {
6667
instanceDirLock flock.Releaser // prevents concurrent use of instance directory
6768
}
6869

69-
func New(conf *Config) (*Node, error) {
70+
func New(conf *nodeconfig.Config) (*Node, error) {
7071
return &Node{
7172
config: conf,
72-
walletConfig: conf.makeWalletConfig(),
73-
viteConfig: conf.makeViteConfig(),
73+
walletConfig: conf.MakeWalletConfig(),
74+
viteConfig: conf.MakeViteConfig(),
7475
ipcEndpoint: conf.IPCEndpoint(),
7576
httpEndpoint: conf.HTTPEndpoint(),
7677
wsEndpoint: conf.WSEndpoint(),
@@ -224,7 +225,7 @@ func (node *Node) Vite() *vite.Vite {
224225
return node.viteServer
225226
}
226227

227-
func (node *Node) Config() *Config {
228+
func (node *Node) Config() *nodeconfig.Config {
228229
return node.config
229230
}
230231

rpc/server.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ package rpc
1919
import (
2020
"context"
2121
"fmt"
22-
"github.com/vitelabs/go-vite/v2/rpcapi/api"
2322
"reflect"
2423
"runtime"
2524
"strings"
2625
"sync"
2726
"sync/atomic"
2827

28+
"github.com/vitelabs/go-vite/v2/rpcapi/api"
29+
2930
mapset "github.com/deckarep/golang-set"
3031
log "github.com/vitelabs/go-vite/v2/log15"
3132
)
@@ -74,6 +75,28 @@ func (s *RPCService) Modules() map[string]string {
7475
return modules
7576
}
7677

78+
// Methods returns the list of RPC services with their method names
79+
func (s *Server) Methods() map[string][]string {
80+
modules := make(map[string][]string)
81+
for svcName, svc := range s.services {
82+
for name := range svc.callbacks {
83+
modules[svcName] = append(modules[svcName], name)
84+
}
85+
}
86+
return modules
87+
}
88+
89+
// Subscriptions returns the list of RPC services with their subscription names
90+
func (s *Server) Subscriptions() map[string][]string {
91+
modules := make(map[string][]string)
92+
for svcName, svc := range s.services {
93+
for name := range svc.subscriptions {
94+
modules[svcName] = append(modules[svcName], name)
95+
}
96+
}
97+
return modules
98+
}
99+
77100
// RegisterName will create chain service for the given rcvr type under the given name. When no methods on the given rcvr
78101
// match the criteria to be either chain RPC method or chain subscription an error is returned. Otherwise chain new service is
79102
// created and added to the service collection this server instance serves.

0 commit comments

Comments
 (0)