gitlab.com/jokerrs1/Sia@v1.3.2/node/api/transactionpool.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/base64"
     5  	"net/http"
     6  
     7  	"github.com/julienschmidt/httprouter"
     8  
     9  	"github.com/NebulousLabs/Sia/crypto"
    10  	"github.com/NebulousLabs/Sia/encoding"
    11  	"github.com/NebulousLabs/Sia/modules"
    12  	"github.com/NebulousLabs/Sia/types"
    13  )
    14  
    15  type (
    16  	// TpoolFeeGET contains the current estimated fee
    17  	TpoolFeeGET struct {
    18  		Minimum types.Currency `json:"minimum"`
    19  		Maximum types.Currency `json:"maximum"`
    20  	}
    21  
    22  	// TpoolRawGET contains the requested transaction encoded to the raw
    23  	// format, along with the id of that transaction.
    24  	TpoolRawGET struct {
    25  		ID          types.TransactionID `json:"id"`
    26  		Parents     []byte              `json:"parents"`
    27  		Transaction []byte              `json:"transaction"`
    28  	}
    29  
    30  	// TpoolConfirmedGET contains information about whether or not
    31  	// the transaction has been seen on the blockhain
    32  	TpoolConfirmedGET struct {
    33  		Confirmed bool `json:"confirmed"`
    34  	}
    35  )
    36  
    37  // decodeTransactionID will decode a transaction id from a string.
    38  func decodeTransactionID(txidStr string) (types.TransactionID, error) {
    39  	txid := new(crypto.Hash)
    40  	err := txid.LoadString(txidStr)
    41  	if err != nil {
    42  		return types.TransactionID{}, err
    43  	}
    44  	return types.TransactionID(*txid), nil
    45  }
    46  
    47  // tpoolFeeHandlerGET returns the current estimated fee. Transactions with
    48  // fees are lower than the estimated fee may take longer to confirm.
    49  func (api *API) tpoolFeeHandlerGET(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
    50  	min, max := api.tpool.FeeEstimation()
    51  	WriteJSON(w, TpoolFeeGET{
    52  		Minimum: min,
    53  		Maximum: max,
    54  	})
    55  }
    56  
    57  // tpoolRawHandlerGET will provide the raw byte representation of a
    58  // transaction that matches the input id.
    59  func (api *API) tpoolRawHandlerGET(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
    60  	txid, err := decodeTransactionID(ps.ByName("id"))
    61  	if err != nil {
    62  		WriteError(w, Error{"error decoding transaction id:" + err.Error()}, http.StatusBadRequest)
    63  		return
    64  	}
    65  	txn, parents, exists := api.tpool.Transaction(txid)
    66  	if !exists {
    67  		WriteError(w, Error{"transaction not found in transaction pool"}, http.StatusBadRequest)
    68  		return
    69  	}
    70  
    71  	WriteJSON(w, TpoolRawGET{
    72  		ID:          txid,
    73  		Parents:     encoding.Marshal(parents),
    74  		Transaction: encoding.Marshal(txn),
    75  	})
    76  }
    77  
    78  // tpoolRawHandlerPOST takes a raw encoded transaction set and posts
    79  // it to the transaction pool, relaying it to the transaction pool's peers
    80  // regardless of if the set is accepted.
    81  func (api *API) tpoolRawHandlerPOST(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    82  	// Try accepting the transactions both as base64 and as clean values.
    83  	rawParents, err := base64.StdEncoding.DecodeString(req.FormValue("parents"))
    84  	if err != nil {
    85  		rawParents = []byte(req.FormValue("parents"))
    86  	}
    87  	rawTransaction, err := base64.StdEncoding.DecodeString(req.FormValue("transaction"))
    88  	if err != nil {
    89  		rawTransaction = []byte(req.FormValue("transaction"))
    90  	}
    91  
    92  	// Decode the transaction and parents into a transaction set that can be
    93  	// given to the transaction pool.
    94  	var parents []types.Transaction
    95  	var txn types.Transaction
    96  	err = encoding.Unmarshal(rawParents, &parents)
    97  	if err != nil {
    98  		WriteError(w, Error{"error decoding parents:" + err.Error()}, http.StatusBadRequest)
    99  		return
   100  	}
   101  	err = encoding.Unmarshal(rawTransaction, &txn)
   102  	if err != nil {
   103  		WriteError(w, Error{"error decoding transaction:" + err.Error()}, http.StatusBadRequest)
   104  		return
   105  	}
   106  	txnSet := append(parents, txn)
   107  
   108  	// Re-broadcast the transactions, so that they are passed to any peers that
   109  	// may have rejected them earlier.
   110  	api.tpool.Broadcast(txnSet)
   111  	err = api.tpool.AcceptTransactionSet(txnSet)
   112  	if err != nil && err != modules.ErrDuplicateTransactionSet {
   113  		WriteError(w, Error{"error accepting transaction set:" + err.Error()}, http.StatusBadRequest)
   114  		return
   115  	}
   116  	WriteSuccess(w)
   117  }
   118  
   119  // tpoolConfirmedGET returns whether the specified transaction has
   120  // been seen on the blockchain.
   121  func (api *API) tpoolConfirmedGET(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
   122  	txid, err := decodeTransactionID(ps.ByName("id"))
   123  	if err != nil {
   124  		WriteError(w, Error{"error decoding transaction id:" + err.Error()}, http.StatusBadRequest)
   125  		return
   126  	}
   127  	WriteJSON(w, TpoolConfirmedGET{
   128  		Confirmed: api.tpool.TransactionConfirmed(txid),
   129  	})
   130  }