From b4ee724f479dae38514fb00df358afdc8e4f47a9 Mon Sep 17 00:00:00 2001 From: Yongfeng LI Date: Wed, 21 Mar 2018 16:41:47 +0800 Subject: [PATCH] put token management from BlockchainReactor to Wallet (#452) --- blockchain/accesstokens.go | 8 ++++---- blockchain/reactor.go | 5 +---- blockchain/wallet/wallet.go | 5 ++++- blockchain/wallet/wallet_test.go | 2 +- node/node.go | 4 ++-- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/blockchain/accesstokens.go b/blockchain/accesstokens.go index 4752a4e2..e5adc2b6 100644 --- a/blockchain/accesstokens.go +++ b/blockchain/accesstokens.go @@ -10,7 +10,7 @@ func (bcr *BlockchainReactor) createAccessToken(ctx context.Context, x struct { ID string `json:"id"` Type string `json:"type"` }) Response { - token, err := bcr.accessTokens.Create(ctx, x.ID, x.Type) + token, err := bcr.wallet.Tokens.Create(ctx, x.ID, x.Type) if err != nil { return NewErrorResponse(err) } @@ -18,7 +18,7 @@ func (bcr *BlockchainReactor) createAccessToken(ctx context.Context, x struct { } func (bcr *BlockchainReactor) listAccessTokens(ctx context.Context) Response { - tokens, err := bcr.accessTokens.List(ctx) + tokens, err := bcr.wallet.Tokens.List(ctx) if err != nil { log.Errorf("listAccessTokens: %v", err) return NewErrorResponse(err) @@ -32,7 +32,7 @@ func (bcr *BlockchainReactor) deleteAccessToken(ctx context.Context, x struct { Token string `json:"token"` }) Response { //TODO Add delete permission verify. - if err := bcr.accessTokens.Delete(ctx, x.ID); err != nil { + if err := bcr.wallet.Tokens.Delete(ctx, x.ID); err != nil { return NewErrorResponse(err) } return NewSuccessResponse(nil) @@ -42,7 +42,7 @@ func (bcr *BlockchainReactor) checkAccessToken(ctx context.Context, x struct { ID string `json:"id"` Secret string `json:"secret"` }) Response { - if _, err := bcr.accessTokens.Check(ctx, x.ID, x.Secret); err != nil { + if _, err := bcr.wallet.Tokens.Check(ctx, x.ID, x.Secret); err != nil { return NewErrorResponse(err) } diff --git a/blockchain/reactor.go b/blockchain/reactor.go index aaf9f71d..b9518770 100755 --- a/blockchain/reactor.go +++ b/blockchain/reactor.go @@ -9,7 +9,6 @@ import ( log "github.com/sirupsen/logrus" cmn "github.com/tendermint/tmlibs/common" - "github.com/bytom/blockchain/accesstoken" "github.com/bytom/blockchain/txfeed" "github.com/bytom/blockchain/wallet" "github.com/bytom/mining/cpuminer" @@ -62,7 +61,6 @@ type BlockchainReactor struct { chain *protocol.Chain wallet *wallet.Wallet - accessTokens *accesstoken.CredentialStore txFeedTracker *txfeed.Tracker blockKeeper *blockKeeper txPool *protocol.TxPool @@ -99,7 +97,7 @@ func maxBytes(h http.Handler) http.Handler { } // NewBlockchainReactor returns the reactor of whole blockchain. -func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, sw *p2p.Switch,wallet *wallet.Wallet, txfeeds *txfeed.Tracker, accessTokens *accesstoken.CredentialStore, miningEnable bool) *BlockchainReactor { +func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, sw *p2p.Switch,wallet *wallet.Wallet, txfeeds *txfeed.Tracker, miningEnable bool) *BlockchainReactor { newBlockCh := make(chan *bc.Hash, maxNewBlockChSize) bcr := &BlockchainReactor{ chain: chain, @@ -111,7 +109,6 @@ func NewBlockchainReactor(chain *protocol.Chain, txPool *protocol.TxPool, sw *p2 mux: http.NewServeMux(), sw: sw, txFeedTracker: txfeeds, - accessTokens: accessTokens, miningEnable: miningEnable, newBlockCh: newBlockCh, } diff --git a/blockchain/wallet/wallet.go b/blockchain/wallet/wallet.go index 938951b3..e7603d2b 100755 --- a/blockchain/wallet/wallet.go +++ b/blockchain/wallet/wallet.go @@ -8,6 +8,7 @@ import ( "github.com/tendermint/go-wire/data/base58" "github.com/tendermint/tmlibs/db" + "github.com/bytom/blockchain/accesstoken" "github.com/bytom/blockchain/account" "github.com/bytom/blockchain/asset" "github.com/bytom/blockchain/pseudohsm" @@ -50,6 +51,7 @@ type Wallet struct { AccountMgr *account.Manager AssetReg *asset.Registry Hsm *pseudohsm.HSM + Tokens *accesstoken.CredentialStore chain *protocol.Chain rescanProgress chan struct{} ImportPrivKey bool @@ -58,13 +60,14 @@ type Wallet struct { //NewWallet return a new wallet instance func NewWallet(walletDB db.DB, account *account.Manager, asset *asset.Registry, hsm *pseudohsm.HSM, - chain *protocol.Chain) (*Wallet, error) { + accessTokens *accesstoken.CredentialStore, chain *protocol.Chain) (*Wallet, error) { w := &Wallet{ DB: walletDB, AccountMgr: account, AssetReg: asset, chain: chain, Hsm: hsm, + Tokens: accessTokens, rescanProgress: make(chan struct{}, 1), keysInfo: make([]KeyInfo, 0), } diff --git a/blockchain/wallet/wallet_test.go b/blockchain/wallet/wallet_test.go index 383b4ad3..c7efb2c9 100755 --- a/blockchain/wallet/wallet_test.go +++ b/blockchain/wallet/wallet_test.go @@ -139,7 +139,7 @@ func TestExportAndImportPrivKey(t *testing.T) { t.Fatal(err) } - w, err := NewWallet(testDB, acntManager, reg, hsm, chain) + w, err := NewWallet(testDB, acntManager, reg, hsm, nil, chain) if err != nil { t.Fatal(err) } diff --git a/node/node.go b/node/node.go index 3e73fb79..19353201 100755 --- a/node/node.go +++ b/node/node.go @@ -197,7 +197,7 @@ func NewNode(config *cfg.Config) *Node { walletDB := dbm.NewDB("wallet", config.DBBackend, config.DBDir()) accounts = account.NewManager(walletDB, chain) assets = asset.NewRegistry(walletDB, chain) - wallet, err = w.NewWallet(walletDB, accounts, assets, hsm, chain) + wallet, err = w.NewWallet(walletDB, accounts, assets, hsm, accessTokens, chain) if err != nil { log.WithField("error", err).Error("init NewWallet") } @@ -210,7 +210,7 @@ func NewNode(config *cfg.Config) *Node { go accounts.ExpireReservations(ctx, expireReservationsPeriod) } - bcReactor := bc.NewBlockchainReactor(chain, txPool,sw, wallet, txFeed, accessTokens, config.Mining) + bcReactor := bc.NewBlockchainReactor(chain, txPool,sw, wallet, txFeed, config.Mining) sw.AddReactor("BLOCKCHAIN", bcReactor) -- 2.11.0