Skip to content

Commit 36ec9a3

Browse files
committed
Kernel: Add functions for working with outpoints
This introduces the transaction outpoint, input and id types. This now allows a user to retrieve a transaction output from a prior transaction that a transaction outpoint is pointing to by either scanning through all available transactions, or maintaining a data structure for lookups. This is exercised in the tests by verifying the script of every transaction in the test chain.
1 parent 5eec7fa commit 36ec9a3

File tree

4 files changed

+417
-1
lines changed

4 files changed

+417
-1
lines changed

src/kernel/bitcoinkernel.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,9 @@ struct btck_BlockSpentOutputs : Handle<btck_BlockSpentOutputs, std::shared_ptr<C
491491
struct btck_TransactionSpentOutputs : Handle<btck_TransactionSpentOutputs, CTxUndo> {};
492492
struct btck_Coin : Handle<btck_Coin, Coin> {};
493493
struct btck_BlockHash : Handle<btck_BlockHash, uint256> {};
494+
struct btck_TransactionInput : Handle<btck_TransactionInput, CTxIn> {};
495+
struct btck_TransactionOutPoint: Handle<btck_TransactionOutPoint, COutPoint> {};
496+
struct btck_Txid: Handle<btck_Txid, Txid> {};
494497

495498
btck_Transaction* btck_transaction_create(const void* raw_transaction, size_t raw_transaction_len)
496499
{
@@ -519,6 +522,17 @@ size_t btck_transaction_count_inputs(const btck_Transaction* transaction)
519522
return btck_Transaction::get(transaction)->vin.size();
520523
}
521524

525+
const btck_TransactionInput* btck_transaction_get_input_at(const btck_Transaction* transaction, size_t input_index)
526+
{
527+
assert(input_index < btck_Transaction::get(transaction)->vin.size());
528+
return btck_TransactionInput::ref(&btck_Transaction::get(transaction)->vin[input_index]);
529+
}
530+
531+
const btck_Txid* btck_transaction_get_txid(const btck_Transaction* transaction)
532+
{
533+
return btck_Txid::ref(&btck_Transaction::get(transaction)->GetHash());
534+
}
535+
522536
btck_Transaction* btck_transaction_copy(const btck_Transaction* transaction)
523537
{
524538
return btck_Transaction::copy(transaction);
@@ -637,6 +651,60 @@ int btck_script_pubkey_verify(const btck_ScriptPubkey* script_pubkey,
637651
return result ? 1 : 0;
638652
}
639653

654+
btck_TransactionInput* btck_transaction_input_copy(const btck_TransactionInput* input)
655+
{
656+
return btck_TransactionInput::copy(input);
657+
}
658+
659+
const btck_TransactionOutPoint* btck_transaction_input_get_out_point(const btck_TransactionInput* input)
660+
{
661+
return btck_TransactionOutPoint::ref(&btck_TransactionInput::get(input).prevout);
662+
}
663+
664+
void btck_transaction_input_destroy(btck_TransactionInput* input)
665+
{
666+
delete input;
667+
}
668+
669+
btck_TransactionOutPoint* btck_transaction_out_point_copy(const btck_TransactionOutPoint* out_point)
670+
{
671+
return btck_TransactionOutPoint::copy(out_point);
672+
}
673+
674+
uint32_t btck_transaction_out_point_get_index(const btck_TransactionOutPoint* out_point)
675+
{
676+
return btck_TransactionOutPoint::get(out_point).n;
677+
}
678+
679+
const btck_Txid* btck_transaction_out_point_get_txid(const btck_TransactionOutPoint* out_point)
680+
{
681+
return btck_Txid::ref(&btck_TransactionOutPoint::get(out_point).hash);
682+
}
683+
684+
void btck_transaction_out_point_destroy(btck_TransactionOutPoint* out_point)
685+
{
686+
delete out_point;
687+
}
688+
689+
btck_Txid* btck_txid_copy(const btck_Txid* txid)
690+
{
691+
return btck_Txid::copy(txid);
692+
}
693+
694+
void btck_txid_to_bytes(const btck_Txid* txid, unsigned char output[32])
695+
{
696+
std::memcpy(output, btck_Txid::get(txid).begin(), 32);
697+
}
698+
699+
int btck_txid_equals(const btck_Txid* txid1, const btck_Txid* txid2)
700+
{
701+
return btck_Txid::get(txid1) == btck_Txid::get(txid2);
702+
}
703+
704+
void btck_txid_destroy(btck_Txid* txid)
705+
{
706+
delete txid;
707+
}
640708

641709
void btck_logging_set_options(const btck_LoggingOptions options)
642710
{

src/kernel/bitcoinkernel.h

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,22 @@ typedef struct btck_Coin btck_Coin;
249249
*/
250250
typedef struct btck_BlockHash btck_BlockHash;
251251

252+
/**
253+
* Opaque data structure for holding a transaction input.
254+
*
255+
* Holds information on the @ref btck_TransactionOutPoint held within.
256+
*/
257+
typedef struct btck_TransactionInput btck_TransactionInput;
258+
259+
/**
260+
* Opaque data structure for holding a transaction out point.
261+
*
262+
* Holds the txid and output index it is pointing to.
263+
*/
264+
typedef struct btck_TransactionOutPoint btck_TransactionOutPoint;
265+
266+
typedef struct btck_Txid btck_Txid;
267+
252268
/** Current sync state passed to tip changed callbacks. */
253269
typedef uint8_t btck_SynchronizationState;
254270
#define btck_SynchronizationState_INIT_REINDEX ((btck_SynchronizationState)(0))
@@ -498,6 +514,18 @@ BITCOINKERNEL_API size_t BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_count
498514
BITCOINKERNEL_API const btck_TransactionOutput* BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_get_output_at(
499515
const btck_Transaction* transaction, size_t output_index) BITCOINKERNEL_ARG_NONNULL(1);
500516

517+
/**
518+
* @brief Get the transaction input at the provided index. The returned
519+
* transaction input is not owned and depends on the lifetime of the
520+
* transaction.
521+
*
522+
* @param[in] transaction Non-null.
523+
* @param[in] input_index The index of the transaction input to be retrieved.
524+
* @return The transaction input
525+
*/
526+
BITCOINKERNEL_API const btck_TransactionInput* BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_get_input_at(
527+
const btck_Transaction* transaction, size_t input_index) BITCOINKERNEL_ARG_NONNULL(1);
528+
501529
/**
502530
* @brief Get the number of inputs of a transaction.
503531
*
@@ -507,6 +535,16 @@ BITCOINKERNEL_API const btck_TransactionOutput* BITCOINKERNEL_WARN_UNUSED_RESULT
507535
BITCOINKERNEL_API size_t BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_count_inputs(
508536
const btck_Transaction* transaction) BITCOINKERNEL_ARG_NONNULL(1);
509537

538+
/**
539+
* @brief Get the txid of a transaction. The returned txid is not owned and
540+
* depends on the lifetime of the transaction.
541+
*
542+
* @param[in] transaction Non-null.
543+
* @return The txid.
544+
*/
545+
BITCOINKERNEL_API const btck_Txid* BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_get_txid(
546+
const btck_Transaction* transaction) BITCOINKERNEL_ARG_NONNULL(1);
547+
510548
/**
511549
* Destroy the transaction.
512550
*/
@@ -1313,6 +1351,119 @@ BITCOINKERNEL_API void btck_transaction_spent_outputs_destroy(btck_TransactionSp
13131351

13141352
///@}
13151353

1354+
/** @name Transaction Input
1355+
* Functions for working with transaction inputs.
1356+
*/
1357+
///@{
1358+
1359+
/**
1360+
* @brief Copy a transaction input.
1361+
*
1362+
* @param[in] transaction_input Non-null.
1363+
* @return The copied transaction input.
1364+
*/
1365+
BITCOINKERNEL_API btck_TransactionInput* BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_input_copy(
1366+
const btck_TransactionInput* transaction_input) BITCOINKERNEL_ARG_NONNULL(1);
1367+
1368+
/**
1369+
* @brief Get the transaction out point. The returned transaction out point is
1370+
* not owned and depends on the lifetime of the transaction.
1371+
*
1372+
* @param[in] transaction_input Non-null.
1373+
* @return The transaction out point.
1374+
*/
1375+
BITCOINKERNEL_API const btck_TransactionOutPoint* BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_input_get_out_point(
1376+
const btck_TransactionInput* transaction_input) BITCOINKERNEL_ARG_NONNULL(1);
1377+
1378+
/**
1379+
* Destroy the transaction input.
1380+
*/
1381+
BITCOINKERNEL_API void btck_transaction_input_destroy(btck_TransactionInput* transaction_input);
1382+
1383+
///@}
1384+
1385+
/** @name Transaction Out Point
1386+
* Functions for working with transaction out points.
1387+
*/
1388+
///@{
1389+
1390+
/**
1391+
* @brief Copy a transaction out point.
1392+
*
1393+
* @param[in] transaction_out_point Non-null.
1394+
* @return The copied transaction out point.
1395+
*/
1396+
BITCOINKERNEL_API btck_TransactionOutPoint* BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_out_point_copy(
1397+
const btck_TransactionOutPoint* transaction_out_point) BITCOINKERNEL_ARG_NONNULL(1);
1398+
1399+
/**
1400+
* @brief Get the output position from the transaction out point.
1401+
*
1402+
* @param[in] transaction_out_point Non-null.
1403+
* @return The output index.
1404+
*/
1405+
BITCOINKERNEL_API uint32_t BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_out_point_get_index(
1406+
const btck_TransactionOutPoint* transaction_out_point) BITCOINKERNEL_ARG_NONNULL(1);
1407+
1408+
/**
1409+
* @brief Get the txid from the transaction out point. The returned txid is
1410+
* not owned and depends on the lifetime of the transaction out point.
1411+
*
1412+
* @param[in] transaction_out_point Non-null.
1413+
* @return The txid.
1414+
*/
1415+
BITCOINKERNEL_API const btck_Txid* BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_out_point_get_txid(
1416+
const btck_TransactionOutPoint* transaction_out_point) BITCOINKERNEL_ARG_NONNULL(1);
1417+
1418+
/**
1419+
* Destroy the transaction out point.
1420+
*/
1421+
BITCOINKERNEL_API void btck_transaction_out_point_destroy(btck_TransactionOutPoint* transaction_out_point);
1422+
1423+
///@}
1424+
1425+
/** @name Txid
1426+
* Functions for working with txids.
1427+
*/
1428+
///@{
1429+
1430+
/**
1431+
* @brief Copy a txid.
1432+
*
1433+
* @param[in] txid Non-null.
1434+
* @return The copied txid.
1435+
*/
1436+
BITCOINKERNEL_API btck_Txid* BITCOINKERNEL_WARN_UNUSED_RESULT btck_txid_copy(
1437+
const btck_Txid* txid) BITCOINKERNEL_ARG_NONNULL(1);
1438+
1439+
/**
1440+
* @brief Check if two txids are equal.
1441+
*
1442+
* @param[in] txid1 Non-null.
1443+
* @param[in] txid2 Non-null.
1444+
* @return 0 if the txid is not equal.
1445+
*/
1446+
BITCOINKERNEL_API int BITCOINKERNEL_WARN_UNUSED_RESULT btck_txid_equals(
1447+
const btck_Txid* txid1, const btck_Txid* txid2) BITCOINKERNEL_ARG_NONNULL(1, 2);
1448+
1449+
/**
1450+
* @brief Serializes the txid to bytes.
1451+
*
1452+
* @param[in] txid Non-null.
1453+
* @param[out] output The serialized txid.
1454+
*/
1455+
BITCOINKERNEL_API void btck_txid_to_bytes(
1456+
const btck_Txid* txid, unsigned char output[32]) BITCOINKERNEL_ARG_NONNULL(1, 2);
1457+
1458+
/**
1459+
* Destroy the txid.
1460+
*/
1461+
BITCOINKERNEL_API void btck_txid_destroy(btck_Txid* txid);
1462+
1463+
///@}
1464+
1465+
///@}
1466+
13161467
/** @name Coin
13171468
* Functions for working with coins.
13181469
*/
@@ -1328,7 +1479,8 @@ BITCOINKERNEL_API btck_Coin* BITCOINKERNEL_WARN_UNUSED_RESULT btck_coin_copy(
13281479
const btck_Coin* coin) BITCOINKERNEL_ARG_NONNULL(1);
13291480

13301481
/**
1331-
* @brief Returns the height of the block that contains the coin's prevout.
1482+
* @brief Returns the block height where the transaction that
1483+
* created this coin was included in.
13321484
*
13331485
* @param[in] coin Non-null.
13341486
* @return The block height of the coin.

0 commit comments

Comments
 (0)