OSDN Git Service

add api for vote num (#149)
authorwz <mars@bytom.io>
Tue, 11 Jun 2019 08:24:14 +0000 (16:24 +0800)
committerPaladz <yzhu101@uottawa.ca>
Tue, 11 Jun 2019 08:24:14 +0000 (16:24 +0800)
* add api for vote num

* fix review

* fix review

api/api.go
api/query.go
wallet/indexer.go
wallet/utxo.go
wallet/utxo_test.go

index 30e7152..074e038 100644 (file)
@@ -25,9 +25,9 @@ import (
        "github.com/vapor/net/websocket"
        "github.com/vapor/netsync/peers"
        "github.com/vapor/p2p"
        "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/protocol"
        "github.com/vapor/wallet"
-       "github.com/vapor/proposal/blockproposer"
 )
 
 var (
 )
 
 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-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))
 
 
                m.Handle("/decode-program", jsonHandler(a.decodeProgram))
 
index a3e927b..938b3d3 100644 (file)
@@ -108,6 +108,26 @@ func (a *API) listBalances(ctx context.Context, filter struct {
        return NewSuccessResponse(balances)
 }
 
        return NewSuccessResponse(balances)
 }
 
+func (a *API) listAccountVotes(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"`
 // POST /get-transaction
 func (a *API) getTransaction(ctx context.Context, txInfo struct {
        TxID string `json:"tx_id"`
@@ -293,7 +313,7 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
                }
                accountID = acc.ID
        }
                }
                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 {
 
        UTXOs := []query.AnnotatedUTXO{}
        for _, utxo := range accountUTXOs {
index f3b7d57..47a8f47 100644 (file)
@@ -2,6 +2,7 @@ package wallet
 
 import (
        "encoding/binary"
 
 import (
        "encoding/binary"
+       "encoding/hex"
        "encoding/json"
        "fmt"
        "sort"
        "encoding/json"
        "fmt"
        "sort"
@@ -11,6 +12,7 @@ import (
        "github.com/vapor/account"
        "github.com/vapor/asset"
        "github.com/vapor/blockchain/query"
        "github.com/vapor/account"
        "github.com/vapor/asset"
        "github.com/vapor/blockchain/query"
+       "github.com/vapor/consensus"
        "github.com/vapor/crypto/sha3pool"
        dbm "github.com/vapor/database/leveldb"
        chainjson "github.com/vapor/encoding/json"
        "github.com/vapor/crypto/sha3pool"
        dbm "github.com/vapor/database/leveldb"
        chainjson "github.com/vapor/encoding/json"
@@ -309,7 +311,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) {
 
 // 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
 }
 
 // AccountBalance account balance
@@ -373,3 +375,72 @@ func (w *Wallet) indexBalances(accountUTXOs []*account.UTXO) ([]AccountBalance,
 
        return balances, nil
 }
 
        return balances, nil
 }
+
+// GetAccountVotes return all account votes
+func (w *Wallet) GetAccountVotes(accountID string, id string) ([]AccountVotes, error) {
+       return w.indexVotes(w.GetAccountUtxos(accountID, "", false, false, true))
+}
+
+type voteDetail struct {
+       Vote       string `json:"vote"`
+       VoteAmount uint64 `json:"vote_amount"`
+}
+
+// AccountVotes account vote
+type AccountVotes struct {
+       AccountID       string       `json:"account_id"`
+       Alias           string       `json:"account_alias"`
+       TotalVoteAmount uint64       `json:"total_vote_amount"`
+       VoteDetails     []voteDetail `json:"vote_details"`
+}
+
+func (w *Wallet) indexVotes(accountUTXOs []*account.UTXO) ([]AccountVotes, error) {
+       accVote := make(map[string]map[string]uint64)
+       votes := []AccountVotes{}
+
+       for _, accountUTXO := range accountUTXOs {
+               if accountUTXO.AssetID != *consensus.BTMAssetID || accountUTXO.Vote == nil {
+                       continue
+               }
+               xpub := hex.EncodeToString(accountUTXO.Vote)
+               if _, ok := accVote[accountUTXO.AccountID]; ok {
+                       accVote[accountUTXO.AccountID][xpub] += accountUTXO.Amount
+               } else {
+                       accVote[accountUTXO.AccountID] = map[string]uint64{xpub: accountUTXO.Amount}
+
+               }
+       }
+
+       var sortedAccount []string
+       for k := range accVote {
+               sortedAccount = append(sortedAccount, k)
+       }
+       sort.Strings(sortedAccount)
+
+       for _, id := range sortedAccount {
+               var sortedXpub []string
+               for k := range accVote[id] {
+                       sortedXpub = append(sortedXpub, k)
+               }
+               sort.Strings(sortedXpub)
+
+               voteDetails := []voteDetail{}
+               voteTotal := uint64(0)
+               for _, xpub := range sortedXpub {
+                       voteDetails = append(voteDetails, voteDetail{
+                               Vote:       xpub,
+                               VoteAmount: accVote[id][xpub],
+                       })
+                       voteTotal += accVote[id][xpub]
+               }
+               alias := w.AccountMgr.GetAliasByID(id)
+               votes = append(votes, AccountVotes{
+                       Alias:           alias,
+                       AccountID:       id,
+                       VoteDetails:     voteDetails,
+                       TotalVoteAmount: voteTotal,
+               })
+       }
+
+       return votes, nil
+}
index 77c956c..e1373f5 100644 (file)
@@ -16,7 +16,7 @@ import (
 )
 
 // GetAccountUtxos return all account unspent outputs
 )
 
 // 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
        prefix := account.UTXOPreFix
        if isSmartContract {
                prefix = account.SUTXOPrefix
@@ -37,6 +37,10 @@ func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSma
                        continue
                }
 
                        continue
                }
 
+               if vote && accountUtxo.Vote == nil {
+                       continue
+               }
+
                if accountID == accountUtxo.AccountID || accountID == "" {
                        accountUtxos = append(accountUtxos, accountUtxo)
                }
                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)
 
                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)
                }
                if !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
                        t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos)
                }