OSDN Git Service

add api for vote num v0.1_votetx_balance
authormars <mars@bytom.io>
Mon, 10 Jun 2019 08:11:47 +0000 (16:11 +0800)
committermars <mars@bytom.io>
Mon, 10 Jun 2019 08:11:47 +0000 (16:11 +0800)
api/api.go
api/query.go
wallet/indexer.go
wallet/utxo.go
wallet/utxo_test.go

index 30e7152..8e7c3b3 100644 (file)
@@ -25,9 +25,9 @@ import (
        "github.com/vapor/net/websocket"
        "github.com/vapor/netsync/peers"
        "github.com/vapor/p2p"
+       "github.com/vapor/proposal/blockproposer"
        "github.com/vapor/protocol"
        "github.com/vapor/wallet"
-       "github.com/vapor/proposal/blockproposer"
 )
 
 var (
@@ -245,6 +245,7 @@ func (a *API) buildHandler() {
 
                m.Handle("/list-balances", jsonHandler(a.listBalances))
                m.Handle("/list-unspent-outputs", jsonHandler(a.listUnspentOutputs))
+               m.Handle("/list-votes", jsonHandler(a.listVotes))
 
                m.Handle("/decode-program", jsonHandler(a.decodeProgram))
 
index a3e927b..c9d6059 100644 (file)
@@ -108,6 +108,27 @@ func (a *API) listBalances(ctx context.Context, filter struct {
        return NewSuccessResponse(balances)
 }
 
+// POST /list-balances
+func (a *API) listVotes(ctx context.Context, filter struct {
+       AccountID    string `json:"account_id"`
+       AccountAlias string `json:"account_alias"`
+}) Response {
+       accountID := filter.AccountID
+       if filter.AccountAlias != "" {
+               acc, err := a.wallet.AccountMgr.FindByAlias(filter.AccountAlias)
+               if err != nil {
+                       return NewErrorResponse(err)
+               }
+               accountID = acc.ID
+       }
+
+       votes, err := a.wallet.GetAccountVotes(accountID, "")
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+       return NewSuccessResponse(votes)
+}
+
 // POST /get-transaction
 func (a *API) getTransaction(ctx context.Context, txInfo struct {
        TxID string `json:"tx_id"`
@@ -293,7 +314,7 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
                }
                accountID = acc.ID
        }
-       accountUTXOs := a.wallet.GetAccountUtxos(accountID, filter.ID, filter.Unconfirmed, filter.SmartContract)
+       accountUTXOs := a.wallet.GetAccountUtxos(accountID, filter.ID, filter.Unconfirmed, filter.SmartContract, false)
 
        UTXOs := []query.AnnotatedUTXO{}
        for _, utxo := range accountUTXOs {
index f3b7d57..8531ca6 100644 (file)
@@ -309,7 +309,7 @@ func (w *Wallet) GetTransactions(accountID string) ([]*query.AnnotatedTx, error)
 
 // GetAccountBalances return all account balances
 func (w *Wallet) GetAccountBalances(accountID string, id string) ([]AccountBalance, error) {
-       return w.indexBalances(w.GetAccountUtxos(accountID, "", false, false))
+       return w.indexBalances(w.GetAccountUtxos(accountID, "", false, false, false))
 }
 
 // AccountBalance account balance
@@ -373,3 +373,70 @@ func (w *Wallet) indexBalances(accountUTXOs []*account.UTXO) ([]AccountBalance,
 
        return balances, nil
 }
+
+// AccountVote account vote
+type AccountVote struct {
+       AccountID       string                 `json:"account_id"`
+       Alias           string                 `json:"account_alias"`
+       AssetAlias      string                 `json:"asset_alias"`
+       AssetID         string                 `json:"asset_id"`
+       Amount          uint64                 `json:"amount"`
+       AssetDefinition map[string]interface{} `json:"asset_definition"`
+}
+
+// GetAccountVotes return all account votes
+func (w *Wallet) GetAccountVotes(accountID string, id string) ([]AccountVote, error) {
+       return w.indexVotes(w.GetAccountUtxos(accountID, "", false, false, true))
+}
+
+func (w *Wallet) indexVotes(accountUTXOs []*account.UTXO) ([]AccountVote, error) {
+       accBalance := make(map[string]map[string]uint64)
+       votes := []AccountVote{}
+
+       for _, accountUTXO := range accountUTXOs {
+               assetID := accountUTXO.AssetID.String()
+               if _, ok := accBalance[accountUTXO.AccountID]; ok {
+                       if _, ok := accBalance[accountUTXO.AccountID][assetID]; ok {
+                               accBalance[accountUTXO.AccountID][assetID] += accountUTXO.Amount
+                       } else {
+                               accBalance[accountUTXO.AccountID][assetID] = accountUTXO.Amount
+                       }
+               } else {
+                       accBalance[accountUTXO.AccountID] = map[string]uint64{assetID: accountUTXO.Amount}
+               }
+       }
+
+       var sortedAccount []string
+       for k := range accBalance {
+               sortedAccount = append(sortedAccount, k)
+       }
+       sort.Strings(sortedAccount)
+
+       for _, id := range sortedAccount {
+               var sortedAsset []string
+               for k := range accBalance[id] {
+                       sortedAsset = append(sortedAsset, k)
+               }
+               sort.Strings(sortedAsset)
+
+               for _, assetID := range sortedAsset {
+                       alias := w.AccountMgr.GetAliasByID(id)
+                       targetAsset, err := w.AssetReg.GetAsset(assetID)
+                       if err != nil {
+                               return nil, err
+                       }
+
+                       assetAlias := *targetAsset.Alias
+                       votes = append(votes, AccountVote{
+                               Alias:           alias,
+                               AccountID:       id,
+                               AssetID:         assetID,
+                               AssetAlias:      assetAlias,
+                               Amount:          accBalance[id][assetID],
+                               AssetDefinition: targetAsset.DefinitionMap,
+                       })
+               }
+       }
+
+       return votes, nil
+}
index 77c956c..e1373f5 100644 (file)
@@ -16,7 +16,7 @@ import (
 )
 
 // GetAccountUtxos return all account unspent outputs
-func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool) []*account.UTXO {
+func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool, vote bool) []*account.UTXO {
        prefix := account.UTXOPreFix
        if isSmartContract {
                prefix = account.SUTXOPrefix
@@ -37,6 +37,10 @@ func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSma
                        continue
                }
 
+               if vote && accountUtxo.Vote == nil {
+                       continue
+               }
+
                if accountID == accountUtxo.AccountID || accountID == "" {
                        accountUtxos = append(accountUtxos, accountUtxo)
                }
index e5fb1b8..c5aad35 100644 (file)
@@ -197,7 +197,7 @@ func TestGetAccountUtxos(t *testing.T) {
 
                w.AccountMgr = account.NewManager(testDB, nil)
                w.AccountMgr.AddUnconfirmedUtxo(c.unconfirmedUtxos)
-               gotUtxos := w.GetAccountUtxos("", c.id, c.unconfirmed, c.isSmartContract)
+               gotUtxos := w.GetAccountUtxos("", c.id, c.unconfirmed, c.isSmartContract, false)
                if !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
                        t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos)
                }