From 99ad5ca90587f12d1a1b8a175d27ca08631944df Mon Sep 17 00:00:00 2001 From: Yongfeng LI Date: Wed, 21 Mar 2018 15:17:01 +0800 Subject: [PATCH] remove accounts and assets from BlockchainReactor (#448) * remove accounts field in BlockchainReactor * remove assets field in BlockchainReactor --- blockchain/accounts.go | 10 +++++----- blockchain/assets.go | 6 +++--- blockchain/query.go | 12 ++++++------ blockchain/reactor.go | 12 +++--------- blockchain/receivers.go | 2 +- blockchain/request.go | 4 ++-- blockchain/rpc_reactor.go | 2 +- blockchain/transact.go | 8 ++++---- node/node.go | 2 +- 9 files changed, 26 insertions(+), 32 deletions(-) diff --git a/blockchain/accounts.go b/blockchain/accounts.go index 251f367a..fba26a77 100644 --- a/blockchain/accounts.go +++ b/blockchain/accounts.go @@ -19,7 +19,7 @@ func (bcr *BlockchainReactor) createAccount(ctx context.Context, ins struct { Alias string `json:"alias"` Tags map[string]interface{} `json:"tags"` }) Response { - acc, err := bcr.accounts.Create(ctx, ins.RootXPubs, ins.Quorum, ins.Alias, ins.Tags) + acc, err := bcr.wallet.AccountMgr.Create(ctx, ins.RootXPubs, ins.Quorum, ins.Alias, ins.Tags) if err != nil { return NewErrorResponse(err) } @@ -40,7 +40,7 @@ func (bcr *BlockchainReactor) updateAccountTags(ctx context.Context, updateTag s Tags map[string]interface{} `json:"tags"` }) Response { - err := bcr.accounts.UpdateTags(nil, updateTag.AccountInfo, updateTag.Tags) + err := bcr.wallet.AccountMgr.UpdateTags(nil, updateTag.AccountInfo, updateTag.Tags) if err != nil { return NewErrorResponse(err) } @@ -53,7 +53,7 @@ func (bcr *BlockchainReactor) updateAccountTags(ctx context.Context, updateTag s func (bcr *BlockchainReactor) deleteAccount(ctx context.Context, in struct { AccountInfo string `json:"account_info"` }) Response { - if err := bcr.accounts.DeleteAccount(in); err != nil { + if err := bcr.wallet.AccountMgr.DeleteAccount(in); err != nil { return NewErrorResponse(err) } return NewSuccessResponse(nil) @@ -92,7 +92,7 @@ func (bcr *BlockchainReactor) validateAddress(ctx context.Context, ins struct { } resp.Vaild = true - resp.IsLocal = bcr.accounts.IsLocalControlProgram(program) + resp.IsLocal = bcr.wallet.AccountMgr.IsLocalControlProgram(program) return NewSuccessResponse(resp) } @@ -102,7 +102,7 @@ type addressResp struct { } func (bcr *BlockchainReactor) listAddresses(ctx context.Context) Response { - cps, err := bcr.accounts.ListControlProgram() + cps, err := bcr.wallet.AccountMgr.ListControlProgram() if err != nil { return NewErrorResponse(err) } diff --git a/blockchain/assets.go b/blockchain/assets.go index 682b7683..178fde7d 100644 --- a/blockchain/assets.go +++ b/blockchain/assets.go @@ -17,7 +17,7 @@ func (bcr *BlockchainReactor) createAsset(ctx context.Context, ins struct { Definition map[string]interface{} `json:"definition"` Tags map[string]interface{} `json:"tags"` }) Response { - ass, err := bcr.assets.Define( + ass, err := bcr.wallet.AssetReg.Define( ins.RootXPubs, ins.Quorum, ins.Definition, @@ -43,7 +43,7 @@ func (bcr *BlockchainReactor) updateAssetTags(ctx context.Context, updateTag str AssetInfo string `json:"asset_info"` Tags map[string]interface{} `json:"tags"` }) Response { - err := bcr.assets.UpdateTags(nil, updateTag.AssetInfo, updateTag.Tags) + err := bcr.wallet.AssetReg.UpdateTags(nil, updateTag.AssetInfo, updateTag.Tags) if err != nil { return NewErrorResponse(err) } @@ -56,7 +56,7 @@ func (bcr *BlockchainReactor) updateAssetAlias(updateAlias struct { OldAlias string `json:"old_alias"` NewAlias string `json:"new_alias"` }) Response { - if err := bcr.assets.UpdateAssetAlias(updateAlias.OldAlias, updateAlias.NewAlias); err != nil { + if err := bcr.wallet.AssetReg.UpdateAssetAlias(updateAlias.OldAlias, updateAlias.NewAlias); err != nil { return NewErrorResponse(err) } diff --git a/blockchain/query.go b/blockchain/query.go index 6686cfb5..ee818e2d 100755 --- a/blockchain/query.go +++ b/blockchain/query.go @@ -15,7 +15,7 @@ import ( func (bcr *BlockchainReactor) listAccounts(ctx context.Context, filter struct { ID string `json:"id"` }) Response { - accounts, err := bcr.accounts.ListAccounts(filter.ID) + accounts, err := bcr.wallet.AccountMgr.ListAccounts(filter.ID) if err != nil { log.Errorf("listAccounts: %v", err) return NewErrorResponse(err) @@ -38,7 +38,7 @@ func (bcr *BlockchainReactor) listAccounts(ctx context.Context, filter struct { func (bcr *BlockchainReactor) listAssets(ctx context.Context, filter struct { ID string `json:"id"` }) Response { - assets, err := bcr.assets.ListAssets(filter.ID) + assets, err := bcr.wallet.AssetReg.ListAssets(filter.ID) if err != nil { log.Errorf("listAssets: %v", err) return NewErrorResponse(err) @@ -100,8 +100,8 @@ func (bcr *BlockchainReactor) indexBalances(accountUTXOs []account.UTXO) []accou for _, assetID := range sortedAsset { - alias := bcr.accounts.GetAliasByID(id) - assetAlias := bcr.assets.GetAliasByID(assetID) + alias := bcr.wallet.AccountMgr.GetAliasByID(id) + assetAlias := bcr.wallet.AssetReg.GetAliasByID(assetID) tmpBalance.Alias = alias tmpBalance.AccountID = id tmpBalance.AssetID = assetID @@ -194,8 +194,8 @@ func (bcr *BlockchainReactor) listUnspentOutputs(ctx context.Context, filter str tmpUTXO.Address = utxo.Address tmpUTXO.ValidHeight = utxo.ValidHeight - tmpUTXO.Alias = bcr.accounts.GetAliasByID(utxo.AccountID) - tmpUTXO.AssetAlias = bcr.assets.GetAliasByID(tmpUTXO.AssetID) + tmpUTXO.Alias = bcr.wallet.AccountMgr.GetAliasByID(utxo.AccountID) + tmpUTXO.AssetAlias = bcr.wallet.AssetReg.GetAliasByID(tmpUTXO.AssetID) UTXOs = append(UTXOs, tmpUTXO) } diff --git a/blockchain/reactor.go b/blockchain/reactor.go index a6aefe9b..284640c6 100755 --- a/blockchain/reactor.go +++ b/blockchain/reactor.go @@ -10,8 +10,6 @@ import ( cmn "github.com/tendermint/tmlibs/common" "github.com/bytom/blockchain/accesstoken" - "github.com/bytom/blockchain/account" - "github.com/bytom/blockchain/asset" "github.com/bytom/blockchain/pseudohsm" "github.com/bytom/blockchain/txfeed" "github.com/bytom/blockchain/wallet" @@ -65,8 +63,6 @@ type BlockchainReactor struct { chain *protocol.Chain wallet *wallet.Wallet - accounts *account.Manager - assets *asset.Registry accessTokens *accesstoken.CredentialStore txFeedTracker *txfeed.Tracker blockKeeper *blockKeeper @@ -105,17 +101,15 @@ func maxBytes(h http.Handler) http.Handler { } // NewBlockchainReactor returns the reactor of whole blockchain. -func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, accounts *account.Manager, assets *asset.Registry, sw *p2p.Switch, hsm *pseudohsm.HSM, wallet *wallet.Wallet, txfeeds *txfeed.Tracker, accessTokens *accesstoken.CredentialStore, miningEnable bool) *BlockchainReactor { +func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, sw *p2p.Switch, hsm *pseudohsm.HSM, wallet *wallet.Wallet, txfeeds *txfeed.Tracker, accessTokens *accesstoken.CredentialStore, miningEnable bool) *BlockchainReactor { newBlockCh := make(chan *bc.Hash, maxNewBlockChSize) bcr := &BlockchainReactor{ chain: chain, wallet: wallet, - accounts: accounts, - assets: assets, blockKeeper: newBlockKeeper(chain, sw), txPool: txPool, - mining: cpuminer.NewCPUMiner(chain, accounts, txPool, newBlockCh), - miningPool: miningpool.NewMiningPool(chain, accounts, txPool, newBlockCh), + mining: cpuminer.NewCPUMiner(chain, wallet.AccountMgr, txPool, newBlockCh), + miningPool: miningpool.NewMiningPool(chain, wallet.AccountMgr, txPool, newBlockCh), mux: http.NewServeMux(), sw: sw, hsm: hsm, diff --git a/blockchain/receivers.go b/blockchain/receivers.go index f415b4ab..321b6ce3 100755 --- a/blockchain/receivers.go +++ b/blockchain/receivers.go @@ -7,7 +7,7 @@ import ( func (bcr *BlockchainReactor) createAccountReceiver(ctx context.Context, ins struct { AccountInfo string `json:"account_info"` }) Response { - receiver, err := bcr.accounts.CreateAccountReceiver(ctx, ins.AccountInfo) + receiver, err := bcr.wallet.AccountMgr.CreateAccountReceiver(ctx, ins.AccountInfo) if err != nil { return NewErrorResponse(err) } diff --git a/blockchain/request.go b/blockchain/request.go index 380d602f..25ab2977 100644 --- a/blockchain/request.go +++ b/blockchain/request.go @@ -30,7 +30,7 @@ func (bcr *BlockchainReactor) filterAliases(ctx context.Context, br *BuildReques case consensus.BTMAlias: m["asset_id"] = consensus.BTMAssetID.String() default: - id, err := bcr.assets.GetIDByAlias(alias) + id, err := bcr.wallet.AssetReg.GetIDByAlias(alias) if err != nil { return errors.WithDetailf(err, "invalid asset alias %s on action %d", alias, i) } @@ -41,7 +41,7 @@ func (bcr *BlockchainReactor) filterAliases(ctx context.Context, br *BuildReques id, _ = m["account_id"].(string) alias, _ = m["account_alias"].(string) if id == "" && alias != "" { - acc, err := bcr.accounts.FindByAlias(ctx, alias) + acc, err := bcr.wallet.AccountMgr.FindByAlias(ctx, alias) if err != nil { return errors.WithDetailf(err, "invalid account alias %s on action %d", alias, i) } diff --git a/blockchain/rpc_reactor.go b/blockchain/rpc_reactor.go index 10ab3127..56556559 100644 --- a/blockchain/rpc_reactor.go +++ b/blockchain/rpc_reactor.go @@ -53,7 +53,7 @@ func webAssetsHandler(next http.Handler) http.Handler { // BuildHandler is in charge of all the rpc handling. func (bcr *BlockchainReactor) BuildHandler() { m := bcr.mux - if bcr.accounts != nil && bcr.assets != nil { + if bcr.wallet.AccountMgr != nil && bcr.wallet.AssetReg != nil { m.Handle("/create-account", jsonHandler(bcr.createAccount)) m.Handle("/update-account-tags", jsonHandler(bcr.updateAccountTags)) m.Handle("/create-account-receiver", jsonHandler(bcr.createAccountReceiver)) diff --git a/blockchain/transact.go b/blockchain/transact.go index d3e4c23a..afcc3b23 100644 --- a/blockchain/transact.go +++ b/blockchain/transact.go @@ -20,7 +20,7 @@ func (bcr *BlockchainReactor) actionDecoder(action string) (func([]byte) (txbuil var decoder func([]byte) (txbuilder.Action, error) switch action { case "control_account": - decoder = bcr.accounts.DecodeControlAction + decoder = bcr.wallet.AccountMgr.DecodeControlAction case "control_address": decoder = txbuilder.DecodeControlAddressAction case "control_program": @@ -28,13 +28,13 @@ func (bcr *BlockchainReactor) actionDecoder(action string) (func([]byte) (txbuil case "control_receiver": decoder = txbuilder.DecodeControlReceiverAction case "issue": - decoder = bcr.assets.DecodeIssueAction + decoder = bcr.wallet.AssetReg.DecodeIssueAction case "retire": decoder = txbuilder.DecodeRetireAction case "spend_account": - decoder = bcr.accounts.DecodeSpendAction + decoder = bcr.wallet.AccountMgr.DecodeSpendAction case "spend_account_unspent_output": - decoder = bcr.accounts.DecodeSpendUTXOAction + decoder = bcr.wallet.AccountMgr.DecodeSpendUTXOAction default: return nil, false } diff --git a/node/node.go b/node/node.go index 4cd01ba1..aeca6c06 100755 --- a/node/node.go +++ b/node/node.go @@ -210,7 +210,7 @@ func NewNode(config *cfg.Config) *Node { go accounts.ExpireReservations(ctx, expireReservationsPeriod) } - bcReactor := bc.NewBlockchainReactor(chain, txPool, accounts, assets, sw, hsm, wallet, txFeed, accessTokens, config.Mining) + bcReactor := bc.NewBlockchainReactor(chain, txPool,sw, hsm, wallet, txFeed, accessTokens, config.Mining) sw.AddReactor("BLOCKCHAIN", bcReactor) -- 2.11.0