OSDN Git Service

fix the bug (#372)
[bytom/vapor.git] / api / query.go
index 79ab3a9..d65acaa 100644 (file)
@@ -5,21 +5,20 @@ import (
        "encoding/hex"
        "fmt"
 
-       "github.com/vapor/blockchain/txbuilder"
-       "github.com/vapor/crypto/ed25519"
-
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/account"
+       "github.com/vapor/asset"
        "github.com/vapor/blockchain/query"
        "github.com/vapor/blockchain/signers"
+       "github.com/vapor/common/arithmetic"
        "github.com/vapor/consensus"
+       "github.com/vapor/crypto/ed25519"
        "github.com/vapor/crypto/ed25519/chainkd"
        chainjson "github.com/vapor/encoding/json"
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
-       bytomtypes "github.com/vapor/protocol/bc/types/bytom/types"
 )
 
 // POST /list-accounts
@@ -54,13 +53,17 @@ func (a *API) listAccounts(ctx context.Context, filter struct {
 func (a *API) getAsset(ctx context.Context, filter struct {
        ID string `json:"id"`
 }) Response {
-       asset, err := a.wallet.AssetReg.GetAsset(filter.ID)
+       ass, err := a.wallet.AssetReg.GetAsset(filter.ID)
        if err != nil {
                log.Errorf("getAsset: %v", err)
                return NewErrorResponse(err)
        }
 
-       return NewSuccessResponse(asset)
+       annotatedAsset, err := asset.Annotated(ass)
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+       return NewSuccessResponse(annotatedAsset)
 }
 
 // POST /list-assets
@@ -73,7 +76,15 @@ func (a *API) listAssets(ctx context.Context, filter struct {
                return NewErrorResponse(err)
        }
 
-       return NewSuccessResponse(assets)
+       annotatedAssets := []*query.AnnotatedAsset{}
+       for _, ass := range assets {
+               annotatedAsset, err := asset.Annotated(ass)
+               if err != nil {
+                       return NewErrorResponse(err)
+               }
+               annotatedAssets = append(annotatedAssets, annotatedAsset)
+       }
+       return NewSuccessResponse(annotatedAssets)
 }
 
 // POST /list-balances
@@ -97,6 +108,26 @@ func (a *API) listBalances(ctx context.Context, filter struct {
        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"`
@@ -118,48 +149,37 @@ func (a *API) getTransaction(ctx context.Context, txInfo struct {
 
 // POST /list-transactions
 func (a *API) listTransactions(ctx context.Context, filter struct {
-       ID          string `json:"id"`
-       AccountID   string `json:"account_id"`
-       Detail      bool   `json:"detail"`
-       Unconfirmed bool   `json:"unconfirmed"`
-       From        uint   `json:"from"`
-       Count       uint   `json:"count"`
+       AccountID    string `json:"account_id"`
+       AccountAlias string `json:"account_alias"`
+       StartTxID    string `json:"start_tx_id"`
+       Detail       bool   `json:"detail"`
+       Unconfirmed  bool   `json:"unconfirmed"`
+       Count        uint   `json:"count"`
 }) Response {
-       transactions := []*query.AnnotatedTx{}
-       var err error
-       var transaction *query.AnnotatedTx
-
-       if filter.ID != "" {
-               transaction, err = a.wallet.GetTransactionByTxID(filter.ID)
-               if err != nil && filter.Unconfirmed {
-                       transaction, err = a.wallet.GetUnconfirmedTxByTxID(filter.ID)
-                       if err != nil {
-                               return NewErrorResponse(err)
-                       }
-               }
-               transactions = []*query.AnnotatedTx{transaction}
-       } else {
-               transactions, err = a.wallet.GetTransactions(filter.AccountID)
+       accountID := filter.AccountID
+       if filter.AccountAlias != "" {
+               acc, err := a.wallet.AccountMgr.FindByAlias(filter.AccountAlias)
                if err != nil {
                        return NewErrorResponse(err)
                }
+               accountID = acc.ID
+       }
 
-               if filter.Unconfirmed {
-                       unconfirmedTxs, err := a.wallet.GetUnconfirmedTxs(filter.AccountID)
-                       if err != nil {
-                               return NewErrorResponse(err)
-                       }
-                       transactions = append(unconfirmedTxs, transactions...)
-               }
+       if accountID == "" {
+               return NewErrorResponse(account.ErrAccountIDEmpty)
+       }
+
+       transactions, err := a.wallet.GetTransactions(accountID, filter.StartTxID, filter.Count, filter.Unconfirmed)
+       if err != nil {
+               return NewErrorResponse(err)
        }
 
        if filter.Detail == false {
                txSummary := a.wallet.GetTransactionsSummary(transactions)
-               start, end := getPageRange(len(txSummary), filter.From, filter.Count)
-               return NewSuccessResponse(txSummary[start:end])
+               return NewSuccessResponse(txSummary)
        }
-       start, end := getPageRange(len(transactions), filter.From, filter.Count)
-       return NewSuccessResponse(transactions[start:end])
+
+       return NewSuccessResponse(transactions)
 }
 
 // POST /get-unconfirmed-transaction
@@ -189,7 +209,9 @@ func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
        resOutID := txDesc.Tx.ResultIds[0]
        resOut := txDesc.Tx.Entries[*resOutID]
        switch out := resOut.(type) {
-       case *bc.Output:
+       case *bc.IntraChainOutput:
+               tx.MuxID = *out.Source.Ref
+       case *bc.VoteOutput:
                tx.MuxID = *out.Source.Ref
        case *bc.Retirement:
                tx.MuxID = *out.Source.Ref
@@ -257,7 +279,7 @@ func (a *API) decodeRawTransaction(ctx context.Context, ins struct {
                tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(&ins.Tx, i))
        }
 
-       tx.Fee = txbuilder.CalculateTxFee(&ins.Tx)
+       tx.Fee, _ = arithmetic.CalculateTxFee(&ins.Tx)
        return NewSuccessResponse(tx)
 }
 
@@ -279,7 +301,7 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
                }
                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 {
@@ -305,7 +327,7 @@ func (a *API) listUnspentOutputs(ctx context.Context, filter struct {
 
 // return gasRate
 func (a *API) gasRate() Response {
-       gasrate := map[string]int64{"gas_rate": consensus.VMGasRate}
+       gasrate := map[string]int64{"gas_rate": consensus.ActiveNetParams.VMGasRate}
        return NewSuccessResponse(gasrate)
 }
 
@@ -411,138 +433,3 @@ func (a *API) listPubKeys(ctx context.Context, ins struct {
                PubKeyInfos: pubKeyInfos,
        })
 }
-
-type GetRawTransationResp struct {
-       Tx        *bytomtypes.Tx `json:"raw_transaction"`
-       BlockHash bc.Hash        `json:"block_hash"`
-}
-
-func (a *API) getRawTransaction(ins struct {
-       RawBlock string `json:"raw_block"`
-       TxID     string `json:"tx_id"`
-}) Response {
-
-       var rawTransaction *bytomtypes.Tx
-       block := &bytomtypes.Block{}
-       err := block.UnmarshalText([]byte(ins.RawBlock))
-       if err != nil {
-               return NewErrorResponse(err)
-       }
-
-       txID := bc.Hash{}
-       txID.UnmarshalText([]byte(ins.TxID))
-       for _, tx := range block.Transactions {
-               if tx.ID.String() == txID.String() {
-                       rawTransaction = tx
-                       break
-               }
-       }
-       if rawTransaction == nil {
-               return NewErrorResponse(errors.New("raw transaction do not find"))
-       }
-       resp := GetRawTransationResp{Tx: rawTransaction, BlockHash: block.Hash()}
-       return NewSuccessResponse(resp)
-}
-
-type GetSideRawTransationResp struct {
-       Tx        *types.Tx `json:"raw_transaction"`
-       BlockHash bc.Hash   `json:"block_hash"`
-}
-
-func (a *API) getSideRawTransaction(ins struct {
-       BlockHeight uint64 `json:"block_height"`
-       TxID        string `json:"tx_id"`
-}) Response {
-       block, err := a.chain.GetBlockByHeight(ins.BlockHeight)
-       if err != nil {
-               return NewErrorResponse(err)
-       }
-
-       var rawTransaction *types.Tx
-
-       txID := bc.Hash{}
-       txID.UnmarshalText([]byte(ins.TxID))
-
-       for _, tx := range block.Transactions {
-               if tx.ID.String() == txID.String() {
-                       rawTransaction = tx
-                       break
-               }
-       }
-       if rawTransaction == nil {
-               return NewErrorResponse(errors.New("raw transaction do not find"))
-       }
-       resp := GetSideRawTransationResp{Tx: rawTransaction, BlockHash: block.Hash()}
-       return NewSuccessResponse(resp)
-}
-
-type utxoResp struct {
-       Utxo account.UTXO `json:"utxo"`
-}
-
-func (a *API) getUnspentOutputs(ins struct {
-       RawBlock string `json:"raw_block"`
-       TxID     string `json:"tx_id"`
-       ID       string `json:"id"`
-       Address  string `json:"address"`
-}) Response {
-       var rawTransaction *bytomtypes.Tx
-       block := &bytomtypes.Block{}
-       err := block.UnmarshalText([]byte(ins.RawBlock))
-       if err != nil {
-               return NewErrorResponse(err)
-       }
-
-       txID := bc.Hash{}
-       txID.UnmarshalText([]byte(ins.TxID))
-
-       for _, tx := range block.Transactions {
-               if tx.ID.String() == txID.String() {
-                       rawTransaction = tx
-                       break
-               }
-       }
-       utxo := account.UTXO{}
-
-       for i, out := range rawTransaction.Outputs {
-               key := rawTransaction.ResultIds[i]
-               if key.String() == ins.ID {
-                       outPut, err := rawTransaction.Output(*key)
-                       if err != nil {
-                               continue
-                       }
-                       outputID := bc.Hash{
-                               V0: key.GetV0(),
-                               V1: key.GetV1(),
-                               V2: key.GetV2(),
-                               V3: key.GetV3(),
-                       }
-
-                       assetID := bc.AssetID{
-                               V0: out.AssetAmount.AssetId.GetV0(),
-                               V1: out.AssetAmount.AssetId.GetV1(),
-                               V2: out.AssetAmount.AssetId.GetV2(),
-                               V3: out.AssetAmount.AssetId.GetV3(),
-                       }
-
-                       sourceID := bc.Hash{
-                               V0: outPut.Source.Ref.GetV0(),
-                               V1: outPut.Source.Ref.GetV1(),
-                               V2: outPut.Source.Ref.GetV2(),
-                               V3: outPut.Source.Ref.GetV3(),
-                       }
-                       utxo = account.UTXO{
-                               OutputID:       outputID,
-                               AssetID:        assetID,
-                               Amount:         out.Amount,
-                               ControlProgram: out.ControlProgram,
-                               SourceID:       sourceID,
-                               SourcePos:      outPut.Source.Position,
-                               ValidHeight:    block.Height,
-                               Address:        ins.Address,
-                       }
-               }
-       }
-
-       return NewSuccessResponse(&utxoResp{Utxo: utxo})
-}