bitxhub/pkg/order/config.go

121 lines
2.4 KiB
Go
Raw Normal View History

2020-03-29 21:32:01 +08:00
package order
import (
"fmt"
"github.com/meshplus/bitxhub-kit/crypto"
"github.com/meshplus/bitxhub-kit/types"
"github.com/meshplus/bitxhub-model/pb"
"github.com/meshplus/bitxhub/pkg/peermgr"
"github.com/sirupsen/logrus"
)
type Config struct {
ID uint64
RepoRoot string
StoragePath string
PluginPath string
PeerMgr peermgr.PeerManager
PrivKey crypto.PrivateKey
Logger logrus.FieldLogger
Nodes map[uint64]types.Address
Applied uint64
Digest string
2020-10-22 13:49:05 +08:00
GetTransactionFunc func(hash *types.Hash) (*pb.Transaction, error)
2020-03-29 21:32:01 +08:00
GetChainMetaFunc func() *pb.ChainMeta
}
type Option func(*Config)
func WithID(id uint64) Option {
return func(config *Config) {
config.ID = id
}
}
func WithRepoRoot(path string) Option {
return func(config *Config) {
config.RepoRoot = path
}
}
func WithStoragePath(path string) Option {
return func(config *Config) {
config.StoragePath = path
}
}
func WithPluginPath(path string) Option {
return func(config *Config) {
config.PluginPath = path
}
}
func WithPeerManager(peerMgr peermgr.PeerManager) Option {
return func(config *Config) {
config.PeerMgr = peerMgr
}
}
func WithPrivKey(privKey crypto.PrivateKey) Option {
return func(config *Config) {
config.PrivKey = privKey
}
}
func WithLogger(logger logrus.FieldLogger) Option {
return func(config *Config) {
config.Logger = logger
}
}
func WithNodes(nodes map[uint64]types.Address) Option {
return func(config *Config) {
config.Nodes = nodes
}
}
func WithApplied(height uint64) Option {
return func(config *Config) {
config.Applied = height
}
}
func WithDigest(digest string) Option {
return func(config *Config) {
config.Digest = digest
}
}
func WithGetChainMetaFunc(f func() *pb.ChainMeta) Option {
return func(config *Config) {
config.GetChainMetaFunc = f
}
}
2020-10-22 13:49:05 +08:00
func WithGetTransactionFunc(f func(hash *types.Hash) (*pb.Transaction, error)) Option {
2020-03-29 21:32:01 +08:00
return func(config *Config) {
config.GetTransactionFunc = f
}
}
func checkConfig(config *Config) error {
if config.Logger == nil {
return fmt.Errorf("logger is nil")
}
return nil
}
func GenerateConfig(opts ...Option) (*Config, error) {
config := &Config{}
for _, opt := range opts {
opt(config)
}
if err := checkConfig(config); err != nil {
return nil, fmt.Errorf("create p2p: %w", err)
}
return config, nil
}