[lbry] blockchain, mempool: validate txscripts

Co-authored-by: Brannon King <countprimes@gmail.com>
This commit is contained in:
Roy Lee 2018-06-15 13:06:26 -07:00
parent 25c026e060
commit a7b3ed5c29
3 changed files with 13 additions and 7 deletions

View file

@ -231,8 +231,8 @@ func withinLevelBounds(reduction int64, lv int64) bool {
} }
// CheckTransactionSanity performs some preliminary checks on a transaction to // CheckTransactionSanity performs some preliminary checks on a transaction to
// ensure it is sane. These checks are context free. // ensure it is sane.
func CheckTransactionSanity(tx *btcutil.Tx) error { func CheckTransactionSanity(tx *btcutil.Tx, enforceSoftFork bool) error {
// A transaction must have at least one input. // A transaction must have at least one input.
msgTx := tx.MsgTx() msgTx := tx.MsgTx()
if len(msgTx.TxIn) == 0 { if len(msgTx.TxIn) == 0 {
@ -291,6 +291,11 @@ func CheckTransactionSanity(tx *btcutil.Tx) error {
btcutil.MaxSatoshi) btcutil.MaxSatoshi)
return ruleError(ErrBadTxOutValue, str) return ruleError(ErrBadTxOutValue, str)
} }
err := txscript.AllClaimsAreSane(txOut.PkScript, enforceSoftFork)
if err != nil {
return ruleError(ErrBadTxOutValue, err.Error())
}
} }
// Check for duplicate transaction inputs. // Check for duplicate transaction inputs.
@ -545,7 +550,7 @@ func checkBlockSanity(block *btcutil.Block, powLimit *big.Int, timeSource Median
// Do some preliminary checks on each transaction to ensure they are // Do some preliminary checks on each transaction to ensure they are
// sane before continuing. // sane before continuing.
for _, tx := range transactions { for _, tx := range transactions {
err := CheckTransactionSanity(tx) err := CheckTransactionSanity(tx, false)
if err != nil { if err != nil {
return err return err
} }

View file

@ -963,7 +963,7 @@ func (mp *TxPool) maybeAcceptTransaction(tx *btcutil.Tx, isNew, rateLimit, rejec
// Perform preliminary sanity checks on the transaction. This makes // Perform preliminary sanity checks on the transaction. This makes
// use of blockchain which contains the invariant rules for what // use of blockchain which contains the invariant rules for what
// transactions are allowed into blocks. // transactions are allowed into blocks.
err := blockchain.CheckTransactionSanity(tx) err := blockchain.CheckTransactionSanity(tx, true)
if err != nil { if err != nil {
if cerr, ok := err.(blockchain.RuleError); ok { if cerr, ok := err.(blockchain.RuleError); ok {
return nil, nil, chainRuleError(cerr) return nil, nil, chainRuleError(cerr)

View file

@ -99,7 +99,7 @@ func checkInputsStandard(tx *btcutil.Tx, utxoView *blockchain.UtxoViewpoint) err
// they have already been checked prior to calling this // they have already been checked prior to calling this
// function. // function.
entry := utxoView.LookupEntry(txIn.PreviousOutPoint) entry := utxoView.LookupEntry(txIn.PreviousOutPoint)
originPkScript := entry.PkScript() originPkScript := txscript.StripClaimScriptPrefix(entry.PkScript())
switch txscript.GetScriptClass(originPkScript) { switch txscript.GetScriptClass(originPkScript) {
case txscript.ScriptHashTy: case txscript.ScriptHashTy:
numSigOps := txscript.GetPreciseSigOpCount( numSigOps := txscript.GetPreciseSigOpCount(
@ -339,8 +339,9 @@ func checkTransactionStandard(tx *btcutil.Tx, height int32,
// be "dust" (except when the script is a null data script). // be "dust" (except when the script is a null data script).
numNullDataOutputs := 0 numNullDataOutputs := 0
for i, txOut := range msgTx.TxOut { for i, txOut := range msgTx.TxOut {
scriptClass := txscript.GetScriptClass(txOut.PkScript) pkScript := txscript.StripClaimScriptPrefix(txOut.PkScript)
err := checkPkScriptStandard(txOut.PkScript, scriptClass) scriptClass := txscript.GetScriptClass(pkScript)
err := checkPkScriptStandard(pkScript, scriptClass)
if err != nil { if err != nil {
// Attempt to extract a reject code from the error so // Attempt to extract a reject code from the error so
// it can be retained. When not possible, fall back to // it can be retained. When not possible, fall back to