Skip to content

Commit

Permalink
change part of the code structure
Browse files Browse the repository at this point in the history
* change global conf struct and rename struct
* upd mod file
* rm unused code
* add tool blockid
  • Loading branch information
scottafk committed Dec 22, 2021
1 parent 3101b5f commit 8e84738
Show file tree
Hide file tree
Showing 57 changed files with 1,166 additions and 1,455 deletions.
Empty file modified build.sh
100644 → 100755
Empty file.
64 changes: 35 additions & 29 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var configCmd = &cobra.Command{
}

if configPath == "" {
configPath = filepath.Join(conf.Config.DataDir, consts.DefaultConfigFile)
configPath = filepath.Join(conf.Config.DirPathConf.DataDir, consts.DefaultConfigFile)
}
err = viper.Unmarshal(&conf.Config)
if err != nil {
Expand All @@ -52,6 +52,32 @@ func init() {
// Command flags
cmdFlags.String("path", "", "Generate config to (default dataDir/config.toml)")

// Etc
cmdFlags.StringVar(&conf.Config.DirPathConf.PidFilePath, "pid", "",
fmt.Sprintf("ibax pid file name (default dataDir/%s)", consts.DefaultPidFilename),
)
cmdFlags.StringVar(&conf.Config.DirPathConf.LockFilePath, "lock", "",
fmt.Sprintf("ibax lock file name (default dataDir/%s)", consts.DefaultLockFilename),
)
cmdFlags.StringVar(&conf.Config.DirPathConf.KeysDir, "keysDir", "", "Keys directory (default dataDir)")
cmdFlags.StringVar(&conf.Config.DirPathConf.DataDir, "dataDir", "", "Data directory (default cwd/data)")
cmdFlags.StringVar(&conf.Config.DirPathConf.TempDir, "tempDir", "", "Temporary directory (default temporary directory of OS)")
cmdFlags.StringVar(&conf.Config.DirPathConf.FirstBlockPath, "firstBlock", "", "First block path (default dataDir/1block)")

// tls
cmdFlags.BoolVar(&conf.Config.TLSConf.Enabled, "tlsEnable", false, "Enable https")
cmdFlags.StringVar(&conf.Config.TLSConf.TLSCert, "tlsCert", "", "Filepath to the fullchain of certificates")
cmdFlags.StringVar(&conf.Config.TLSConf.TLSKey, "tlsKey", "", "Filepath to the private key")

//Bootstrap
cmdFlags.StringSliceVar(&conf.Config.BootNodes.NodesAddr, "bootNodes", []string{}, "List of addresses for downloading blockchain")

//LocalConf
cmdFlags.Int64Var(&conf.Config.LocalConf.MaxPageGenerationTime, "mpgt", 3000, "Max page generation time in ms")
cmdFlags.Int64Var(&conf.Config.LocalConf.HTTPServerMaxBodySize, "mbs", 1<<20, "Max server body size in byte")
cmdFlags.Int64Var(&conf.Config.LocalConf.NetworkID, "networkID", 1, "Network ID")
cmdFlags.StringVar(&conf.Config.LocalConf.RunNodeMode, "runMode", consts.NoneOBS, "running node mode, example NONE|OBS|OBSMaster|SubNode")

// TCP Server
cmdFlags.StringVar(&conf.Config.TCPServer.Host, "tcpHost", "127.0.0.1", "Node TCP host")
cmdFlags.IntVar(&conf.Config.TCPServer.Port, "tcpPort", 7078, "Node TCP port")
Expand All @@ -72,11 +98,11 @@ func init() {
cmdFlags.IntVar(&conf.Config.DB.MaxOpenConns, "dbMaxOpenConns", 100, "sets the maximum number of open connections to the database")

//Redis
cmdFlags.BoolVar(&conf.Config.Redis.Enable, "redisenable", false, "enable redis")
cmdFlags.StringVar(&conf.Config.Redis.Host, "redishost", "localhost", "redis host")
cmdFlags.StringVar(&conf.Config.Redis.Port, "redisport", "6379", "redis port")
cmdFlags.IntVar(&conf.Config.Redis.DbName, "redisdb", 0, "redis db")
cmdFlags.StringVar(&conf.Config.Redis.Password, "redispassword", "123456", "redis password")
cmdFlags.BoolVar(&conf.Config.Redis.Enable, "redisEnable", false, "enable redis")
cmdFlags.StringVar(&conf.Config.Redis.Host, "redisHost", "localhost", "redis host")
cmdFlags.IntVar(&conf.Config.Redis.Port, "redisPort", 6379, "redis port")
cmdFlags.IntVar(&conf.Config.Redis.DbName, "redisDb", 0, "redis db")
cmdFlags.StringVar(&conf.Config.Redis.Password, "redisPassword", "123456", "redis password")

// StatsD
cmdFlags.StringVar(&conf.Config.StatsD.Host, "statsdHost", "127.0.0.1", "StatsD host")
Expand Down Expand Up @@ -108,29 +134,9 @@ func init() {
cmdFlags.IntVar(&conf.Config.BanKey.BanTime, "banTime", 15, "Ban time in minutes")
cmdFlags.IntVar(&conf.Config.BanKey.BadTx, "badTx", 5, "Maximum bad tx during badTime minutes")

// Etc
cmdFlags.StringVar(&conf.Config.PidFilePath, "pid", "",
fmt.Sprintf("ibax pid file name (default dataDir/%s)", consts.DefaultPidFilename),
)
cmdFlags.StringVar(&conf.Config.LockFilePath, "lock", "",
fmt.Sprintf("ibax lock file name (default dataDir/%s)", consts.DefaultLockFilename),
)
cmdFlags.StringVar(&conf.Config.KeysDir, "keysDir", "", "Keys directory (default dataDir)")
cmdFlags.StringVar(&conf.Config.DataDir, "dataDir", "", "Data directory (default cwd/data)")
cmdFlags.StringVar(&conf.Config.TempDir, "tempDir", "", "Temporary directory (default temporary directory of OS)")
cmdFlags.StringVar(&conf.Config.FirstBlockPath, "firstBlock", "", "First block path (default dataDir/1block)")
cmdFlags.BoolVar(&conf.Config.TLS, "tls", false, "Enable https")
cmdFlags.StringVar(&conf.Config.TLSCert, "tls-cert", "", "Filepath to the fullchain of certificates")
cmdFlags.StringVar(&conf.Config.TLSKey, "tls-key", "", "Filepath to the private key")
cmdFlags.Int64Var(&conf.Config.MaxPageGenerationTime, "mpgt", 3000, "Max page generation time in ms")
cmdFlags.Int64Var(&conf.Config.HTTPServerMaxBodySize, "mbs", 1<<20, "Max server body size in byte")
cmdFlags.StringSliceVar(&conf.Config.NodesAddr, "nodesAddr", []string{}, "List of addresses for downloading blockchain")
cmdFlags.Int64Var(&conf.Config.NetworkID, "networkID", 1, "Network ID")
cmdFlags.StringVar(&conf.Config.OBSMode, "obsMode", consts.NoneOBS, "OBS running mode")

// GFiles
cmdFlags.BoolVar(&conf.Config.GFiles.GFiles, "gfs", false, "Enable GFiles")
cmdFlags.StringVar(&conf.Config.GFiles.Host, "gFilesHost", "127.0.0.1:5001", "GFiles host")
// IPFS
cmdFlags.BoolVar(&conf.Config.IpfsConf.Enabled, "ipfsEnable", false, "Enable IPFS")
cmdFlags.StringVar(&conf.Config.IpfsConf.Host, "ipfsHost", "127.0.0.1:5001", "IPFS host")

// CryptoSettings
cmdFlags.StringVar(&conf.Config.CryptoSettings.Hasher, "hasher", "SHA256", "Hash Algorithm")
Expand Down
11 changes: 5 additions & 6 deletions cmd/generateFirstBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/IBAX-io/go-ibax/packages/converter"
"github.com/IBAX-io/go-ibax/packages/crypto"
"github.com/IBAX-io/go-ibax/packages/types"
"github.com/IBAX-io/go-ibax/packages/utils"

log "github.com/sirupsen/logrus"
)
Expand All @@ -38,7 +37,7 @@ var generateFirstBlockCmd = &cobra.Command{
if err != nil {
log.WithFields(log.Fields{"type": consts.MarshallingError, "error": err}).Fatal("first block marshalling")
}
os.WriteFile(conf.Config.FirstBlockPath, block, 0644)
os.WriteFile(conf.Config.DirPathConf.FirstBlockPath, block, 0644)
log.Info("first block generated")
},
}
Expand All @@ -51,7 +50,7 @@ func init() {

func genesisBlock() ([]byte, error) {
now := time.Now().Unix()
header := &utils.BlockData{
header := &types.BlockData{
BlockID: 1,
Time: now,
EcosystemID: 0,
Expand All @@ -61,7 +60,7 @@ func genesisBlock() ([]byte, error) {
}

decodeKeyFile := func(kName string) []byte {
filepath := filepath.Join(conf.Config.KeysDir, kName)
filepath := filepath.Join(conf.Config.DirPathConf.KeysDir, kName)
data, err := os.ReadFile(filepath)
if err != nil {
log.WithError(err).WithFields(log.Fields{"key": kName, "filepath": filepath}).Fatal("Reading key data")
Expand All @@ -78,7 +77,7 @@ func genesisBlock() ([]byte, error) {
var stopNetworkCert []byte
if len(stopNetworkBundleFilepath) > 0 {
var err error
fp := filepath.Join(conf.Config.KeysDir, stopNetworkBundleFilepath)
fp := filepath.Join(conf.Config.DirPathConf.KeysDir, stopNetworkBundleFilepath)
if stopNetworkCert, err = os.ReadFile(fp); err != nil {
log.WithError(err).WithFields(log.Fields{"filepath": fp}).Fatal("Reading cert data")
}
Expand Down Expand Up @@ -116,7 +115,7 @@ func genesisBlock() ([]byte, error) {
log.WithFields(log.Fields{"type": consts.MarshallingError, "error": err}).Fatal("first block body bin marshalling")
}

return block.MarshallBlock(header, [][]byte{tx}, &utils.BlockData{
return block.MarshallBlock(header, [][]byte{tx}, &types.BlockData{
Hash: []byte(`0`),
RollbacksHash: []byte(`0`),
}, "")
Expand Down
16 changes: 8 additions & 8 deletions cmd/generateKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"path/filepath"
"strconv"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/IBAX-io/go-ibax/packages/conf"
"github.com/IBAX-io/go-ibax/packages/consts"
"github.com/IBAX-io/go-ibax/packages/crypto"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const fileMode = 0600
Expand All @@ -28,23 +28,23 @@ var generateKeysCmd = &cobra.Command{
PreRun: loadConfig,
Run: func(cmd *cobra.Command, args []string) {
_, publicKey, err := createKeyPair(
filepath.Join(conf.Config.KeysDir, consts.PrivateKeyFilename),
filepath.Join(conf.Config.KeysDir, consts.PublicKeyFilename),
filepath.Join(conf.Config.DirPathConf.KeysDir, consts.PrivateKeyFilename),
filepath.Join(conf.Config.DirPathConf.KeysDir, consts.PublicKeyFilename),
)
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("generating user keys")
return
}
_, _, err = createKeyPair(
filepath.Join(conf.Config.KeysDir, consts.NodePrivateKeyFilename),
filepath.Join(conf.Config.KeysDir, consts.NodePublicKeyFilename),
filepath.Join(conf.Config.DirPathConf.KeysDir, consts.NodePrivateKeyFilename),
filepath.Join(conf.Config.DirPathConf.KeysDir, consts.NodePublicKeyFilename),
)
if err != nil {
log.WithFields(log.Fields{"error": err}).Fatal("generating node keys")
return
}
address := crypto.Address(publicKey)
keyIDPath := filepath.Join(conf.Config.KeysDir, consts.KeyIDFilename)
keyIDPath := filepath.Join(conf.Config.DirPathConf.KeysDir, consts.KeyIDFilename)
err = createFile(keyIDPath, []byte(strconv.FormatInt(address, 10)))
if err != nil {
log.WithFields(log.Fields{"error": err, "path": keyIDPath}).Fatal("generating node keys")
Expand Down
10 changes: 2 additions & 8 deletions cmd/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,10 @@ var rollbackCmd = &cobra.Command{
Short: "Rollback blockchain to blockID",
PreRun: loadConfigWKey,
Run: func(cmd *cobra.Command, args []string) {
f := utils.LockOrDie(conf.Config.LockFilePath)
f := utils.LockOrDie(conf.Config.DirPathConf.LockFilePath)
defer f.Unlock()

if err := model.GormInit(
conf.Config.DB.Host,
conf.Config.DB.Port,
conf.Config.DB.User,
conf.Config.DB.Password,
conf.Config.DB.Name,
); err != nil {
if err := model.GormInit(conf.Config.DB); err != nil {
log.WithError(err).Fatal("init db")
return
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/stopNetwork.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var stopNetworkCmd = &cobra.Command{
Short: "Sending a special transaction to stop the network",
PreRun: loadConfigWKey,
Run: func(cmd *cobra.Command, args []string) {
fp := filepath.Join(conf.Config.KeysDir, stopNetworkCertFilepath)
fp := filepath.Join(conf.Config.DirPathConf.KeysDir, stopNetworkCertFilepath)
stopNetworkCert, err := os.ReadFile(fp)
if err != nil {
log.WithFields(log.Fields{"error": err, "type": consts.IOError, "filepath": fp}).Fatal("Reading cert data")
Expand Down
Loading

0 comments on commit 8e84738

Please sign in to comment.