OSDN Git Service

refactor list-unspent-outputs (#465)
authorYongfeng LI <wliyongfeng@gmail.com>
Thu, 22 Mar 2018 10:03:45 +0000 (18:03 +0800)
committerPaladz <yzhu101@uottawa.ca>
Thu, 22 Mar 2018 10:03:45 +0000 (18:03 +0800)
blockchain/query.go
blockchain/query/annotated.go
blockchain/query/filter/doc.go [deleted file]
blockchain/query/transactions.go

index 69287b1..29794d6 100755 (executable)
@@ -96,50 +96,32 @@ func (bcr *BlockchainReactor) listTransactions(ctx context.Context, filter struc
        return NewSuccessResponse(transactions)
 }
 
-type annotatedUTXO struct {
-       Alias               string `json:"account_alias"`
-       OutputID            string `json:"id"`
-       AssetID             string `json:"asset_id"`
-       AssetAlias          string `json:"asset_alias"`
-       Amount              uint64 `json:"amount"`
-       AccountID           string `json:"account_id"`
-       Address             string `json:"address"`
-       ControlProgramIndex uint64 `json:"control_program_index"`
-       Program             string `json:"program"`
-       SourceID            string `json:"source_id"`
-       SourcePos           uint64 `json:"source_pos"`
-       ValidHeight         uint64 `json:"valid_height"`
-}
-
 // POST /list-unspent-outputs
 func (bcr *BlockchainReactor) listUnspentOutputs(ctx context.Context, filter struct {
        ID string `json:"id"`
 }) Response {
-       tmpUTXO := annotatedUTXO{}
-       UTXOs := make([]annotatedUTXO, 0)
-
        accountUTXOs, err := bcr.wallet.GetAccountUTXOs(filter.ID)
        if err != nil {
                log.Errorf("list Unspent Outputs: %v", err)
                return NewErrorResponse(err)
        }
 
+       var UTXOs []query.AnnotatedUTXO
        for _, utxo := range accountUTXOs {
-               tmpUTXO.AccountID = utxo.AccountID
-               tmpUTXO.OutputID = utxo.OutputID.String()
-               tmpUTXO.SourceID = utxo.SourceID.String()
-               tmpUTXO.AssetID = utxo.AssetID.String()
-               tmpUTXO.Amount = utxo.Amount
-               tmpUTXO.SourcePos = utxo.SourcePos
-               tmpUTXO.Program = fmt.Sprintf("%x", utxo.ControlProgram)
-               tmpUTXO.ControlProgramIndex = utxo.ControlProgramIndex
-               tmpUTXO.Address = utxo.Address
-               tmpUTXO.ValidHeight = utxo.ValidHeight
-
-               tmpUTXO.Alias = bcr.wallet.AccountMgr.GetAliasByID(utxo.AccountID)
-               tmpUTXO.AssetAlias = bcr.wallet.AssetReg.GetAliasByID(tmpUTXO.AssetID)
-
-               UTXOs = append(UTXOs, tmpUTXO)
+               UTXOs = append(UTXOs, query.AnnotatedUTXO{
+                       AccountID:           utxo.AccountID,
+                       OutputID:            utxo.OutputID.String(),
+                       SourceID:            utxo.SourceID.String(),
+                       AssetID:             utxo.AssetID.String(),
+                       Amount:              utxo.Amount,
+                       SourcePos:           utxo.SourcePos,
+                       Program:             fmt.Sprintf("%x", utxo.ControlProgram),
+                       ControlProgramIndex: utxo.ControlProgramIndex,
+                       Address:             utxo.Address,
+                       ValidHeight:         utxo.ValidHeight,
+                       Alias:               bcr.wallet.AccountMgr.GetAliasByID(utxo.AccountID),
+                       AssetAlias:          bcr.wallet.AssetReg.GetAliasByID(utxo.AssetID.String()),
+               })
        }
 
        return NewSuccessResponse(UTXOs)
index f97a217..b3ec8a8 100755 (executable)
@@ -78,3 +78,18 @@ type AssetKey struct {
        AssetPubkey         chainjson.HexBytes   `json:"asset_pubkey"`
        AssetDerivationPath []chainjson.HexBytes `json:"asset_derivation_path"`
 }
+
+type AnnotatedUTXO struct {
+       Alias               string `json:"account_alias"`
+       OutputID            string `json:"id"`
+       AssetID             string `json:"asset_id"`
+       AssetAlias          string `json:"asset_alias"`
+       Amount              uint64 `json:"amount"`
+       AccountID           string `json:"account_id"`
+       Address             string `json:"address"`
+       ControlProgramIndex uint64 `json:"control_program_index"`
+       Program             string `json:"program"`
+       SourceID            string `json:"source_id"`
+       SourcePos           uint64 `json:"source_pos"`
+       ValidHeight         uint64 `json:"valid_height"`
+}
diff --git a/blockchain/query/filter/doc.go b/blockchain/query/filter/doc.go
deleted file mode 100644 (file)
index c1bed44..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
-Package filter parses and evaluates Chain filter expressions. A
-predicate is a boolean expression with zero or more placeholder
-values ($1, $2, etc) that are initially unconstrained. The
-predicate is evaluated in an environment (such as a transaction
-object or a UTXO) that determines the value of all non-placeholder
-terms. The predicate and its fixed values together constrain the
-placeholders.
-
-Expressions in a filter expression have the following forms:
-
-  Form                     Type     Subexpression types
-  expr1 "OR" expr2         bool     bool, bool
-  expr1 "AND" expr2        bool     bool, bool
-  ident "(" expr ")"       bool     list, bool
-  expr1 "=" expr2          bool     any (must match)
-  expr "." ident           any      object
-  "(" expr ")"             any      any
-  ident                    any      n/a
-  placeholder              scalar   n/a
-  string                   string   n/a
-  int                      int      n/a
-
-  ident is an alphanumeric identifier
-  placeholder is a decimal int with prefix "$"
-  scalar means int or string
-  string is single-quoted, and cannot contain backslash
-  int is decimal or hexadecimal (with prefix "0x")
-  list is a slice of environments
-
-The environment is a map from names to values. Identifier
-expressions get their values from the environment map.
-
-The form 'ident(expr)' is an existential quantifier. The environment
-value for 'ident' must be a list of subenvironments. The
-subexpression 'expr' is evaluated in each subenvironment, and if
-there exists one subenvironment for which 'expr' is true, the
-expression as a whole is true.
-
-Filters are statically type-checked: if a subexpression doesn't have
-the appropriate type, Parse will return an error.
-
-*/
-package filter
index 28b30f4..00f39bb 100644 (file)
@@ -1,9 +1,6 @@
 package query
 
 import (
-       "fmt"
-       "math"
-
        "github.com/bytom/blockchain/query/filter"
        "github.com/bytom/errors"
 )
@@ -26,41 +23,6 @@ var (
        ErrParameterCountMismatch = errors.New("wrong number of parameters to query")
 )
 
-//TxAfter means the last query block by a list-transactions query.
-type TxAfter struct {
-       // FromBlockHeight and FromPosition uniquely identify the last transaction returned
-       // by a list-transactions query.
-       //
-       // If list-transactions is called with a time range instead of an `after`, these fields
-       // are populated with the position of the transaction at the start of the time range.
-       FromBlockHeight uint64 // exclusive
-       FromPosition    uint32 // exclusive
-
-       // StopBlockHeight identifies the last block that should be included in a transaction
-       // list. It is used when list-transactions is called with a time range instead
-       // of an `after`.
-       StopBlockHeight uint64 // inclusive
-}
-
-func (after TxAfter) String() string {
-       return fmt.Sprintf("%d:%d-%d", after.FromBlockHeight, after.FromPosition, after.StopBlockHeight)
-}
-
-//DecodeTxAfter decode tx from the last block.
-func DecodeTxAfter(str string) (c TxAfter, err error) {
-       var from, pos, stop uint64
-       _, err = fmt.Sscanf(str, "%d:%d-%d", &from, &pos, &stop)
-       if err != nil {
-               return c, errors.Sub(ErrBadAfter, err)
-       }
-       if from > math.MaxInt64 ||
-               pos > math.MaxUint32 ||
-               stop > math.MaxInt64 {
-               return c, errors.Wrap(ErrBadAfter)
-       }
-       return TxAfter{FromBlockHeight: from, FromPosition: uint32(pos), StopBlockHeight: stop}, nil
-}
-
 //ValidateTransactionFilter verify txfeed filter validity.
 func ValidateTransactionFilter(filt string) error {
        _, err := filter.Parse(filt, &filterTable, nil)