btcd源码中对tx的校验
接上文,在这个函数中
func (sm *SyncManager) handleTxMsg(tmsg *txMsg)

调用txMemPool成员函数ProcessTransaction对tx进行校验。在syncmanager对象中,txMemPool,顾名思义就是交易池。负责对交易的验证和管理。

tx的数据结构如下:
// Tx defines a bitcoin transaction that provides easier and more efficient
// manipulation of raw transactions. It also memoizes the hash for the
// transaction on its first access so subsequent accesses don't have to repeat
// the relatively expensive hashing operations.
type Tx struct {
msgTx *wire.MsgTx // Underlying MsgTx
txHash *chainhash.Hash // Cached transaction hash
txHashWitness *chainhash.Hash // Cached transaction witness hash
txHasWitness *bool // If the transaction has witness data
txIndex int // Position within a block or TxIndexUnknown
}
在ProcessTransaction函数中调用了maybeAcceptTransaction,其中CheckTransactionSanity,主要对交易的输入个数和及交易的序列化大小、交易输入的前向节点,交易输出的比特币大小进行验证

接下来会调用fetchInputUtxos函数,获取交易输入的utxo。

函数的内部实现:

cfg的FetchUtxoView接口在初始化的配置如下:

所以调用的函数:

生成了一个map结构,存储了当前tx的hash和要花费的之前交易的hash。当前tx的hash,因为还没有被打包进区块链,理论上应该查找不到有效的utxo。这个只是一个检查。PreviousOutPoint的数据结构如下,hash值代表的是当前的交易,index是代表交易的第几个输出。一个交易有若干个输出,所以要指定交易的输出索引。因为之前的交易已经打包到block中,所以数据库中应该存有相应的信息。

浙公网安备 33010602011771号