2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2012-2015 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.
|
|
|
|
|
2019-07-23 11:20:23 +02:00
|
|
|
#include "consensus/tx_verify.h"
|
2016-03-31 14:54:58 +02:00
|
|
|
#include "core_io.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
#include "key.h"
|
|
|
|
#include "keystore.h"
|
2017-08-09 02:19:06 +02:00
|
|
|
#include "validation.h"
|
2015-06-24 07:25:30 +02:00
|
|
|
#include "policy/policy.h"
|
2014-08-20 17:37:40 +02:00
|
|
|
#include "script/script.h"
|
2014-11-13 20:27:38 +01:00
|
|
|
#include "script/script_error.h"
|
2014-08-27 17:22:33 +02:00
|
|
|
#include "script/sign.h"
|
2016-04-19 10:49:40 +02:00
|
|
|
#include "script/ismine.h"
|
2016-03-03 20:20:32 +01:00
|
|
|
#include "test/test_dash.h"
|
2014-08-29 22:07:39 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-11-13 20:27:38 +01:00
|
|
|
Verify(const CScript& scriptSig, const CScript& scriptPubKey, bool fStrict, ScriptError& err)
|
2012-01-05 03:40:52 +01:00
|
|
|
{
|
|
|
|
// Create dummy to/from transactions:
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txFrom;
|
2012-01-05 03:40:52 +01:00
|
|
|
txFrom.vout.resize(1);
|
|
|
|
txFrom.vout[0].scriptPubKey = scriptPubKey;
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txTo;
|
2012-01-05 03:40:52 +01:00
|
|
|
txTo.vin.resize(1);
|
|
|
|
txTo.vout.resize(1);
|
|
|
|
txTo.vin[0].prevout.n = 0;
|
|
|
|
txTo.vin[0].prevout.hash = txFrom.GetHash();
|
|
|
|
txTo.vin[0].scriptSig = scriptSig;
|
|
|
|
txTo.vout[0].nValue = 1;
|
|
|
|
|
2016-03-31 14:51:29 +02:00
|
|
|
return VerifyScript(scriptSig, scriptPubKey, fStrict ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE, MutableTransactionSignatureChecker(&txTo, 0, txFrom.vout[0].nValue), &err);
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-12 09:34:42 +01:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(script_P2SH_tests, BasicTestingSetup)
|
2012-01-05 03:40:52 +01:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(sign)
|
|
|
|
{
|
2014-04-23 08:05:05 +02:00
|
|
|
LOCK(cs_main);
|
2012-01-05 03:40:52 +01:00
|
|
|
// Pay-to-script-hash looks like this:
|
|
|
|
// scriptSig: <sig> <sig...> <serialized_script>
|
|
|
|
// scriptPubKey: HASH160 <hash> EQUAL
|
|
|
|
|
|
|
|
// Test SignSignature() (and therefore the version of Solver() that signs transactions)
|
|
|
|
CBasicKeyStore keystore;
|
|
|
|
CKey key[4];
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2012-02-20 18:32:33 +01:00
|
|
|
key[i].MakeNewKey(true);
|
2012-01-05 03:40:52 +01:00
|
|
|
keystore.AddKey(key[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 8 Scripts: checking all combinations of
|
|
|
|
// different keys, straight/P2SH, pubkey/pubkeyhash
|
|
|
|
CScript standardScripts[4];
|
2014-09-25 04:54:08 +02:00
|
|
|
standardScripts[0] << ToByteVector(key[0].GetPubKey()) << OP_CHECKSIG;
|
2014-09-11 19:15:29 +02:00
|
|
|
standardScripts[1] = GetScriptForDestination(key[1].GetPubKey().GetID());
|
2014-09-25 04:54:08 +02:00
|
|
|
standardScripts[2] << ToByteVector(key[1].GetPubKey()) << OP_CHECKSIG;
|
2014-09-11 19:15:29 +02:00
|
|
|
standardScripts[3] = GetScriptForDestination(key[2].GetPubKey().GetID());
|
2012-01-05 03:40:52 +01:00
|
|
|
CScript evalScripts[4];
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
keystore.AddCScript(standardScripts[i]);
|
2014-09-25 04:24:46 +02:00
|
|
|
evalScripts[i] = GetScriptForDestination(CScriptID(standardScripts[i]));
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txFrom; // Funding transaction:
|
2017-01-05 11:31:56 +01:00
|
|
|
std::string reason;
|
2012-01-05 03:40:52 +01:00
|
|
|
txFrom.vout.resize(8);
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
txFrom.vout[i].scriptPubKey = evalScripts[i];
|
2013-04-25 00:27:00 +02:00
|
|
|
txFrom.vout[i].nValue = COIN;
|
2012-01-05 03:40:52 +01:00
|
|
|
txFrom.vout[i+4].scriptPubKey = standardScripts[i];
|
2013-04-25 00:27:00 +02:00
|
|
|
txFrom.vout[i+4].nValue = COIN;
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
2013-06-23 08:05:25 +02:00
|
|
|
BOOST_CHECK(IsStandardTx(txFrom, reason));
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txTo[8]; // Spending transactions
|
2012-01-05 03:40:52 +01:00
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
txTo[i].vin.resize(1);
|
|
|
|
txTo[i].vout.resize(1);
|
|
|
|
txTo[i].vin[0].prevout.n = i;
|
|
|
|
txTo[i].vin[0].prevout.hash = txFrom.GetHash();
|
|
|
|
txTo[i].vout[0].nValue = 1;
|
|
|
|
BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
{
|
2016-03-31 14:54:58 +02:00
|
|
|
BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0, SIGHASH_ALL), strprintf("SignSignature %d", i));
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
// All of the above should be OK, and the txTos have valid signatures
|
|
|
|
// Check to make sure signature verification fails if we use the wrong ScriptSig:
|
2016-09-01 12:09:36 +02:00
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
PrecomputedTransactionData txdata(txTo[i]);
|
2012-01-05 03:40:52 +01:00
|
|
|
for (int j = 0; j < 8; j++)
|
|
|
|
{
|
|
|
|
CScript sigSave = txTo[i].vin[0].scriptSig;
|
|
|
|
txTo[i].vin[0].scriptSig = txTo[j].vin[0].scriptSig;
|
2017-09-23 01:10:09 +02:00
|
|
|
bool sigOK = CScriptCheck(txFrom.vout[txTo[i].vin[0].prevout.n], txTo[i], 0, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, false, &txdata)();
|
2012-01-05 03:40:52 +01:00
|
|
|
if (i == j)
|
|
|
|
BOOST_CHECK_MESSAGE(sigOK, strprintf("VerifySignature %d %d", i, j));
|
|
|
|
else
|
|
|
|
BOOST_CHECK_MESSAGE(!sigOK, strprintf("VerifySignature %d %d", i, j));
|
|
|
|
txTo[i].vin[0].scriptSig = sigSave;
|
|
|
|
}
|
2016-09-01 12:09:36 +02:00
|
|
|
}
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(norecurse)
|
|
|
|
{
|
2014-11-13 20:27:38 +01:00
|
|
|
ScriptError err;
|
2012-01-05 03:40:52 +01:00
|
|
|
// Make sure only the outer pay-to-script-hash does the
|
|
|
|
// extra-validation thing:
|
|
|
|
CScript invalidAsScript;
|
|
|
|
invalidAsScript << OP_INVALIDOPCODE << OP_INVALIDOPCODE;
|
|
|
|
|
2014-09-25 04:24:46 +02:00
|
|
|
CScript p2sh = GetScriptForDestination(CScriptID(invalidAsScript));
|
2012-01-05 03:40:52 +01:00
|
|
|
|
|
|
|
CScript scriptSig;
|
|
|
|
scriptSig << Serialize(invalidAsScript);
|
|
|
|
|
|
|
|
// Should not verify, because it will try to execute OP_INVALIDOPCODE
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK(!Verify(scriptSig, p2sh, true, err));
|
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_BAD_OPCODE, ScriptErrorString(err));
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2012-07-26 02:48:39 +02:00
|
|
|
// Try to recur, and verification should succeed because
|
2012-01-05 03:40:52 +01:00
|
|
|
// the inner HASH160 <> EQUAL should only check the hash:
|
2014-09-25 04:24:46 +02:00
|
|
|
CScript p2sh2 = GetScriptForDestination(CScriptID(p2sh));
|
2012-01-05 03:40:52 +01:00
|
|
|
CScript scriptSig2;
|
|
|
|
scriptSig2 << Serialize(invalidAsScript) << Serialize(p2sh);
|
|
|
|
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK(Verify(scriptSig2, p2sh2, true, err));
|
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(set)
|
|
|
|
{
|
2014-04-23 08:05:05 +02:00
|
|
|
LOCK(cs_main);
|
2012-01-05 03:40:52 +01:00
|
|
|
// Test the CScript::Set* methods
|
|
|
|
CBasicKeyStore keystore;
|
|
|
|
CKey key[4];
|
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 < 4; i++)
|
|
|
|
{
|
2012-02-20 18:32:33 +01:00
|
|
|
key[i].MakeNewKey(true);
|
2012-01-05 03:40:52 +01:00
|
|
|
keystore.AddKey(key[i]);
|
2013-05-01 06:52:05 +02:00
|
|
|
keys.push_back(key[i].GetPubKey());
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CScript inner[4];
|
2014-09-11 19:15:29 +02:00
|
|
|
inner[0] = GetScriptForDestination(key[0].GetPubKey().GetID());
|
|
|
|
inner[1] = GetScriptForMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
|
|
|
|
inner[2] = GetScriptForMultisig(1, std::vector<CPubKey>(keys.begin(), keys.begin()+2));
|
|
|
|
inner[3] = GetScriptForMultisig(2, std::vector<CPubKey>(keys.begin(), keys.begin()+3));
|
2012-01-05 03:40:52 +01:00
|
|
|
|
|
|
|
CScript outer[4];
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2014-09-25 04:24:46 +02:00
|
|
|
outer[i] = GetScriptForDestination(CScriptID(inner[i]));
|
2012-01-05 03:40:52 +01:00
|
|
|
keystore.AddCScript(inner[i]);
|
|
|
|
}
|
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txFrom; // Funding transaction:
|
2017-01-05 11:31:56 +01:00
|
|
|
std::string reason;
|
2012-01-05 03:40:52 +01:00
|
|
|
txFrom.vout.resize(4);
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
txFrom.vout[i].scriptPubKey = outer[i];
|
2013-04-25 00:27:00 +02:00
|
|
|
txFrom.vout[i].nValue = CENT;
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
2013-06-23 08:05:25 +02:00
|
|
|
BOOST_CHECK(IsStandardTx(txFrom, reason));
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txTo[4]; // Spending transactions
|
2012-01-05 03:40:52 +01:00
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
txTo[i].vin.resize(1);
|
|
|
|
txTo[i].vout.resize(1);
|
|
|
|
txTo[i].vin[0].prevout.n = i;
|
|
|
|
txTo[i].vin[0].prevout.hash = txFrom.GetHash();
|
2013-04-25 00:27:00 +02:00
|
|
|
txTo[i].vout[0].nValue = 1*CENT;
|
2012-01-05 03:40:52 +01:00
|
|
|
txTo[i].vout[0].scriptPubKey = inner[i];
|
|
|
|
BOOST_CHECK_MESSAGE(IsMine(keystore, txFrom.vout[i].scriptPubKey), strprintf("IsMine %d", i));
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
2016-03-31 14:54:58 +02:00
|
|
|
BOOST_CHECK_MESSAGE(SignSignature(keystore, txFrom, txTo[i], 0, SIGHASH_ALL), strprintf("SignSignature %d", i));
|
2013-06-23 08:05:25 +02:00
|
|
|
BOOST_CHECK_MESSAGE(IsStandardTx(txTo[i], reason), strprintf("txTo[%d].IsStandard", i));
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(is)
|
|
|
|
{
|
|
|
|
// Test CScript::IsPayToScriptHash()
|
2014-12-15 09:11:16 +01:00
|
|
|
uint160 dummy;
|
2012-01-05 03:40:52 +01:00
|
|
|
CScript p2sh;
|
2014-09-25 04:54:08 +02:00
|
|
|
p2sh << OP_HASH160 << ToByteVector(dummy) << OP_EQUAL;
|
2012-01-05 03:40:52 +01:00
|
|
|
BOOST_CHECK(p2sh.IsPayToScriptHash());
|
|
|
|
|
|
|
|
// Not considered pay-to-script-hash if using one of the OP_PUSHDATA opcodes:
|
|
|
|
static const unsigned char direct[] = { OP_HASH160, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
|
|
|
|
BOOST_CHECK(CScript(direct, direct+sizeof(direct)).IsPayToScriptHash());
|
|
|
|
static const unsigned char pushdata1[] = { OP_HASH160, OP_PUSHDATA1, 20, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
|
|
|
|
BOOST_CHECK(!CScript(pushdata1, pushdata1+sizeof(pushdata1)).IsPayToScriptHash());
|
|
|
|
static const unsigned char pushdata2[] = { OP_HASH160, OP_PUSHDATA2, 20,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
|
|
|
|
BOOST_CHECK(!CScript(pushdata2, pushdata2+sizeof(pushdata2)).IsPayToScriptHash());
|
|
|
|
static const unsigned char pushdata4[] = { OP_HASH160, OP_PUSHDATA4, 20,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, OP_EQUAL };
|
|
|
|
BOOST_CHECK(!CScript(pushdata4, pushdata4+sizeof(pushdata4)).IsPayToScriptHash());
|
|
|
|
|
|
|
|
CScript not_p2sh;
|
|
|
|
BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
|
|
|
|
|
2014-09-25 04:54:08 +02:00
|
|
|
not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << ToByteVector(dummy) << OP_EQUAL;
|
2012-01-05 03:40:52 +01:00
|
|
|
BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
|
|
|
|
|
2014-09-25 04:54:08 +02:00
|
|
|
not_p2sh.clear(); not_p2sh << OP_NOP << ToByteVector(dummy) << OP_EQUAL;
|
2012-01-05 03:40:52 +01:00
|
|
|
BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
|
|
|
|
|
2014-09-25 04:54:08 +02:00
|
|
|
not_p2sh.clear(); not_p2sh << OP_HASH160 << ToByteVector(dummy) << OP_CHECKSIG;
|
2012-01-05 03:40:52 +01:00
|
|
|
BOOST_CHECK(!not_p2sh.IsPayToScriptHash());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(switchover)
|
|
|
|
{
|
2012-07-26 02:48:39 +02:00
|
|
|
// Test switch over code
|
2012-01-05 03:40:52 +01:00
|
|
|
CScript notValid;
|
2014-11-13 20:27:38 +01:00
|
|
|
ScriptError err;
|
2012-01-05 03:40:52 +01:00
|
|
|
notValid << OP_11 << OP_12 << OP_EQUALVERIFY;
|
|
|
|
CScript scriptSig;
|
|
|
|
scriptSig << Serialize(notValid);
|
|
|
|
|
2014-09-25 04:24:46 +02:00
|
|
|
CScript fund = GetScriptForDestination(CScriptID(notValid));
|
2012-01-05 03:40:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
// Validation should succeed under old rules (hash is correct):
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK(Verify(scriptSig, fund, false, err));
|
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_OK, ScriptErrorString(err));
|
2012-01-05 03:40:52 +01:00
|
|
|
// Fail under new:
|
2014-11-13 20:27:38 +01:00
|
|
|
BOOST_CHECK(!Verify(scriptSig, fund, true, err));
|
|
|
|
BOOST_CHECK_MESSAGE(err == SCRIPT_ERR_EQUALVERIFY, ScriptErrorString(err));
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(AreInputsStandard)
|
|
|
|
{
|
2014-04-23 08:05:05 +02:00
|
|
|
LOCK(cs_main);
|
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
|
|
|
CCoinsView coinsDummy;
|
2014-09-24 03:19:04 +02:00
|
|
|
CCoinsViewCache coins(&coinsDummy);
|
2012-01-05 03:40:52 +01:00
|
|
|
CBasicKeyStore keystore;
|
2014-06-17 20:18:13 +02:00
|
|
|
CKey key[6];
|
2017-01-05 11:31:56 +01:00
|
|
|
std::vector<CPubKey> keys;
|
2014-06-17 20:18:13 +02:00
|
|
|
for (int i = 0; i < 6; i++)
|
2012-01-05 03:40:52 +01:00
|
|
|
{
|
2012-02-20 18:32:33 +01:00
|
|
|
key[i].MakeNewKey(true);
|
2012-01-05 03:40:52 +01:00
|
|
|
keystore.AddKey(key[i]);
|
|
|
|
}
|
2014-06-17 20:18:13 +02:00
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
keys.push_back(key[i].GetPubKey());
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txFrom;
|
2014-06-17 20:18:13 +02:00
|
|
|
txFrom.vout.resize(7);
|
2012-01-05 03:40:52 +01:00
|
|
|
|
|
|
|
// First three are standard:
|
2014-09-11 19:15:29 +02:00
|
|
|
CScript pay1 = GetScriptForDestination(key[0].GetPubKey().GetID());
|
2012-01-05 03:40:52 +01:00
|
|
|
keystore.AddCScript(pay1);
|
2014-09-11 19:15:29 +02:00
|
|
|
CScript pay1of3 = GetScriptForMultisig(1, keys);
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-09-25 04:24:46 +02:00
|
|
|
txFrom.vout[0].scriptPubKey = GetScriptForDestination(CScriptID(pay1)); // P2SH (OP_CHECKSIG)
|
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
|
|
|
txFrom.vout[0].nValue = 1000;
|
2014-06-17 20:18:13 +02:00
|
|
|
txFrom.vout[1].scriptPubKey = pay1; // ordinary OP_CHECKSIG
|
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
|
|
|
txFrom.vout[1].nValue = 2000;
|
2014-06-17 20:18:13 +02:00
|
|
|
txFrom.vout[2].scriptPubKey = pay1of3; // ordinary OP_CHECKMULTISIG
|
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
|
|
|
txFrom.vout[2].nValue = 3000;
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-06-17 20:18:13 +02:00
|
|
|
// vout[3] is complicated 1-of-3 AND 2-of-3
|
|
|
|
// ... that is OK if wrapped in P2SH:
|
|
|
|
CScript oneAndTwo;
|
2014-09-25 04:54:08 +02:00
|
|
|
oneAndTwo << OP_1 << ToByteVector(key[0].GetPubKey()) << ToByteVector(key[1].GetPubKey()) << ToByteVector(key[2].GetPubKey());
|
2014-06-17 20:18:13 +02:00
|
|
|
oneAndTwo << OP_3 << OP_CHECKMULTISIGVERIFY;
|
2014-09-25 04:54:08 +02:00
|
|
|
oneAndTwo << OP_2 << ToByteVector(key[3].GetPubKey()) << ToByteVector(key[4].GetPubKey()) << ToByteVector(key[5].GetPubKey());
|
2014-06-17 20:18:13 +02:00
|
|
|
oneAndTwo << OP_3 << OP_CHECKMULTISIG;
|
|
|
|
keystore.AddCScript(oneAndTwo);
|
2014-09-25 04:24:46 +02:00
|
|
|
txFrom.vout[3].scriptPubKey = GetScriptForDestination(CScriptID(oneAndTwo));
|
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
|
|
|
txFrom.vout[3].nValue = 4000;
|
2014-06-17 20:18:13 +02:00
|
|
|
|
|
|
|
// vout[4] is max sigops:
|
|
|
|
CScript fifteenSigops; fifteenSigops << OP_1;
|
2014-06-30 16:37:59 +02:00
|
|
|
for (unsigned i = 0; i < MAX_P2SH_SIGOPS; i++)
|
2014-09-25 04:54:08 +02:00
|
|
|
fifteenSigops << ToByteVector(key[i%3].GetPubKey());
|
2014-06-17 20:18:13 +02:00
|
|
|
fifteenSigops << OP_15 << OP_CHECKMULTISIG;
|
|
|
|
keystore.AddCScript(fifteenSigops);
|
2014-09-25 04:24:46 +02:00
|
|
|
txFrom.vout[4].scriptPubKey = GetScriptForDestination(CScriptID(fifteenSigops));
|
Ultraprune
This switches bitcoin's transaction/block verification logic to use a
"coin database", which contains all unredeemed transaction output scripts,
amounts and heights.
The name ultraprune comes from the fact that instead of a full transaction
index, we only (need to) keep an index with unspent outputs. For now, the
blocks themselves are kept as usual, although they are only necessary for
serving, rescanning and reorganizing.
The basic datastructures are CCoins (representing the coins of a single
transaction), and CCoinsView (representing a state of the coins database).
There are several implementations for CCoinsView. A dummy, one backed by
the coins database (coins.dat), one backed by the memory pool, and one
that adds a cache on top of it. FetchInputs, ConnectInputs, ConnectBlock,
DisconnectBlock, ... now operate on a generic CCoinsView.
The block switching logic now builds a single cached CCoinsView with
changes to be committed to the database before any changes are made.
This means no uncommitted changes are ever read from the database, and
should ease the transition to another database layer which does not
support transactions (but does support atomic writes), like LevelDB.
For the getrawtransaction() RPC call, access to a txid-to-disk index
would be preferable. As this index is not necessary or even useful
for any other part of the implementation, it is not provided. Instead,
getrawtransaction() uses the coin database to find the block height,
and then scans that block to find the requested transaction. This is
slow, but should suffice for debug purposes.
2012-07-01 18:54:00 +02:00
|
|
|
txFrom.vout[4].nValue = 5000;
|
2014-06-17 20:18:13 +02:00
|
|
|
|
|
|
|
// vout[5/6] are non-standard because they exceed MAX_P2SH_SIGOPS
|
|
|
|
CScript sixteenSigops; sixteenSigops << OP_16 << OP_CHECKMULTISIG;
|
|
|
|
keystore.AddCScript(sixteenSigops);
|
2014-09-25 04:24:46 +02:00
|
|
|
txFrom.vout[5].scriptPubKey = GetScriptForDestination(CScriptID(fifteenSigops));
|
2014-06-17 20:18:13 +02:00
|
|
|
txFrom.vout[5].nValue = 5000;
|
|
|
|
CScript twentySigops; twentySigops << OP_CHECKMULTISIG;
|
|
|
|
keystore.AddCScript(twentySigops);
|
2014-09-25 04:24:46 +02:00
|
|
|
txFrom.vout[6].scriptPubKey = GetScriptForDestination(CScriptID(twentySigops));
|
2014-06-17 20:18:13 +02:00
|
|
|
txFrom.vout[6].nValue = 6000;
|
|
|
|
|
2017-06-02 00:47:58 +02:00
|
|
|
AddCoins(coins, txFrom, 0);
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-06-07 13:53:27 +02:00
|
|
|
CMutableTransaction txTo;
|
2012-01-05 03:40:52 +01:00
|
|
|
txTo.vout.resize(1);
|
2014-09-11 19:15:29 +02:00
|
|
|
txTo.vout[0].scriptPubKey = GetScriptForDestination(key[1].GetPubKey().GetID());
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-06-17 20:18:13 +02:00
|
|
|
txTo.vin.resize(5);
|
|
|
|
for (int i = 0; i < 5; i++)
|
|
|
|
{
|
|
|
|
txTo.vin[i].prevout.n = i;
|
|
|
|
txTo.vin[i].prevout.hash = txFrom.GetHash();
|
|
|
|
}
|
2016-03-31 14:54:58 +02:00
|
|
|
BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 0, SIGHASH_ALL));
|
|
|
|
BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 1, SIGHASH_ALL));
|
|
|
|
BOOST_CHECK(SignSignature(keystore, txFrom, txTo, 2, SIGHASH_ALL));
|
2014-06-17 20:18:13 +02:00
|
|
|
// SignSignature doesn't know how to sign these. We're
|
|
|
|
// not testing validating signatures, so just create
|
|
|
|
// dummy signatures that DO include the correct P2SH scripts:
|
2017-01-05 11:31:56 +01:00
|
|
|
txTo.vin[3].scriptSig << OP_11 << OP_11 << std::vector<unsigned char>(oneAndTwo.begin(), oneAndTwo.end());
|
|
|
|
txTo.vin[4].scriptSig << std::vector<unsigned char>(fifteenSigops.begin(), fifteenSigops.end());
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2013-01-08 13:17:15 +01:00
|
|
|
BOOST_CHECK(::AreInputsStandard(txTo, coins));
|
2014-06-17 20:18:13 +02:00
|
|
|
// 22 P2SH sigops for all inputs (1 for vin[0], 6 for vin[3], 15 for vin[4]
|
|
|
|
BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txTo, coins), 22U);
|
2012-01-05 03:40:52 +01:00
|
|
|
|
2014-06-17 20:18:13 +02:00
|
|
|
CMutableTransaction txToNonStd1;
|
|
|
|
txToNonStd1.vout.resize(1);
|
2014-09-11 19:15:29 +02:00
|
|
|
txToNonStd1.vout[0].scriptPubKey = GetScriptForDestination(key[1].GetPubKey().GetID());
|
2014-06-17 20:18:13 +02:00
|
|
|
txToNonStd1.vout[0].nValue = 1000;
|
|
|
|
txToNonStd1.vin.resize(1);
|
|
|
|
txToNonStd1.vin[0].prevout.n = 5;
|
|
|
|
txToNonStd1.vin[0].prevout.hash = txFrom.GetHash();
|
2017-01-05 11:31:56 +01:00
|
|
|
txToNonStd1.vin[0].scriptSig << std::vector<unsigned char>(sixteenSigops.begin(), sixteenSigops.end());
|
2014-06-17 20:18:13 +02:00
|
|
|
|
|
|
|
BOOST_CHECK(!::AreInputsStandard(txToNonStd1, coins));
|
|
|
|
BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txToNonStd1, coins), 16U);
|
|
|
|
|
|
|
|
CMutableTransaction txToNonStd2;
|
|
|
|
txToNonStd2.vout.resize(1);
|
2014-09-11 19:15:29 +02:00
|
|
|
txToNonStd2.vout[0].scriptPubKey = GetScriptForDestination(key[1].GetPubKey().GetID());
|
2014-06-17 20:18:13 +02:00
|
|
|
txToNonStd2.vout[0].nValue = 1000;
|
|
|
|
txToNonStd2.vin.resize(1);
|
|
|
|
txToNonStd2.vin[0].prevout.n = 6;
|
|
|
|
txToNonStd2.vin[0].prevout.hash = txFrom.GetHash();
|
2017-01-05 11:31:56 +01:00
|
|
|
txToNonStd2.vin[0].scriptSig << std::vector<unsigned char>(twentySigops.begin(), twentySigops.end());
|
2014-06-17 20:18:13 +02:00
|
|
|
|
|
|
|
BOOST_CHECK(!::AreInputsStandard(txToNonStd2, coins));
|
|
|
|
BOOST_CHECK_EQUAL(GetP2SHSigOpCount(txToNonStd2, coins), 20U);
|
2012-01-05 03:40:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|