Skip to content

Commit

Permalink
change p2p config. more friendly log
Browse files Browse the repository at this point in the history
  • Loading branch information
jerry-vite committed Aug 17, 2018
1 parent 34a1d8d commit 950cc37
Show file tree
Hide file tree
Showing 7 changed files with 211 additions and 193 deletions.
45 changes: 42 additions & 3 deletions README.md
@@ -1,7 +1,46 @@
# go-vite

Official Go implementation of the Vite protocol


## BUILD
1. [Install the Golang](https://golang.org/doc/install)
2. Run ```go get https://github.com/vitelabs/go-vite``` in your terminal, you will find the source code here: ``~/go/src/github.com/vitelabs/go-vite/``
3. Go to the source code directory and run ```make all```, you will get executable files of darwin, linux, windows here: ``~/go/src/github.com/vitelabs/go-vite/build/cmd/rpc``

1. [Install Go](https://golang.org/doc/install)
2. Run `go get github.com/vitelabs/go-vite` in your terminal, then you will find the source code here: `$GOPATH/src/github.com/vitelabs/go-vite/` (as default, $GOPATH is `~/go`)
3. Go to the source code directory and run `make all`, you will get executable files of darwin, linux, windows here: `$GOPATH/src/github.com/vitelabs/go-vite/build/cmd/rpc`
4. Run the appropriate binary file on your OS.


## CONFIG

As default, Vite will give a default config. but you can set your config use two way as following.

### cmd

| key | type | default | meaning |
|:--|:--|:--|
| name | string | "vite-server" | the server name, use for log |
| maxpeers | number | 50 | the maximum number of peers can be connected |
| addr | string | "0.0.0.0:8483" | will be listen by vite |
| dir | string | "~/viteisbest" | the directory in which vite will store all files (like log, ledger) |
| netid | number | 2 | the network vite will connect, default 2 means TestNet |


### configFile

we can also use config file `vite.config.json` to set Config. for example:

```json
{
"P2P": {
"Name": "vite-server",
"PrivateKey": "",
"MaxPeers": 100,
"Addr": "0.0.0.0:8483",
"NetID": 2
},
"DataDir": ""
}
```

`vite.config.json` should be in the same directory of vite.
40 changes: 23 additions & 17 deletions cmd/gvite/main.go
Expand Up @@ -12,14 +12,15 @@ import (
)

var (
nameFlag = flag.String("name", "", "boot name")
sigFlag = flag.String("sig", "", "boot sig")
maxPeers = flag.Uint("maxpeers", 0, "max number of connections will be connected")
passRatio = flag.Uint("passration", 0, "max passive connections will be connected")

minerFlag = flag.Bool("miner", false, "boot miner")
minerInterval = flag.Int("minerInterval", 6, "miner interval(unit sec).")
coinbaseFlag = flag.String("coinbaseAddress", "", "boot coinbaseAddress")
nameFlag = flag.String("name", "", "boot name")
maxPeersFlag = flag.Uint("maxpeers", 0, "max number of connections will be connected")
addrFlag = flag.String("addr", "0.0.0.0:8483", "will be listen by vite")
privateKeyFlag = flag.String("priv", "", "use for sign message")
dataDirFlag = flag.String("dir", "", "use for store all files")
netIdFlag = flag.Uint("netid", 0, "the network vite will connect")
//minerFlag = flag.Bool("miner", false, "boot miner")
//minerInterval = flag.Int("minerInterval", 6, "miner interval(unit sec).")
//coinbaseFlag = flag.String("coinbaseAddress", "", "boot coinbaseAddress")
)

func main() {
Expand All @@ -39,19 +40,24 @@ func main() {

globalConfig := config.GlobalConfig

if *dataDirFlag != "" {
globalConfig.DataDir = *dataDirFlag
}

globalConfig.P2P = config.MergeP2PConfig(&config.P2P{
Name: *nameFlag,
Sig: *sigFlag,
MaxPeers: uint32(*maxPeers),
MaxPassivePeersRatio: uint32(*passRatio),
Name: *nameFlag,
MaxPeers: uint32(*maxPeersFlag),
Addr: *addrFlag,
PrivateKey: *privateKeyFlag,
NetID: *netIdFlag,
})
globalConfig.P2P.Datadir = globalConfig.DataDir

globalConfig.Miner = config.MergeMinerConfig(&config.Miner{
Miner: *minerFlag,
Coinbase: *coinbaseFlag,
MinerInterval: *minerInterval,
})
//globalConfig.Miner = config.MergeMinerConfig(&config.Miner{
// Miner: *minerFlag,
// Coinbase: *coinbaseFlag,
// MinerInterval: *minerInterval,
//})

if s, e := config.GlobalConfig.RunLogDirFile(); e == nil {
log15.Root().SetHandler(
Expand Down
22 changes: 6 additions & 16 deletions config/config.go
Expand Up @@ -2,8 +2,8 @@ package config

import (
"encoding/json"
"fmt"
"github.com/vitelabs/go-vite/common"
"github.com/vitelabs/go-vite/log15"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -31,16 +31,7 @@ func (c Config) RunLogDirFile() (string, error) {

}

//func (c Config) ConfigureLog() {
// if s, e := c.RunLogDirFile(); e == nil {
// log15.Root().SetHandler(
// log15.MultiHandler(
// log15.LvlFilterHandler(log15.LvlInfo, log15.StdoutHandler),
// log15.LvlFilterHandler(log15.LvlInfo, log15.Must.FileHandler(s, log15.TerminalFormat())),
// ),
// )
// }
//}
var log = log15.New("module", "config")

const configFileName = "vite.config.json"

Expand All @@ -50,9 +41,7 @@ func RecoverConfig() {
GlobalConfig = &Config{
P2P: P2P{
Name: "vite-server",
Sig: "",
PrivateKey: "",
PublicKey: "",
MaxPeers: 100,
MaxPassivePeersRatio: 2,
MaxPendingPeers: 20,
Expand All @@ -73,16 +62,17 @@ func RecoverConfig() {
func init() {
GlobalConfig = new(Config)

if text, err := ioutil.ReadFile("vite.config.json"); err == nil {
if text, err := ioutil.ReadFile(configFileName); err == nil {
err = json.Unmarshal(text, GlobalConfig)
if err != nil {
fmt.Println("config file unmarshal error: ", err)
log.Info("cannot unmarshal the config file content, will use the default config", "error", err)
}
} else {
log.Info("cannot read the config file, will use the default config", "error", err)
}

// set default value global keys
if GlobalConfig.DataDir == "" {
GlobalConfig.DataDir = common.DefaultDataDir()
}

}
12 changes: 0 additions & 12 deletions config/p2p.go
Expand Up @@ -3,12 +3,8 @@ package config
type P2P struct {
Name string `json:"Name""`

Sig string `json:"Sig"`

// use for sign data
PrivateKey string `json:"PrivateKey"`
// use for NodeID
PublicKey string `json:"PublicKey"`

// `MaxPeers` is the maximum number of peers that can be connected.
MaxPeers uint32 `json:"MaxPeers"`
Expand Down Expand Up @@ -40,18 +36,10 @@ func MergeP2PConfig(cfg *P2P) P2P {
p2p.Name = cfg.Name
}

if cfg.Sig != "" {
p2p.Sig = cfg.Sig
}

if cfg.PrivateKey != "" {
p2p.PrivateKey = cfg.PrivateKey
}

if cfg.PublicKey != "" {
p2p.PublicKey = cfg.PublicKey
}

if cfg.MaxPeers != 0 {
p2p.MaxPeers = cfg.MaxPeers
}
Expand Down
98 changes: 56 additions & 42 deletions p2p/config.go
@@ -1,62 +1,76 @@
package p2p

import (
"encoding/hex"
"github.com/vitelabs/go-vite/crypto"
"fmt"
"github.com/vitelabs/go-vite/crypto/ed25519"
"github.com/vitelabs/go-vite/log15"
"os"
"path/filepath"
)

var pubs = [...]string{
"33e43481729850fc66cef7f42abebd8cb2f1c74f0b09a5bf03da34780a0a5606",
"7194af5b7032cb470c41b313e2675e2c3ba3377e66617247012b8d638552fb17",
"087c45631c3ec9a5dbd1189084ee40c8c4c0f36731ef2c2cb7987da421d08ba9",
"7c6a2b920764b6dddbca05bb6efa1c9bcd90d894f6e9b107f137fc496c802346",
"2840979ae06833634764c19e72e6edbf39595ff268f558afb16af99895aba3d8",
"298a693584e4fceebb3f7abab2c25e7a6c6b911f15c12362b23144dd72822c02",
//"75f962a81a6d52d9dcc830ff7dc2f21c424eeed4a0f2e5bab8f39f44df833153",
//"491d7a992cad4ba82ea10ad0f5da86a2e40f4068918bd50d8faae1f1b69d8510",
//"c3debe5fc3f8839c4351834d454a0940872f973ad0d332811f0d9e84953cfdc2",
//"ba351c5df80eea3d78561507e40b3160ade4daf20571c5a011ca358b81d630f7",
//"a1d437148c48a44b709b0a0c1e99c847bb4450384a6eea899e35e78a1e54c92b",
//"657f6706b95005a33219ae26944e6db15edfe8425b46fa73fb6cae57707b4403",
//"20fd0ec94071ea99019d3c18a5311993b8fa91920b36803e5558783ca346cec1",
//"8669feb2fdeeedfbbfe8754050c0bd211a425e3b999d44856867d504cf13243e",
//"802d8033f6adea154e5fa8b356a45213e8a4e5e85e54da19688cb0ad3520db2b",
//"180af9c90f8444a452a9fb53f3a1975048e9b23ec492636190f9741ed3888a62",
//"6c8ce60b87199d46f7524d7da5a06759c054b9192818d0e8b211768412efec51",
//"3b3e7164827eb339548b9c895fc56acee7980a0044b5bef2e6f465e030944e40",
//"de1f3a591b551591fb7d20e478da0371def3d62726b90ffca6932e38a25ebe84",
//"9df2e11399398176fa58638592cf1b2e0e804ae92ac55f09905618fdb239c03c",
//"0fbb7d7a2cf44755019b7ab939e33f75a3bc9540997df2146ac3a977efaf2858",
//"24a19ee5109bd5c6229df087bf295f543002f1f00026f8d0ade9dcccc36fcb6f",
//"dcf3e93b0ab38e1f359af9772d493e20414fbd552b21768f9837ae7b0f01f0cb",
//"58b9ac27b5578d0632ef9ac204298b3eba5047488c10cce1eb2e191db1c054f1",
//"fdddbd58b6fd9059d7751b16f51fb1db77cb2f0801aa6c447eefacbcecb6945d",
}
const privKeyFileName = "priv.key"

var log = log15.New("module", "p2p/config")

func getServerKey(p2pDir string) (pub ed25519.PublicKey, priv ed25519.PrivateKey, err error) {
pub = make([]byte, 32)
priv = make([]byte, 64)

privKeyFile := filepath.Join(p2pDir, privKeyFileName)

fd, err := os.Open(privKeyFile)
getKeyOK := true

func pickPub(data, sign string) ed25519.PublicKey {
payload := []byte(data)
sig, err := hex.DecodeString(sign)
if err != nil {
log15.Root().Crit("invalid signature from cmd", "err", err)
}
getKeyOK = false
log.Info("cannot read p2p priv.key, use random key", "error", err)
pub, priv, err = ed25519.GenerateKey(nil)
if err != nil {
return nil, nil, err
}

for _, str := range pubs {
pub, err := hex.DecodeString(str)
fd, err = os.Create(privKeyFile)
if err != nil {
continue
log.Info("create p2p priv.key fail", "error", err)
} else {
defer fd.Close()
}
} else {
defer fd.Close()

n, err := fd.Read([]byte(priv))

valid, err := crypto.VerifySig(pub, payload, sig)
if err != nil {
continue
getKeyOK = false
log.Info("cannot read p2p priv.key, use random key", "error", err)
}

if n != len(priv) {
log.Info("read incompelete p2p priv.key, use random key")
getKeyOK = false
}

if valid {
return pub
if !getKeyOK {
pub, priv, err = ed25519.GenerateKey(nil)
if err != nil {
return nil, nil, err
}
} else {
pub = priv.PubByte()
}
}

if !getKeyOK {
n, err := fd.Write([]byte(priv))
fmt.Println(fd == nil)
if err != nil {
log.Info("write privateKey to p2p priv.key fail", "error", err)
} else if n != len(priv) {
log.Info("write incompelete privateKey to p2p priv.key")
} else {
log.Info("write privateKey to p2p priv.key")
}
}

return nil
return
}

0 comments on commit 950cc37

Please sign in to comment.