2015-08-26 06:03:18 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-07-18 16:49:28 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-30 21:54:30 +01:00
|
|
|
package blockchain
|
2013-07-18 16:49:28 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-02 18:04:59 +02:00
|
|
|
"math"
|
|
|
|
"runtime"
|
2016-10-19 03:33:20 +02:00
|
|
|
"time"
|
2014-07-02 18:04:59 +02:00
|
|
|
|
2015-01-30 19:08:47 +01:00
|
|
|
"github.com/btcsuite/btcd/txscript"
|
2015-02-05 22:16:39 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2015-01-15 17:23:47 +01:00
|
|
|
"github.com/btcsuite/btcutil"
|
2013-07-18 16:49:28 +02:00
|
|
|
)
|
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
// txValidateItem holds a transaction along with which input to validate.
|
|
|
|
type txValidateItem struct {
|
|
|
|
txInIndex int
|
2015-02-05 22:16:39 +01:00
|
|
|
txIn *wire.TxIn
|
2014-01-16 19:48:37 +01:00
|
|
|
tx *btcutil.Tx
|
2016-10-19 03:33:20 +02:00
|
|
|
sigHashes *txscript.TxSigHashes
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
// txValidator provides a type which asynchronously validates transaction
|
|
|
|
// inputs. It provides several channels for communication and a processing
|
|
|
|
// function that is intended to be in run multiple goroutines.
|
|
|
|
type txValidator struct {
|
|
|
|
validateChan chan *txValidateItem
|
2014-07-02 18:00:47 +02:00
|
|
|
quitChan chan struct{}
|
2014-01-16 19:48:37 +01:00
|
|
|
resultChan chan error
|
2015-08-26 06:03:18 +02:00
|
|
|
utxoView *UtxoViewpoint
|
2015-01-30 19:08:47 +01:00
|
|
|
flags txscript.ScriptFlags
|
2015-09-25 01:22:00 +02:00
|
|
|
sigCache *txscript.SigCache
|
2016-10-19 03:33:20 +02:00
|
|
|
hashCache *txscript.HashCache
|
2014-01-16 19:48:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// sendResult sends the result of a script pair validation on the internal
|
|
|
|
// result channel while respecting the quit channel. The allows orderly
|
|
|
|
// shutdown when the validation process is aborted early due to a validation
|
|
|
|
// error in one of the other goroutines.
|
|
|
|
func (v *txValidator) sendResult(result error) {
|
|
|
|
select {
|
|
|
|
case v.resultChan <- result:
|
|
|
|
case <-v.quitChan:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateHandler consumes items to validate from the internal validate channel
|
|
|
|
// and returns the result of the validation on the internal result channel. It
|
|
|
|
// must be run as a goroutine.
|
|
|
|
func (v *txValidator) validateHandler() {
|
|
|
|
out:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case txVI := <-v.validateChan:
|
|
|
|
// Ensure the referenced input transaction is available.
|
|
|
|
txIn := txVI.txIn
|
2014-10-01 14:53:47 +02:00
|
|
|
originTxHash := &txIn.PreviousOutPoint.Hash
|
2015-08-26 06:03:18 +02:00
|
|
|
originTxIndex := txIn.PreviousOutPoint.Index
|
|
|
|
txEntry := v.utxoView.LookupEntry(originTxHash)
|
|
|
|
if txEntry == nil {
|
2014-06-27 06:29:57 +02:00
|
|
|
str := fmt.Sprintf("unable to find input "+
|
2014-01-16 19:48:37 +01:00
|
|
|
"transaction %v referenced from "+
|
2014-06-27 06:29:57 +02:00
|
|
|
"transaction %v", originTxHash,
|
2016-08-08 21:04:33 +02:00
|
|
|
txVI.tx.Hash())
|
2017-08-14 07:22:40 +02:00
|
|
|
err := ruleError(ErrMissingTxOut, str)
|
2014-01-16 19:48:37 +01:00
|
|
|
v.sendResult(err)
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
|
2015-08-26 06:03:18 +02:00
|
|
|
// Ensure the referenced input transaction public key
|
|
|
|
// script is available.
|
|
|
|
pkScript := txEntry.PkScriptByIndex(originTxIndex)
|
|
|
|
if pkScript == nil {
|
|
|
|
str := fmt.Sprintf("unable to find unspent "+
|
|
|
|
"output %v script referenced from "+
|
|
|
|
"transaction %s:%d",
|
2016-08-08 21:04:33 +02:00
|
|
|
txIn.PreviousOutPoint, txVI.tx.Hash(),
|
2015-08-26 06:03:18 +02:00
|
|
|
txVI.txInIndex)
|
2014-06-27 06:29:57 +02:00
|
|
|
err := ruleError(ErrBadTxInput, str)
|
2014-01-16 19:48:37 +01:00
|
|
|
v.sendResult(err)
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new script engine for the script pair.
|
|
|
|
sigScript := txIn.SignatureScript
|
2016-10-19 03:33:20 +02:00
|
|
|
witness := txIn.Witness
|
|
|
|
inputAmount := txEntry.AmountByIndex(originTxIndex)
|
2015-04-20 22:28:00 +02:00
|
|
|
vm, err := txscript.NewEngine(pkScript, txVI.tx.MsgTx(),
|
2016-10-19 03:33:20 +02:00
|
|
|
txVI.txInIndex, v.flags, v.sigCache, txVI.sigHashes,
|
|
|
|
inputAmount)
|
2014-01-16 19:48:37 +01:00
|
|
|
if err != nil {
|
2014-06-27 06:29:57 +02:00
|
|
|
str := fmt.Sprintf("failed to parse input "+
|
|
|
|
"%s:%d which references output %s:%d - "+
|
2016-10-19 03:33:20 +02:00
|
|
|
"%v (input witness %x, input script "+
|
|
|
|
"bytes %x, prev output script bytes %x)",
|
|
|
|
txVI.tx.Hash(), txVI.txInIndex, originTxHash,
|
|
|
|
originTxIndex, err, witness, sigScript,
|
|
|
|
pkScript)
|
2014-06-27 06:29:57 +02:00
|
|
|
err := ruleError(ErrScriptMalformed, str)
|
2014-01-16 19:48:37 +01:00
|
|
|
v.sendResult(err)
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the script pair.
|
2015-04-20 22:28:00 +02:00
|
|
|
if err := vm.Execute(); err != nil {
|
2014-06-27 06:29:57 +02:00
|
|
|
str := fmt.Sprintf("failed to validate input "+
|
|
|
|
"%s:%d which references output %s:%d - "+
|
2016-10-19 03:33:20 +02:00
|
|
|
"%v (input witness %x, input script "+
|
|
|
|
"bytes %x, prev output script bytes %x)",
|
|
|
|
txVI.tx.Hash(), txVI.txInIndex,
|
|
|
|
originTxHash, originTxIndex, err,
|
|
|
|
witness, sigScript, pkScript)
|
2014-06-27 06:29:57 +02:00
|
|
|
err := ruleError(ErrScriptValidation, str)
|
2014-01-16 19:48:37 +01:00
|
|
|
v.sendResult(err)
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validation succeeded.
|
|
|
|
v.sendResult(nil)
|
|
|
|
|
|
|
|
case <-v.quitChan:
|
|
|
|
break out
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the scripts for all of the passed transaction inputs using
|
|
|
|
// multiple goroutines.
|
|
|
|
func (v *txValidator) Validate(items []*txValidateItem) error {
|
|
|
|
if len(items) == 0 {
|
2013-07-18 16:49:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
// Limit the number of goroutines to do script validation based on the
|
|
|
|
// number of processor cores. This help ensure the system stays
|
|
|
|
// reasonably responsive under heavy load.
|
|
|
|
maxGoRoutines := runtime.NumCPU() * 3
|
|
|
|
if maxGoRoutines <= 0 {
|
|
|
|
maxGoRoutines = 1
|
|
|
|
}
|
|
|
|
if maxGoRoutines > len(items) {
|
|
|
|
maxGoRoutines = len(items)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
// Start up validation handlers that are used to asynchronously
|
|
|
|
// validate each transaction input.
|
|
|
|
for i := 0; i < maxGoRoutines; i++ {
|
|
|
|
go v.validateHandler()
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
// Validate each of the inputs. The quit channel is closed when any
|
|
|
|
// errors occur so all processing goroutines exit regardless of which
|
|
|
|
// input had the validation error.
|
|
|
|
numInputs := len(items)
|
|
|
|
currentItem := 0
|
|
|
|
processedItems := 0
|
|
|
|
for processedItems < numInputs {
|
|
|
|
// Only send items while there are still items that need to
|
|
|
|
// be processed. The select statement will never select a nil
|
|
|
|
// channel.
|
|
|
|
var validateChan chan *txValidateItem
|
|
|
|
var item *txValidateItem
|
|
|
|
if currentItem < numInputs {
|
|
|
|
validateChan = v.validateChan
|
|
|
|
item = items[currentItem]
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case validateChan <- item:
|
|
|
|
currentItem++
|
|
|
|
|
|
|
|
case err := <-v.resultChan:
|
|
|
|
processedItems++
|
|
|
|
if err != nil {
|
|
|
|
close(v.quitChan)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
close(v.quitChan)
|
2013-07-18 16:49:28 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
// newTxValidator returns a new instance of txValidator to be used for
|
|
|
|
// validating transaction scripts asynchronously.
|
2016-10-19 03:33:20 +02:00
|
|
|
func newTxValidator(utxoView *UtxoViewpoint, flags txscript.ScriptFlags,
|
|
|
|
sigCache *txscript.SigCache, hashCache *txscript.HashCache) *txValidator {
|
2014-01-16 19:48:37 +01:00
|
|
|
return &txValidator{
|
|
|
|
validateChan: make(chan *txValidateItem),
|
2014-07-02 18:00:47 +02:00
|
|
|
quitChan: make(chan struct{}),
|
2014-01-16 19:48:37 +01:00
|
|
|
resultChan: make(chan error),
|
2015-08-26 06:03:18 +02:00
|
|
|
utxoView: utxoView,
|
2015-09-25 01:22:00 +02:00
|
|
|
sigCache: sigCache,
|
2016-10-19 03:33:20 +02:00
|
|
|
hashCache: hashCache,
|
2014-01-16 19:48:37 +01:00
|
|
|
flags: flags,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-30 23:47:37 +02:00
|
|
|
// ValidateTransactionScripts validates the scripts for the passed transaction
|
|
|
|
// using multiple goroutines.
|
2016-10-19 03:33:20 +02:00
|
|
|
func ValidateTransactionScripts(tx *btcutil.Tx, utxoView *UtxoViewpoint,
|
|
|
|
flags txscript.ScriptFlags, sigCache *txscript.SigCache,
|
|
|
|
hashCache *txscript.HashCache) error {
|
|
|
|
|
2017-03-09 23:44:07 +01:00
|
|
|
// First determine if segwit is active according to the scriptFlags. If
|
|
|
|
// it isn't then we don't need to interact with the HashCache.
|
|
|
|
segwitActive := flags&txscript.ScriptVerifyWitness == txscript.ScriptVerifyWitness
|
|
|
|
|
2016-10-19 03:33:20 +02:00
|
|
|
// If the hashcache doesn't yet has the sighash midstate for this
|
|
|
|
// transaction, then we'll compute them now so we can re-use them
|
|
|
|
// amongst all worker validation goroutines.
|
2017-03-09 23:44:07 +01:00
|
|
|
if segwitActive && tx.MsgTx().HasWitness() &&
|
|
|
|
!hashCache.ContainsHashes(tx.Hash()) {
|
2016-10-19 03:33:20 +02:00
|
|
|
hashCache.AddSigHashes(tx.MsgTx())
|
|
|
|
}
|
|
|
|
|
2017-03-09 23:44:07 +01:00
|
|
|
var cachedHashes *txscript.TxSigHashes
|
|
|
|
if segwitActive && tx.MsgTx().HasWitness() {
|
|
|
|
// The same pointer to the transaction's sighash midstate will
|
|
|
|
// be re-used amongst all validation goroutines. By
|
|
|
|
// pre-computing the sighash here instead of during validation,
|
|
|
|
// we ensure the sighashes
|
|
|
|
// are only computed once.
|
|
|
|
cachedHashes, _ = hashCache.GetSigHashes(tx.Hash())
|
|
|
|
}
|
2016-10-19 03:33:20 +02:00
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
// Collect all of the transaction inputs and required information for
|
|
|
|
// validation.
|
|
|
|
txIns := tx.MsgTx().TxIn
|
|
|
|
txValItems := make([]*txValidateItem, 0, len(txIns))
|
|
|
|
for txInIdx, txIn := range txIns {
|
|
|
|
// Skip coinbases.
|
2014-10-01 14:53:47 +02:00
|
|
|
if txIn.PreviousOutPoint.Index == math.MaxUint32 {
|
2014-01-16 19:48:37 +01:00
|
|
|
continue
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
txVI := &txValidateItem{
|
|
|
|
txInIndex: txInIdx,
|
|
|
|
txIn: txIn,
|
|
|
|
tx: tx,
|
2016-10-19 03:33:20 +02:00
|
|
|
sigHashes: cachedHashes,
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
2014-01-16 19:48:37 +01:00
|
|
|
txValItems = append(txValItems, txVI)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
2014-01-16 19:48:37 +01:00
|
|
|
|
|
|
|
// Validate all of the inputs.
|
2016-10-19 03:33:20 +02:00
|
|
|
validator := newTxValidator(utxoView, flags, sigCache, hashCache)
|
2016-11-03 05:02:04 +01:00
|
|
|
return validator.Validate(txValItems)
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// checkBlockScripts executes and validates the scripts for all transactions in
|
2015-08-26 06:03:18 +02:00
|
|
|
// the passed block using multiple goroutines.
|
2016-10-19 03:33:20 +02:00
|
|
|
func checkBlockScripts(block *btcutil.Block, utxoView *UtxoViewpoint,
|
|
|
|
scriptFlags txscript.ScriptFlags, sigCache *txscript.SigCache,
|
|
|
|
hashCache *txscript.HashCache) error {
|
|
|
|
|
2017-01-04 06:14:05 +01:00
|
|
|
// First determine if segwit is active according to the scriptFlags. If
|
|
|
|
// it isn't then we don't need to interact with the HashCache.
|
|
|
|
segwitActive := scriptFlags&txscript.ScriptVerifyWitness == txscript.ScriptVerifyWitness
|
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
// Collect all of the transaction inputs and required information for
|
|
|
|
// validation for all transactions in the block into a single slice.
|
|
|
|
numInputs := 0
|
|
|
|
for _, tx := range block.Transactions() {
|
|
|
|
numInputs += len(tx.MsgTx().TxIn)
|
2013-10-12 00:14:58 +02:00
|
|
|
}
|
2014-01-16 19:48:37 +01:00
|
|
|
txValItems := make([]*txValidateItem, 0, numInputs)
|
|
|
|
for _, tx := range block.Transactions() {
|
2016-10-19 03:33:20 +02:00
|
|
|
hash := tx.Hash()
|
|
|
|
|
|
|
|
// If the HashCache is present, and it doesn't yet contain the
|
|
|
|
// partial sighashes for this transaction, then we add the
|
|
|
|
// sighashes for the transaction. This allows us to take
|
|
|
|
// advantage of the potential speed savings due to the new
|
|
|
|
// digest algorithm (BIP0143).
|
2017-03-09 23:44:07 +01:00
|
|
|
if segwitActive && tx.HasWitness() && hashCache != nil &&
|
2016-10-19 03:33:20 +02:00
|
|
|
!hashCache.ContainsHashes(hash) {
|
|
|
|
|
|
|
|
hashCache.AddSigHashes(tx.MsgTx())
|
|
|
|
}
|
|
|
|
|
|
|
|
var cachedHashes *txscript.TxSigHashes
|
2017-03-09 23:44:07 +01:00
|
|
|
if segwitActive && tx.HasWitness() {
|
2016-10-19 03:33:20 +02:00
|
|
|
if hashCache != nil {
|
|
|
|
cachedHashes, _ = hashCache.GetSigHashes(hash)
|
|
|
|
} else {
|
|
|
|
cachedHashes = txscript.NewTxSigHashes(tx.MsgTx())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-16 19:48:37 +01:00
|
|
|
for txInIdx, txIn := range tx.MsgTx().TxIn {
|
|
|
|
// Skip coinbases.
|
2014-10-01 14:53:47 +02:00
|
|
|
if txIn.PreviousOutPoint.Index == math.MaxUint32 {
|
2014-01-16 19:48:37 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
txVI := &txValidateItem{
|
|
|
|
txInIndex: txInIdx,
|
|
|
|
txIn: txIn,
|
|
|
|
tx: tx,
|
2016-10-19 03:33:20 +02:00
|
|
|
sigHashes: cachedHashes,
|
2013-10-12 00:14:58 +02:00
|
|
|
}
|
2014-01-16 19:48:37 +01:00
|
|
|
txValItems = append(txValItems, txVI)
|
2013-10-12 00:14:58 +02:00
|
|
|
}
|
|
|
|
}
|
2014-01-16 19:48:37 +01:00
|
|
|
|
|
|
|
// Validate all of the inputs.
|
2016-10-19 03:33:20 +02:00
|
|
|
validator := newTxValidator(utxoView, scriptFlags, sigCache, hashCache)
|
|
|
|
start := time.Now()
|
|
|
|
if err := validator.Validate(txValItems); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
|
|
|
log.Tracef("block %v took %v to verify", block.Hash(), elapsed)
|
|
|
|
|
|
|
|
// If the HashCache is present, once we have validated the block, we no
|
|
|
|
// longer need the cached hashes for these transactions, so we purge
|
|
|
|
// them from the cache.
|
2017-01-04 06:14:05 +01:00
|
|
|
if segwitActive && hashCache != nil {
|
2016-10-19 03:33:20 +02:00
|
|
|
for _, tx := range block.Transactions() {
|
2017-03-09 23:44:07 +01:00
|
|
|
if tx.MsgTx().HasWitness() {
|
|
|
|
hashCache.PurgeSigHashes(tx.Hash())
|
|
|
|
}
|
2016-10-19 03:33:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2013-07-18 16:49:28 +02:00
|
|
|
}
|