Modify count sigops calls for new btcscript API.

The btcscript API for GetSigOpCount and GetPreciseSigOpCount
recently changed to no longer return an error.  This change was necessary
to mirror the behavior of bitcoind signature operations counting.  This
commit modifies the code accordingly to use the new API.
This commit is contained in:
Dave Collins 2013-07-29 16:18:15 -05:00
parent c3b330e42d
commit 6409879e14

View file

@ -304,37 +304,23 @@ func (b *BlockChain) checkProofOfWork(block *btcutil.Block) error {
// input and output scripts in the provided transaction. This uses the // input and output scripts in the provided transaction. This uses the
// quicker, but imprecise, signature operation counting mechanism from // quicker, but imprecise, signature operation counting mechanism from
// btcscript. // btcscript.
func countSigOps(msgTx *btcwire.MsgTx, isCoinBaseTx bool) (int, error) { func countSigOps(msgTx *btcwire.MsgTx) int {
// Choose the starting transaction input based on whether this is a
// coinbase transaction since the coinbase input script should not be
// executed.
txIns := msgTx.TxIn
if isCoinBaseTx {
txIns = txIns[1:]
}
// Accumulate the number of signature operations in all transaction // Accumulate the number of signature operations in all transaction
// inputs (except the first input if this is a coinbase transaction). // inputs.
totalSigOps := 0 totalSigOps := 0
for _, txIn := range txIns { for _, txIn := range msgTx.TxIn {
numSigOps, err := btcscript.GetSigOpCount(txIn.SignatureScript) numSigOps := btcscript.GetSigOpCount(txIn.SignatureScript)
if err != nil {
return 0, err
}
totalSigOps += numSigOps totalSigOps += numSigOps
} }
// Accumulate the number of signature operations in all transaction // Accumulate the number of signature operations in all transaction
// outputs. // outputs.
for _, txOut := range msgTx.TxOut { for _, txOut := range msgTx.TxOut {
numSigOps, err := btcscript.GetSigOpCount(txOut.PkScript) numSigOps := btcscript.GetSigOpCount(txOut.PkScript)
if err != nil {
return 0, err
}
totalSigOps += numSigOps totalSigOps += numSigOps
} }
return totalSigOps, nil return totalSigOps
} }
// countP2SHSigOps returns the number of signature operations for all input // countP2SHSigOps returns the number of signature operations for all input
@ -360,7 +346,7 @@ func countP2SHSigOps(msgTx *btcwire.MsgTx, isCoinBaseTx bool, txStore map[btcwir
// Ensure the referenced input transaction is available. // Ensure the referenced input transaction is available.
txInHash := &txIn.PreviousOutpoint.Hash txInHash := &txIn.PreviousOutpoint.Hash
originTx, exists := txStore[*txInHash] originTx, exists := txStore[*txInHash]
if !exists { if !exists || originTx.err != nil || originTx.tx == nil {
return 0, fmt.Errorf("unable to find input transaction "+ return 0, fmt.Errorf("unable to find input transaction "+
"%v referenced from transaction %v", txHash, "%v referenced from transaction %v", txHash,
txInHash) txInHash)
@ -385,11 +371,8 @@ func countP2SHSigOps(msgTx *btcwire.MsgTx, isCoinBaseTx bool, txStore map[btcwir
// Count the precise number of signature operations in the // Count the precise number of signature operations in the
// referenced public key script. // referenced public key script.
sigScript := txIn.SignatureScript sigScript := txIn.SignatureScript
numSigOps, err := btcscript.GetPreciseSigOpCount(sigScript, numSigOps := btcscript.GetPreciseSigOpCount(sigScript, pkScript,
pkScript, true) true)
if err != nil {
return 0, err
}
// We could potentially overflow the accumulator so check for // We could potentially overflow the accumulator so check for
// overflow. // overflow.
@ -492,21 +475,11 @@ func (b *BlockChain) checkBlockSanity(block *btcutil.Block) error {
// The number of signature operations must be less than the maximum // The number of signature operations must be less than the maximum
// allowed per block. // allowed per block.
totalSigOps := 0 totalSigOps := 0
for i, tx := range transactions { for _, tx := range transactions {
// Since the first (and only the first) transaction has already
// been verified above to be a coinbase transaction, use i == 0
// as an optimization for the flag to countSigOps for whether
// or not the transaction is a coinbase transaction rather than
// having to do a full coinbase check again.
numSigOps, err := countSigOps(tx, i == 0)
if err != nil {
return err
}
// We could potentially overflow the accumulator so check for // We could potentially overflow the accumulator so check for
// overflow. // overflow.
lastSigOps := totalSigOps lastSigOps := totalSigOps
totalSigOps += numSigOps totalSigOps += countSigOps(tx)
if totalSigOps < lastSigOps || totalSigOps > maxSigOpsPerBlock { if totalSigOps < lastSigOps || totalSigOps > maxSigOpsPerBlock {
str := fmt.Sprintf("block contains too many signature "+ str := fmt.Sprintf("block contains too many signature "+
"operations - got %v, max %v", totalSigOps, "operations - got %v, max %v", totalSigOps,
@ -783,16 +756,14 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block *btcutil.Block) er
transactions := block.MsgBlock().Transactions transactions := block.MsgBlock().Transactions
totalSigOps := 0 totalSigOps := 0
for i, tx := range transactions { for i, tx := range transactions {
// Since the first (and only the first) transaction has already numsigOps := countSigOps(tx)
// been verified to be a coinbase transaction, use i == 0
// as an optimization for the flag to countSigOps for whether
// or not the transaction is a coinbase transaction rather than
// having to do a full coinbase check again.
numsigOps, err := countSigOps(tx, i == 0)
if err != nil {
return err
}
if enforceBIP0016 { if enforceBIP0016 {
// Since the first (and only the first) transaction has
// already been verified to be a coinbase transaction,
// use i == 0 as an optimization for the flag to
// countP2SHSigOps for whether or not the transaction is
// a coinbase transaction rather than having to do a
// full coinbase check again.
numP2SHSigOps, err := countP2SHSigOps(tx, i == 0, numP2SHSigOps, err := countP2SHSigOps(tx, i == 0,
txInputStore) txInputStore)
if err != nil { if err != nil {