Skip to content

Commit 86d5e7a

Browse files
committed
chore: refactor db creation
1 parent 75c9750 commit 86d5e7a

File tree

1 file changed

+51
-40
lines changed

1 file changed

+51
-40
lines changed

cmd/p2p/sensor/sensor.go

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sensor
22

33
import (
4+
"context"
45
"crypto/ecdsa"
56
"errors"
67
"fmt"
@@ -37,6 +38,7 @@ type (
3738
Bootnodes string
3839
NetworkID uint64
3940
NodesFile string
41+
StaticNodesFile string
4042
TrustedNodesFile string
4143
ProjectID string
4244
DatabaseID string
@@ -63,14 +65,14 @@ type (
6365
ForkID []byte
6466
DialRatio int
6567
NAT string
66-
QuickStart bool
6768
TTL time.Duration
6869
DiscoveryDNS string
6970
Database string
7071
NoDiscovery bool
7172

7273
bootnodes []*enode.Node
7374
nodes []*enode.Node
75+
staticNodes []*enode.Node
7476
trustedNodes []*enode.Node
7577
privateKey *ecdsa.PrivateKey
7678
nat nat.Interface
@@ -95,6 +97,13 @@ var SensorCmd = &cobra.Command{
9597
log.Warn().Err(err).Msgf("Creating nodes file %v because it does not exist", inputSensorParams.NodesFile)
9698
}
9799

100+
if len(inputSensorParams.StaticNodesFile) > 0 {
101+
inputSensorParams.staticNodes, err = p2p.ReadNodeSet(inputSensorParams.StaticNodesFile)
102+
if err != nil {
103+
log.Warn().Err(err).Msgf("Static nodes file %v not found", inputSensorParams.StaticNodesFile)
104+
}
105+
}
106+
98107
if len(inputSensorParams.TrustedNodesFile) > 0 {
99108
inputSensorParams.trustedNodes, err = p2p.ReadNodeSet(inputSensorParams.TrustedNodesFile)
100109
if err != nil {
@@ -159,37 +168,9 @@ var SensorCmd = &cobra.Command{
159168
return nil
160169
},
161170
RunE: func(cmd *cobra.Command, args []string) error {
162-
var db database.Database
163-
switch inputSensorParams.Database {
164-
case "datastore":
165-
db = database.NewDatastore(cmd.Context(), database.DatastoreOptions{
166-
ProjectID: inputSensorParams.ProjectID,
167-
DatabaseID: inputSensorParams.DatabaseID,
168-
SensorID: inputSensorParams.SensorID,
169-
ChainID: inputSensorParams.NetworkID,
170-
MaxConcurrency: inputSensorParams.MaxDatabaseConcurrency,
171-
ShouldWriteBlocks: inputSensorParams.ShouldWriteBlocks,
172-
ShouldWriteBlockEvents: inputSensorParams.ShouldWriteBlockEvents,
173-
ShouldWriteTransactions: inputSensorParams.ShouldWriteTransactions,
174-
ShouldWriteTransactionEvents: inputSensorParams.ShouldWriteTransactionEvents,
175-
ShouldWritePeers: inputSensorParams.ShouldWritePeers,
176-
TTL: inputSensorParams.TTL,
177-
})
178-
case "json":
179-
db = database.NewJSONDatabase(database.JSONDatabaseOptions{
180-
SensorID: inputSensorParams.SensorID,
181-
ChainID: inputSensorParams.NetworkID,
182-
MaxConcurrency: inputSensorParams.MaxDatabaseConcurrency,
183-
ShouldWriteBlocks: inputSensorParams.ShouldWriteBlocks,
184-
ShouldWriteBlockEvents: inputSensorParams.ShouldWriteBlockEvents,
185-
ShouldWriteTransactions: inputSensorParams.ShouldWriteTransactions,
186-
ShouldWriteTransactionEvents: inputSensorParams.ShouldWriteTransactionEvents,
187-
ShouldWritePeers: inputSensorParams.ShouldWritePeers,
188-
})
189-
case "none":
190-
db = database.NoDatabase()
191-
default:
192-
return fmt.Errorf("invalid database option: %s", inputSensorParams.Database)
171+
db, err := newDatabase(cmd.Context())
172+
if err != nil {
173+
return err
193174
}
194175

195176
// Fetch the latest block which will be used later when crafting the status
@@ -237,6 +218,7 @@ var SensorCmd = &cobra.Command{
237218
config := ethp2p.Config{
238219
PrivateKey: inputSensorParams.privateKey,
239220
BootstrapNodes: inputSensorParams.bootnodes,
221+
StaticNodes: inputSensorParams.staticNodes,
240222
TrustedNodes: inputSensorParams.trustedNodes,
241223
MaxPeers: inputSensorParams.MaxPeers,
242224
ListenAddr: fmt.Sprintf(":%d", inputSensorParams.Port),
@@ -252,10 +234,6 @@ var SensorCmd = &cobra.Command{
252234
},
253235
}
254236

255-
if inputSensorParams.QuickStart {
256-
config.StaticNodes = inputSensorParams.nodes
257-
}
258-
259237
server := ethp2p.Server{Config: config}
260238

261239
log.Info().Str("enode", server.Self().URLv4()).Msg("Starting sensor")
@@ -413,6 +391,42 @@ func getLatestBlock(url string) (*rpctypes.RawBlockResponse, error) {
413391
return &block, nil
414392
}
415393

394+
// newDatabase creates and configures the appropriate database backend based
395+
// on the sensor parameters.
396+
func newDatabase(ctx context.Context) (database.Database, error) {
397+
switch inputSensorParams.Database {
398+
case "datastore":
399+
return database.NewDatastore(ctx, database.DatastoreOptions{
400+
ProjectID: inputSensorParams.ProjectID,
401+
DatabaseID: inputSensorParams.DatabaseID,
402+
SensorID: inputSensorParams.SensorID,
403+
ChainID: inputSensorParams.NetworkID,
404+
MaxConcurrency: inputSensorParams.MaxDatabaseConcurrency,
405+
ShouldWriteBlocks: inputSensorParams.ShouldWriteBlocks,
406+
ShouldWriteBlockEvents: inputSensorParams.ShouldWriteBlockEvents,
407+
ShouldWriteTransactions: inputSensorParams.ShouldWriteTransactions,
408+
ShouldWriteTransactionEvents: inputSensorParams.ShouldWriteTransactionEvents,
409+
ShouldWritePeers: inputSensorParams.ShouldWritePeers,
410+
TTL: inputSensorParams.TTL,
411+
}), nil
412+
case "json":
413+
return database.NewJSONDatabase(database.JSONDatabaseOptions{
414+
SensorID: inputSensorParams.SensorID,
415+
ChainID: inputSensorParams.NetworkID,
416+
MaxConcurrency: inputSensorParams.MaxDatabaseConcurrency,
417+
ShouldWriteBlocks: inputSensorParams.ShouldWriteBlocks,
418+
ShouldWriteBlockEvents: inputSensorParams.ShouldWriteBlockEvents,
419+
ShouldWriteTransactions: inputSensorParams.ShouldWriteTransactions,
420+
ShouldWriteTransactionEvents: inputSensorParams.ShouldWriteTransactionEvents,
421+
ShouldWritePeers: inputSensorParams.ShouldWritePeers,
422+
}), nil
423+
case "none":
424+
return database.NoDatabase(), nil
425+
default:
426+
return nil, fmt.Errorf("invalid database option: %s", inputSensorParams.Database)
427+
}
428+
}
429+
416430
func init() {
417431
SensorCmd.Flags().StringVarP(&inputSensorParams.Bootnodes, "bootnodes", "b", "", "Comma separated nodes used for bootstrapping")
418432
SensorCmd.Flags().Uint64VarP(&inputSensorParams.NetworkID, "network-id", "n", 0, "Filter discovered nodes by this network ID")
@@ -457,10 +471,7 @@ significantly increase CPU and memory usage.`)
457471
`Ratio of inbound to dialed connections. A dial ratio of 2 allows 1/2 of
458472
connections to be dialed. Setting this to 0 defaults it to 3.`)
459473
SensorCmd.Flags().StringVar(&inputSensorParams.NAT, "nat", "any", "NAT port mapping mechanism (any|none|upnp|pmp|pmp:<IP>|extip:<IP>)")
460-
SensorCmd.Flags().BoolVar(&inputSensorParams.QuickStart, "quick-start", false,
461-
`Whether to load the nodes.json as static nodes to quickly start the network.
462-
This produces faster development cycles but can prevent the sensor from being to
463-
connect to new peers if the nodes.json file is large.`)
474+
SensorCmd.Flags().StringVar(&inputSensorParams.StaticNodesFile, "static-nodes", "", "Static nodes file")
464475
SensorCmd.Flags().StringVar(&inputSensorParams.TrustedNodesFile, "trusted-nodes", "", "Trusted nodes file")
465476
SensorCmd.Flags().DurationVar(&inputSensorParams.TTL, "ttl", 14*24*time.Hour, "Time to live")
466477
SensorCmd.Flags().StringVar(&inputSensorParams.DiscoveryDNS, "discovery-dns", "", "DNS discovery ENR tree url")

0 commit comments

Comments
 (0)