Golang implemented sidechain for Bytom
リビジョン | 5daf9f0c3aa5740e9282404f79f5bd22558e0162 (tree) |
---|---|
日時 | 2019-06-10 17:11:47 |
作者 | mars <mars@byto...> |
コミッター | mars |
add api for vote num
@@ -25,9 +25,9 @@ import ( | ||
25 | 25 | "github.com/vapor/net/websocket" |
26 | 26 | "github.com/vapor/netsync/peers" |
27 | 27 | "github.com/vapor/p2p" |
28 | + "github.com/vapor/proposal/blockproposer" | |
28 | 29 | "github.com/vapor/protocol" |
29 | 30 | "github.com/vapor/wallet" |
30 | - "github.com/vapor/proposal/blockproposer" | |
31 | 31 | ) |
32 | 32 | |
33 | 33 | var ( |
@@ -245,6 +245,7 @@ func (a *API) buildHandler() { | ||
245 | 245 | |
246 | 246 | m.Handle("/list-balances", jsonHandler(a.listBalances)) |
247 | 247 | m.Handle("/list-unspent-outputs", jsonHandler(a.listUnspentOutputs)) |
248 | + m.Handle("/list-votes", jsonHandler(a.listVotes)) | |
248 | 249 | |
249 | 250 | m.Handle("/decode-program", jsonHandler(a.decodeProgram)) |
250 | 251 |
@@ -108,6 +108,27 @@ func (a *API) listBalances(ctx context.Context, filter struct { | ||
108 | 108 | return NewSuccessResponse(balances) |
109 | 109 | } |
110 | 110 | |
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 | + | |
111 | 132 | // POST /get-transaction |
112 | 133 | func (a *API) getTransaction(ctx context.Context, txInfo struct { |
113 | 134 | TxID string `json:"tx_id"` |
@@ -293,7 +314,7 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct { | ||
293 | 314 | } |
294 | 315 | accountID = acc.ID |
295 | 316 | } |
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) | |
297 | 318 | |
298 | 319 | UTXOs := []query.AnnotatedUTXO{} |
299 | 320 | for _, utxo := range accountUTXOs { |
@@ -309,7 +309,7 @@ func (w *Wallet) GetTransactions(accountID string) ([]*query.AnnotatedTx, error) | ||
309 | 309 | |
310 | 310 | // GetAccountBalances return all account balances |
311 | 311 | 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)) | |
313 | 313 | } |
314 | 314 | |
315 | 315 | // AccountBalance account balance |
@@ -373,3 +373,70 @@ func (w *Wallet) indexBalances(accountUTXOs []*account.UTXO) ([]AccountBalance, | ||
373 | 373 | |
374 | 374 | return balances, nil |
375 | 375 | } |
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 | +} |
@@ -16,7 +16,7 @@ import ( | ||
16 | 16 | ) |
17 | 17 | |
18 | 18 | // 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 { | |
20 | 20 | prefix := account.UTXOPreFix |
21 | 21 | if isSmartContract { |
22 | 22 | prefix = account.SUTXOPrefix |
@@ -37,6 +37,10 @@ func (w *Wallet) GetAccountUtxos(accountID string, id string, unconfirmed, isSma | ||
37 | 37 | continue |
38 | 38 | } |
39 | 39 | |
40 | + if vote && accountUtxo.Vote == nil { | |
41 | + continue | |
42 | + } | |
43 | + | |
40 | 44 | if accountID == accountUtxo.AccountID || accountID == "" { |
41 | 45 | accountUtxos = append(accountUtxos, accountUtxo) |
42 | 46 | } |
@@ -197,7 +197,7 @@ func TestGetAccountUtxos(t *testing.T) { | ||
197 | 197 | |
198 | 198 | w.AccountMgr = account.NewManager(testDB, nil) |
199 | 199 | 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) | |
201 | 201 | if !testutil.DeepEqual(gotUtxos, c.wantUtxos) { |
202 | 202 | t.Errorf("case %d: got %v want %v", i, gotUtxos, c.wantUtxos) |
203 | 203 | } |