Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct local env setup and replace io.ioutil.* by os.* #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
)

type DbConfig struct {
Host string `toml:"host"`
Port int `toml:"port"`
Username string `toml:"username"`
Password string `toml:"password"`
Schema string `toml:"schema"`
InMemory bool `toml:"in-memory"` // Should only used in tests
Host string `toml:"host"`
Port int `toml:"port"`
Username string `toml:"username"`
Password string `toml:"password"`
Schema string `toml:"schema"`
InMemory bool `toml:"in-memory"` // Should only used in tests
MigrationPath string `toml:"migration-path"`
}

type HeartConfig struct {
Expand Down
36 changes: 29 additions & 7 deletions core/config/gen-localhost/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package main

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/sisu-network/dheart/core/config"
p2ptypes "github.com/sisu-network/dheart/p2p/types"
)

func genLocalhostConfig() {
Expand All @@ -17,16 +17,37 @@ func genLocalhostConfig() {
cfg.Port = 28300
cfg.SisuServerUrl = "http://0.0.0.0:25456"

path, _ := os.Getwd()

cfg.Db = config.DbConfig{
Host: "0.0.0.0",
Port: 3306,
Username: "root",
Password: "password",
Schema: "dheart",
Host: "0.0.0.0",
Port: 3306,
Username: "root",
Password: "password",
Schema: "dheart",
MigrationPath: path + "/db/migrations/",
}

peer := &p2ptypes.Peer{
Address: "/dns/dheart0/tcp/28300/p2p/12D3KooWD6JaQEHnpeCKHZ1bYA9axESG1MyqTRWRLhkf7btYYpRk",
PubKey: "30a84ac6ed8306d5d5160c763cd90a0450eff4f77e3bc1f0fd2cff9abdca0d5f",
PubKeyType: "ed25519",
}
peers := make([]*p2ptypes.Peer, 0)
peers = append(peers, peer)

cfg.Connection = p2ptypes.ConnectionsConfig{
Host: "127.0.0.1",
Port: 28300,
Rendezvous: "rendezvous",
Peers: peers,
}

configFilePath := filepath.Join(cfg.HomeDir, "./dheart.toml")

os.MkdirAll(cfg.HomeDir, os.ModePerm)
config.WriteConfigFile(configFilePath, cfg)
fmt.Printf("Successfully write config file %s\n", configFilePath)
}

func genEnv() {
Expand All @@ -35,7 +56,8 @@ func genEnv() {
AES_KEY_HEX=c787ef22ade5afc8a5e22041c17869e7e4714190d88ecec0a84e241c9431add0
`, homeDir)

ioutil.WriteFile(".env", []byte(content), 0600)
os.WriteFile(".env", []byte(content), 0600)
fmt.Printf("Successfully write config file .env\n")
}

func main() {
Expand Down
34 changes: 14 additions & 20 deletions core/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package config

import (
"bytes"
"io/ioutil"
"os"
"text/template"
"time"
)

const defaultConfigTemplate = `# This is a TOML config file.
Expand All @@ -20,17 +19,22 @@ port = {{ .Port }}
### Database Configuration ###
###############################################################################
[db]
host = "{{ .Db.Host }}"
port = {{ .Db.Port }}
username = "{{ .Db.Username }}"
password = "{{ .Db.Password }}"
schema = "{{ .Db.Schema }}"
migration-path = "{{ .Db.MigrationPath }}"
host = "{{ .Db.Host }}"
port = {{ .Db.Port }}
username = "{{ .Db.Username }}"
password = "{{ .Db.Password }}"
schema = "{{ .Db.Schema }}"
migration-path = "{{ .Db.MigrationPath }}"
[connection]
host = "0.0.0.0"
port = 28300
rendezvous = "rendezvous"
peers = {{ .Connection.BootstrapPeers }}
{{ range .Connection.Peers }}
[[connection.peers]]
address = "{{ .Address }}"
pubkey = "{{ .PubKey }}"
pubkey_type = "{{ .PubKeyType }}"
{{end}}
`

var configTemplate *template.Template
Expand All @@ -52,15 +56,5 @@ func WriteConfigFile(configFilePath string, config HeartConfig) {
panic(err)
}

ioutil.WriteFile(configFilePath, buffer.Bytes(), 0600)
}

type duration struct {
time.Duration
}

func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
os.WriteFile(configFilePath, buffer.Bytes(), 0600)
}
3 changes: 1 addition & 2 deletions db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -185,7 +184,7 @@ func (d *SqlDatabase) inMemoryMigration() error {
}
defer os.RemoveAll(migrationDir)

files, err := ioutil.ReadDir(migrationDir)
files, err := os.ReadDir(migrationDir)
if err != nil {
return err
}
Expand Down
20 changes: 10 additions & 10 deletions worker/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package worker

import (
"encoding/hex"
"os"

"encoding/json"
"fmt"
"io/ioutil"
"math/big"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -221,12 +221,12 @@ func GetTestSavedFileName(dirFormat, fileFormat string, index int) string {

func SaveTestPreparams(index int, bz []byte) error {
fileName := GetTestSavedFileName(TestEcPreparamsFixtureDirFormat, TestEcPreparamsFixtureFileFormat, index)
return ioutil.WriteFile(fileName, bz, 0600)
return os.WriteFile(fileName, bz, 0600)
}

func LoadEcPreparams(index int) *eckeygen.LocalPreParams {
fileName := GetTestSavedFileName(TestEcPreparamsFixtureDirFormat, TestEcPreparamsFixtureFileFormat, index)
bz, err := ioutil.ReadFile(fileName)
bz, err := os.ReadFile(fileName)
if err != nil {
panic(err)
}
Expand All @@ -249,7 +249,7 @@ func SaveEcKeygenOutput(outputs []*eckeygen.LocalPartySaveData) error {
panic(err)
}

if err := ioutil.WriteFile(fileName, bz, 0600); err != nil {
if err := os.WriteFile(fileName, bz, 0600); err != nil {
return err
}
}
Expand All @@ -264,7 +264,7 @@ func LoadEcKeygenSavedData(pids tss.SortedPartyIDs) []*eckeygen.LocalPartySaveDa
for i := 0; i < len(PRIVATE_KEY_HEX); i++ {
fileName := GetTestSavedFileName(TestEcKeygenSavedDataFixtureDirFormat, TestEcKeygenSavedDataFixtureFileFormat, i)

bz, err := ioutil.ReadFile(fileName)
bz, err := os.ReadFile(fileName)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -302,7 +302,7 @@ func SaveEcPresignData(n int, keygenOutputs []*eckeygen.LocalPartySaveData, data
panic(err)
}

if err := ioutil.WriteFile(fileName, bz, 0600); err != nil {
if err := os.WriteFile(fileName, bz, 0600); err != nil {
return err
}

Expand All @@ -311,7 +311,7 @@ func SaveEcPresignData(n int, keygenOutputs []*eckeygen.LocalPartySaveData, data

func LoadEcPresignSavedData() *PresignDataWrapper {
fileName := GetTestSavedFileName(TestEcPresignSavedDataFixtureDirFormat, TestEcPresignSavedDataFixtureFileFormat, 0)
bz, err := ioutil.ReadFile(fileName)
bz, err := os.ReadFile(fileName)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -342,7 +342,7 @@ func SaveEdKeygenOutput(data []*edkeygen.LocalPartySaveData) error {
panic(err)
}

if err := ioutil.WriteFile(fileName, bz, 0600); err != nil {
if err := os.WriteFile(fileName, bz, 0600); err != nil {
return err
}
}
Expand All @@ -355,7 +355,7 @@ func LoadEdKeygenSavedData(pids tss.SortedPartyIDs) []*edkeygen.LocalPartySaveDa

for i := 0; i < len(pids); i++ {
fileName := GetTestSavedFileName(TestEdKeygenSavedDataFixtureDirFormat, TestEdKeygenSavedDataFixtureFileFormat, i)
bz, err := ioutil.ReadFile(fileName)
bz, err := os.ReadFile(fileName)
if err != nil {
panic(err)
}
Expand All @@ -379,7 +379,7 @@ func LoadEdKeygenSavedData(pids tss.SortedPartyIDs) []*edkeygen.LocalPartySaveDa
return savedData
}

/////
// ///
type MockMessageDispatcher struct {
BroadcastMessageFunc func(pIDs []*tss.PartyID, tssMessage *common.TssMessage)
UnicastMessageFunc func(dest *tss.PartyID, tssMessage *common.TssMessage)
Expand Down