From 13d143fa3aa00b9d433c2dffb7a97a3b94fbe1d2 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ Date: Thu, 23 May 2019 04:02:13 +0800 Subject: [PATCH] wip config --- asset/asset.go | 6 +++++- asset/builder.go | 11 +++++++---- config/config.go | 23 ++++++++++++++++++----- config/toml.go | 13 +++++++++---- node/node.go | 6 +++++- test/wallet_test_util.go | 2 +- wallet/unconfirmed_test.go | 2 +- wallet/wallet_test.go | 4 ++-- 8 files changed, 48 insertions(+), 19 deletions(-) diff --git a/asset/asset.go b/asset/asset.go index 3328ee52..bd18f271 100644 --- a/asset/asset.go +++ b/asset/asset.go @@ -8,6 +8,7 @@ import ( "github.com/golang/groupcache/lru" + cfg "github.com/vapor/config" "github.com/vapor/consensus" dbm "github.com/vapor/database/leveldb" chainjson "github.com/vapor/encoding/json" @@ -70,9 +71,10 @@ var ( ) //NewRegistry create new registry -func NewRegistry(db dbm.DB, chain *protocol.Chain) *Registry { +func NewRegistry(db dbm.DB, chain *protocol.Chain, config *cfg.Config) *Registry { initNativeAsset() return &Registry{ + config: config, db: db, chain: chain, cache: lru.New(maxAssetCache), @@ -82,6 +84,8 @@ func NewRegistry(db dbm.DB, chain *protocol.Chain) *Registry { // Registry tracks and stores all known assets on a blockchain. type Registry struct { + config *cfg.Config + db dbm.DB chain *protocol.Chain diff --git a/asset/builder.go b/asset/builder.go index 3cd4bfcd..ad9be421 100644 --- a/asset/builder.go +++ b/asset/builder.go @@ -31,17 +31,20 @@ type crossInAction struct { bc.AssetAmount SourceID string `json:"source_id"` SourcePos uint64 `json:"source_pos"` - FedXPubs []chainkd.XPub `json:"fed_xpubs"` - Quorum int `json:"fed_quorum"` AssetDefinition map[string]interface{} `json:"asset_definition"` + // FedXPubs []chainkd.XPub `json:"fed_xpubs"` + // Quorum int `json:"fed_quorum"` } // TODO: also need to hard-code mapTx func (a *crossInAction) Build(ctx context.Context, builder *txbuilder.TemplateBuilder) error { var missing []string - if len(a.FedXPubs) == 0 { + if len(a.reg.config.Fed.XPubs) <= 1 { missing = append(missing, "fed_xpubs") } + if a.reg.config.Fed.Quorum == 0 { + missing = append(missing, "fed_quorum") + } if a.SourceID == "" { missing = append(missing, "source_id") } @@ -84,7 +87,7 @@ func (a *crossInAction) Build(ctx context.Context, builder *txbuilder.TemplateBu a.reg.SaveExtAsset(asset, extAlias) } - assetSigner, err := signers.Create("asset", a.FedXPubs, a.Quorum, 1, signers.BIP0032) + assetSigner, err := signers.Create("asset", a.reg.config.Fed.XPubs, a.reg.config.Fed.Quorum, 1, signers.BIP0032) if err != nil { return err } diff --git a/config/config.go b/config/config.go index 69b4882e..5ee5f205 100644 --- a/config/config.go +++ b/config/config.go @@ -22,11 +22,12 @@ type Config struct { // Top level options use an anonymous struct BaseConfig `mapstructure:",squash"` // Options for services - P2P *P2PConfig `mapstructure:"p2p"` - Wallet *WalletConfig `mapstructure:"wallet"` - Auth *RPCAuthConfig `mapstructure:"auth"` - Web *WebConfig `mapstructure:"web"` - Websocket *WebsocketConfig `mapstructure:"ws"` + P2P *P2PConfig `mapstructure:"p2p"` + Wallet *WalletConfig `mapstructure:"wallet"` + Auth *RPCAuthConfig `mapstructure:"auth"` + Web *WebConfig `mapstructure:"web"` + Websocket *WebsocketConfig `mapstructure:"ws"` + Fed *FederationConfig `mapstructure:"fed"` } // Default configurable parameters. @@ -38,6 +39,7 @@ func DefaultConfig() *Config { Auth: DefaultRPCAuthConfig(), Web: DefaultWebConfig(), Websocket: DefaultWebsocketConfig(), + Fed: DefaultFederationConfig(), } } @@ -193,6 +195,13 @@ type WebsocketConfig struct { MaxNumConcurrentReqs int `mapstructure:"max_num_concurrent_reqs"` } +type FederationConfig struct { + // XPubs []chainkd.XPub `mapstructure:"xpubs"` + // Quorum int `mapstructure:"quorum"` + XPubs string `mapstructure:"xpubs"` + Quorum int `mapstructure:"quorum"` +} + // Default configurable rpc's auth parameters. func DefaultRPCAuthConfig() *RPCAuthConfig { return &RPCAuthConfig{ @@ -224,6 +233,10 @@ func DefaultWebsocketConfig() *WebsocketConfig { } } +func DefaultFederationConfig() *FederationConfig { + return &FederationConfig{} +} + //----------------------------------------------------------------------------- // Utils diff --git a/config/toml.go b/config/toml.go index ceb448c5..44c640e3 100644 --- a/config/toml.go +++ b/config/toml.go @@ -51,16 +51,21 @@ laddr = "tcp://0.0.0.0:56659" seeds = "" ` +var FederationConfigTmpl = `[fed] +xpubs = "1,2" +quorum = 1 +` + // Select network seeds to merge a new string. func selectNetwork(network string) string { switch network { case "mainnet": - return defaultConfigTmpl + mainNetConfigTmpl + return defaultConfigTmpl + mainNetConfigTmpl + FederationConfigTmpl case "testnet": - return defaultConfigTmpl + testNetConfigTmpl + return defaultConfigTmpl + testNetConfigTmpl + FederationConfigTmpl case "vapor": - return defaultConfigTmpl + vaporNetConfigTmpl + return defaultConfigTmpl + vaporNetConfigTmpl + FederationConfigTmpl default: - return defaultConfigTmpl + soloNetConfigTmpl + return defaultConfigTmpl + soloNetConfigTmpl + FederationConfigTmpl } } diff --git a/node/node.go b/node/node.go index b590a7c2..0a8c0a83 100644 --- a/node/node.go +++ b/node/node.go @@ -102,9 +102,13 @@ func NewNode(config *cfg.Config) *Node { } if !config.Wallet.Disable { + + log.Info(config.Fed.XPubs) + log.Info(config.Fed.Quorum) + walletDB := dbm.NewDB("wallet", config.DBBackend, config.DBDir()) accounts = account.NewManager(walletDB, chain) - assets = asset.NewRegistry(walletDB, chain) + assets = asset.NewRegistry(walletDB, chain, config) wallet, err = w.NewWallet(walletDB, accounts, assets, hsm, chain, dispatcher, config.Wallet.TxIndex) if err != nil { log.WithFields(log.Fields{"module": logModule, "error": err}).Error("init NewWallet") diff --git a/test/wallet_test_util.go b/test/wallet_test_util.go index b03b430b..97be7229 100644 --- a/test/wallet_test_util.go +++ b/test/wallet_test_util.go @@ -242,7 +242,7 @@ func (cfg *walletTestConfig) Run() error { } walletDB := dbm.NewDB("wallet", "leveldb", path.Join(dirPath, "wallet_db")) accountManager := account.NewManager(walletDB, chain) - assets := asset.NewRegistry(walletDB, chain) + assets := asset.NewRegistry(walletDB, chain, nil) dispatcher := event.NewDispatcher() wallet, err := w.NewWallet(walletDB, accountManager, assets, hsm, chain, dispatcher, false) if err != nil { diff --git a/wallet/unconfirmed_test.go b/wallet/unconfirmed_test.go index 60ece604..7231afda 100644 --- a/wallet/unconfirmed_test.go +++ b/wallet/unconfirmed_test.go @@ -52,7 +52,7 @@ func TestWalletUnconfirmedTxs(t *testing.T) { controlProg.KeyIndex = 1 - reg := asset.NewRegistry(testDB, nil) + reg := asset.NewRegistry(testDB, nil, nil) asset := bc.AssetID{V0: 5} dispatcher := event.NewDispatcher() diff --git a/wallet/wallet_test.go b/wallet/wallet_test.go index a0a3708f..5d2c83bb 100644 --- a/wallet/wallet_test.go +++ b/wallet/wallet_test.go @@ -148,7 +148,7 @@ func TestWalletUpdate(t *testing.T) { controlProg.KeyIndex = 1 - reg := asset.NewRegistry(testDB, chain) + reg := asset.NewRegistry(testDB, chain, nil) asset := bc.AssetID{V0: 5} utxos := []*account.UTXO{} @@ -289,7 +289,7 @@ func TestMemPoolTxQueryLoop(t *testing.T) { controlProg.KeyIndex = 1 - reg := asset.NewRegistry(testDB, chain) + reg := asset.NewRegistry(testDB, chain, nil) asset := bc.AssetID{V0: 5} utxos := []*account.UTXO{} -- 2.11.0