10 log "github.com/sirupsen/logrus"
12 "github.com/vapor/account"
13 "github.com/vapor/blockchain/query"
14 "github.com/vapor/consensus"
15 "github.com/vapor/crypto/sha3pool"
16 chainjson "github.com/vapor/encoding/json"
17 "github.com/vapor/protocol/bc"
18 "github.com/vapor/protocol/bc/types"
21 func parseGlobalTxIdx(globalTxIdx []byte) (*bc.Hash, uint64) {
22 var hashBytes [32]byte
23 copy(hashBytes[:], globalTxIdx[:32])
24 hash := bc.NewHash(hashBytes)
25 position := binary.BigEndian.Uint64(globalTxIdx[32:])
26 return &hash, position
29 // saveExternalAssetDefinition save external and local assets definition,
30 // when query ,query local first and if have no then query external
31 // details see getAliasDefinition
32 func saveExternalAssetDefinition(b *types.Block, store WalletStorer) error {
34 defer store.CommitBatch()
36 for _, tx := range b.Transactions {
37 for _, orig := range tx.Inputs {
38 if cci, ok := orig.TypedInput.(*types.CrossChainInput); ok {
39 assetID := cci.AssetId
40 assetExist, err := store.GetAssetDefinition(assetID)
44 if assetExist == nil {
45 store.SetAssetDefinition(assetID, cci.AssetDefinition)
53 // Summary is the struct of transaction's input and output summary
55 Type string `json:"type"`
56 AssetID bc.AssetID `json:"asset_id,omitempty"`
57 AssetAlias string `json:"asset_alias,omitempty"`
58 Amount uint64 `json:"amount,omitempty"`
59 AccountID string `json:"account_id,omitempty"`
60 AccountAlias string `json:"account_alias,omitempty"`
61 Arbitrary chainjson.HexBytes `json:"arbitrary,omitempty"`
64 // TxSummary is the struct of transaction summary
65 type TxSummary struct {
66 ID bc.Hash `json:"tx_id"`
67 Timestamp uint64 `json:"block_time"`
68 Inputs []Summary `json:"inputs"`
69 Outputs []Summary `json:"outputs"`
72 // indexTransactions saves all annotated transactions to the database.
73 func (w *Wallet) indexTransactions(b *types.Block, txStatus *bc.TransactionStatus, annotatedTxs []*query.AnnotatedTx) error {
74 for _, tx := range annotatedTxs {
75 rawTx, err := json.Marshal(tx)
77 log.WithFields(log.Fields{"module": logModule, "err": err}).Error("inserting annotated_txs to db")
81 w.store.SetTransaction(b.Height, tx.Position, tx.ID.String(), rawTx)
82 w.store.DeleteUnconfirmedTransaction(tx.ID.String())
89 for position, globalTx := range b.Transactions {
90 blockHash := b.BlockHeader.Hash()
91 w.store.SetGlobalTransactionIndex(globalTx.ID.String(), &blockHash, uint64(position))
97 // filterAccountTxs related and build the fully annotated transactions.
98 func (w *Wallet) filterAccountTxs(b *types.Block, txStatus *bc.TransactionStatus) []*query.AnnotatedTx {
99 annotatedTxs := make([]*query.AnnotatedTx, 0, len(b.Transactions))
102 for pos, tx := range b.Transactions {
103 statusFail, _ := txStatus.GetStatus(pos)
104 for _, v := range tx.Outputs {
106 sha3pool.Sum256(hash[:], v.ControlProgram())
108 if bytes := w.store.GetRawProgram(hash); bytes != nil {
109 annotatedTxs = append(annotatedTxs, w.buildAnnotatedTransaction(tx, b, statusFail, pos))
110 continue transactionLoop
114 for _, v := range tx.Inputs {
115 outid, err := v.SpentOutputID()
119 if bytes := w.store.GetStandardUTXO(outid); bytes != nil {
120 annotatedTxs = append(annotatedTxs, w.buildAnnotatedTransaction(tx, b, statusFail, pos))
121 continue transactionLoop
129 // GetTransactionByTxID get transaction by txID
130 func (w *Wallet) GetTransactionByTxID(txID string) (*query.AnnotatedTx, error) {
131 if annotatedTx, err := w.getAccountTxByTxID(txID); err == nil {
132 return annotatedTx, nil
133 } else if !w.TxIndexFlag {
137 return w.getGlobalTxByTxID(txID)
140 func (w *Wallet) getAccountTxByTxID(txID string) (*query.AnnotatedTx, error) {
141 annotatedTx := &query.AnnotatedTx{}
142 txInfo, err := w.store.GetTransaction(txID)
147 if err := json.Unmarshal(txInfo, annotatedTx); err != nil {
151 annotateTxsAsset(w, []*query.AnnotatedTx{annotatedTx})
152 return annotatedTx, nil
155 func (w *Wallet) getGlobalTxByTxID(txID string) (*query.AnnotatedTx, error) {
156 globalTxIdx := w.store.GetGlobalTransaction(txID)
157 if globalTxIdx == nil {
158 return nil, fmt.Errorf("No transaction(tx_id=%s) ", txID)
161 blockHash, pos := parseGlobalTxIdx(globalTxIdx)
162 block, err := w.chain.GetBlockByHash(blockHash)
167 txStatus, err := w.chain.GetTransactionStatus(blockHash)
172 statusFail, err := txStatus.GetStatus(int(pos))
177 tx := block.Transactions[int(pos)]
178 return w.buildAnnotatedTransaction(tx, block, statusFail, int(pos)), nil
181 // GetTransactionsSummary get transactions summary
182 func (w *Wallet) GetTransactionsSummary(transactions []*query.AnnotatedTx) []TxSummary {
185 for _, annotatedTx := range transactions {
186 tmpTxSummary := TxSummary{
187 Inputs: make([]Summary, len(annotatedTx.Inputs)),
188 Outputs: make([]Summary, len(annotatedTx.Outputs)),
190 Timestamp: annotatedTx.Timestamp,
193 for i, input := range annotatedTx.Inputs {
194 tmpTxSummary.Inputs[i].Type = input.Type
195 tmpTxSummary.Inputs[i].AccountID = input.AccountID
196 tmpTxSummary.Inputs[i].AccountAlias = input.AccountAlias
197 tmpTxSummary.Inputs[i].AssetID = input.AssetID
198 tmpTxSummary.Inputs[i].AssetAlias = input.AssetAlias
199 tmpTxSummary.Inputs[i].Amount = input.Amount
200 tmpTxSummary.Inputs[i].Arbitrary = input.Arbitrary
202 for j, output := range annotatedTx.Outputs {
203 tmpTxSummary.Outputs[j].Type = output.Type
204 tmpTxSummary.Outputs[j].AccountID = output.AccountID
205 tmpTxSummary.Outputs[j].AccountAlias = output.AccountAlias
206 tmpTxSummary.Outputs[j].AssetID = output.AssetID
207 tmpTxSummary.Outputs[j].AssetAlias = output.AssetAlias
208 tmpTxSummary.Outputs[j].Amount = output.Amount
211 Txs = append(Txs, tmpTxSummary)
217 func findTransactionsByAccount(annotatedTx *query.AnnotatedTx, accountID string) bool {
218 for _, input := range annotatedTx.Inputs {
219 if input.AccountID == accountID {
224 for _, output := range annotatedTx.Outputs {
225 if output.AccountID == accountID {
233 // GetTransactions get all walletDB transactions, and filter transactions by accountID optional
234 func (w *Wallet) GetTransactions(accountID string) ([]*query.AnnotatedTx, error) {
235 annotatedTxs := []*query.AnnotatedTx{}
236 annotatedTxs, err := w.store.GetTransactions()
241 newAnnotatedTxs := []*query.AnnotatedTx{}
242 for _, annotatedTx := range annotatedTxs {
243 if accountID == "" || findTransactionsByAccount(annotatedTx, accountID) {
244 annotateTxsAsset(w, []*query.AnnotatedTx{annotatedTx})
245 newAnnotatedTxs = append([]*query.AnnotatedTx{annotatedTx}, newAnnotatedTxs...)
249 return newAnnotatedTxs, nil
252 // GetAccountBalances return all account balances
253 func (w *Wallet) GetAccountBalances(accountID string, id string) ([]AccountBalance, error) {
254 return w.indexBalances(w.GetAccountUtxos(accountID, "", false, false, false))
257 // AccountBalance account balance
258 type AccountBalance struct {
259 AccountID string `json:"account_id"`
260 Alias string `json:"account_alias"`
261 AssetAlias string `json:"asset_alias"`
262 AssetID string `json:"asset_id"`
263 Amount uint64 `json:"amount"`
264 AssetDefinition map[string]interface{} `json:"asset_definition"`
267 func (w *Wallet) indexBalances(accountUTXOs []*account.UTXO) ([]AccountBalance, error) {
268 accBalance := make(map[string]map[string]uint64)
269 balances := []AccountBalance{}
271 for _, accountUTXO := range accountUTXOs {
272 assetID := accountUTXO.AssetID.String()
273 if _, ok := accBalance[accountUTXO.AccountID]; ok {
274 if _, ok := accBalance[accountUTXO.AccountID][assetID]; ok {
275 accBalance[accountUTXO.AccountID][assetID] += accountUTXO.Amount
277 accBalance[accountUTXO.AccountID][assetID] = accountUTXO.Amount
280 accBalance[accountUTXO.AccountID] = map[string]uint64{assetID: accountUTXO.Amount}
284 var sortedAccount []string
285 for k := range accBalance {
286 sortedAccount = append(sortedAccount, k)
288 sort.Strings(sortedAccount)
290 for _, id := range sortedAccount {
291 var sortedAsset []string
292 for k := range accBalance[id] {
293 sortedAsset = append(sortedAsset, k)
295 sort.Strings(sortedAsset)
297 for _, assetID := range sortedAsset {
298 alias := w.AccountMgr.GetAliasByID(id)
299 targetAsset, err := w.AssetReg.GetAsset(assetID)
304 assetAlias := *targetAsset.Alias
305 balances = append(balances, AccountBalance{
309 AssetAlias: assetAlias,
310 Amount: accBalance[id][assetID],
311 AssetDefinition: targetAsset.DefinitionMap,
319 // GetAccountVotes return all account votes
320 func (w *Wallet) GetAccountVotes(accountID string, id string) ([]AccountVotes, error) {
321 return w.indexVotes(w.GetAccountUtxos(accountID, "", false, false, true))
324 type voteDetail struct {
325 Vote string `json:"vote"`
326 VoteNumber uint64 `json:"vote_number"`
329 // AccountVotes account vote
330 type AccountVotes struct {
331 AccountID string `json:"account_id"`
332 Alias string `json:"account_alias"`
333 TotalVoteNumber uint64 `json:"total_vote_number"`
334 VoteDetails []voteDetail `json:"vote_details"`
337 func (w *Wallet) indexVotes(accountUTXOs []*account.UTXO) ([]AccountVotes, error) {
338 accVote := make(map[string]map[string]uint64)
339 votes := []AccountVotes{}
341 for _, accountUTXO := range accountUTXOs {
342 if accountUTXO.AssetID != *consensus.BTMAssetID || accountUTXO.Vote == nil {
345 xpub := hex.EncodeToString(accountUTXO.Vote)
346 if _, ok := accVote[accountUTXO.AccountID]; ok {
347 accVote[accountUTXO.AccountID][xpub] += accountUTXO.Amount
349 accVote[accountUTXO.AccountID] = map[string]uint64{xpub: accountUTXO.Amount}
354 var sortedAccount []string
355 for k := range accVote {
356 sortedAccount = append(sortedAccount, k)
358 sort.Strings(sortedAccount)
360 for _, id := range sortedAccount {
361 var sortedXpub []string
362 for k := range accVote[id] {
363 sortedXpub = append(sortedXpub, k)
365 sort.Strings(sortedXpub)
367 voteDetails := []voteDetail{}
368 voteTotal := uint64(0)
369 for _, xpub := range sortedXpub {
370 voteDetails = append(voteDetails, voteDetail{
372 VoteNumber: accVote[id][xpub],
374 voteTotal += accVote[id][xpub]
376 alias := w.AccountMgr.GetAliasByID(id)
377 votes = append(votes, AccountVotes{
380 VoteDetails: voteDetails,
381 TotalVoteNumber: voteTotal,