Skip to content

Commit

Permalink
feat(devtools): made change to restore previous state on app start
Browse files Browse the repository at this point in the history
  • Loading branch information
aeddi committed Nov 9, 2018
1 parent 64fb659 commit 4a1d0de
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
56 changes: 49 additions & 7 deletions client/react-native/gomobile/core/core.go
@@ -1,24 +1,29 @@
package core

import (
"errors"
"encoding/json"
"fmt"
"net"
"strconv"
"strings"
"time"

"github.com/pkg/errors"

account "berty.tech/core/manager/account"
reuse "github.com/libp2p/go-reuseport"
"go.uber.org/zap"
)

var (
accountName = ""
appConfig *account.StateDB
)

func logger() *zap.Logger {
return zap.L().Named("client.rn.gomobile")
}

var accountName = ""

func panicHandler() {
if r := recover(); r != nil {
logger().Error(fmt.Sprintf("%+v", r))
Expand Down Expand Up @@ -66,6 +71,27 @@ func ListAccounts(datastorePath string) (string, error) {
return strings.Join(accounts, ":"), nil
}

func initOrRestoreAppState(datastorePath string) error {
initialJSONNetConf, err := json.Marshal(initialNetConf)
if err != nil {
return err
}

// Needed by OpenStateDB to init DB if no previous config is found (first launch)
initialState := account.StateDB{
JSONNetConf: string(initialJSONNetConf),
BotMode: initialBotMode,
}

appState, err := account.OpenStateDB(datastorePath+"berty.state.db", initialState)
if err != nil {
return errors.Wrap(err, "state DB init failed")
}

appConfig = appState
return nil
}

func Start(nickname, datastorePath string, loggerNative Logger) error {

defer panicHandler()
Expand All @@ -76,6 +102,11 @@ func Start(nickname, datastorePath string, loggerNative Logger) error {
if a != nil {
return errors.New("daemon already started")
}

if err := initOrRestoreAppState(datastorePath); err != nil {
return errors.Wrap(err, "app init/restore state failed")
}

run(nickname, datastorePath, loggerNative)
waitDaemon(nickname)
return nil
Expand Down Expand Up @@ -133,7 +164,6 @@ func waitDaemon(nickname string) {
}

func daemon(nickname, datastorePath string, loggerNative Logger) error {

defer panicHandler()

grpcPort, err := getRandomPort()
Expand All @@ -146,14 +176,20 @@ func daemon(nickname, datastorePath string, loggerNative Logger) error {
}

var a *account.Account
a, err = account.New(

netConf, err := createNetworkConfig()
if err != nil {
return err
}

accountOptions := account.Options{
account.WithName(nickname),
account.WithPassphrase("secure"),
account.WithDatabase(&account.DatabaseOptions{
Path: datastorePath,
Drop: false,
}),
account.WithP2PNetwork(createNetworkConfig()),
account.WithP2PNetwork(netConf),
account.WithGrpcServer(&account.GrpcServerOptions{
Bind: fmt.Sprintf(":%d", grpcPort),
Interceptors: false,
Expand All @@ -162,7 +198,13 @@ func daemon(nickname, datastorePath string, loggerNative Logger) error {
Bind: fmt.Sprintf(":%d", gqlPort),
Interceptors: false,
}),
)
}

if appConfig.BotMode {
accountOptions = append(accountOptions, account.WithBot())
}

a, err = account.New(accountOptions...)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions client/react-native/gomobile/go.mod
Expand Up @@ -5,6 +5,7 @@ require (
github.com/ipfs/go-log v1.5.7
github.com/libp2p/go-buffer-pool v0.1.2-0.20181009094743-058210c5a0d0 // indirect
github.com/libp2p/go-reuseport v0.1.18
github.com/pkg/errors v0.8.0
github.com/whyrusleeping/go-logging v0.0.0-20170515211332-0457bb6b88fc
go.uber.org/zap v1.9.1
golang.org/x/mobile v0.0.0-20181010163405-92f3b9caf7ba // indirect
Expand Down
15 changes: 13 additions & 2 deletions core/manager/account/db.go
Expand Up @@ -11,9 +11,12 @@ type StateDB struct {
gorm.Model

StartCounter int

JSONNetConf string
BotMode bool
}

func OpenStateDB(path string) (*StateDB, error) {
func OpenStateDB(path string, initialState StateDB) (*StateDB, error) {
// open db
db, err := gorm.Open("sqlite3", path)
if err != nil {
Expand All @@ -30,7 +33,15 @@ func OpenStateDB(path string) (*StateDB, error) {
if err := db.FirstOrInit(&state).Error; err != nil {
return nil, err
}
state.gorm = db

// if no previous state found, set initial state
if state.StartCounter == 0 {
state = initialState
state.gorm = db
state.Save()
} else {
state.gorm = db
}

return &state, nil
}
Expand Down

0 comments on commit 4a1d0de

Please sign in to comment.