From 26d38dc9e2ac05161d32d4e07b397bca0db38645 Mon Sep 17 00:00:00 2001 From: Lan Phan Date: Sat, 7 Oct 2023 16:41:28 +0700 Subject: [PATCH] correct local env setup and replace io.ioutil.* by os.* --- core/config/config.go | 13 +++++------ core/config/gen-localhost/main.go | 36 +++++++++++++++++++++++++------ core/config/toml.go | 34 ++++++++++++----------------- db/database.go | 3 +-- worker/mock.go | 20 ++++++++--------- 5 files changed, 61 insertions(+), 45 deletions(-) diff --git a/core/config/config.go b/core/config/config.go index 80d393db..19527de2 100644 --- a/core/config/config.go +++ b/core/config/config.go @@ -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 { diff --git a/core/config/gen-localhost/main.go b/core/config/gen-localhost/main.go index 234d3834..477d2828 100644 --- a/core/config/gen-localhost/main.go +++ b/core/config/gen-localhost/main.go @@ -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() { @@ -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() { @@ -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() { diff --git a/core/config/toml.go b/core/config/toml.go index 755cfe0a..1a0e96ac 100644 --- a/core/config/toml.go +++ b/core/config/toml.go @@ -2,9 +2,8 @@ package config import ( "bytes" - "io/ioutil" + "os" "text/template" - "time" ) const defaultConfigTemplate = `# This is a TOML config file. @@ -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 @@ -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) } diff --git a/db/database.go b/db/database.go index d9b31565..35cd385f 100644 --- a/db/database.go +++ b/db/database.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "os" "path/filepath" "sort" @@ -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 } diff --git a/worker/mock.go b/worker/mock.go index 383d7c22..7c26de89 100644 --- a/worker/mock.go +++ b/worker/mock.go @@ -2,10 +2,10 @@ package worker import ( "encoding/hex" + "os" "encoding/json" "fmt" - "io/ioutil" "math/big" "path/filepath" "runtime" @@ -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) } @@ -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 } } @@ -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) } @@ -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 } @@ -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) } @@ -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 } } @@ -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) } @@ -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)