-
Notifications
You must be signed in to change notification settings - Fork 17
/
cmd-rpc-server-filecoin.go
182 lines (163 loc) · 5 KB
/
cmd-rpc-server-filecoin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/rpcpool/yellowstone-faithful/compactindex36"
"github.com/rpcpool/yellowstone-faithful/gsfa"
"github.com/urfave/cli/v2"
)
func newCmd_rpcServerFilecoin() *cli.Command {
var listenOn string
var gsfaOnlySignatures bool
return &cli.Command{
Name: "rpc-server-filecoin",
Description: "Start a Solana JSON RPC that exposes getTransaction and getBlock",
ArgsUsage: "<slot-to-cid-index-filepath-or-url> <sig-to-cid-index-filepath-or-url> <gsfa-index-dir>",
Before: func(c *cli.Context) error {
return nil
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "listen",
Usage: "Listen address",
Value: ":8899",
Destination: &listenOn,
},
&cli.StringFlag{
Name: "config",
Usage: "Load config from file instead of arguments",
Value: "",
},
&cli.BoolFlag{
Name: "gsfa-only-signatures",
Usage: "gSFA: only return signatures",
Value: false,
Destination: &gsfaOnlySignatures,
},
},
Action: func(c *cli.Context) error {
config, err := rpcServerFilecoinLoadConfig(c)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
spew.Dump(config)
if config.Indexes.SlotToCid == "" {
return cli.Exit("Must provide a slot-to-CID index filepath/url", 1)
}
if config.Indexes.SigToCid == "" {
return cli.Exit("Must provide a signature-to-CID index filepath/url", 1)
}
slotToCidIndexFile, err := openIndexStorage(
c.Context,
config.Indexes.SlotToCid,
DebugMode,
)
if err != nil {
return fmt.Errorf("failed to open slot-to-cid index file: %w", err)
}
defer slotToCidIndexFile.Close()
slotToCidIndex, err := compactindex36.Open(slotToCidIndexFile)
if err != nil {
return fmt.Errorf("failed to open slot-to-cid index: %w", err)
}
sigToCidIndexFile, err := openIndexStorage(
c.Context,
config.Indexes.SigToCid,
DebugMode,
)
if err != nil {
return fmt.Errorf("failed to open sig-to-cid index file: %w", err)
}
defer sigToCidIndexFile.Close()
sigToCidIndex, err := compactindex36.Open(sigToCidIndexFile)
if err != nil {
return fmt.Errorf("failed to open sig-to-cid index: %w", err)
}
ls, err := newLassieWrapper(c, globalFetchProviderAddrInfos)
if err != nil {
return fmt.Errorf("newLassieWrapper: %w", err)
}
var gsfaIndex *gsfa.GsfaReader
if config.Indexes.Gsfa != "" {
gsfaIndex, err = gsfa.NewGsfaReader(config.Indexes.Gsfa)
if err != nil {
return fmt.Errorf("failed to open gsfa index: %w", err)
}
defer gsfaIndex.Close()
}
options := &RpcServerOptions{
ListenOn: listenOn,
GsfaOnlySignatures: gsfaOnlySignatures,
}
return createAndStartRPCServer_lassie(
c.Context,
options,
ls,
slotToCidIndex,
sigToCidIndex,
gsfaIndex,
)
},
}
}
func rpcServerFilecoinLoadConfig(c *cli.Context) (*RpcServerFilecoinConfig, error) {
// Either load from config file or from args:
cfg := &RpcServerFilecoinConfig{}
if slotToCidIndexFilepath := c.Args().Get(0); slotToCidIndexFilepath != "" {
cfg.Indexes.SlotToCid = slotToCidIndexFilepath
}
if sigToCidIndexFilepath := c.Args().Get(1); sigToCidIndexFilepath != "" {
cfg.Indexes.SigToCid = sigToCidIndexFilepath
}
if gsfaIndexDir := c.Args().Get(2); gsfaIndexDir != "" {
cfg.Indexes.Gsfa = gsfaIndexDir
}
// if a file is specified, load it:
if configFilepath := c.String("config"); configFilepath != "" {
configFromFile, err := loadRpcServerFilecoinConfig(configFilepath)
if err != nil {
return nil, fmt.Errorf("failed to load config: %w", err)
}
if cfg.Indexes.SlotToCid == "" {
cfg.Indexes.SlotToCid = configFromFile.Indexes.SlotToCid
}
if cfg.Indexes.SigToCid == "" {
cfg.Indexes.SigToCid = configFromFile.Indexes.SigToCid
}
if cfg.Indexes.Gsfa == "" {
cfg.Indexes.Gsfa = configFromFile.Indexes.Gsfa
}
}
return cfg, nil
}
func loadRpcServerFilecoinConfig(configFilepath string) (*RpcServerFilecoinConfig, error) {
cfg := &RpcServerFilecoinConfig{}
err := cfg.load(configFilepath)
if err != nil {
return nil, fmt.Errorf("failed to load config: %w", err)
}
return cfg, nil
}
func (cfg *RpcServerFilecoinConfig) load(configFilepath string) error {
// if is json, load from json:
if isJSONFile(configFilepath) {
return cfg.loadFromJSON(configFilepath)
}
if isYAMLFile(configFilepath) {
return cfg.loadFromYAML(configFilepath)
}
return fmt.Errorf("unknown file type for config: %s", configFilepath)
}
func (cfg *RpcServerFilecoinConfig) loadFromJSON(configFilepath string) error {
return loadFromJSON(configFilepath, cfg)
}
func (cfg *RpcServerFilecoinConfig) loadFromYAML(configFilepath string) error {
return loadFromYAML(configFilepath, cfg)
}
type RpcServerFilecoinConfig struct {
Indexes struct {
SlotToCid string `json:"slot_to_cid" yaml:"slot_to_cid"`
SigToCid string `json:"sig_to_cid" yaml:"sig_to_cid"`
Gsfa string `json:"gsfa" yaml:"gsfa"`
} `json:"indexes" yaml:"indexes"`
}