Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ln/txn ids only #62

Merged
merged 3 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 41 additions & 26 deletions routes/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,23 +423,23 @@ type TransactionResponse struct {
TransactionIDBase58Check string
// The raw hex of the transaction data. This can be fully-constructed from
// the human-readable portions of this object.
RawTransactionHex string
RawTransactionHex string `json:",omitempty"`
// The inputs and outputs for this transaction.
Inputs []*InputResponse
Outputs []*OutputResponse
Inputs []*InputResponse `json:",omitempty"`
Outputs []*OutputResponse `json:",omitempty"`
// The signature of the transaction in hex format.
SignatureHex string
SignatureHex string `json:",omitempty"`
// Will always be “0” for basic transfers
TransactionType string
TransactionType string `json:",omitempty"`
// TODO: Create a TransactionMeta portion for the response.

// The hash of the block in which this transaction was mined. If the
// transaction is unconfirmed, this field will be empty. To look up
// how many confirmations a transaction has, simply plug this value
// into the "block" endpoint.
BlockHashHex string
BlockHashHex string `json:",omitempty"`

TransactionMetadata *lib.TransactionMetadata
TransactionMetadata *lib.TransactionMetadata `json:",omitempty"`
}

// TransactionInfoResponse contains information about the transaction
Expand Down Expand Up @@ -720,6 +720,8 @@ type APITransactionInfoRequest struct {
// with “BC”) to get transaction IDs for. When set,
// TransactionIDBase58Check is ignored.
PublicKeyBase58Check string

IDsOnly bool
}

// APITransactionInfoResponse specifies the response for a call to the
Expand Down Expand Up @@ -799,16 +801,21 @@ func (fes *APIServer) APITransactionInfo(ww http.ResponseWriter, rr *http.Reques
res := &APITransactionInfoResponse{}
res.Transactions = []*TransactionResponse{}
for _, poolTx := range poolTxns {
txnMeta, err := lib.ConnectTxnAndComputeTransactionMetadata(
poolTx.Tx, utxoView, &lib.BlockHash{} /*Block hash*/, nextBlockHeight,
uint64(0) /*txnIndexInBlock*/)
if err != nil {
APIAddError(ww, fmt.Sprintf("Update: Error connecting "+
"txn for mempool request: %v: %v", poolTx.Tx, err))
return
if transactionInfoRequest.IDsOnly {
res.Transactions = append(res.Transactions,
&TransactionResponse{ TransactionIDBase58Check: lib.PkToString(poolTx.Tx.Hash()[:], fes.Params) })
} else {
txnMeta, err := lib.ConnectTxnAndComputeTransactionMetadata(
poolTx.Tx, utxoView, &lib.BlockHash{} /*Block hash*/, nextBlockHeight,
uint64(0) /*txnIndexInBlock*/)
if err != nil {
APIAddError(ww, fmt.Sprintf("Update: Error connecting "+
"txn for mempool request: %v: %v", poolTx.Tx, err))
return
}
res.Transactions = append(res.Transactions,
APITransactionToResponse(poolTx.Tx, txnMeta, fes.Params))
}
res.Transactions = append(res.Transactions,
APITransactionToResponse(poolTx.Tx, txnMeta, fes.Params))
}

// At this point, all the transactions should have been added to the request.
Expand Down Expand Up @@ -945,17 +952,21 @@ func (fes *APIServer) APITransactionInfo(ww http.ResponseWriter, rr *http.Reques
// Process all the transactions found and add them to the response.
for _, txHash := range txHashes {
txIDString := lib.PkToString(txHash[:], fes.Params)
// In this case we need to look up the full transaction and convert
// it into a proper transaction response.
fullTxn, txnMeta := lib.DbGetTxindexFullTransactionByTxID(
fes.TXIndex.TXIndexChain.DB(), fes.blockchain.DB(), txHash)
if fullTxn == nil || txnMeta == nil {
APIAddError(ww, fmt.Sprintf("APITransactionInfo: Problem looking up "+
"transaction with TxID: %v; this should never happen", txIDString))
return
if transactionInfoRequest.IDsOnly {
res.Transactions = append(res.Transactions, &TransactionResponse{ TransactionIDBase58Check: txIDString })
} else {
// In this case we need to look up the full transaction and convert
// it into a proper transaction response.
fullTxn, txnMeta := lib.DbGetTxindexFullTransactionByTxID(
fes.TXIndex.TXIndexChain.DB(), fes.blockchain.DB(), txHash)
if fullTxn == nil || txnMeta == nil {
APIAddError(ww, fmt.Sprintf("APITransactionInfo: Problem looking up "+
"transaction with TxID: %v; this should never happen", txIDString))
return
}
res.Transactions = append(res.Transactions,
APITransactionToResponse(fullTxn, txnMeta, fes.Params))
}
res.Transactions = append(res.Transactions,
APITransactionToResponse(fullTxn, txnMeta, fes.Params))
}

// Get all the txns from the mempool.
Expand Down Expand Up @@ -1004,6 +1015,10 @@ func (fes *APIServer) APITransactionInfo(ww http.ResponseWriter, rr *http.Reques
}
// Finally, add the transaction to our list if it's relevant
if isRelevantTxn {
if transactionInfoRequest.IDsOnly {
res.Transactions = append(res.Transactions,
&TransactionResponse{ TransactionIDBase58Check: lib.PkToString(poolTx.Tx.Hash()[:], fes.Params) })
}
res.Transactions = append(res.Transactions,
APITransactionToResponse(poolTx.Tx, txnMeta, fes.Params))
}
Expand Down
25 changes: 25 additions & 0 deletions routes/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,31 @@ func TestAPI(t *testing.T) {
assert.Equal(0, len(transactionInfoRes.Transactions[0].Inputs))
assert.Equal(1, len(transactionInfoRes.Transactions[0].Outputs))
}
{
// Test IDs only
transactionInfoRequest := &APITransactionInfoRequest{
PublicKeyBase58Check: senderPkString,
IDsOnly: true,
}
jsonRequest, err := json.Marshal(transactionInfoRequest)
require.NoError(err)
request, _ := http.NewRequest(
"POST", RoutePathAPITransactionInfo, bytes.NewBuffer(jsonRequest))
request.Header.Set("Content-Type", "application/json")
response := httptest.NewRecorder()
apiServer.router.ServeHTTP(response, request)
assert.Equal(200, response.Code, "200 response expected")

decoder := json.NewDecoder(io.LimitReader(response.Body, MaxRequestBodySizeBytes))
transactionInfoRes := APITransactionInfoResponse{}
if err := decoder.Decode(&transactionInfoRes); err != nil {
require.NoError(err, "Problem decoding response")
}
assert.Equal("", transactionInfoRes.Error)
assert.Equal(1, len(transactionInfoRes.Transactions))
assert.Equal(lib.PkToString(firstBlockTxn.Hash()[:], apiServer.Params),
transactionInfoRes.Transactions[0].TransactionIDBase58Check)
}

// Roll back the change we made to the chain.
apiServer.blockchain.SetBestChain(oldBestChain)
Expand Down