forked from statechannels/go-nitro
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
272 lines (250 loc) · 8.44 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package main
import (
"crypto/tls"
"log"
"log/slog"
"os"
"os/signal"
"strings"
"syscall"
"github.com/ethereum/go-ethereum/common"
"github.com/statechannels/go-nitro/internal/logging"
"github.com/statechannels/go-nitro/internal/node"
"github.com/statechannels/go-nitro/internal/rpc"
"github.com/statechannels/go-nitro/node/engine/chainservice"
p2pms "github.com/statechannels/go-nitro/node/engine/messageservice/p2p-message-service"
"github.com/statechannels/go-nitro/node/engine/store"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v2/altsrc"
)
func main() {
const (
CONFIG = "config"
// Connectivity
CONNECTIVITY_CATEGORY = "Connectivity:"
USE_NATS = "usenats"
CHAIN_URL = "chainurl"
CHAIN_START_BLOCK = "chainstartblock"
CHAIN_AUTH_TOKEN = "chainauthtoken"
NA_ADDRESS = "naaddress"
VPA_ADDRESS = "vpaaddress"
CA_ADDRESS = "caaddress"
PUBLIC_IP = "publicip"
MSG_PORT = "msgport"
RPC_PORT = "rpcport"
GUI_PORT = "guiport"
BOOT_PEERS = "bootpeers"
// Keys
KEYS_CATEGORY = "Keys:"
PK = "pk"
CHAIN_PK = "chainpk"
// Storage
STORAGE_CATEGORY = "Storage:"
USE_DURABLE_STORE = "usedurablestore"
DURABLE_STORE_FOLDER = "durablestorefolder"
// TLS
TLS_CATEGORY = "TLS:"
TLS_CERT_FILEPATH = "tlscertfilepath"
TLS_KEY_FILEPATH = "tlskeyfilepath"
)
var pkString, chainUrl, chainAuthToken, naAddress, vpaAddress, caAddress, chainPk, durableStoreFolder, bootPeers, publicIp string
var msgPort, rpcPort, guiPort int
var chainStartBlock uint64
var useNats, useDurableStore bool
var tlsCertFilepath, tlsKeyFilepath string
// urfave default precedence for flag value sources (highest to lowest):
// 1. Command line flag value
// 2. Environment variable (if specified)
// 3. Configuration file (if specified)
// 4. Default defined on the flag
flags := []cli.Flag{
&cli.StringFlag{
Name: CONFIG,
Usage: "Load config options from `config.toml`",
EnvVars: []string{"NITRO_CONFIG_PATH"},
},
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: USE_NATS,
Usage: "Specifies whether to use NATS or http/ws for the rpc server.",
Value: false,
Category: CONNECTIVITY_CATEGORY,
Destination: &useNats,
}),
altsrc.NewBoolFlag(&cli.BoolFlag{
Name: USE_DURABLE_STORE,
Usage: "Specifies whether to use a durable store or an in-memory store.",
Category: STORAGE_CATEGORY,
Value: false,
Destination: &useDurableStore,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: PK,
Usage: "Specifies the private key used by the nitro node.",
Category: KEYS_CATEGORY,
Destination: &pkString,
EnvVars: []string{"SC_PK"},
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: CHAIN_URL,
Usage: "Specifies the url of a RPC endpoint for the chain.",
Value: "ws://127.0.0.1:8545",
DefaultText: "hardhat / anvil default",
Category: CONNECTIVITY_CATEGORY,
Destination: &chainUrl,
EnvVars: []string{"CHAIN_URL"},
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: CHAIN_AUTH_TOKEN,
Usage: "The bearer token used for auth when making requests to the chain's RPC endpoint.",
Category: CONNECTIVITY_CATEGORY,
Destination: &chainAuthToken,
EnvVars: []string{"CHAIN_AUTH_TOKEN"},
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: CHAIN_PK,
Usage: "Specifies the private key to use when interacting with the chain.",
Category: KEYS_CATEGORY,
Destination: &chainPk,
EnvVars: []string{"CHAIN_PK"},
}),
altsrc.NewUint64Flag(&cli.Uint64Flag{
Name: CHAIN_START_BLOCK,
Usage: "Specifies the block number to start looking for nitro adjudicator events.",
Value: 0,
Category: CONNECTIVITY_CATEGORY,
Destination: &chainStartBlock,
EnvVars: []string{"CHAIN_START_BLOCK"},
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: NA_ADDRESS,
Usage: "Specifies the address of the nitro adjudicator contract.",
Category: CONNECTIVITY_CATEGORY,
Destination: &naAddress,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: VPA_ADDRESS,
Usage: "Specifies the address of the virtual payment app.",
Category: CONNECTIVITY_CATEGORY,
Destination: &vpaAddress,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: CA_ADDRESS,
Usage: "Specifies the address of the consensus app.",
Category: CONNECTIVITY_CATEGORY,
Destination: &caAddress,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: PUBLIC_IP,
Usage: "Specifies the public ip used for the message service.",
Value: "127.0.0.1",
Category: CONNECTIVITY_CATEGORY,
Destination: &publicIp,
EnvVars: []string{"NITRO_PUBLIC_IP"},
}),
altsrc.NewIntFlag(&cli.IntFlag{
Name: MSG_PORT,
Usage: "Specifies the tcp port for the message service.",
Value: 3005,
Category: CONNECTIVITY_CATEGORY,
Destination: &msgPort,
}),
altsrc.NewIntFlag(&cli.IntFlag{
Name: RPC_PORT,
Usage: "Specifies the tcp port for the rpc server.",
Value: 4005,
Category: CONNECTIVITY_CATEGORY,
Destination: &rpcPort,
}),
altsrc.NewIntFlag(&cli.IntFlag{
Name: GUI_PORT,
Usage: "Specifies the tcp port for the Nitro Connect GUI.",
Value: 5005,
Category: CONNECTIVITY_CATEGORY,
Destination: &guiPort,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: DURABLE_STORE_FOLDER,
Usage: "Specifies the folder for the durable store data storage.",
Category: STORAGE_CATEGORY,
Destination: &durableStoreFolder,
Value: "./data/nitro-store",
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: BOOT_PEERS,
Usage: "Comma-delimited list of peer multiaddrs the messaging service will connect to when initialized.",
Value: "",
Category: CONNECTIVITY_CATEGORY,
Destination: &bootPeers,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: TLS_CERT_FILEPATH,
Usage: "Filepath to the TLS certificate. If not specified, TLS will not be used with the RPC transport.",
Value: "./tls/statechannels.org.pem",
Category: TLS_CATEGORY,
Destination: &tlsCertFilepath,
}),
altsrc.NewStringFlag(&cli.StringFlag{
Name: TLS_KEY_FILEPATH,
Usage: "Filepath to the TLS private key. If not specified, TLS will not be used with the RPC transport.",
Value: "./tls/statechannels.org_key.pem",
Category: TLS_CATEGORY,
Destination: &tlsKeyFilepath,
}),
}
app := &cli.App{
Name: "go-nitro",
Usage: "Nitro as a service. State channel node with RPC server.",
Flags: flags,
Before: altsrc.InitInputSourceWithContext(flags, altsrc.NewTomlSourceFromFlagFunc(CONFIG)),
Action: func(cCtx *cli.Context) error {
chainOpts := chainservice.ChainOpts{
ChainUrl: chainUrl,
ChainStartBlock: chainStartBlock,
ChainAuthToken: chainAuthToken,
ChainPk: chainPk,
NaAddress: common.HexToAddress(naAddress),
VpaAddress: common.HexToAddress(vpaAddress),
CaAddress: common.HexToAddress(caAddress),
}
storeOpts := store.StoreOpts{
PkBytes: common.Hex2Bytes(pkString),
UseDurableStore: useDurableStore,
DurableStoreFolder: durableStoreFolder,
}
var peerSlice []string
if bootPeers != "" {
peerSlice = strings.Split(bootPeers, ",")
}
messageOpts := p2pms.MessageOpts{
PkBytes: common.Hex2Bytes(pkString),
Port: msgPort,
BootPeers: peerSlice,
PublicIp: publicIp,
}
logging.SetupDefaultLogger(os.Stdout, slog.LevelDebug)
node, _, _, _, err := node.InitializeNode(chainOpts, storeOpts, messageOpts)
if err != nil {
return err
}
var cert tls.Certificate
if tlsCertFilepath != "" && tlsKeyFilepath != "" {
cert, err = tls.LoadX509KeyPair(tlsCertFilepath, tlsKeyFilepath)
if err != nil {
panic(err)
}
}
rpcServer, err := rpc.InitializeRpcServer(node, rpcPort, useNats, &cert)
if err != nil {
return err
}
hostNitroUI(uint(guiPort), uint(rpcPort))
stopChan := make(chan os.Signal, 2)
signal.Notify(stopChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
<-stopChan // wait for interrupt or terminate signal
return rpcServer.Close()
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}