@@ -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
5249const 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
5754func 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
467460func generateIdentity (keyType string ) (identity.FullIdentity , error ) {
0 commit comments