ValidateTransactionScripts now accepts script flags.

This commit modifies the ValidateTransactionScripts API to accept the
recently added ScriptFlags from btcscript.  This provides flexibility to
the caller to choose validation behavior based on those new flags.
This commit is contained in:
Dave Collins 2013-10-25 10:33:37 -05:00
parent b7b9823f6c
commit 89d86d07ac

View file

@ -10,7 +10,6 @@ import (
"github.com/conformal/btcutil" "github.com/conformal/btcutil"
"github.com/conformal/btcwire" "github.com/conformal/btcwire"
"math" "math"
"time"
) )
// txValidate is used to track results of validating scripts for each // txValidate is used to track results of validating scripts for each
@ -29,7 +28,7 @@ type txProcessList struct {
// validateTxIn validates a the script pair for the passed spending transaction // validateTxIn validates a the script pair for the passed spending transaction
// (along with the specific input index) and origin transaction (with the // (along with the specific input index) and origin transaction (with the
// specific output index). // specific output index).
func validateTxIn(txInIdx int, txin *btcwire.TxIn, txSha *btcwire.ShaHash, tx *btcwire.MsgTx, timestamp time.Time, originTx *btcwire.MsgTx) error { func validateTxIn(txInIdx int, txin *btcwire.TxIn, txSha *btcwire.ShaHash, tx *btcwire.MsgTx, originTx *btcwire.MsgTx, flags btcscript.ScriptFlags) error {
// If the input transaction has no previous input, there is nothing // If the input transaction has no previous input, there is nothing
// to check. // to check.
originTxIdx := txin.PreviousOutpoint.Index originTxIdx := txin.PreviousOutpoint.Index
@ -39,14 +38,15 @@ func validateTxIn(txInIdx int, txin *btcwire.TxIn, txSha *btcwire.ShaHash, tx *b
if originTxIdx >= uint32(len(originTx.TxOut)) { if originTxIdx >= uint32(len(originTx.TxOut)) {
originTxSha := &txin.PreviousOutpoint.Hash originTxSha := &txin.PreviousOutpoint.Hash
log.Warnf("unable to locate source tx %v spending tx %v", originTxSha, &txSha) log.Warnf("unable to locate source tx %v spending tx %v",
originTxSha, &txSha)
return fmt.Errorf("invalid index %x", originTxIdx) return fmt.Errorf("invalid index %x", originTxIdx)
} }
sigScript := txin.SignatureScript sigScript := txin.SignatureScript
pkScript := originTx.TxOut[originTxIdx].PkScript pkScript := originTx.TxOut[originTxIdx].PkScript
engine, err := btcscript.NewScript(sigScript, pkScript, txInIdx, tx, engine, err := btcscript.NewScript(sigScript, pkScript, txInIdx, tx,
timestamp.After(btcscript.Bip16Activation)) flags)
if err != nil { if err != nil {
return err return err
} }
@ -62,7 +62,7 @@ func validateTxIn(txInIdx int, txin *btcwire.TxIn, txSha *btcwire.ShaHash, tx *b
// ValidateTransactionScripts validates the scripts for the passed transaction // ValidateTransactionScripts validates the scripts for the passed transaction
// using multiple goroutines. // using multiple goroutines.
func ValidateTransactionScripts(tx *btcwire.MsgTx, txHash *btcwire.ShaHash, timestamp time.Time, txStore TxStore) (err error) { func ValidateTransactionScripts(tx *btcwire.MsgTx, txHash *btcwire.ShaHash, txStore TxStore, flags btcscript.ScriptFlags) (err error) {
c := make(chan txValidate) c := make(chan txValidate)
job := tx.TxIn job := tx.TxIn
resultErrors := make([]error, len(job)) resultErrors := make([]error, len(job))
@ -87,8 +87,8 @@ func ValidateTransactionScripts(tx *btcwire.MsgTx, txHash *btcwire.ShaHash, time
} }
originTx = txInfo.Tx originTx = txInfo.Tx
} }
err := validateTxIn(txInIdx, job[txInIdx], txHash, tx, timestamp, err := validateTxIn(txInIdx, job[txInIdx], txHash, tx, originTx,
originTx) flags)
r := txValidate{txInIdx, err} r := txValidate{txInIdx, err}
c <- r c <- r
} }
@ -123,7 +123,12 @@ func ValidateTransactionScripts(tx *btcwire.MsgTx, txHash *btcwire.ShaHash, time
// checkBlockScripts executes and validates the scripts for all transactions in // checkBlockScripts executes and validates the scripts for all transactions in
// the passed block. // the passed block.
func checkBlockScripts(block *btcutil.Block, txStore TxStore) error { func checkBlockScripts(block *btcutil.Block, txStore TxStore) error {
timestamp := block.MsgBlock().Header.Timestamp // Setup the script validation flags. Blocks created after the BIP0016
// activation time need to have the pay-to-script-hash checks enabled.
var flags btcscript.ScriptFlags
if block.MsgBlock().Header.Timestamp.After(btcscript.Bip16Activation) {
flags |= btcscript.ScriptBip16
}
txList := block.MsgBlock().Transactions txList := block.MsgBlock().Transactions
c := make(chan txValidate) c := make(chan txValidate)
@ -135,7 +140,7 @@ func checkBlockScripts(block *btcutil.Block, txStore TxStore) error {
tx := txList[txIdx] tx := txList[txIdx]
txHash, _ := block.TxSha(txIdx) txHash, _ := block.TxSha(txIdx)
err := ValidateTransactionScripts(tx, txHash, timestamp, txStore) err := ValidateTransactionScripts(tx, txHash, txStore, flags)
r := txValidate{txIdx, err} r := txValidate{txIdx, err}
c <- r c <- r
} }