Skip to content

Commit

Permalink
config: move config.go out of config/
Browse files Browse the repository at this point in the history
closes #423
  • Loading branch information
AnnaShaleva committed Mar 31, 2020
1 parent 396c78c commit d5435a0
Show file tree
Hide file tree
Showing 7 changed files with 217 additions and 199 deletions.
196 changes: 0 additions & 196 deletions config/config.go

This file was deleted.

30 changes: 30 additions & 0 deletions pkg/config/application_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

import (
"time"

"github.com/nspcc-dev/neo-go/pkg/core/storage"
"github.com/nspcc-dev/neo-go/pkg/network/metrics"
"github.com/nspcc-dev/neo-go/pkg/rpc"
"github.com/nspcc-dev/neo-go/pkg/wallet"
)

// ApplicationConfiguration config specific to the node.
type ApplicationConfiguration struct {
LogPath string `yaml:"LogPath"`
DBConfiguration storage.DBConfiguration `yaml:"DBConfiguration"`
Address string `yaml:"Address"`
NodePort uint16 `yaml:"NodePort"`
Relay bool `yaml:"Relay"`
DialTimeout time.Duration `yaml:"DialTimeout"`
ProtoTickInterval time.Duration `yaml:"ProtoTickInterval"`
PingInterval time.Duration `yaml:"PingInterval"`
PingTimeout time.Duration `yaml:"PingTimeout"`
MaxPeers int `yaml:"MaxPeers"`
AttemptConnPeers int `yaml:"AttemptConnPeers"`
MinPeers int `yaml:"MinPeers"`
Prometheus metrics.Config `yaml:"Prometheus"`
Pprof metrics.Config `yaml:"Pprof"`
RPC rpc.Config `yaml:"RPC"`
UnlockWallet wallet.Config `yaml:"UnlockWallet"`
}
60 changes: 60 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package config

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

"github.com/go-yaml/yaml"
"github.com/pkg/errors"
)

const userAgentFormat = "/NEO-GO:%s/"

var
// Version the version of the node, set at build time.
Version string

type
// Config top level struct representing the config
// for the node.
Config struct {
ProtocolConfiguration ProtocolConfiguration `yaml:"ProtocolConfiguration"`
ApplicationConfiguration ApplicationConfiguration `yaml:"ApplicationConfiguration"`
}

// GenerateUserAgent creates user agent string based on build time environment.
func (c Config) GenerateUserAgent() string {
return fmt.Sprintf(userAgentFormat, Version)
}

// Load attempts to load the config from the given
// path for the given netMode.
func Load(path string, netMode NetMode) (Config, error) {
configPath := fmt.Sprintf("%s/protocol.%s.yml", path, netMode)
if _, err := os.Stat(configPath); os.IsNotExist(err) {
return Config{}, errors.Wrap(err, "Unable to load config")
}

configData, err := ioutil.ReadFile(configPath)
if err != nil {
return Config{}, errors.Wrap(err, "Unable to read config")
}

config := Config{
ProtocolConfiguration: ProtocolConfiguration{
SystemFee: SystemFee{},
},
ApplicationConfiguration: ApplicationConfiguration{
PingInterval: 30,
PingTimeout: 90,
},
}

err = yaml.Unmarshal(configData, &config)
if err != nil {
return Config{}, errors.Wrap(err, "Problem unmarshaling config json data")
}

return config, nil
}
90 changes: 90 additions & 0 deletions pkg/config/protocol_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package config

import (
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/util"
)

const (
// ModeMainNet contains magic code used in the NEO main official network.
ModeMainNet NetMode = 0x00746e41 // 7630401
// ModeTestNet contains magic code used in the NEO testing network.
ModeTestNet NetMode = 0x74746e41 // 1953787457
// ModePrivNet contains magic code usually used for NEO private networks.
ModePrivNet NetMode = 56753 // docker privnet
// ModeUnitTestNet is a stub magic code used for testing purposes.
ModeUnitTestNet NetMode = 0
)

// ProtocolConfiguration represents the protocol config.
type (
ProtocolConfiguration struct {
Magic NetMode `yaml:"Magic"`
AddressVersion byte `yaml:"AddressVersion"`
SecondsPerBlock int `yaml:"SecondsPerBlock"`
LowPriorityThreshold float64 `yaml:"LowPriorityThreshold"`
MaxTransactionsPerBlock int `yaml:"MaxTransactionsPerBlock"`
MemPoolSize int `yaml:"MemPoolSize"`
StandbyValidators []string `yaml:"StandbyValidators"`
SeedList []string `yaml:"SeedList"`
SystemFee SystemFee `yaml:"SystemFee"`
// Whether to verify received blocks.
VerifyBlocks bool `yaml:"VerifyBlocks"`
// Whether to verify transactions in received blocks.
VerifyTransactions bool `yaml:"VerifyTransactions"`
// FreeGasLimit is an amount of GAS which can be spent for free.
FreeGasLimit util.Fixed8 `yaml:"FreeGasLimit"`
// SaveStorageBatch enables storage batch saving before every persist.
SaveStorageBatch bool `yaml:"SaveStorageBatch"`
// Maximum number of low priority transactions accepted into block.
MaxFreeTransactionsPerBlock int `yaml:"MaxFreeTransactionsPerBlock"`
// Maximum size of low priority transaction in bytes.
MaxFreeTransactionSize int `yaml:"MaxFreeTransactionSize"`
// FeePerExtraByte sets the expected per-byte fee for
// transactions exceeding the MaxFreeTransactionSize.
FeePerExtraByte float64 `yaml:"FeePerExtraByte"`
}

// SystemFee fees related to system.
SystemFee struct {
EnrollmentTransaction int64 `yaml:"EnrollmentTransaction"`
IssueTransaction int64 `yaml:"IssueTransaction"`
PublishTransaction int64 `yaml:"PublishTransaction"`
RegisterTransaction int64 `yaml:"RegisterTransaction"`
}

// NetMode describes the mode the blockchain will operate on.
NetMode uint32
)

// String implements the stringer interface.
func (n NetMode) String() string {
switch n {
case ModePrivNet:
return "privnet"
case ModeTestNet:
return "testnet"
case ModeMainNet:
return "mainnet"
case ModeUnitTestNet:
return "unit_testnet"
default:
return "net unknown"
}
}

// TryGetValue returns the system fee base on transaction type.
func (s SystemFee) TryGetValue(txType transaction.TXType) util.Fixed8 {
switch txType {
case transaction.EnrollmentType:
return util.Fixed8FromInt64(s.EnrollmentTransaction)
case transaction.IssueType:
return util.Fixed8FromInt64(s.IssueTransaction)
case transaction.PublishType:
return util.Fixed8FromInt64(s.PublishTransaction)
case transaction.RegisterType:
return util.Fixed8FromInt64(s.RegisterTransaction)
default:
return util.Fixed8FromInt64(0)
}
}
Loading

0 comments on commit d5435a0

Please sign in to comment.