Reorg file paths, and initialize peer store

This commit is contained in:
Richard Ramos 2021-04-13 14:54:06 -04:00
parent 1b746cdec8
commit 11b588a46f
No known key found for this signature in database
GPG Key ID: 80D4B01265FDFE8F
3 changed files with 55 additions and 26 deletions

2
build/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -2,7 +2,7 @@ package main
import ( import (
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
"github.com/status-im/go-waku/cmd" "github.com/status-im/go-waku/waku"
) )
func main() { func main() {
@ -12,5 +12,5 @@ func main() {
} }
logging.SetAllLoggers(lvl) logging.SetAllLoggers(lvl)
cmd.Execute() waku.Execute()
} }

View File

@ -1,9 +1,10 @@
package cmd package waku
import ( import (
"context" "context"
"crypto/rand" "crypto/rand"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"net" "net"
"os" "os"
@ -11,11 +12,20 @@ import (
"syscall" "syscall"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
dssql "github.com/ipfs/go-ds-sql"
logging "github.com/ipfs/go-log"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p-peerstore/pstoreds"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/status-im/go-waku/waku/persistence"
"github.com/status-im/go-waku/waku/persistence/sqlite"
"github.com/status-im/go-waku/waku/v2/node" "github.com/status-im/go-waku/waku/v2/node"
) )
var log = logging.Logger("wakunode")
func randomHex(n int) (string, error) { func randomHex(n int) (string, error) {
bytes := make([]byte, n) bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil { if _, err := rand.Read(bytes); err != nil {
@ -24,6 +34,15 @@ func randomHex(n int) (string, error) {
return hex.EncodeToString(bytes), nil return hex.EncodeToString(bytes), nil
} }
func checkError(err error, msg string) {
if err != nil {
if msg != "" {
msg = msg + ": "
}
log.Fatal(msg, err)
}
}
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "waku", Use: "waku",
Short: "Start a waku node", Short: "Start a waku node",
@ -41,46 +60,52 @@ var rootCmd = &cobra.Command{
hostAddr, _ := net.ResolveTCPAddr("tcp", fmt.Sprint("0.0.0.0:", port)) hostAddr, _ := net.ResolveTCPAddr("tcp", fmt.Sprint("0.0.0.0:", port))
var err error
if key == "" { if key == "" {
var err error
key, err = randomHex(32) key, err = randomHex(32)
if err != nil { checkError(err, "Could not generate random key")
fmt.Println("Could not generate random key")
return
}
} }
prvKey, err := crypto.HexToECDSA(key) prvKey, err := crypto.HexToECDSA(key)
ctx := context.Background() if dbPath == "" {
wakuNode, err := node.New(ctx, prvKey, []net.Addr{hostAddr}) checkError(errors.New("dbpath can't be null"), "")
if err != nil {
fmt.Println(err)
return
} }
db, err := sqlite.NewDB(dbPath)
checkError(err, "Could not connect to DB")
ctx := context.Background()
// Create persistent peerstore
queries, err := sqlite.NewQueries("peerstore", db)
checkError(err, "Peerstore")
datastore := dssql.NewDatastore(db, queries)
opts := pstoreds.DefaultOpts()
peerStore, err := pstoreds.NewPeerstore(ctx, datastore, opts)
checkError(err, "Peerstore")
wakuNode, err := node.New(ctx, prvKey, []net.Addr{hostAddr}, libp2p.Peerstore(peerStore))
checkError(err, "Wakunode")
if relay { if relay {
wakuNode.MountRelay() wakuNode.MountRelay()
} }
if store && dbPath != "" { if store {
db, err := NewDBStore(dbPath) dbStore, err := persistence.NewDBStore(persistence.WithDB(db))
if err != nil { checkError(err, "DBStore")
fmt.Println(err)
return err = wakuNode.MountStore(dbStore)
} checkError(err, "Error mounting store")
err = wakuNode.MountStore(db)
if err != nil {
fmt.Println(err)
return
}
wakuNode.StartStore() wakuNode.StartStore()
} }
if storenode != "" && !store { if storenode != "" && !store {
fmt.Println("Store protocol was not started") checkError(errors.New("Store protocol was not started"), "")
return
} else { } else {
if storenode != "" { if storenode != "" {
wakuNode.AddStorePeer(storenode) wakuNode.AddStorePeer(storenode)
@ -101,6 +126,8 @@ var rootCmd = &cobra.Command{
// shut the node down // shut the node down
wakuNode.Stop() wakuNode.Stop()
err = db.Close()
checkError(err, "DBClose")
}, },
} }