Skip to content

Commit 0975bb8

Browse files
authored
feat: Consolidate node options (#4501)
## Relevant issue(s) Resolves #4154 ## Description Convert node options to follow new mongo-like options style.
1 parent b471703 commit 0975bb8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1355
-1017
lines changed

cbindings/node.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ import (
2222
"strconv"
2323
"time"
2424

25-
"github.com/sourcenetwork/go-p2p"
26-
27-
"github.com/sourcenetwork/defradb/internal/db"
25+
"github.com/sourcenetwork/defradb/client/options"
2826
"github.com/sourcenetwork/defradb/node"
2927
)
3028

@@ -40,42 +38,40 @@ func NewNode(cOptions C.NodeInitOptions) C.NewNodeResult {
4038

4139
ctx := context.Background()
4240

43-
opts := []node.Option{
44-
db.WithLensRuntime(db.Wazero),
45-
}
41+
opts := options.Node()
42+
opts.DB().SetLensRuntime(options.NodeWASMLensRuntime)
43+
4644
if gocOptions.DbPath != "" {
47-
opts = append(opts, node.WithStorePath(gocOptions.DbPath))
45+
opts.Store().SetPath(gocOptions.DbPath)
4846
}
4947
if len(listeningAddresses) > 0 {
50-
opts = append(opts, p2p.WithListenAddresses(listeningAddresses...))
48+
opts.P2P().SetListenAddresses(listeningAddresses...)
5149
}
5250
maxTxnRetries := gocOptions.MaxTransactionRetries
5351
if maxTxnRetries > 0 {
54-
opts = append(opts, db.WithMaxRetries(maxTxnRetries))
52+
opts.DB().SetMaxTxnRetries(maxTxnRetries)
5553
}
5654
disableP2PFlag := gocOptions.DisableP2P != 0
5755
if disableP2PFlag {
58-
opts = append(opts, node.WithDisableP2P(true))
56+
opts.SetDisableP2P(true)
5957
}
6058
disableAPIFlag := gocOptions.DisableAPI != 0
6159
if disableAPIFlag {
62-
opts = append(opts, node.WithDisableAPI(true))
60+
opts.SetDisableAPI(true)
6361
}
6462
if inMemoryFlag {
65-
opts = append(opts, node.WithBadgerInMemory(true))
63+
opts.Store().SetBadgerInMemory(true)
6664
}
6765
peers := splitCommaSeparatedString(gocOptions.Peers)
6866
if len(peers) > 0 {
69-
opts = append(opts, p2p.WithBootstrapPeers(peers...))
67+
opts.P2P().SetBootstrapPeers(peers...)
7068
}
7169
if gocOptions.Identity != nil {
72-
opts = append(opts, db.WithNodeIdentity(gocOptions.Identity))
70+
opts.DB().SetNodeIdentity(gocOptions.Identity)
7371
}
7472
if gocOptions.EnableNodeACP != 0 {
75-
opts = append(opts, node.WithEnableNodeACP(true))
73+
opts.NodeACP().SetEnabled(true)
7674
}
77-
opts = append(opts, node.WithDocumentACPPath(""))
78-
opts = append(opts, node.WithNodeACPPath(""))
7975

8076
// Configure the replicator retry times. Go from string slice -> time.Duration slice
8177
replicatorRetryTimes := splitCommaSeparatedString(gocOptions.ReplicatorRetryIntervals)
@@ -91,10 +87,10 @@ func NewNode(cOptions C.NodeInitOptions) C.NewNodeResult {
9187
replicatorRetryIntervals = append(replicatorRetryIntervals, time.Duration(n)*time.Second)
9288
}
9389
if len(replicatorRetryIntervals) > 0 {
94-
opts = append(opts, db.WithRetryInterval(replicatorRetryIntervals))
90+
opts.DB().SetRetryIntervals(replicatorRetryIntervals)
9591
}
9692

97-
n, err := node.New(ctx, opts...)
93+
n, err := node.New(ctx, opts)
9894
if err != nil {
9995
return returnNewNodeResultC(1, err.Error(), nil)
10096
}

cli/server_dump.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ package cli
1313
import (
1414
"github.com/spf13/cobra"
1515

16-
"github.com/sourcenetwork/defradb/acp/dac"
1716
"github.com/sourcenetwork/defradb/cli/config"
17+
"github.com/sourcenetwork/defradb/client/options"
1818
"github.com/sourcenetwork/defradb/errors"
1919
"github.com/sourcenetwork/defradb/internal/db"
2020
acpDB "github.com/sourcenetwork/defradb/internal/db/acp"
@@ -34,10 +34,7 @@ func MakeServerDumpCmd() *cobra.Command {
3434
return errors.New("server-side dump is only supported for the Badger datastore")
3535
}
3636
badgerPath := cfg.GetString("datastore.badger.path")
37-
storeOpts := []node.StoreOpt{
38-
node.WithStorePath(badgerPath),
39-
}
40-
rootstore, _, err := node.NewStore(ctx, storeOpts...)
37+
rootstore, _, err := node.NewStore(ctx, options.NodeStore().SetPath(badgerPath))
4138
if err != nil {
4239
return err
4340
}
@@ -49,7 +46,6 @@ func MakeServerDumpCmd() *cobra.Command {
4946
ctx,
5047
rootstore,
5148
nacInfo,
52-
dac.NoDocumentACP,
5349
)
5450
if err != nil {
5551
return errors.Wrap("failed to initialize database", err)

cli/start.go

Lines changed: 58 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ import (
2121
"github.com/spf13/cobra"
2222
"github.com/spf13/viper"
2323

24-
"github.com/sourcenetwork/go-p2p"
25-
"github.com/sourcenetwork/immutable"
26-
2724
"github.com/sourcenetwork/defradb/acp/identity"
2825
"github.com/sourcenetwork/defradb/cli/config"
2926
"github.com/sourcenetwork/defradb/client"
27+
"github.com/sourcenetwork/defradb/client/options"
3028
"github.com/sourcenetwork/defradb/crypto"
3129
"github.com/sourcenetwork/defradb/errors"
3230
"github.com/sourcenetwork/defradb/event"
3331
"github.com/sourcenetwork/defradb/http"
34-
"github.com/sourcenetwork/defradb/internal/db"
3532
"github.com/sourcenetwork/defradb/internal/telemetry"
3633
"github.com/sourcenetwork/defradb/keyring"
3734
"github.com/sourcenetwork/defradb/node"
@@ -51,8 +48,8 @@ const devModeBanner = `
5148

5249
const developmentDescription = `Enables a set of features that make development easier but should not be enabled ` +
5350
`in production:
54-
- allows purging of all persisted data
55-
- generates temporary node identity if keyring is disabled`
51+
- allows purging of all persisted data
52+
- generates temporary node identity if one doesn't exist in the keyring`
5653

5754
func MakeStartCommand(ctx context.Context) *cobra.Command {
5855
var identity string
@@ -90,40 +87,39 @@ func MakeStartCommand(ctx context.Context) *cobra.Command {
9087
replicatorRetryIntervals = append(replicatorRetryIntervals, time.Duration(interval)*time.Second)
9188
}
9289

93-
opts := []node.Option{
94-
node.WithEnableNodeACP(enableNAC),
95-
node.WithDisableP2P(cfg.GetBool("net.p2pDisabled")),
96-
node.WithSourceHubChainID(cfg.GetString("acp.document.sourceHub.ChainID")),
97-
node.WithSourceHubGRPCAddress(cfg.GetString("acp.document.sourceHub.GRPCAddress")),
98-
node.WithSourceHubCometRPCAddress(cfg.GetString("acp.document.sourceHub.CometRPCAddress")),
99-
node.WithEnableDevelopment(cfg.GetBool("development")),
100-
// store options
101-
node.WithStorePath(cfg.GetString("datastore.badger.path")),
102-
node.WithBadgerInMemory(cfg.GetString("datastore.store") == config.ConfigStoreMemory),
103-
// db options
104-
db.WithMaxRetries(cfg.GetInt("datastore.MaxTxnRetries")),
105-
db.WithRetryInterval(replicatorRetryIntervals),
106-
db.WithLensRuntime(db.LensRuntimeType(cfg.GetString("lens.runtime"))),
107-
// net node options
108-
p2p.WithListenAddresses(cfg.GetStringSlice("net.p2pAddresses")...),
109-
p2p.WithEnablePubSub(cfg.GetBool("net.pubSubEnabled")),
110-
p2p.WithEnableRelay(cfg.GetBool("net.relayEnabled")),
111-
p2p.WithBootstrapPeers(cfg.GetStringSlice("net.peers")...),
112-
113-
// http server options
114-
http.WithAddress(cfg.GetString("api.address")),
115-
http.WithAllowedOrigins(cfg.GetStringSlice("api.allowed-origins")...),
116-
http.WithTLSCertPath(cfg.GetString("api.pubKeyPath")),
117-
http.WithTLSKeyPath(cfg.GetString("api.privKeyPath")),
118-
}
119-
120-
if cfg.GetString("datastore.store") != config.ConfigStoreMemory {
90+
inMem := cfg.GetString("datastore.store") == config.ConfigStoreMemory
91+
92+
opts := options.Node().
93+
SetEnableDevelopment(cfg.GetBool("development")).
94+
SetDisableP2P(cfg.GetBool("net.p2pDisabled"))
95+
opts.Store().
96+
SetPath(cfg.GetString("datastore.badger.path")).
97+
SetBadgerInMemory(inMem)
98+
opts.DB().
99+
SetMaxTxnRetries(cfg.GetInt("datastore.MaxTxnRetries")).
100+
SetRetryIntervals(replicatorRetryIntervals).
101+
SetLensRuntime(options.NodeLensRuntimeType(cfg.GetString("lens.runtime")))
102+
opts.P2P().
103+
SetListenAddresses(cfg.GetStringSlice("net.p2pAddresses")...).
104+
SetEnablePubSub(cfg.GetBool("net.pubSubEnabled")).
105+
SetEnableRelay(cfg.GetBool("net.relayEnabled")).
106+
SetBootstrapPeers(cfg.GetStringSlice("net.peers")...)
107+
opts.HTTP().
108+
SetAddress(cfg.GetString("api.address")).
109+
SetAllowedOrigins(cfg.GetStringSlice("api.allowed-origins")...).
110+
SetCertPath(cfg.GetString("api.pubKeyPath")).
111+
SetKeyPath(cfg.GetString("api.privKeyPath"))
112+
opts.DocumentACP().
113+
SetChainID(cfg.GetString("acp.document.sourceHub.ChainID")).
114+
SetGRPCAddress(cfg.GetString("acp.document.sourceHub.GRPCAddress")).
115+
SetCometRPCAddress(cfg.GetString("acp.document.sourceHub.CometRPCAddress"))
116+
opts.NodeACP().
117+
SetEnabled(enableNAC)
118+
119+
if !inMem {
121120
rootDir := mustGetContextRootDir(cmd)
122-
// TODO-ACP: Infuture when we add support for the --no-acp flag when node acp is implemented,
123-
// we can allow starting of db without acp. Currently that can only be done programmatically.
124-
// https://github.com/sourcenetwork/defradb/issues/2271
125-
opts = append(opts, node.WithDocumentACPPath(rootDir))
126-
opts = append(opts, node.WithNodeACPPath(rootDir))
121+
opts.DocumentACP().SetPath(rootDir).
122+
NodeACP().SetPath(rootDir)
127123
}
128124

129125
if enableNAC && identity == "" {
@@ -132,37 +128,41 @@ func MakeStartCommand(ctx context.Context) *cobra.Command {
132128

133129
documentACPType := cfg.GetString("acp.document.type")
134130
if documentACPType != "" {
135-
opts = append(opts, node.WithDocumentACPType(node.DocumentACPType(documentACPType)))
131+
opts.DocumentACP().SetType(options.NodeDocumentACPType(documentACPType))
136132
}
137133

138134
if !cfg.GetBool("keyring.disabled") {
139135
kr, err := openKeyring(cmd)
140136
if err != nil {
141137
return err
142138
}
143-
opts, err = getOrCreatePeerKey(kr, opts)
139+
peerKey, err := getOrCreatePeerKey(kr)
144140
if err != nil {
145141
return err
146142
}
143+
opts.P2P().SetPrivateKey(peerKey)
147144

148145
if !cfg.GetBool("datastore.noencryption") {
149-
opts, err = getOrCreateEncryptionKey(kr, opts)
146+
encKey, err := getOrCreateEncryptionKey(kr)
150147
if err != nil {
151148
return err
152149
}
150+
opts.Store().SetBadgerEncryptionKey(encKey)
153151
}
154152

155153
if !cfg.GetBool("datastore.nosearchableencryption") {
156-
opts, err = getOrCreateSearchableEncryptionKey(kr, opts)
154+
seKey, err := getOrCreateSearchableEncryptionKey(kr)
157155
if err != nil {
158156
return err
159157
}
158+
opts.DB().SetSearchableEncryptionKey(seKey)
160159
}
161160

162-
opts, err = getOrCreateIdentity(kr, opts, cfg)
161+
ident, err := getOrCreateIdentity(kr, cfg)
163162
if err != nil {
164163
return err
165164
}
165+
opts.DB().SetNodeIdentity(ident)
166166

167167
// setup the sourcehub transaction signer
168168
sourceHubKeyName := cfg.GetString("acp.document.sourceHub.KeyName")
@@ -171,26 +171,23 @@ func MakeStartCommand(ctx context.Context) *cobra.Command {
171171
if err != nil {
172172
return err
173173
}
174-
opts = append(opts, node.WithTxnSigner(immutable.Some[node.TxSigner](signer)))
174+
opts.DocumentACP().SetTxnSigner(signer)
175175
}
176176
}
177177

178-
opts = append(opts, db.WithEnabledSigning(!cfg.GetBool("datastore.nosigning")))
178+
opts.DB().SetEnableSigning(!cfg.GetBool("datastore.nosigning"))
179179

180180
isDevMode := cfg.GetBool("development")
181181
http.IsDevMode = isDevMode
182182
if isDevMode {
183183
cmd.Printf(devModeBanner)
184184
if cfg.GetBool("keyring.disabled") {
185-
var err error
186185
// Generate an ephemeral identity for the node
187-
// TODO: we want to persist this identity so we can restart the node with the same identity
188-
// even in development mode. https://github.com/sourcenetwork/defradb/issues/3148
189186
ident, err := generateIdentity(cfg.GetString("datastore.defaultkeytype"))
190187
if err != nil {
191188
return err
192189
}
193-
opts = append(opts, db.WithNodeIdentity(ident))
190+
opts.DB().SetNodeIdentity(ident)
194191
}
195192
}
196193

@@ -208,7 +205,7 @@ func MakeStartCommand(ctx context.Context) *cobra.Command {
208205
signalCh := make(chan os.Signal, 1)
209206
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM)
210207

211-
n, err := node.New(cmd.Context(), opts...)
208+
n, err := node.New(cmd.Context(), opts)
212209
if err != nil {
213210
return err
214211
}
@@ -358,7 +355,7 @@ func MakeStartCommand(ctx context.Context) *cobra.Command {
358355
return cmd
359356
}
360357

361-
func getOrCreateEncryptionKey(kr keyring.Keyring, opts []node.Option) ([]node.Option, error) {
358+
func getOrCreateEncryptionKey(kr keyring.Keyring) ([]byte, error) {
362359
encryptionKey, err := kr.Get(encryptionKeyName)
363360
if err != nil {
364361
if !errors.Is(err, keyring.ErrNotFound) {
@@ -374,13 +371,12 @@ func getOrCreateEncryptionKey(kr keyring.Keyring, opts []node.Option) ([]node.Op
374371
}
375372
log.Info("generated encryption key")
376373
}
377-
opts = append(opts, node.WithBadgerEncryptionKey(encryptionKey))
378-
return opts, nil
374+
return encryptionKey, nil
379375
}
380376

381377
// getOrCreateSearchableEncryptionKey generates or retrieves the searchable encryption key
382-
// from the keyring and adds it to the node options.
383-
func getOrCreateSearchableEncryptionKey(kr keyring.Keyring, opts []node.Option) ([]node.Option, error) {
378+
// from the keyring.
379+
func getOrCreateSearchableEncryptionKey(kr keyring.Keyring) ([]byte, error) {
384380
seKey, err := kr.Get(searchableEncryptionKeyName)
385381
if err != nil {
386382
if !errors.Is(err, keyring.ErrNotFound) {
@@ -396,13 +392,10 @@ func getOrCreateSearchableEncryptionKey(kr keyring.Keyring, opts []node.Option)
396392
}
397393
log.Info("generated searchable encryption key")
398394
}
399-
400-
// Add the searchable encryption key to node options
401-
opts = append(opts, db.WithSearchableEncryptionKey(seKey))
402-
return opts, nil
395+
return seKey, nil
403396
}
404397

405-
func getOrCreatePeerKey(kr keyring.Keyring, opts []node.Option) ([]node.Option, error) {
398+
func getOrCreatePeerKey(kr keyring.Keyring) ([]byte, error) {
406399
peerKey, err := kr.Get(peerKeyName)
407400
if err != nil && errors.Is(err, keyring.ErrNotFound) {
408401
peerKey, err = crypto.GenerateEd25519()
@@ -417,10 +410,10 @@ func getOrCreatePeerKey(kr keyring.Keyring, opts []node.Option) ([]node.Option,
417410
} else if err != nil {
418411
return nil, err
419412
}
420-
return append(opts, p2p.WithPrivateKey(peerKey)), nil
413+
return peerKey, nil
421414
}
422415

423-
func getOrCreateIdentity(kr keyring.Keyring, opts []node.Option, cfg *viper.Viper) ([]node.Option, error) {
416+
func getOrCreateIdentity(kr keyring.Keyring, cfg *viper.Viper) (identity.Identity, error) {
424417
identityBytes, err := kr.Get(nodeIdentityKeyName)
425418
if err != nil {
426419
if !errors.Is(err, keyring.ErrNotFound) {
@@ -449,7 +442,7 @@ func getOrCreateIdentity(kr keyring.Keyring, opts []node.Option, cfg *viper.Vipe
449442
if err != nil {
450443
return nil, err
451444
}
452-
return getOrCreateIdentity(kr, opts, cfg)
445+
return getOrCreateIdentity(kr, cfg)
453446
}
454447
keyType := string(identityBytes[:sepPos])
455448
privateKey, err := crypto.PrivateKeyFromBytes(crypto.KeyType(keyType), identityBytes[sepPos+1:])
@@ -461,7 +454,7 @@ func getOrCreateIdentity(kr keyring.Keyring, opts []node.Option, cfg *viper.Vipe
461454
return nil, err
462455
}
463456

464-
return append(opts, db.WithNodeIdentity(ident)), nil
457+
return ident, nil
465458
}
466459

467460
func generateIdentity(keyType string) (identity.FullIdentity, error) {

0 commit comments

Comments
 (0)