2016-12-31 19:01:21 +01:00
|
|
|
// Copyright (c) 2012-2016 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 10:11:00 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2016-07-11 16:34:21 +02:00
|
|
|
#include "consensus/tx_verify.h"
|
2017-06-17 02:18:42 +02:00
|
|
|
#include "consensus/validation.h"
|
2014-10-28 22:47:18 +01:00
|
|
|
#include "pubkey.h"
|
2012-01-05 03:40:52 +01:00
|
|
|
#include "key.h"
|
2014-08-20 17:37:40 +02:00
|
|
|
#include "script/script.h"
|
2014-09-11 19:15:29 +02:00
|
|
|
#include "script/standard.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "uint256.h"
|
2015-03-12 09:34:42 +01:00
|
|
|
#include "test/test_bitcoin.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
2012-01-05 03:40:52 +01:00
|
|
|
|
|
|
|
// Helpers:
|
|
|
|
static std::vector<unsigned char>
|
|
|
|
Serialize(const CScript& s)
|
|
|
|
{
|
2015-10-29 07:11:24 +01:00
|
|
|
std::vector<unsigned char> sSerialized(s.begin(), s.end());
|
2012-01-05 03:40:52 +01:00
|
|
|
return sSerialized;
|
|
|
|
}
|
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(sigopcount_tests, BasicTestingSetup)
|
2012-01-05 03:40:52 +01:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(GetSigOpCount)
|
|
|
|
{
|
|
|
|
// Test CScript::GetSigOpCount()
|
|
|
|
CScript s1;
|
2013-03-07 18:38:25 +01:00
|
|
|
BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 0U);
|
|
|
|
BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 0U);
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-12-15 09:11:16 +01:00
|
|
|
uint160 dummy;
|
2014-09-25 04:54:08 +02:00
|
|
|
s1 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << OP_2 << OP_CHECKMULTISIG;
|
2013-03-07 18:38:25 +01:00
|
|
|
BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 2U);
|
2012-01-05 03:40:52 +01:00
|
|
|
s1 << OP_IF << OP_CHECKSIG << OP_ENDIF;
|
2013-03-07 18:38:25 +01:00
|
|
|
BOOST_CHECK_EQUAL(s1.GetSigOpCount(true), 3U);
|
|
|
|
BOOST_CHECK_EQUAL(s1.GetSigOpCount(false), 21U);
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-09-25 04:24:46 +02:00
|
|
|
CScript p2sh = GetScriptForDestination(CScriptID(s1));
|
2012-01-05 03:40:52 +01:00
|
|
|
CScript scriptSig;
|
|
|
|
scriptSig << OP_0 << Serialize(s1);
|
2013-03-07 18:38:25 +01:00
|
|
|
BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig), 3U);
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
std::vector<CPubKey> keys;
|
2012-01-05 03:40:52 +01:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
{
|
|
|
|
CKey k;
|
2012-02-20 18:32:33 +01:00
|
|
|
k.MakeNewKey(true);
|
2013-05-01 06:52:05 +02:00
|
|
|
keys.push_back(k.GetPubKey());
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
2014-09-11 19:15:29 +02:00
|
|
|
CScript s2 = GetScriptForMultisig(1, keys);
|
2013-03-07 18:38:25 +01:00
|
|
|
BOOST_CHECK_EQUAL(s2.GetSigOpCount(true), 3U);
|
|
|
|
BOOST_CHECK_EQUAL(s2.GetSigOpCount(false), 20U);
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-09-25 04:24:46 +02:00
|
|
|
p2sh = GetScriptForDestination(CScriptID(s2));
|
2013-03-07 18:38:25 +01:00
|
|
|
BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(true), 0U);
|
|
|
|
BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(false), 0U);
|
2012-01-05 03:40:52 +01:00
|
|
|
CScript scriptSig2;
|
2014-09-25 04:54:08 +02:00
|
|
|
scriptSig2 << OP_1 << ToByteVector(dummy) << ToByteVector(dummy) << Serialize(s2);
|
2013-03-07 18:38:25 +01:00
|
|
|
BOOST_CHECK_EQUAL(p2sh.GetSigOpCount(scriptSig2), 3U);
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
|
2016-05-28 19:22:13 +02:00
|
|
|
/**
|
|
|
|
* Verifies script execution of the zeroth scriptPubKey of tx output and
|
|
|
|
* zeroth scriptSig and witness of tx input.
|
|
|
|
*/
|
|
|
|
ScriptError VerifyWithFlag(const CTransaction& output, const CMutableTransaction& input, int flags)
|
|
|
|
{
|
|
|
|
ScriptError error;
|
|
|
|
CTransaction inputi(input);
|
2016-08-04 02:49:16 +02:00
|
|
|
bool ret = VerifyScript(inputi.vin[0].scriptSig, output.vout[0].scriptPubKey, &inputi.vin[0].scriptWitness, flags, TransactionSignatureChecker(&inputi, 0, output.vout[0].nValue), &error);
|
2016-05-28 19:22:13 +02:00
|
|
|
BOOST_CHECK((ret == true) == (error == SCRIPT_ERR_OK));
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds a creationTx from scriptPubKey and a spendingTx from scriptSig
|
|
|
|
* and witness such that spendingTx spends output zero of creationTx.
|
|
|
|
* Also inserts creationTx's output into the coins view.
|
|
|
|
*/
|
2016-08-04 02:49:16 +02:00
|
|
|
void BuildTxs(CMutableTransaction& spendingTx, CCoinsViewCache& coins, CMutableTransaction& creationTx, const CScript& scriptPubKey, const CScript& scriptSig, const CScriptWitness& witness)
|
2016-05-28 19:22:13 +02:00
|
|
|
{
|
|
|
|
creationTx.nVersion = 1;
|
|
|
|
creationTx.vin.resize(1);
|
|
|
|
creationTx.vin[0].prevout.SetNull();
|
|
|
|
creationTx.vin[0].scriptSig = CScript();
|
|
|
|
creationTx.vout.resize(1);
|
|
|
|
creationTx.vout[0].nValue = 1;
|
|
|
|
creationTx.vout[0].scriptPubKey = scriptPubKey;
|
|
|
|
|
|
|
|
spendingTx.nVersion = 1;
|
|
|
|
spendingTx.vin.resize(1);
|
|
|
|
spendingTx.vin[0].prevout.hash = creationTx.GetHash();
|
|
|
|
spendingTx.vin[0].prevout.n = 0;
|
|
|
|
spendingTx.vin[0].scriptSig = scriptSig;
|
2016-08-04 02:49:16 +02:00
|
|
|
spendingTx.vin[0].scriptWitness = witness;
|
2016-05-28 19:22:13 +02:00
|
|
|
spendingTx.vout.resize(1);
|
|
|
|
spendingTx.vout[0].nValue = 1;
|
|
|
|
spendingTx.vout[0].scriptPubKey = CScript();
|
|
|
|
|
2017-04-25 20:29:30 +02:00
|
|
|
AddCoins(coins, creationTx, 0);
|
2016-05-28 19:22:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(GetTxSigOpCost)
|
|
|
|
{
|
|
|
|
// Transaction creates outputs
|
|
|
|
CMutableTransaction creationTx;
|
|
|
|
// Transaction that spends outputs and whose
|
|
|
|
// sig op cost is going to be tested
|
|
|
|
CMutableTransaction spendingTx;
|
|
|
|
|
|
|
|
// Create utxo set
|
|
|
|
CCoinsView coinsDummy;
|
|
|
|
CCoinsViewCache coins(&coinsDummy);
|
|
|
|
// Create key
|
|
|
|
CKey key;
|
|
|
|
key.MakeNewKey(true);
|
|
|
|
CPubKey pubkey = key.GetPubKey();
|
|
|
|
// Default flags
|
|
|
|
int flags = SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH;
|
|
|
|
|
|
|
|
// Multisig script (legacy counting)
|
|
|
|
{
|
|
|
|
CScript scriptPubKey = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
|
|
|
|
// Do not use a valid signature to avoid using wallet operations.
|
|
|
|
CScript scriptSig = CScript() << OP_0 << OP_0;
|
|
|
|
|
2016-08-04 02:49:16 +02:00
|
|
|
BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
|
2016-05-28 19:22:13 +02:00
|
|
|
// Legacy counting only includes signature operations in scriptSigs and scriptPubKeys
|
|
|
|
// of a transaction and does not take the actual executed sig operations into account.
|
|
|
|
// spendingTx in itself does not contain a signature operation.
|
|
|
|
assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
|
|
|
|
// creationTx contains two signature operations in its scriptPubKey, but legacy counting
|
|
|
|
// is not accurate.
|
|
|
|
assert(GetTransactionSigOpCost(CTransaction(creationTx), coins, flags) == MAX_PUBKEYS_PER_MULTISIG * WITNESS_SCALE_FACTOR);
|
|
|
|
// Sanity check: script verification fails because of an invalid signature.
|
|
|
|
assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Multisig nested in P2SH
|
|
|
|
{
|
|
|
|
CScript redeemScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
|
|
|
|
CScript scriptPubKey = GetScriptForDestination(CScriptID(redeemScript));
|
|
|
|
CScript scriptSig = CScript() << OP_0 << OP_0 << ToByteVector(redeemScript);
|
|
|
|
|
2016-08-04 02:49:16 +02:00
|
|
|
BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, CScriptWitness());
|
2016-05-28 19:22:13 +02:00
|
|
|
assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2 * WITNESS_SCALE_FACTOR);
|
|
|
|
assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// P2WPKH witness program
|
|
|
|
{
|
|
|
|
CScript p2pk = CScript() << ToByteVector(pubkey) << OP_CHECKSIG;
|
|
|
|
CScript scriptPubKey = GetScriptForWitness(p2pk);
|
|
|
|
CScript scriptSig = CScript();
|
|
|
|
CScriptWitness scriptWitness;
|
2016-12-05 08:03:53 +01:00
|
|
|
scriptWitness.stack.push_back(std::vector<unsigned char>(0));
|
|
|
|
scriptWitness.stack.push_back(std::vector<unsigned char>(0));
|
2016-05-28 19:22:13 +02:00
|
|
|
|
|
|
|
|
2016-08-04 02:49:16 +02:00
|
|
|
BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
|
2016-05-28 19:22:13 +02:00
|
|
|
assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
|
|
|
|
// No signature operations if we don't verify the witness.
|
|
|
|
assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags & ~SCRIPT_VERIFY_WITNESS) == 0);
|
|
|
|
assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
|
|
|
|
|
|
|
|
// The sig op cost for witness version != 0 is zero.
|
|
|
|
assert(scriptPubKey[0] == 0x00);
|
|
|
|
scriptPubKey[0] = 0x51;
|
2016-08-04 02:49:16 +02:00
|
|
|
BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
|
2016-05-28 19:22:13 +02:00
|
|
|
assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
|
|
|
|
scriptPubKey[0] = 0x00;
|
2016-08-04 02:49:16 +02:00
|
|
|
BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
|
2016-05-28 19:22:13 +02:00
|
|
|
|
|
|
|
// The witness of a coinbase transaction is not taken into account.
|
|
|
|
spendingTx.vin[0].prevout.SetNull();
|
|
|
|
assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// P2WPKH nested in P2SH
|
|
|
|
{
|
|
|
|
CScript p2pk = CScript() << ToByteVector(pubkey) << OP_CHECKSIG;
|
|
|
|
CScript scriptSig = GetScriptForWitness(p2pk);
|
|
|
|
CScript scriptPubKey = GetScriptForDestination(CScriptID(scriptSig));
|
|
|
|
scriptSig = CScript() << ToByteVector(scriptSig);
|
|
|
|
CScriptWitness scriptWitness;
|
2016-12-05 08:03:53 +01:00
|
|
|
scriptWitness.stack.push_back(std::vector<unsigned char>(0));
|
|
|
|
scriptWitness.stack.push_back(std::vector<unsigned char>(0));
|
2016-05-28 19:22:13 +02:00
|
|
|
|
2016-08-04 02:49:16 +02:00
|
|
|
BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
|
2016-05-28 19:22:13 +02:00
|
|
|
assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 1);
|
|
|
|
assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_EQUALVERIFY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// P2WSH witness program
|
|
|
|
{
|
|
|
|
CScript witnessScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
|
|
|
|
CScript scriptPubKey = GetScriptForWitness(witnessScript);
|
|
|
|
CScript scriptSig = CScript();
|
|
|
|
CScriptWitness scriptWitness;
|
2016-12-05 08:03:53 +01:00
|
|
|
scriptWitness.stack.push_back(std::vector<unsigned char>(0));
|
|
|
|
scriptWitness.stack.push_back(std::vector<unsigned char>(0));
|
|
|
|
scriptWitness.stack.push_back(std::vector<unsigned char>(witnessScript.begin(), witnessScript.end()));
|
2016-05-28 19:22:13 +02:00
|
|
|
|
2016-08-04 02:49:16 +02:00
|
|
|
BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
|
2016-05-28 19:22:13 +02:00
|
|
|
assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
|
|
|
|
assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags & ~SCRIPT_VERIFY_WITNESS) == 0);
|
|
|
|
assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
|
|
|
|
}
|
|
|
|
|
|
|
|
// P2WSH nested in P2SH
|
|
|
|
{
|
|
|
|
CScript witnessScript = CScript() << 1 << ToByteVector(pubkey) << ToByteVector(pubkey) << 2 << OP_CHECKMULTISIGVERIFY;
|
|
|
|
CScript redeemScript = GetScriptForWitness(witnessScript);
|
|
|
|
CScript scriptPubKey = GetScriptForDestination(CScriptID(redeemScript));
|
|
|
|
CScript scriptSig = CScript() << ToByteVector(redeemScript);
|
|
|
|
CScriptWitness scriptWitness;
|
2016-12-05 08:03:53 +01:00
|
|
|
scriptWitness.stack.push_back(std::vector<unsigned char>(0));
|
|
|
|
scriptWitness.stack.push_back(std::vector<unsigned char>(0));
|
|
|
|
scriptWitness.stack.push_back(std::vector<unsigned char>(witnessScript.begin(), witnessScript.end()));
|
2016-05-28 19:22:13 +02:00
|
|
|
|
2016-08-04 02:49:16 +02:00
|
|
|
BuildTxs(spendingTx, coins, creationTx, scriptPubKey, scriptSig, scriptWitness);
|
2016-05-28 19:22:13 +02:00
|
|
|
assert(GetTransactionSigOpCost(CTransaction(spendingTx), coins, flags) == 2);
|
|
|
|
assert(VerifyWithFlag(creationTx, spendingTx, flags) == SCRIPT_ERR_CHECKMULTISIGVERIFY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-05 03:40:52 +01:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|