cbitcoin
 All Data Structures Files Functions Variables Typedefs
src/CBValidationFunctions.h File Reference

Here are functions for doing parts of bitcoin validation. cbitcoin is designed to make a broad variety of bitcoin development easier and is not designed to implement a client node. Therefore, cbitcoin does not provide provide functions for validating block chains as there are different methods for doing so. So wether you want to use SPV, block-chain pruning, full validation or whatever, you can use these functions to help. Also see CBScript.h and CBMerkleNode.h for validation. CBNetworkCommunicator.h has message size checks and message deserialisation functions check messages for syntax and sizes. More...

#include <stdint.h>
#include "CBConstants.h"
#include "CBBlock.h"

Go to the source code of this file.

Functions

uint64_t CBCalculateBlockReward (uint64_t blockHeight)
 Calculates the block reward at a particular block height.
CBBigInt CBCalculateBlockWork (uint32_t target)
 Calculates the block work which is 256^31 divided by the target.
void CBCalculateMerkleRoot (uint8_t *hashes, uint32_t hashNum)
 Calculates the merkle root from a list of hashes.
uint32_t CBCalculateTarget (uint32_t oldTarget, uint32_t time)
 Recalcultes the target for every 2016 blocks.
uint32_t CBTransactionGetSigOps (CBTransaction *tx)
 Returns the number of sigops from a transaction but does not include P2SH scripts. P2SH sigop counting requires that the previous output is known to be a P2SH output.
CBPrevOutCBTransactionValidateBasic (CBTransaction *tx, bool coinbase, bool *err)
 Validates a transaction has outputs and inputs, is below the maximum block size, has outputs that do not overflow and that there are no duplicate spent transaction outputs. This function also checks coinbase transactions have input scripts between 2 and 100 bytes and non-coinbase transactions do not have a NULL previous output. Further validation can be done by checking the transaction against input transactions. With simplified payment verification instead the validation is done through trusting miners but the basic validation can still be done for these basic checks.
bool CBTransactionIsFinal (CBTransaction *tx, uint64_t time, uint64_t height)
 Determines if a transaction is final and therefore can exist in a block. A transaction is final if the lockTime has been reached or if all inputs are final.
bool CBValidateProofOfWork (CBByteArray *hash, uint32_t target)
 Validates proof of work.

Detailed Description

Here are functions for doing parts of bitcoin validation. cbitcoin is designed to make a broad variety of bitcoin development easier and is not designed to implement a client node. Therefore, cbitcoin does not provide provide functions for validating block chains as there are different methods for doing so. So wether you want to use SPV, block-chain pruning, full validation or whatever, you can use these functions to help. Also see CBScript.h and CBMerkleNode.h for validation. CBNetworkCommunicator.h has message size checks and message deserialisation functions check messages for syntax and sizes.


Function Documentation

uint64_t CBCalculateBlockReward ( uint64_t  blockHeight)

Calculates the block reward at a particular block height.

Parameters:
blockHeightThe height
Returns:
The reward for generating the block, not including transaction fees.
CBBigInt CBCalculateBlockWork ( uint32_t  target)

Calculates the block work which is 256^31 divided by the target.

Parameters:
targetThe target to calculate the work for.
Returns:
The block work as a CBBigInt.
void CBCalculateMerkleRoot ( uint8_t *  hashes,
uint32_t  hashNum 
)

Calculates the merkle root from a list of hashes.

Parameters:
hashesThe hashes stored as continuous byte data with each 32 byte hash after each other. The data pointed to by "hashes" will be modified and will result in the merkle root as the first 32 bytes.
hashNumThe number of hashes in the memory block
uint32_t CBCalculateTarget ( uint32_t  oldTarget,
uint32_t  time 
)

Recalcultes the target for every 2016 blocks.

Parameters:
oldTargetThe old target in compact form.
timeThe time between the newer block and older block for recalulating the target.
Returns:
the new target in compact form.

Returns the number of sigops from a transaction but does not include P2SH scripts. P2SH sigop counting requires that the previous output is known to be a P2SH output.

Parameters:
txThe transaction.
Returns:
the number of sigops for validation.
bool CBTransactionIsFinal ( CBTransaction tx,
uint64_t  time,
uint64_t  height 
)

Determines if a transaction is final and therefore can exist in a block. A transaction is final if the lockTime has been reached or if all inputs are final.

Parameters:
txThe transaction.
timeThe time for determining if the transaction is final.
heightThe block height for determining if the transaction is final.
Returns:
true if final and false if not final.
CBPrevOut* CBTransactionValidateBasic ( CBTransaction tx,
bool  coinbase,
bool *  err 
)

Validates a transaction has outputs and inputs, is below the maximum block size, has outputs that do not overflow and that there are no duplicate spent transaction outputs. This function also checks coinbase transactions have input scripts between 2 and 100 bytes and non-coinbase transactions do not have a NULL previous output. Further validation can be done by checking the transaction against input transactions. With simplified payment verification instead the validation is done through trusting miners but the basic validation can still be done for these basic checks.

Parameters:
txThe transaction to validate. This should be deserialised.
coinbasetrue to validate for a coinbase transaction, false to validate for a non-coinbase transaction.
errPointer to error which is set true when a memory failure occurs.
Returns:
A memory block of previous transaction ouputs spent by this transaction if validation passed or NULL. The outputs can be used for further validation that the transaction is not a double-spend. The block of CBPrevOuts needs to be freed. The hashes are not retained, so the data is good until the transaction is freed. Do not try to release the hashes, thinking they have been retained in this function.
bool CBValidateProofOfWork ( CBByteArray hash,
uint32_t  target 
)

Validates proof of work.

hash Block hash.

Returns:
true if the validation passed, false otherwise.