X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=wallet%2Findexer.go;h=bc61dc33d9f70acf51c51ce6804c4f65197526b4;hb=0bd22d212b767702bd1c99edb05d5f3a57cb572c;hp=2afcfa44fa25c3d99b3d10c3b6ba8b43e961f722;hpb=db158dcf09436b003defd333f1a665e7e051d820;p=bytom%2Fvapor.git diff --git a/wallet/indexer.go b/wallet/indexer.go index 2afcfa44..bc61dc33 100644 --- a/wallet/indexer.go +++ b/wallet/indexer.go @@ -2,6 +2,7 @@ package wallet import ( "encoding/binary" + "encoding/hex" "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/consensus" "github.com/vapor/crypto/sha3pool" dbm "github.com/vapor/database/leveldb" chainjson "github.com/vapor/encoding/json" @@ -88,12 +90,10 @@ func saveExternalAssetDefinition(b *types.Block, walletDB dbm.DB) { for _, tx := range b.Transactions { for _, orig := range tx.Inputs { - if ii, ok := orig.TypedInput.(*types.IssuanceInput); ok { - if isValidJSON(ii.AssetDefinition) { - assetID := ii.AssetID() - if assetExist := walletDB.Get(asset.ExtAssetKey(&assetID)); assetExist == nil { - storeBatch.Set(asset.ExtAssetKey(&assetID), ii.AssetDefinition) - } + if cci, ok := orig.TypedInput.(*types.CrossChainInput); ok { + assetID := cci.AssetId + if assetExist := walletDB.Get(asset.ExtAssetKey(assetID)); assetExist == nil { + storeBatch.Set(asset.ExtAssetKey(assetID), cci.AssetDefinition) } } } @@ -160,7 +160,7 @@ transactionLoop: statusFail, _ := txStatus.GetStatus(pos) for _, v := range tx.Outputs { var hash [32]byte - sha3pool.Sum256(hash[:], v.ControlProgram) + sha3pool.Sum256(hash[:], v.ControlProgram()) if bytes := w.DB.Get(account.ContractKey(hash)); bytes != nil { annotatedTxs = append(annotatedTxs, w.buildAnnotatedTransaction(tx, b, statusFail, pos)) @@ -311,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) { - return w.indexBalances(w.GetAccountUtxos(accountID, "", false, false)) + return w.indexBalances(w.GetAccountUtxos(accountID, "", false, false, false)) } // AccountBalance account balance @@ -375,3 +375,72 @@ func (w *Wallet) indexBalances(accountUTXOs []*account.UTXO) ([]AccountBalance, 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"` + VoteNumber uint64 `json:"vote_number"` +} + +// AccountVotes account vote +type AccountVotes struct { + AccountID string `json:"account_id"` + Alias string `json:"account_alias"` + TotalVoteNumber uint64 `json:"total_vote_number"` + 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, + VoteNumber: accVote[id][xpub], + }) + voteTotal += accVote[id][xpub] + } + alias := w.AccountMgr.GetAliasByID(id) + votes = append(votes, AccountVotes{ + Alias: alias, + AccountID: id, + VoteDetails: voteDetails, + TotalVoteNumber: voteTotal, + }) + } + + return votes, nil +}