X-Git-Url: http://git.osdn.net/view?p=bytom%2Fvapor.git;a=blobdiff_plain;f=api%2Fapi.go;h=cc54ae8a6a5b1089464f70634a6945ea3d89a43d;hp=b45a30bfc83717642680bbb337c1c3a351bafaf0;hb=1337be95f74a1d2a1a7316737efde413f29bcb2f;hpb=ddc7106558f020bde24cc337d51649611dddaba8 diff --git a/api/api.go b/api/api.go index b45a30bf..cc54ae8a 100644 --- a/api/api.go +++ b/api/api.go @@ -11,23 +11,22 @@ import ( log "github.com/sirupsen/logrus" cmn "github.com/tendermint/tmlibs/common" - "github.com/vapor/accesstoken" - "github.com/vapor/blockchain/txfeed" - cfg "github.com/vapor/config" - "github.com/vapor/dashboard/dashboard" - "github.com/vapor/dashboard/equity" - "github.com/vapor/errors" - "github.com/vapor/mining/miner" - "github.com/vapor/mining/miningpool" - "github.com/vapor/net/http/authn" - "github.com/vapor/net/http/gzip" - "github.com/vapor/net/http/httpjson" - "github.com/vapor/net/http/static" - "github.com/vapor/net/websocket" - "github.com/vapor/netsync" - "github.com/vapor/protocol" - "github.com/vapor/protocol/bc" - "github.com/vapor/wallet" + "github.com/bytom/vapor/accesstoken" + cfg "github.com/bytom/vapor/config" + "github.com/bytom/vapor/dashboard/dashboard" + "github.com/bytom/vapor/dashboard/equity" + "github.com/bytom/vapor/errors" + "github.com/bytom/vapor/event" + "github.com/bytom/vapor/net/http/authn" + "github.com/bytom/vapor/net/http/gzip" + "github.com/bytom/vapor/net/http/httpjson" + "github.com/bytom/vapor/net/http/static" + "github.com/bytom/vapor/net/websocket" + "github.com/bytom/vapor/netsync/peers" + "github.com/bytom/vapor/p2p" + "github.com/bytom/vapor/proposal/blockproposer" + "github.com/bytom/vapor/protocol" + "github.com/bytom/vapor/wallet" ) var ( @@ -40,7 +39,8 @@ const ( // SUCCESS indicates the rpc calling is successful. SUCCESS = "success" // FAIL indicated the rpc calling is failed. - FAIL = "fail" + FAIL = "fail" + logModule = "api" ) // Response describes the response standard. @@ -105,18 +105,15 @@ func (wh *waitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { // API is the scheduling center for server type API struct { - sync *netsync.SyncManager - wallet *wallet.Wallet - accessTokens *accesstoken.CredentialStore - chain *protocol.Chain - server *http.Server - handler http.Handler - txFeedTracker *txfeed.Tracker - //cpuMiner *cpuminer.CPUMiner - miner *miner.Miner - miningPool *miningpool.MiningPool + sync NetSync + wallet *wallet.Wallet + accessTokens *accesstoken.CredentialStore + chain *protocol.Chain + server *http.Server + handler http.Handler + blockProposer *blockproposer.BlockProposer notificationMgr *websocket.WSNotificationManager - newBlockCh chan *bc.Hash + eventDispatcher *event.Dispatcher } func (a *API) initServer(config *cfg.Config) { @@ -153,7 +150,7 @@ func (a *API) initServer(config *cfg.Config) { // StartServer start the server func (a *API) StartServer(address string) { - log.WithField("api address:", address).Info("Rpc listen") + log.WithFields(log.Fields{"module": logModule, "api address:": address}).Info("Rpc listen") listener, err := net.Listen("tcp", address) if err != nil { cmn.Exit(cmn.Fmt("Failed to register tcp port: %v", err)) @@ -164,23 +161,31 @@ func (a *API) StartServer(address string) { // we call it. go func() { if err := a.server.Serve(listener); err != nil { - log.WithField("error", errors.Wrap(err, "Serve")).Error("Rpc server") + log.WithFields(log.Fields{"module": logModule, "error": errors.Wrap(err, "Serve")}).Error("Rpc server") } }() } +type NetSync interface { + IsListening() bool + IsCaughtUp() bool + PeerCount() int + GetNetwork() string + BestPeer() *peers.PeerInfo + DialPeerWithAddress(addr *p2p.NetAddress) error + GetPeerInfos() []*peers.PeerInfo + StopPeer(peerID string) error +} + // NewAPI create and initialize the API -func NewAPI(sync *netsync.SyncManager, wallet *wallet.Wallet, txfeeds *txfeed.Tracker, miner *miner.Miner, miningPool *miningpool.MiningPool, chain *protocol.Chain, config *cfg.Config, token *accesstoken.CredentialStore, newBlockCh chan *bc.Hash, notificationMgr *websocket.WSNotificationManager) *API { +func NewAPI(sync NetSync, wallet *wallet.Wallet, blockProposer *blockproposer.BlockProposer, chain *protocol.Chain, config *cfg.Config, token *accesstoken.CredentialStore, dispatcher *event.Dispatcher, notificationMgr *websocket.WSNotificationManager) *API { api := &API{ - sync: sync, - wallet: wallet, - chain: chain, - accessTokens: token, - txFeedTracker: txfeeds, - miner: miner, - miningPool: miningPool, - - newBlockCh: newBlockCh, + sync: sync, + wallet: wallet, + chain: chain, + accessTokens: token, + blockProposer: blockProposer, + eventDispatcher: dispatcher, notificationMgr: notificationMgr, } api.buildHandler() @@ -199,7 +204,6 @@ func (a *API) buildHandler() { m := http.NewServeMux() if a.wallet != nil { walletEnable = true - m.Handle("/create-account", jsonHandler(a.createAccount)) m.Handle("/update-account-alias", jsonHandler(a.updateAccountAlias)) m.Handle("/list-accounts", jsonHandler(a.listAccounts)) @@ -216,7 +220,6 @@ func (a *API) buildHandler() { m.Handle("/get-coinbase-arbitrary", jsonHandler(a.getCoinbaseArbitrary)) m.Handle("/set-coinbase-arbitrary", jsonHandler(a.setCoinbaseArbitrary)) - m.Handle("/create-asset", jsonHandler(a.createAsset)) m.Handle("/update-asset-alias", jsonHandler(a.updateAssetAlias)) m.Handle("/get-asset", jsonHandler(a.getAsset)) m.Handle("/list-assets", jsonHandler(a.listAssets)) @@ -239,6 +242,7 @@ func (a *API) buildHandler() { m.Handle("/list-balances", jsonHandler(a.listBalances)) m.Handle("/list-unspent-outputs", jsonHandler(a.listUnspentOutputs)) + m.Handle("/list-account-votes", jsonHandler(a.listAccountVotes)) m.Handle("/decode-program", jsonHandler(a.decodeProgram)) @@ -247,16 +251,6 @@ func (a *API) buildHandler() { m.Handle("/rescan-wallet", jsonHandler(a.rescanWallet)) m.Handle("/wallet-info", jsonHandler(a.getWalletInfo)) m.Handle("/recovery-wallet", jsonHandler(a.recoveryFromRootXPubs)) - - m.Handle("/get-pegin-address", jsonHandler(a.getPeginAddress)) - m.Handle("/get-pegin-contract-address", jsonHandler(a.getPeginContractAddress)) - m.Handle("/claim-pegin-transaction", jsonHandler(a.claimContractPeginTx)) - m.Handle("/create-key-pair", jsonHandler(a.createXKeys)) - m.Handle("/get-utxo-from-transaction", jsonHandler(a.getUnspentOutputs)) - m.Handle("/get-side-raw-transaction", jsonHandler(a.getSideRawTransaction)) - m.Handle("/build-mainchain-tx", jsonHandler(a.buildMainChainTxForContract)) - m.Handle("/sign-with-key", jsonHandler(a.signWithKey)) - m.Handle("/dpos", jsonHandler(a.dpos)) } else { log.Warn("Please enable wallet") } @@ -269,12 +263,6 @@ func (a *API) buildHandler() { m.Handle("/delete-access-token", jsonHandler(a.deleteAccessToken)) m.Handle("/check-access-token", jsonHandler(a.checkAccessToken)) - m.Handle("/create-transaction-feed", jsonHandler(a.createTxFeed)) - m.Handle("/get-transaction-feed", jsonHandler(a.getTxFeed)) - m.Handle("/update-transaction-feed", jsonHandler(a.updateTxFeed)) - m.Handle("/delete-transaction-feed", jsonHandler(a.deleteTxFeed)) - m.Handle("/list-transaction-feeds", jsonHandler(a.listTxFeeds)) - m.Handle("/submit-transaction", jsonHandler(a.submit)) m.Handle("/submit-transactions", jsonHandler(a.submitTxs)) m.Handle("/estimate-transaction-gas", jsonHandler(a.estimateTxGas)) @@ -282,25 +270,16 @@ func (a *API) buildHandler() { m.Handle("/get-unconfirmed-transaction", jsonHandler(a.getUnconfirmedTx)) m.Handle("/list-unconfirmed-transactions", jsonHandler(a.listUnconfirmedTxs)) m.Handle("/decode-raw-transaction", jsonHandler(a.decodeRawTransaction)) - m.Handle("/get-raw-transaction", jsonHandler(a.getRawTransaction)) m.Handle("/get-block", jsonHandler(a.getBlock)) m.Handle("/get-raw-block", jsonHandler(a.getRawBlock)) m.Handle("/get-block-hash", jsonHandler(a.getBestBlockHash)) m.Handle("/get-block-header", jsonHandler(a.getBlockHeader)) m.Handle("/get-block-count", jsonHandler(a.getBlockCount)) - m.Handle("/get-difficulty", jsonHandler(a.getDifficulty)) - m.Handle("/get-hash-rate", jsonHandler(a.getHashRate)) m.Handle("/is-mining", jsonHandler(a.isMining)) m.Handle("/set-mining", jsonHandler(a.setMining)) - m.Handle("/get-work", jsonHandler(a.getWork)) - m.Handle("/get-work-json", jsonHandler(a.getWorkJSON)) - m.Handle("/submit-block", jsonHandler(a.submitBlock)) - m.Handle("/submit-work", jsonHandler(a.submitWork)) - m.Handle("/submit-work-json", jsonHandler(a.submitWorkJSON)) - m.Handle("/verify-message", jsonHandler(a.verifyMessage)) m.Handle("/compile", jsonHandler(a.compileEquity)) @@ -313,12 +292,13 @@ func (a *API) buildHandler() { m.Handle("/get-merkle-proof", jsonHandler(a.getMerkleProof)) + m.Handle("/get-vote-result", jsonHandler(a.getVoteResult)) + m.HandleFunc("/websocket-subscribe", a.websocketHandler) - handler := latencyHandler(m, walletEnable) + handler := walletHandler(m, walletEnable) handler = webAssetsHandler(handler) handler = gzip.Handler{Handler: handler} - a.handler = handler } @@ -359,7 +339,7 @@ func AuthHandler(handler http.Handler, accessTokens *accesstoken.CredentialStore // TODO(tessr): check that this path exists; return early if this path isn't legit req, err := authenticator.Authenticate(req) if err != nil { - log.WithField("error", errors.Wrap(err, "Serve")).Error("Authenticate fail") + log.WithFields(log.Fields{"module": logModule, "error": errors.Wrap(err, "Serve")}).Error("Authenticate fail") err = errors.WithDetail(errNotAuthenticated, err.Error()) errorFormatter.Write(req.Context(), rw, err) return @@ -379,14 +359,8 @@ func RedirectHandler(next http.Handler) http.Handler { }) } -// latencyHandler take latency for the request url path, and redirect url path to wait-disable when wallet is closed -func latencyHandler(m *http.ServeMux, walletEnable bool) http.Handler { +func walletHandler(m *http.ServeMux, walletEnable bool) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - // latency for the request url path - if l := latency(m, req); l != nil { - defer l.RecordSince(time.Now()) - } - // when the wallet is not been opened and the url path is not been found, modify url path to error, // and redirect handler to error if _, pattern := m.Handler(req); pattern != req.URL.Path && !walletEnable {