• R/O
  • HTTP
  • SSH
  • HTTPS

vapor: コミット

Golang implemented sidechain for Bytom


コミットメタ情報

リビジョン5daf9f0c3aa5740e9282404f79f5bd22558e0162 (tree)
日時2019-06-10 17:11:47
作者mars <mars@byto...>
コミッターmars

ログメッセージ

add api for vote num

変更サマリ

差分

--- a/api/api.go
+++ b/api/api.go
@@ -25,9 +25,9 @@ import (
2525 "github.com/vapor/net/websocket"
2626 "github.com/vapor/netsync/peers"
2727 "github.com/vapor/p2p"
28+ "github.com/vapor/proposal/blockproposer"
2829 "github.com/vapor/protocol"
2930 "github.com/vapor/wallet"
30- "github.com/vapor/proposal/blockproposer"
3131 )
3232
3333 var (
@@ -245,6 +245,7 @@ func (a *API) buildHandler() {
245245
246246 m.Handle("/list-balances", jsonHandler(a.listBalances))
247247 m.Handle("/list-unspent-outputs", jsonHandler(a.listUnspentOutputs))
248+ m.Handle("/list-votes", jsonHandler(a.listVotes))
248249
249250 m.Handle("/decode-program", jsonHandler(a.decodeProgram))
250251
--- a/api/query.go
+++ b/api/query.go
@@ -108,6 +108,27 @@ func (a *API) listBalances(ctx context.Context, filter struct {
108108 return NewSuccessResponse(balances)
109109 }
110110
111+// POST /list-balances
112+func (a *API) listVotes(ctx context.Context, filter struct {
113+ AccountID string `json:"account_id"`
114+ AccountAlias string `json:"account_alias"`
115+}) Response {
116+ accountID := filter.AccountID
117+ if filter.AccountAlias != "" {
118+ acc, err := a.wallet.AccountMgr.FindByAlias(filter.AccountAlias)
119+ if err != nil {
120+ return NewErrorResponse(err)
121+ }
122+ accountID = acc.ID
123+ }
124+
125+ votes, err := a.wallet.GetAccountVotes(accountID, "")
126+ if err != nil {
127+ return NewErrorResponse(err)
128+ }
129+ return NewSuccessResponse(votes)
130+}
131+
111132 // POST /get-transaction
112133 func (a *API) getTransaction(ctx context.Context, txInfo struct {
113134 TxID string `json:"tx_id"`
@@ -293,7 +314,7 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
293314 }
294315 accountID = acc.ID
295316 }
296- accountUTXOs := a.wallet.GetAccountUtxos(accountID, filter.ID, filter.Unconfirmed, filter.SmartContract)
317+ accountUTXOs := a.wallet.GetAccountUtxos(accountID, filter.ID, filter.Unconfirmed, filter.SmartContract, false)
297318
298319 UTXOs := []query.AnnotatedUTXO{}
299320 for _, utxo := range accountUTXOs {
--- a/wallet/indexer.go
+++ b/wallet/indexer.go
@@ -309,7 +309,7 @@ func (w *Wallet) GetTransactions(accountID string) ([]*query.AnnotatedTx, error)
309309
310310 // GetAccountBalances return all account balances
311311 func (w *Wallet) GetAccountBalances(accountID string, id string) ([]AccountBalance, error) {
312- return w.indexBalances(w.GetAccountUtxos(accountID, "", false, false))
312+ return w.indexBalances(w.GetAccountUtxos(accountID, "", false, false, false))
313313 }
314314
315315 // AccountBalance account balance
@@ -373,3 +373,70 @@ func (w *Wallet) indexBalances(accountUTXOs []*account.UTXO) ([]AccountBalance,
373373
374374 return balances, nil
375375 }
376+
377+// AccountVote account vote
378+type AccountVote struct {
379+ AccountID string `json:"account_id"`
380+ Alias string `json:"account_alias"`
381+ AssetAlias string `json:"asset_alias"`
382+ AssetID string `json:"asset_id"`
383+ Amount uint64 `json:"amount"`
384+ AssetDefinition map[string]interface{} `json:"asset_definition"`
385+}
386+
387+// GetAccountVotes return all account votes
388+func (w *Wallet) GetAccountVotes(accountID string, id string) ([]AccountVote, error) {
389+ return w.indexVotes(w.GetAccountUtxos(accountID, "", false, false, true))
390+}
391+
392+func (w *Wallet) indexVotes(accountUTXOs []*account.UTXO) ([]AccountVote, error) {
393+ accBalance := make(map[string]map[string]uint64)
394+ votes := []AccountVote{}
395+
396+ for _, accountUTXO := range accountUTXOs {
397+ assetID := accountUTXO.AssetID.String()
398+ if _, ok := accBalance[accountUTXO.AccountID]; ok {
399+ if _, ok := accBalance[accountUTXO.AccountID][assetID]; ok {
400+ accBalance[accountUTXO.AccountID][assetID] += accountUTXO.Amount
401+ } else {
402+ accBalance[accountUTXO.AccountID][assetID] = accountUTXO.Amount
403+ }
404+ } else {
405+ accBalance[accountUTXO.AccountID] = map[string]uint64{assetID: accountUTXO.Amount}
406+ }
407+ }
408+
409+ var sortedAccount []string
410+ for k := range accBalance {
411+ sortedAccount = append(sortedAccount, k)
412+ }
413+ sort.Strings(sortedAccount)
414+
415+ for _, id := range sortedAccount {
416+ var sortedAsset []string
417+ for k := range accBalance[id] {
418+ sortedAsset = append(sortedAsset, k)
419+ }
420+ sort.Strings(sortedAsset)
421+
422+ for _, assetID := range sortedAsset {
423+ alias := w.AccountMgr.GetAliasByID(id)
424+ targetAsset, err := w.AssetReg.GetAsset(assetID)
425+ if err != nil {
426+ return nil, err
427+ }
428+
429+ assetAlias := *targetAsset.Alias
430+ votes = append(votes, AccountVote{
431+ Alias: alias,
432+ AccountID: id,
433+ AssetID: assetID,
434+ AssetAlias: assetAlias,
435+ Amount: accBalance[id][assetID],
436+ AssetDefinition: targetAsset.DefinitionMap,
437+ })
438+ }
439+ }
440+
441+ return votes, nil
442+}
--- a/wallet/utxo.go
+++ b/wallet/utxo.go
@@ -16,7 +16,7 @@ import (
1616 )
1717
1818 // GetAccountUtxos return all account unspent outputs
19-func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool) []*account.UTXO {
19+func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSmartContract bool, vote bool) []*account.UTXO {
2020 prefix := account.UTXOPreFix
2121 if isSmartContract {
2222 prefix = account.SUTXOPrefix
@@ -37,6 +37,10 @@ func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSma
3737 continue
3838 }
3939
40+ if vote && accountUtxo.Vote == nil {
41+ continue
42+ }
43+
4044 if accountID == accountUtxo.AccountID || accountID == "" {
4145 accountUtxos = append(accountUtxos, accountUtxo)
4246 }
--- a/wallet/utxo_test.go
+++ b/wallet/utxo_test.go
@@ -197,7 +197,7 @@ func TestGetAccountUtxos(t *testing.T) {
197197
198198 w.AccountMgr = account.NewManager(testDB, nil)
199199 w.AccountMgr.AddUnconfirmedUtxo(c.unconfirmedUtxos)
200- gotUtxos := w.GetAccountUtxos("", c.id, c.unconfirmed, c.isSmartContract)
200+ gotUtxos := w.GetAccountUtxos("", c.id, c.unconfirmed, c.isSmartContract, false)
201201 if !testutil.DeepEqual(gotUtxos, c.wantUtxos) {
202202 t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos)
203203 }
旧リポジトリブラウザで表示