From 9597df993caa3d04d5d21dee2a0c68ce32d43262 Mon Sep 17 00:00:00 2001 From: Yongfeng LI Date: Thu, 22 Mar 2018 18:03:45 +0800 Subject: [PATCH] refactor list-unspent-outputs (#465) --- blockchain/query.go | 48 +++++++++++++--------------------------- blockchain/query/annotated.go | 15 +++++++++++++ blockchain/query/filter/doc.go | 44 ------------------------------------ blockchain/query/transactions.go | 38 ------------------------------- 4 files changed, 30 insertions(+), 115 deletions(-) delete mode 100644 blockchain/query/filter/doc.go diff --git a/blockchain/query.go b/blockchain/query.go index 69287b1a..29794d64 100755 --- a/blockchain/query.go +++ b/blockchain/query.go @@ -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) diff --git a/blockchain/query/annotated.go b/blockchain/query/annotated.go index f97a2178..b3ec8a8d 100755 --- a/blockchain/query/annotated.go +++ b/blockchain/query/annotated.go @@ -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 index c1bed44e..00000000 --- a/blockchain/query/filter/doc.go +++ /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 diff --git a/blockchain/query/transactions.go b/blockchain/query/transactions.go index 28b30f49..00f39bbb 100644 --- a/blockchain/query/transactions.go +++ b/blockchain/query/transactions.go @@ -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) -- 2.11.0