neoneo-go/config/config.go
dauTT 19201dcf52 Implemented rpc server method GetRawTransaction (#135)
* Added utility function GetVarSize

* 1) Added Size method: this implied that Fixed8 implements now the serializable interface. 2) Added few arithmetic operation (Add, Sub, div): this will be used to calculated networkfeeand feePerByte. Changed return value of the Value() method to int instead of int64. Modified fixed8_test accordingly.

* Implemented Size or MarshalJSON method.
- Structs accepting the Size method implement the serializable interface.
- Structs accepting the MarshalJSON method implements the customized json marshaller interface.

* Added fee calculation

* Implemented rcp server method GetRawTransaction

* Updated Tests

* Fixed:
1) NewFixed8 will accept as input int64
2) race condition affecting configDeafault, blockchainDefault

* Simplified Size calculation

* 1) Removed global variable blockchainDefault, configDefault
2) Extended Blockchainer interface to include the methods: References, FeePerByte, SystemFee, NetworkFee
3) Deleted fee_test.go, fee.go. Moved corresponding methods to blockchain_test.go and blockchain.go respectively
4) Amended tx_raw_output.go

* Simplified GetVarSize Method

* Replaced ValueAtAndType with ValueWithType

* Cosmetic changes + Added test case getrawtransaction_7

* Clean up Print statement

* Filled up keys

* Aligned verbose logic with the C#-neo implementation

* Implemented @Kim requests
Refactor server_test.go

* Small fixes

* Fixed verbose logic
Added more tests
Cosmetic changes

* Replaced assert.NoError with require.NoError

* Fixed tests by adding context.Background() as argument

* Fixed tests
2019-02-20 18:39:32 +01:00

138 lines
3.9 KiB
Go

package config
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/CityOfZion/neo-go/pkg/core/transaction"
"github.com/CityOfZion/neo-go/pkg/util"
"github.com/go-yaml/yaml"
"github.com/pkg/errors"
)
const (
userAgentFormat = "/NEO-GO:%s/"
// Valid NetMode constants.
ModeMainNet NetMode = 0x00746e41 // 7630401
ModeTestNet NetMode = 0x74746e41 // 1953787457
ModePrivNet NetMode = 56753 // docker privnet
ModeUnitTestNet NetMode = 0
)
var (
// Version the version of the node, set at build time.
Version string
// BuildTime the time and date the current version of the node built,
// set at build time.
BuildTime string
)
type (
// Config top level struct representing the config
// for the node.
Config struct {
ProtocolConfiguration ProtocolConfiguration `yaml:"ProtocolConfiguration"`
ApplicationConfiguration ApplicationConfiguration `yaml:"ApplicationConfiguration"`
}
// ProtocolConfiguration represents the protocol config.
ProtocolConfiguration struct {
Magic NetMode `yaml:"Magic"`
AddressVersion int64 `yaml:"AddressVersion"`
MaxTransactionsPerBlock int64 `yaml:"MaxTransactionsPerBlock"`
StandbyValidators []string `yaml:"StandbyValidators"`
SeedList []string `yaml:"SeedList"`
SystemFee SystemFee `yaml:"SystemFee"`
}
// SystemFee fees related to system.
SystemFee struct {
EnrollmentTransaction int64 `yaml:"EnrollmentTransaction"`
IssueTransaction int64 `yaml:"IssueTransaction"`
PublishTransaction int64 `yaml:"PublishTransaction"`
RegisterTransaction int64 `yaml:"RegisterTransaction"`
}
// ApplicationConfiguration config specific to the node.
ApplicationConfiguration struct {
DataDirectoryPath string `yaml:"DataDirectoryPath"`
RPCPort uint16 `yaml:"RPCPort"`
NodePort uint16 `yaml:"NodePort"`
Relay bool `yaml:"Relay"`
DialTimeout time.Duration `yaml:"DialTimeout"`
ProtoTickInterval time.Duration `yaml:"ProtoTickInterval"`
MaxPeers int `yaml:"MaxPeers"`
}
// 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"
}
}
// GenerateUserAgent creates user agent string based on build time environment.
func (c Config) GenerateUserAgent() string {
return fmt.Sprintf(userAgentFormat, Version)
}
// Loadattempts to load the config from the give
// path and 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{},
}
err = yaml.Unmarshal(configData, &config)
if err != nil {
return Config{}, errors.Wrap(err, "Problem unmarshaling config json data")
}
return config, nil
}
// 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.NewFixed8(s.EnrollmentTransaction)
case transaction.IssueType:
return util.NewFixed8(s.IssueTransaction)
case transaction.PublishType:
return util.NewFixed8(s.PublishTransaction)
case transaction.RegisterType:
return util.NewFixed8(s.RegisterTransaction)
default:
return util.NewFixed8(0)
}
}