Skip to content
Snippets Groups Projects
Commit 3e4b45d2 authored by Tigran Avakyan's avatar Tigran Avakyan
Browse files

Changing to simpler flat structure and encapsulating param creation in createParams

parent 50a1489d
Branches
Tags
No related merge requests found
......@@ -11,14 +11,9 @@ import (
//"encoding/binary"
jww "github.com/spf13/jwalterweatherman"
"github.com/spf13/viper"
//"gitlab.com/elixxir/comms/connect"
//"gitlab.com/elixxir/comms/node"
//"gitlab.com/elixxir/crypto/cyclic"
//"gitlab.com/elixxir/primitives/id"
// "gitlab.com/elixxir/server/cryptops/realtime"
//"gitlab.com/elixxir/server/globals"
//"gitlab.com/elixxir/server/io"
"gitlab.com/elixxir/server/server/conf"
"runtime"
"strconv"
"strings"
"sync"
......@@ -177,10 +172,7 @@ func readSignal(rDone chan bool, realtimeSignal *sync.Cond) {
}
// StartServer reads configuration options and starts the cMix server
func StartServer(serverIndex int, batchSize uint64) {
viper.Debug()
jww.INFO.Printf("Log Filename: %v\n", viper.GetString("logPath"))
jww.INFO.Printf("Config Filename: %v\n", viper.ConfigFileUsed())
func StartServer(params conf.Params) {
//Set the max number of processes
runtime.GOMAXPROCS(runtime.NumCPU() * 2)
......@@ -188,6 +180,11 @@ func StartServer(serverIndex int, batchSize uint64) {
//Start the performance monitor
go MonitorMemoryUsage()
viper.Debug()
jww.INFO.Printf("Log Filename: %v\n", params.LogPath)
jww.INFO.Printf("Config Filename: %v\n", viper.ConfigFileUsed())
// Set global batch size
//globals.BatchSize = batchSize
jww.INFO.Printf("Batch Size: %v\n", batchSize)
......@@ -207,6 +204,7 @@ func StartServer(serverIndex int, batchSize uint64) {
dbAddresses := viper.GetStringSlice("dbAddresses")
//dbAddress := ""
serverIndex := params.ServerIdx
if (serverIndex >= 0) && (serverIndex < len(dbAddresses)) {
// There's a DB address for this server in the list and we can
// use it
......
......@@ -8,6 +8,7 @@
package cmd
import (
"gitlab.com/elixxir/server/server/conf"
//"gitlab.com/elixxir/server/globals"
"os"
"time"
......@@ -70,11 +71,37 @@ communications.`,
}
// FIXME This way of getting the server index from the
// config file seems odd.
serverIdx = viper.GetInt("index")
StartServer(serverIdx, uint64(viper.GetInt("batchsize")))
params := createParams()
StartServer(params)
},
}
// createParams creates the conf.Params obj from viper
func createParams() conf.Params {
params := conf.Params{
DBName: viper.GetString("dbName"),
DBUsername: viper.GetString("dbUsername"),
DBPassword: viper.GetString("dbPassword"),
DBAddresses: viper.GetStringSlice("dbAddresses"),
CMix: getGroupFromConfig(viper.GetStringMapString("groups.cmix")),
E2E: getGroupFromConfig(viper.GetStringMapString("groups.e2e")),
CertPath: viper.GetString("certPath"),
KeyPath: viper.GetString("keyPath"),
LogPath: viper.GetString("logPath"),
Servers: viper.GetStringSlice("servers"),
NodeID: viper.GetUint64("nodeId"),
SkipReg: viper.GetBool("skipReg"),
Gateways: viper.GetStringSlice("gateways"),
BatchSize: viper.GetUint64("batchsize"),
ServerIdx: viper.GetInt("index"),
}
return params
}
// getLargeIntFromString checks the first 2 bytes. If '0x' parse a base16 number
// otherwise it parses a base10
func getLargeIntFromString(largeIntStr string) *large.Int {
......
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2019 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package conf
// Contains DB config params
type DB struct {
Name string
Username string
Password string
Addresses []string
}
---
name: "name"
username: "username"
password: "password"
addresses:
- "127.0.0.1:80"
- "127.0.0.1:80"
- "127.0.0.1:80"
...
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2019 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package conf
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"reflect"
"testing"
)
// This test checks that unmarshalling the db.yaml file
// is equal to the expected DB object.
func TestDB_UnmarshallingFileEqualsExpected(t *testing.T) {
expected := DB{
Name: "name",
Username: "username",
Password: "password",
Addresses: []string{
"127.0.0.1:80",
"127.0.0.1:80",
"127.0.0.1:80",
},
}
buf, _ := ioutil.ReadFile("./db.yaml")
actual := DB{}
err := yaml.Unmarshal(buf, &actual)
if err != nil {
t.Errorf("Unable to decode into struct, %v", err)
}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("DB object did not match expected values")
}
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2019 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package conf
import (
jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/large"
"strings"
)
// Contains the cyclic group config params
type Groups struct {
CMix *cyclic.Group
E2E *cyclic.Group
}
// TODO: field names start with a capital by convention
// but perhaps we should override to force a consistent scheme
// See yaml package documentation for more info.
type groups struct {
Cmix map[string]string
E2e map[string]string
}
// UnmarshalYAML defines custom unmarshalling behavior
// such that exported Group structure can contain cyclic Groups
// using the internal Groups struct which contains string mappings
func (Grps *Groups) UnmarshalYAML(unmarshal func(interface{}) error) error {
grps := groups {}
err := unmarshal(&grps)
if err != nil {
return err
}
Grps.CMix = toGroup(grps.Cmix)
Grps.E2E = toGroup(grps.E2e)
return nil
}
// toGroup takes a group represented by a map of string to string
// and uses the prime, small prime and generator to created
// and returns a a cyclic group object.
func toGroup(grp map[string]string) *cyclic.Group {
pStr, pOk := grp["prime"]
qStr, qOk := grp["smallprime"]
gStr, gOk := grp["generator"]
if !gOk || !qOk || !pOk {
jww.FATAL.Panicf("Invalid Group Config "+
"(prime: %v, smallPrime: %v, generator: %v",
pOk, qOk, gOk)
}
// TODO: Is there any error checking we should do here? If so, what?
p := toLargeInt(strings.ReplaceAll(pStr, " ", ""))
q := toLargeInt(strings.ReplaceAll(qStr, " ", ""))
g := toLargeInt(strings.ReplaceAll(gStr, " ", ""))
return cyclic.NewGroup(p, g, q)
}
// toLargeInt takes in a string representation of a large int.
// If the first 2 bytes are '0x' it parses a base 16 number,
// otherwise it parses a base 10 and returns the result.
func toLargeInt(str string) *large.Int {
if len(str) > 2 && "0x" == str[:2] {
return large.NewIntFromString(str[2:], 16)
}
return large.NewIntFromString(str, 10)
}
---
cmix:
prime: "17"
smallprime: "11"
generator: "4"
e2e:
prime: "0x11"
smallprime: "0x0B"
generator: "0x04"
...
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2019 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package conf
import (
"gitlab.com/elixxir/crypto/cyclic"
"gitlab.com/elixxir/crypto/large"
"gopkg.in/yaml.v2"
"io/ioutil"
"testing"
)
// This test checks that unmarshalling the groups.yaml file
// is equal to the expected groups object.
func TestGroups_UnmarshallingFileEqualsExpected(t *testing.T) {
prime := large.NewInt(int64(17))
smallPrime := large.NewInt(int64(11))
generator := large.NewInt(int64(4))
cmix := cyclic.NewGroup(prime, generator, smallPrime)
e2e := cyclic.NewGroup(prime, generator, smallPrime)
expected := Groups{
CMix: cmix,
E2E: e2e,
}
actual := Groups{}
buf, _ := ioutil.ReadFile("./Groups.yaml")
err := yaml.Unmarshal(buf, &actual)
if err != nil {
t.Errorf("Unable to decode into struct, %v", err)
}
if actual.E2E.GetFingerprint() != expected.E2E.GetFingerprint() {
t.Errorf("Groups object did not match expected values for E2E")
}
if actual.CMix.GetFingerprint() != expected.CMix.GetFingerprint() {
t.Errorf("Groups object did not match expected values for E2E")
}
}
......@@ -6,15 +6,27 @@
package conf
import "gitlab.com/elixxir/crypto/cyclic"
// This object is used by the server instance.
// A viper (or any yaml based) configuration
// can be unmarshalled into this object.
// For viper just use Unmarshal(&params).
type Params struct {
Database DB
Groups Groups
Paths Paths
DBName string
DBUsername string
DBPassword string
DBAddresses []string
CMix *cyclic.Group
E2E *cyclic.Group
CertPath string
KeyPath string
LogPath string
Servers []string
NodeID int `yaml:"nodeId"`
SkipReg bool `yaml:"skipReg"`
Gateways []string
NodeID uint64
SkipReg bool
BatchSize uint64
ServerIdx int
}
---
# Example yaml params file used for testing params object
database:
name: "name"
username: "username"
password: "password"
addresses:
- "127.0.0.1:80"
- "127.0.0.1:80"
- "127.0.0.1:80"
groups:
cmix:
prime: "17"
smallprime: "11"
generator: "4"
e2e:
prime: "0x11"
smallprime: "0x0B"
generator: "0x04"
paths:
cert: "~/.elixxir/cert.crt"
key: "~/.elixxir/key.pem"
log: "~/.elixxir/server.log"
servers:
- "127.0.0.1:80"
- "127.0.0.1:80"
- "127.0.0.1:80"
nodeId: 100
skipReg: true
...
\ No newline at end of file
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2019 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package conf
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"testing"
)
// This test checks that unmarshalling the params.yaml file
// is equal to the expected Params object.
func TestParams_UnmarshallingFileEqualsExpected(t *testing.T) {
buf, _ := ioutil.ReadFile("./params.yaml")
actual := Params{}
err := yaml.Unmarshal(buf, &actual)
if err != nil {
t.Errorf("Unable to decode into struct, %v", err)
}
if actual.NodeID != 100 {
t.Errorf("Params object did not match expected value for NodeID")
}
if actual.SkipReg != true {
t.Errorf("Params object did not match expected value for SkipReg")
}
}
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2019 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package conf
// Paths contains the config params for
// required file paths used by the system
// TODO: maybe create a paths object
// and have this one contain the actual file obj
type Paths struct {
Cert string
Key string
Log string
}
---
cert: "~/.elixxir/cert.crt"
key: "~/.elixxir/key.pem"
log: "~/.elixxir/server.log"
...
\ No newline at end of file
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2019 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package conf
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"reflect"
"testing"
)
// This test checks that unmarshalling the path.yaml file
// is equal to the expected Paths object.
func TestPaths_UnmarshallingFileEqualsExpected(t *testing.T) {
expected := Paths {
Cert: "~/.elixxir/cert.crt",
Key: "~/.elixxir/key.pem",
Log: "~/.elixxir/server.log",
}
buf, _ := ioutil.ReadFile("./paths.yaml")
actual := Paths{}
err := yaml.Unmarshal(buf, &actual)
if err != nil {
t.Errorf("Unable to decode into struct, %v", err)
}
if !reflect.DeepEqual(expected, actual) {
t.Errorf("Paths object did not match expected values")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment