Merge pull request #78 from meshplus/refactor/genesis
refactor(config): read genesis config from genesis.json
This commit is contained in:
commit
b9c3a2b4e9
|
@ -15,28 +15,30 @@ import (
|
|||
)
|
||||
|
||||
type ChainBrokerService struct {
|
||||
config *repo.Config
|
||||
api api.CoreAPI
|
||||
server *grpc.Server
|
||||
logger logrus.FieldLogger
|
||||
config *repo.Config
|
||||
genesis *repo.Genesis
|
||||
api api.CoreAPI
|
||||
server *grpc.Server
|
||||
logger logrus.FieldLogger
|
||||
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func NewChainBrokerService(api api.CoreAPI, config *repo.Config) (*ChainBrokerService, error) {
|
||||
func NewChainBrokerService(api api.CoreAPI, config *repo.Config, genesis *repo.Genesis) (*ChainBrokerService, error) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
server := grpc.NewServer(
|
||||
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
|
||||
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
|
||||
grpc.MaxConcurrentStreams(1000))
|
||||
return &ChainBrokerService{
|
||||
logger: loggers.Logger(loggers.API),
|
||||
config: config,
|
||||
api: api,
|
||||
server: server,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
logger: loggers.Logger(loggers.API),
|
||||
config: config,
|
||||
genesis: genesis,
|
||||
api: api,
|
||||
server: server,
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ func GetChainStatus(cbs *ChainBrokerService) (*pb.Response, error) {
|
|||
}
|
||||
|
||||
func GetValidators(cbs *ChainBrokerService) (*pb.Response, error) {
|
||||
addresses := cbs.config.Genesis.Addresses
|
||||
addresses := cbs.genesis.Addresses
|
||||
v, err := prettyjson.Marshal(addresses)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -81,7 +81,7 @@ func start(ctx *cli.Context) error {
|
|||
}
|
||||
|
||||
// start grpc service
|
||||
b, err := grpc.NewChainBrokerService(api, repo.Config)
|
||||
b, err := grpc.NewChainBrokerService(api, repo.Config, repo.Genesis)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -34,12 +34,4 @@ solo = false
|
|||
verify = true
|
||||
|
||||
[order]
|
||||
plugin = "plugins/raft.so"
|
||||
|
||||
[genesis]
|
||||
addresses = [
|
||||
"0xe6f8c9cf6e38bd506fae93b73ee5e80cc8f73667",
|
||||
"0x8374bb1e41d4a4bb4ac465e74caa37d242825efc",
|
||||
"0x759801eab44c9a9bbc3e09cb7f1f85ac57298708",
|
||||
"0xf2d66e2c27e93ff083ee3999acb678a36bb349bb"
|
||||
]
|
||||
plugin = "plugins/raft.so"
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"addresses": [
|
||||
"0xe6f8c9cf6e38bd506fae93b73ee5e80cc8f73667",
|
||||
"0x8374bb1e41d4a4bb4ac465e74caa37d242825efc",
|
||||
"0x759801eab44c9a9bbc3e09cb7f1f85ac57298708",
|
||||
"0xf2d66e2c27e93ff083ee3999acb678a36bb349bb"
|
||||
]
|
||||
}
|
|
@ -5,10 +5,6 @@ import (
|
|||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/meshplus/bitxhub/pkg/order/etcdraft"
|
||||
|
||||
"github.com/meshplus/bitxhub/pkg/order"
|
||||
|
||||
"github.com/common-nighthawk/go-figure"
|
||||
"github.com/meshplus/bitxhub-kit/types"
|
||||
"github.com/meshplus/bitxhub/internal/executor"
|
||||
|
@ -19,6 +15,8 @@ import (
|
|||
"github.com/meshplus/bitxhub/internal/repo"
|
||||
"github.com/meshplus/bitxhub/internal/router"
|
||||
"github.com/meshplus/bitxhub/internal/storages"
|
||||
"github.com/meshplus/bitxhub/pkg/order"
|
||||
"github.com/meshplus/bitxhub/pkg/order/etcdraft"
|
||||
"github.com/meshplus/bitxhub/pkg/peermgr"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
@ -51,7 +49,7 @@ func NewBitXHub(rep *repo.Repo) (*BitXHub, error) {
|
|||
|
||||
if !rep.Config.Solo {
|
||||
for i, node := range rep.NetworkConfig.Nodes {
|
||||
m[node.ID] = types.String2Address(rep.Config.Genesis.Addresses[i])
|
||||
m[node.ID] = types.String2Address(rep.Genesis.Addresses[i])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -107,7 +105,7 @@ func generateBitXHubWithoutOrder(rep *repo.Repo) (*BitXHub, error) {
|
|||
}
|
||||
|
||||
if ldg.GetChainMeta().Height == 0 {
|
||||
if err := genesis.Initialize(rep.Config, ldg); err != nil {
|
||||
if err := genesis.Initialize(rep.Genesis, ldg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logger.Info("Initialize genesis")
|
||||
|
@ -147,7 +145,7 @@ func NewTesterBitXHub(rep *repo.Repo) (*BitXHub, error) {
|
|||
|
||||
if !rep.Config.Solo {
|
||||
for i, node := range rep.NetworkConfig.Nodes {
|
||||
m[node.ID] = types.String2Address(rep.Config.Genesis.Addresses[i])
|
||||
m[node.ID] = types.String2Address(rep.Genesis.Addresses[i])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,12 +15,12 @@ var (
|
|||
)
|
||||
|
||||
// Initialize initialize block
|
||||
func Initialize(config *repo.Config, lg ledger.Ledger) error {
|
||||
for _, addr := range config.Addresses {
|
||||
func Initialize(genesis *repo.Genesis, lg ledger.Ledger) error {
|
||||
for _, addr := range genesis.Addresses {
|
||||
lg.SetBalance(types.String2Address(addr), 100000000)
|
||||
}
|
||||
|
||||
body, err := json.Marshal(config.Genesis.Addresses)
|
||||
body, err := json.Marshal(genesis.Addresses)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package repo
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
@ -36,7 +37,6 @@ type Config struct {
|
|||
Gateway `json:"gateway"`
|
||||
Log `json:"log"`
|
||||
Cert `json:"cert"`
|
||||
Genesis `json:"genesis"`
|
||||
Txpool `json:"txpool"`
|
||||
Order `json:"order"`
|
||||
}
|
||||
|
@ -164,10 +164,10 @@ func UnmarshalConfig(repoRoot string) (*Config, error) {
|
|||
return config, nil
|
||||
}
|
||||
|
||||
func ReadConfig(path string, config interface{}) error {
|
||||
func ReadConfig(path, configType string, config interface{}) error {
|
||||
v := viper.New()
|
||||
v.SetConfigFile(path)
|
||||
v.SetConfigType("toml")
|
||||
v.SetConfigType(configType)
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -195,3 +195,16 @@ func PathRootWithDefault(path string) (string, error) {
|
|||
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func loadGenesis(repoRoot string) (*Genesis, error) {
|
||||
genesis := &Genesis{}
|
||||
if err := ReadConfig(filepath.Join(repoRoot, "genesis.json"), "json", genesis); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(genesis.Addresses) == 0 {
|
||||
return nil, fmt.Errorf("wrong genesis address number")
|
||||
}
|
||||
|
||||
return genesis, nil
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
func TestReadConfig(t *testing.T) {
|
||||
path := "../../config/network.toml"
|
||||
cfg := &NetworkConfig{}
|
||||
err := ReadConfig(path, cfg)
|
||||
err := ReadConfig(path, "toml", cfg)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.True(t, 1 == cfg.ID)
|
||||
|
|
|
@ -35,7 +35,7 @@ func AddrToPeerInfo(multiAddr string) (*peer.AddrInfo, error) {
|
|||
|
||||
func loadNetworkConfig(repoRoot string) (*NetworkConfig, error) {
|
||||
networkConfig := &NetworkConfig{}
|
||||
if err := ReadConfig(filepath.Join(repoRoot, "network.toml"), networkConfig); err != nil {
|
||||
if err := ReadConfig(filepath.Join(repoRoot, "network.toml"), "toml", networkConfig); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
type Repo struct {
|
||||
Config *Config
|
||||
NetworkConfig *NetworkConfig
|
||||
Genesis *Genesis
|
||||
Key *Key
|
||||
Certs *Certs
|
||||
}
|
||||
|
@ -24,6 +25,11 @@ func Load(repoRoot string) (*Repo, error) {
|
|||
return nil, fmt.Errorf("load network config: %w", err)
|
||||
}
|
||||
|
||||
genesis, err := loadGenesis(repoRoot)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load genesis: %w", err)
|
||||
}
|
||||
|
||||
certs, err := loadCerts(repoRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -37,6 +43,7 @@ func Load(repoRoot string) (*Repo, error) {
|
|||
return &Repo{
|
||||
Config: config,
|
||||
NetworkConfig: networkConfig,
|
||||
Genesis: genesis,
|
||||
Key: key,
|
||||
Certs: certs,
|
||||
}, nil
|
||||
|
|
|
@ -30,12 +30,4 @@ solo = false
|
|||
verify = true
|
||||
|
||||
[order]
|
||||
plugin = "plugins/raft.so"
|
||||
|
||||
[genesis]
|
||||
addresses = [
|
||||
"0xe6f8c9cf6e38bd506fae93b73ee5e80cc8f73667",
|
||||
"0x8374bb1e41d4a4bb4ac465e74caa37d242825efc",
|
||||
"0x759801eab44c9a9bbc3e09cb7f1f85ac57298708",
|
||||
"0xf2d66e2c27e93ff083ee3999acb678a36bb349bb"
|
||||
]
|
||||
plugin = "plugins/raft.so"
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"addresses": [
|
||||
"0xe6f8c9cf6e38bd506fae93b73ee5e80cc8f73667",
|
||||
"0x8374bb1e41d4a4bb4ac465e74caa37d242825efc",
|
||||
"0x759801eab44c9a9bbc3e09cb7f1f85ac57298708",
|
||||
"0xf2d66e2c27e93ff083ee3999acb678a36bb349bb"
|
||||
]
|
||||
}
|
|
@ -30,12 +30,4 @@ solo = false
|
|||
verify = true
|
||||
|
||||
[order]
|
||||
plugin = "plugins/raft.so"
|
||||
|
||||
[genesis]
|
||||
addresses = [
|
||||
"0xe6f8c9cf6e38bd506fae93b73ee5e80cc8f73667",
|
||||
"0x8374bb1e41d4a4bb4ac465e74caa37d242825efc",
|
||||
"0x759801eab44c9a9bbc3e09cb7f1f85ac57298708",
|
||||
"0xf2d66e2c27e93ff083ee3999acb678a36bb349bb"
|
||||
]
|
||||
plugin = "plugins/raft.so"
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"addresses": [
|
||||
"0xe6f8c9cf6e38bd506fae93b73ee5e80cc8f73667",
|
||||
"0x8374bb1e41d4a4bb4ac465e74caa37d242825efc",
|
||||
"0x759801eab44c9a9bbc3e09cb7f1f85ac57298708",
|
||||
"0xf2d66e2c27e93ff083ee3999acb678a36bb349bb"
|
||||
]
|
||||
}
|
|
@ -30,12 +30,4 @@ solo = false
|
|||
verify = true
|
||||
|
||||
[order]
|
||||
plugin = "plugins/raft3.so"
|
||||
|
||||
[genesis]
|
||||
addresses = [
|
||||
"0xe6f8c9cf6e38bd506fae93b73ee5e80cc8f73667",
|
||||
"0x8374bb1e41d4a4bb4ac465e74caa37d242825efc",
|
||||
"0x759801eab44c9a9bbc3e09cb7f1f85ac57298708",
|
||||
"0xf2d66e2c27e93ff083ee3999acb678a36bb349bb"
|
||||
]
|
||||
plugin = "plugins/raft3.so"
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"addresses": [
|
||||
"0xe6f8c9cf6e38bd506fae93b73ee5e80cc8f73667",
|
||||
"0x8374bb1e41d4a4bb4ac465e74caa37d242825efc",
|
||||
"0x759801eab44c9a9bbc3e09cb7f1f85ac57298708",
|
||||
"0xf2d66e2c27e93ff083ee3999acb678a36bb349bb"
|
||||
]
|
||||
}
|
|
@ -30,12 +30,4 @@ solo = false
|
|||
verify = true
|
||||
|
||||
[order]
|
||||
plugin = "plugins/raft4.so"
|
||||
|
||||
[genesis]
|
||||
addresses = [
|
||||
"0xe6f8c9cf6e38bd506fae93b73ee5e80cc8f73667",
|
||||
"0x8374bb1e41d4a4bb4ac465e74caa37d242825efc",
|
||||
"0x759801eab44c9a9bbc3e09cb7f1f85ac57298708",
|
||||
"0xf2d66e2c27e93ff083ee3999acb678a36bb349bb"
|
||||
]
|
||||
plugin = "plugins/raft4.so"
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"addresses": [
|
||||
"0xe6f8c9cf6e38bd506fae93b73ee5e80cc8f73667",
|
||||
"0x8374bb1e41d4a4bb4ac465e74caa37d242825efc",
|
||||
"0x759801eab44c9a9bbc3e09cb7f1f85ac57298708",
|
||||
"0xf2d66e2c27e93ff083ee3999acb678a36bb349bb"
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue