OSDN Git Service

Refactor api (#472)
authorYongfeng LI <wliyongfeng@gmail.com>
Sat, 24 Mar 2018 02:22:14 +0000 (10:22 +0800)
committerGitHub <noreply@github.com>
Sat, 24 Mar 2018 02:22:14 +0000 (10:22 +0800)
* move listAccounts to API

* add chain to API

* call walllet from API directly

* move NewSuccessResponse and NewErrorResponse to api.go

blockchain/accounts.go
blockchain/api.go
blockchain/blockchain_reactor.go
blockchain/query.go
blockchain/reactor.go
blockchain/transact.go

index f475f3b..8fbdc92 100644 (file)
@@ -40,7 +40,6 @@ func (a *API) updateAccountTags(ctx context.Context, updateTag struct {
        AccountInfo string                 `json:"account_info"`
        Tags        map[string]interface{} `json:"tags"`
 }) Response {
-
        err := a.wallet.AccountMgr.UpdateTags(nil, updateTag.AccountInfo, updateTag.Tags)
        if err != nil {
                return NewErrorResponse(err)
index e62b557..1f49ecc 100644 (file)
@@ -18,6 +18,7 @@ import (
        "github.com/bytom/net/http/authn"
        "github.com/bytom/net/http/httpjson"
        "github.com/bytom/net/http/static"
+       "github.com/bytom/protocol"
        "github.com/bytom/blockchain/wallet"
 )
 
@@ -27,6 +28,16 @@ var (
        httpWriteTimeout    = time.Hour
 )
 
+//NewSuccessResponse success response
+func NewSuccessResponse(data interface{}) Response {
+       return Response{Status: SUCCESS, Data: data}
+}
+
+//NewErrorResponse error response
+func NewErrorResponse(err error) Response {
+       return Response{Status: FAIL, Msg: err.Error()}
+}
+
 type waitHandler struct {
        h  http.Handler
        wg sync.WaitGroup
@@ -45,6 +56,7 @@ func (wh *waitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
 type API struct {
        bcr     *BlockchainReactor
        wallet  *wallet.Wallet
+       chain   *protocol.Chain
        server  *http.Server
        handler http.Handler
 }
@@ -60,7 +72,7 @@ func (a *API) initServer(config *cfg.Config) {
        var handler http.Handler = mux
 
        if config.Auth.Disable == false {
-               handler = AuthHandler(handler, a.bcr.wallet.Tokens)
+               handler = AuthHandler(handler, a.wallet.Tokens)
        }
        handler = RedirectHandler(handler)
 
@@ -104,6 +116,7 @@ func NewAPI(bcr *BlockchainReactor, config *cfg.Config) *API {
        api := &API{
                bcr:    bcr,
                wallet: bcr.wallet,
+               chain:  bcr.chain,
        }
        api.buildHandler()
        api.initServer(config)
@@ -118,11 +131,11 @@ func (a *API) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
 // buildHandler is in charge of all the rpc handling.
 func (a *API) buildHandler() {
        m := http.NewServeMux()
-       if a.bcr.wallet != nil && a.bcr.wallet.AccountMgr != nil && a.bcr.wallet.AssetReg != nil {
+       if a.wallet != nil && a.wallet.AccountMgr != nil && a.wallet.AssetReg != nil {
                m.Handle("/create-account", jsonHandler(a.createAccount))
                m.Handle("/update-account-tags", jsonHandler(a.updateAccountTags))
                m.Handle("/create-account-receiver", jsonHandler(a.createAccountReceiver))
-               m.Handle("/list-accounts", jsonHandler(a.bcr.listAccounts))
+               m.Handle("/list-accounts", jsonHandler(a.listAccounts))
                m.Handle("/list-addresses", jsonHandler(a.listAddresses))
                m.Handle("/delete-account", jsonHandler(a.deleteAccount))
                m.Handle("/validate-address", jsonHandler(a.validateAddress))
index 4a23943..1642276 100644 (file)
@@ -35,7 +35,7 @@ func (a *API) getNetInfo() Response {
 
 // return best block hash
 func (a *API) getBestBlockHash() Response {
-       blockHash := map[string]string{"blockHash": a.bcr.chain.BestBlockHash().String()}
+       blockHash := map[string]string{"blockHash": a.chain.BestBlockHash().String()}
        return NewSuccessResponse(blockHash)
 }
 
@@ -46,7 +46,7 @@ func (a *API) getBlockHeaderByHash(strHash string) Response {
                log.WithField("error", err).Error("Error occurs when transforming string hash to hash struct")
                return NewErrorResponse(err)
        }
-       block, err := a.bcr.chain.GetBlockByHash(&hash)
+       block, err := a.chain.GetBlockByHash(&hash)
        if err != nil {
                log.WithField("error", err).Error("Fail to get block by hash")
                return NewErrorResponse(err)
@@ -97,16 +97,16 @@ func (a *API) getBlock(ins GetBlockReq) Response {
                b32 := [32]byte{}
                copy(b32[:], ins.BlockHash)
                hash := bc.NewHash(b32)
-               block, err = a.bcr.chain.GetBlockByHash(&hash)
+               block, err = a.chain.GetBlockByHash(&hash)
        } else {
-               block, err = a.bcr.chain.GetBlockByHeight(ins.BlockHeight)
+               block, err = a.chain.GetBlockByHeight(ins.BlockHeight)
        }
        if err != nil {
                return NewErrorResponse(err)
        }
 
        blockHash := block.Hash()
-       txStatus, err := a.bcr.chain.GetTransactionStatus(&blockHash)
+       txStatus, err := a.chain.GetTransactionStatus(&blockHash)
        rawBlock, err := block.MarshalText()
        if err != nil {
                return NewErrorResponse(err)
@@ -160,7 +160,7 @@ func (a *API) getBlockTransactionsCountByHash(strHash string) Response {
                return NewErrorResponse(err)
        }
 
-       legacyBlock, err := a.bcr.chain.GetBlockByHash(&hash)
+       legacyBlock, err := a.chain.GetBlockByHash(&hash)
        if err != nil {
                log.WithField("error", err).Error("Fail to get block by hash")
                return NewErrorResponse(err)
@@ -172,7 +172,7 @@ func (a *API) getBlockTransactionsCountByHash(strHash string) Response {
 
 // return block transactions count by height
 func (a *API) getBlockTransactionsCountByHeight(height uint64) Response {
-       legacyBlock, err := a.bcr.chain.GetBlockByHeight(height)
+       legacyBlock, err := a.chain.GetBlockByHeight(height)
        if err != nil {
                log.WithField("error", err).Error("Fail to get block by hash")
                return NewErrorResponse(err)
@@ -184,7 +184,7 @@ func (a *API) getBlockTransactionsCountByHeight(height uint64) Response {
 
 // return current block count
 func (a *API) getBlockCount() Response {
-       blockHeight := map[string]uint64{"block_count": a.bcr.chain.Height()}
+       blockHeight := map[string]uint64{"block_count": a.chain.Height()}
        return NewSuccessResponse(blockHeight)
 }
 
index 86dd0c3..39891be 100755 (executable)
@@ -11,10 +11,10 @@ import (
 )
 
 // POST /list-accounts
-func (bcr *BlockchainReactor) listAccounts(ctx context.Context, filter struct {
+func (a *API) listAccounts(ctx context.Context, filter struct {
        ID string `json:"id"`
 }) Response {
-       accounts, err := bcr.wallet.AccountMgr.ListAccounts(filter.ID)
+       accounts, err := a.wallet.AccountMgr.ListAccounts(filter.ID)
        if err != nil {
                log.Errorf("listAccounts: %v", err)
                return NewErrorResponse(err)
index a4b4bd4..6d74689 100755 (executable)
@@ -45,16 +45,6 @@ type Response struct {
        Data   interface{} `json:"data,omitempty"`
 }
 
-//NewSuccessResponse success response
-func NewSuccessResponse(data interface{}) Response {
-       return Response{Status: SUCCESS, Data: data}
-}
-
-//NewErrorResponse error response
-func NewErrorResponse(err error) Response {
-       return Response{Status: FAIL, Msg: err.Error()}
-}
-
 //BlockchainReactor handles long-term catchup syncing.
 type BlockchainReactor struct {
        p2p.BaseReactor
index 1bf5b35..ae41457 100644 (file)
@@ -142,7 +142,7 @@ func (a *API) submitSingle(ctx context.Context, tpl *txbuilder.Template) (map[st
                return nil, errors.Wrap(txbuilder.ErrMissingRawTx)
        }
 
-       if err := txbuilder.FinalizeTx(ctx, a.bcr.chain, tpl.Transaction); err != nil {
+       if err := txbuilder.FinalizeTx(ctx, a.chain, tpl.Transaction); err != nil {
                return nil, errors.Wrapf(err, "tx %s", tpl.Transaction.ID.String())
        }
 
@@ -224,7 +224,7 @@ type submitTxResp struct {
 func (a *API) submit(ctx context.Context, ins struct {
        Tx types.Tx `json:"raw_transaction"`
 }) Response {
-       if err := txbuilder.FinalizeTx(ctx, a.bcr.chain, &ins.Tx); err != nil {
+       if err := txbuilder.FinalizeTx(ctx, a.chain, &ins.Tx); err != nil {
                return NewErrorResponse(err)
        }