Skip to content

Commit

Permalink
sync universe
Browse files Browse the repository at this point in the history
  • Loading branch information
TATAUFO committed Feb 10, 2020
1 parent 9ef67dc commit 2f21351
Show file tree
Hide file tree
Showing 10 changed files with 626 additions and 261 deletions.
146 changes: 6 additions & 140 deletions cmd/pdu/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,25 @@
package main

import (
"bufio"
"encoding/json"
"errors"
"fmt"
"github.com/mitchellh/go-homedir"
"os"
"strings"

"github.com/pdupub/go-pdu/common"
"github.com/pdupub/go-pdu/core"
"github.com/pdupub/go-pdu/crypto"
"github.com/pdupub/go-pdu/db"
"github.com/pdupub/go-pdu/db/bolt"
"github.com/pdupub/go-pdu/params"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"math/big"
"os"
"path"
"strings"
)

// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a new PDU Universe",
RunE: func(_ *cobra.Command, args []string) error {

if err := initDir(); err != nil {
return err
}

if err := initConfig(); err != nil {
return err
}

udb, err := initDB()
udb, err := initNodeDir()
if err != nil {
return err
}
Expand All @@ -77,7 +62,7 @@ func initUniverseAndSave(udb db.UDB) error {
return err
}

if err := saveRootUsers(users, udb); err != nil {
if err := db.SaveRootUsers(udb, users); err != nil {
return err
}

Expand All @@ -101,7 +86,7 @@ func initUniverseAndSave(udb db.UDB) error {
return err
}

if err := saveMsg(msg, udb); err != nil {
if err := db.SaveMsg(udb, msg); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -139,72 +124,6 @@ func createFirstMsg(users []*core.User, priKeys []*crypto.PrivateKey) (*core.Mes
return msg, nil
}

func scanLine(input *string) {
reader := bufio.NewReader(os.Stdin)
data, _, _ := reader.ReadLine()
*input = string(data)
}

func saveMsg(msg *core.Message, udb db.UDB) error {
msgBytes, err := json.Marshal(msg)
if err != nil {
return err
}
countBytes, err := udb.Get(db.BucketConfig, db.ConfigMsgCount)
if err != nil {
return err
}
count := new(big.Int).SetBytes(countBytes)
err = udb.Set(db.BucketMsg, common.Hash2String(msg.ID()), msgBytes)
if err != nil {
return err
}

err = udb.Set(db.BucketMID, count.String(), []byte(common.Hash2String(msg.ID())))
if err != nil {
return err
}
count = count.Add(count, big.NewInt(1))
err = udb.Set(db.BucketConfig, db.ConfigMsgCount, count.Bytes())
if err != nil {
return err
}

err = udb.Set(db.BucketLastMID, common.Hash2String(msg.SenderID), []byte(common.Hash2String(msg.ID())))
if err != nil {
return err
}
return nil
}

func saveRootUsers(users []*core.User, udb db.UDB) (err error) {
// save root users
var root0, root1 []byte
if root0, err = json.Marshal(users[0]); err != nil {
return err
}
if err = udb.Set(db.BucketConfig, db.ConfigRoot0, root0); err != nil {
return err
}
if err = udb.Set(db.BucketUser, common.Hash2String(users[0].ID()), root0); err != nil {
return err
}

if root1, err = json.Marshal(users[1]); err != nil {
return err
}

if err = udb.Set(db.BucketConfig, db.ConfigRoot1, root1); err != nil {
return err
}

if err = udb.Set(db.BucketUser, common.Hash2String(users[1].ID()), root1); err != nil {
return err
}

return nil
}

func createRootUsers() (users []*core.User, priKeys []*crypto.PrivateKey, err error) {

for i := 0; i < 2; i++ {
Expand Down Expand Up @@ -233,59 +152,6 @@ func createRootUsers() (users []*core.User, priKeys []*crypto.PrivateKey, err er
return users, priKeys, err
}

func initDB() (db.UDB, error) {
dbFilePath := path.Join(dataDir, "u.db")
udb, err := bolt.NewDB(dbFilePath)
if err != nil {
return nil, err
}

if err := udb.CreateBucket(db.BucketConfig); err != nil {
return nil, err
}
if err := udb.Set(db.BucketConfig, db.ConfigMsgCount, big.NewInt(0).Bytes()); err != nil {
return nil, err
}

if err := udb.CreateBucket(db.BucketUser); err != nil {
return nil, err
}
if err := udb.CreateBucket(db.BucketMsg); err != nil {
return nil, err
}
if err := udb.CreateBucket(db.BucketMID); err != nil {
return nil, err
}
if err := udb.CreateBucket(db.BucketLastMID); err != nil {
return nil, err
}

if err := udb.CreateBucket(db.BucketPeer); err != nil {
return nil, err
}

return udb, nil
}

func initConfig() error {
viper.SetConfigType(params.DefaultConfigType)
viper.Set("CONFIG_NAME", "PDU")
return viper.WriteConfigAs(path.Join(dataDir, params.DefaultConfigFile))
}

func initDir() error {
if dataDir == "" {
home, _ := homedir.Dir()
dataDir = path.Join(home, params.DefaultPath)
}
err := os.Mkdir(dataDir, os.ModePerm)
if err != nil {
return err
}

return nil
}

func init() {
createCmd.PersistentFlags().StringVar(&dataDir, "datadir", "", fmt.Sprintf("(default $HOME/%s)", params.DefaultPath))
rootCmd.AddCommand(createCmd)
Expand Down
37 changes: 26 additions & 11 deletions cmd/pdu/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"os/signal"
"path"

"github.com/mitchellh/go-homedir"
"github.com/pdupub/go-pdu/common"
"github.com/pdupub/go-pdu/common/log"
Expand All @@ -31,34 +35,43 @@ import (
"github.com/pdupub/go-pdu/params"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"os/signal"
"path"
)

// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "Start to run PDU Universe",
RunE: func(_ *cobra.Command, args []string) error {
err := initConfigLoad()
if err != nil {

if err := updateDataDir(); err != nil {
return err
}

if exist, err := pathExists(dataDir); err != nil {
return err
} else if !exist {
if newdb, err := initNodeDir(); err != nil {
return err
} else if err := newdb.Close(); err != nil {
return err
}
log.Info("Database initialized successfully", dataDir)
}

if err := initConfigLoad(); err != nil {
return err
}
log.Info("Starting p2p node")
log.Info("CONFIG_NAME", viper.GetString("CONFIG_NAME"))

udb, err := initDBLoad()
if err != nil {
return err
}

pn, err := node.New(udb)
if err != nil {
return err
}

// for all node mode need to unlock account
var unlockedUser core.User
var unlockedPrivateKey *crypto.PrivateKey
Expand Down Expand Up @@ -129,18 +142,20 @@ var startCmd = &cobra.Command{
},
}

// initConfigLoad reads in config file and ENV variables if set.
func initConfigLoad() error {
func updateDataDir() error {
if dataDir == "" {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
return err
}
dataDir = path.Join(home, params.DefaultPath)

}
return nil
}

// initConfigLoad reads in config file and ENV variables if set.
func initConfigLoad() error {
viper.SetConfigFile(path.Join(dataDir, params.DefaultConfigFile))
viper.SetConfigType("yml")
viper.AutomaticEnv() // read in environment variables that match
Expand All @@ -164,7 +179,7 @@ func initDBLoad() (db.UDB, error) {
func init() {
startCmd.PersistentFlags().StringVar(&dataDir, "datadir", "", fmt.Sprintf("(default $HOME/%s)", params.DefaultPath))
startCmd.PersistentFlags().StringVar(&nodeAddressList, "nodes", "", "pdu nodes list, split by comma [userid@ip:port/nodeKey]")
startCmd.PersistentFlags().Uint64Var(&localPort, "localPort", node.DefaultLocalPort, "local port")
startCmd.PersistentFlags().Uint64Var(&localPort, "port", node.DefaultLocalPort, "local port")

// time proof
startCmd.PersistentFlags().BoolVar(&nodeTPEnable, "tp", false, "time proof enable")
Expand Down

0 comments on commit 2f21351

Please sign in to comment.