OSDN Git Service

add getWalletInfo API to acquire rescan wallet schedule (#1098)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Wed, 27 Jun 2018 06:30:04 +0000 (14:30 +0800)
committerPaladz <yzhu101@uottawa.ca>
Wed, 27 Jun 2018 06:30:04 +0000 (14:30 +0800)
api/api.go
api/nodeinfo.go
api/wallet.go
cmd/bytomcli/commands/bytomcli.go
cmd/bytomcli/commands/wallet.go [new file with mode: 0644]
wallet/wallet.go

index c1f1341..d47898f 100644 (file)
@@ -226,6 +226,7 @@ func (a *API) buildHandler() {
                m.Handle("/backup-wallet", jsonHandler(a.backupWalletImage))
                m.Handle("/restore-wallet", jsonHandler(a.restoreWalletImage))
                m.Handle("/rescan-wallet", jsonHandler(a.rescanWallet))
+               m.Handle("/wallet-info", jsonHandler(a.getWalletInfo))
        } else {
                log.Warn("Please enable wallet")
        }
index 62f1d51..1a8d2ca 100644 (file)
@@ -34,7 +34,7 @@ func (a *API) GetNodeInfo() *NetInfo {
        return info
 }
 
-// getNetInfo return network infomation
+// getNetInfo return network information
 func (a *API) getNetInfo() Response {
        return NewSuccessResponse(a.GetNodeInfo())
 }
index e002c60..5eb122e 100644 (file)
@@ -61,3 +61,19 @@ func (a *API) rescanWallet() Response {
        a.wallet.RescanBlocks()
        return NewSuccessResponse(nil)
 }
+
+// WalletInfo return wallet information
+type WalletInfo struct {
+       BestBlockHeight uint64 `json:"best_block_height"`
+       WalletHeight    uint64 `json:"wallet_height"`
+}
+
+func (a *API) getWalletInfo() Response {
+       bestBlockHeight := a.chain.BestBlockHeight()
+       walletStatus := a.wallet.GetWalletStatusInfo()
+
+       return NewSuccessResponse(&WalletInfo{
+               BestBlockHeight: bestBlockHeight,
+               WalletHeight:    walletStatus.WorkHeight,
+       })
+}
index ddb5542..5be02b3 100644 (file)
@@ -134,6 +134,9 @@ func AddCommands() {
        BytomcliCmd.AddCommand(listUnspentOutputsCmd)
        BytomcliCmd.AddCommand(listBalancesCmd)
 
+       BytomcliCmd.AddCommand(rescanWalletCmd)
+       BytomcliCmd.AddCommand(walletInfoCmd)
+
        BytomcliCmd.AddCommand(buildTransactionCmd)
        BytomcliCmd.AddCommand(signTransactionCmd)
        BytomcliCmd.AddCommand(submitTransactionCmd)
@@ -199,6 +202,9 @@ func AddTemplateFunc() {
                listTransactionsCmd.Name(),
                listUnspentOutputsCmd.Name(),
                listBalancesCmd.Name(),
+
+               rescanWalletCmd.Name(),
+               walletInfoCmd.Name(),
        }
 
        cobra.AddTemplateFunc("WalletEnable", func(cmdName string) bool {
diff --git a/cmd/bytomcli/commands/wallet.go b/cmd/bytomcli/commands/wallet.go
new file mode 100644 (file)
index 0000000..b617fe0
--- /dev/null
@@ -0,0 +1,36 @@
+package commands
+
+import (
+       "os"
+
+       "github.com/spf13/cobra"
+       jww "github.com/spf13/jwalterweatherman"
+
+       "github.com/bytom/util"
+)
+
+var walletInfoCmd = &cobra.Command{
+       Use:   "wallet-info",
+       Short: "Print the block information for wallet",
+       Args:  cobra.NoArgs,
+       Run: func(cmd *cobra.Command, args []string) {
+               data, exitCode := util.ClientCall("/wallet-info")
+               if exitCode != util.Success {
+                       os.Exit(exitCode)
+               }
+               printJSON(data)
+       },
+}
+
+var rescanWalletCmd = &cobra.Command{
+       Use:   "rescan-wallet",
+       Short: "Print the block information for wallet",
+       Args:  cobra.NoArgs,
+       Run: func(cmd *cobra.Command, args []string) {
+               if _, exitCode := util.ClientCall("/rescan-wallet"); exitCode != util.Success {
+                       os.Exit(exitCode)
+               }
+
+               jww.FEEDBACK.Println("Successfully trigger rescan wallet")
+       },
+}
index 028cfac..751f9f3 100644 (file)
@@ -2,6 +2,7 @@ package wallet
 
 import (
        "encoding/json"
+       "sync"
 
        log "github.com/sirupsen/logrus"
        "github.com/tendermint/tmlibs/db"
@@ -34,6 +35,7 @@ type StatusInfo struct {
 //Wallet is related to storing account unspent outputs
 type Wallet struct {
        DB         db.DB
+       rw         sync.RWMutex
        status     StatusInfo
        AccountMgr *account.Manager
        AssetReg   *asset.Registry
@@ -93,6 +95,9 @@ func (w *Wallet) commitWalletInfo(batch db.Batch) error {
 
 // AttachBlock attach a new block
 func (w *Wallet) AttachBlock(block *types.Block) error {
+       w.rw.Lock()
+       defer w.rw.Unlock()
+
        if block.PreviousBlockHash != w.status.WorkHash {
                log.Warn("wallet skip attachBlock due to status hash not equal to previous hash")
                return nil
@@ -119,6 +124,9 @@ func (w *Wallet) AttachBlock(block *types.Block) error {
 
 // DetachBlock detach a block and rollback state
 func (w *Wallet) DetachBlock(block *types.Block) error {
+       w.rw.Lock()
+       defer w.rw.Unlock()
+
        blockHash := block.Hash()
        txStatus, err := w.chain.GetTransactionStatus(&blockHash)
        if err != nil {
@@ -195,12 +203,21 @@ func (w *Wallet) GetNewTxCh() chan *types.Tx {
        return w.newTxCh
 }
 
+// UnconfirmedTxCollector collector unconfirmed transaction
 func (w *Wallet) UnconfirmedTxCollector() {
        for {
                w.SaveUnconfirmedTx(<-w.newTxCh)
        }
 }
 
+// GetWalletStatusInfo return current wallet StatusInfo
+func (w *Wallet) GetWalletStatusInfo() StatusInfo {
+       w.rw.RLock()
+       defer w.rw.RUnlock()
+
+       return w.status
+}
+
 func (w *Wallet) createProgram(account *account.Account, XPub *pseudohsm.XPub, index uint64) error {
        for i := uint64(0); i < index; i++ {
                if _, err := w.AccountMgr.CreateAddress(nil, account.ID, false); err != nil {