github.com/fiagdao/tendermint@v0.32.11-0.20220824195748-2087fcc480c1/rpc/core/tx.go (about)

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
     9  	ctypes "github.com/tendermint/tendermint/rpc/core/types"
    10  	rpctypes "github.com/tendermint/tendermint/rpc/jsonrpc/types"
    11  	"github.com/tendermint/tendermint/state/txindex/null"
    12  	"github.com/tendermint/tendermint/types"
    13  )
    14  
    15  // Tx allows you to query the transaction results. `nil` could mean the
    16  // transaction is in the mempool, invalidated, or was not sent in the first
    17  // place.
    18  // More: https://docs.tendermint.com/master/rpc/#/Info/tx
    19  func Tx(ctx *rpctypes.Context, hash []byte, prove bool) (*ctypes.ResultTx, error) {
    20  	// if index is disabled, return error
    21  	if _, ok := env.TxIndexer.(*null.TxIndex); ok {
    22  		return nil, fmt.Errorf("transaction indexing is disabled")
    23  	}
    24  
    25  	r, err := env.TxIndexer.Get(hash)
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  
    30  	if r == nil {
    31  		return nil, fmt.Errorf("tx (%X) not found", hash)
    32  	}
    33  
    34  	height := r.Height
    35  	index := r.Index
    36  
    37  	var proof types.TxProof
    38  	if prove {
    39  		block := env.BlockStore.LoadBlock(height)
    40  		proof = block.Data.Txs.Proof(int(index)) // XXX: overflow on 32-bit machines
    41  	}
    42  
    43  	return &ctypes.ResultTx{
    44  		Hash:     hash,
    45  		Height:   height,
    46  		Index:    index,
    47  		TxResult: r.Result,
    48  		Tx:       r.Tx,
    49  		Proof:    proof,
    50  	}, nil
    51  }
    52  
    53  // TxSearch allows you to query for multiple transactions results. It returns a
    54  // list of transactions (maximum ?per_page entries) and the total count.
    55  // More: https://docs.tendermint.com/master/rpc/#/Info/tx_search
    56  func TxSearch(ctx *rpctypes.Context, query string, prove bool, page, perPage int, orderBy string) (
    57  	*ctypes.ResultTxSearch, error) {
    58  	// if index is disabled, return error
    59  	if _, ok := env.TxIndexer.(*null.TxIndex); ok {
    60  		return nil, errors.New("transaction indexing is disabled")
    61  	}
    62  
    63  	q, err := tmquery.New(query)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	q.AddPage(perPage, validateSkipCount(page, perPage), orderBy)
    68  
    69  	results, total, err := env.TxIndexer.Search(ctx.Context(), q)
    70  	if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	apiResults := make([]*ctypes.ResultTx, 0)
    75  	for _, r := range results {
    76  		var proof types.TxProof
    77  		if prove {
    78  			block := env.BlockStore.LoadBlock(r.Height)
    79  			proof = block.Data.Txs.Proof(int(r.Index)) // XXX: overflow on 32-bit machines
    80  		}
    81  
    82  		apiResults = append(apiResults, &ctypes.ResultTx{
    83  			Hash:     r.Tx.Hash(),
    84  			Height:   r.Height,
    85  			Index:    r.Index,
    86  			TxResult: r.Result,
    87  			Tx:       r.Tx,
    88  			Proof:    proof,
    89  		})
    90  	}
    91  
    92  	return &ctypes.ResultTxSearch{Txs: apiResults, TotalCount: total}, nil
    93  }
    94