Browse Source

Merge #13797: [0.16] bitcoinconsensus: invalid flags should be set to bitcoinconsensus_error type, add test cases covering bitcoinconsensus error codes

1cdbea7f74 bitcoinconsensus: invalid flags should be set to bitcoinconsensus_error type, add test cases covering bitcoinconsensus error codes (Thomas Kerin)

Pull request description:

  Backport of #13655 to 0.16

Tree-SHA512: b62e185f2aa957f09255090e59f96c039f47a5623d68b6fef8d1dd831c6d3135b039be5cfad0f823687ff2a5143d24e34bd83fefcc9ba5b68f43054cbd9d909d
0.16
Wladimir J. van der Laan 6 years ago
parent
commit
6518bcd56c
No known key found for this signature in database
GPG Key ID: 1E4AED62986CD25D
  1. 2
      src/script/bitcoinconsensus.cpp
  2. 142
      src/test/script_tests.cpp

2
src/script/bitcoinconsensus.cpp

@ -81,7 +81,7 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err) unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
{ {
if (!verify_flags(flags)) { if (!verify_flags(flags)) {
return bitcoinconsensus_ERR_INVALID_FLAGS; return set_error(err, bitcoinconsensus_ERR_INVALID_FLAGS);
} }
try { try {
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen); TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);

142
src/test/script_tests.cpp

@ -1481,4 +1481,146 @@ BOOST_AUTO_TEST_CASE(script_can_append_self)
BOOST_CHECK(s == d); BOOST_CHECK(s == d);
} }
#if defined(HAVE_CONSENSUS_LIB)
/* Test simple (successful) usage of bitcoinconsensus_verify_script */
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_returns_true)
{
unsigned int libconsensus_flags = 0;
int nIn = 0;
CScript scriptPubKey;
CScript scriptSig;
CScriptWitness wit;
scriptPubKey << OP_1;
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << spendTx;
bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), nIn, libconsensus_flags, &err);
BOOST_CHECK_EQUAL(result, 1);
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_OK);
}
/* Test bitcoinconsensus_verify_script returns invalid tx index err*/
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_index_err)
{
unsigned int libconsensus_flags = 0;
int nIn = 3;
CScript scriptPubKey;
CScript scriptSig;
CScriptWitness wit;
scriptPubKey << OP_EQUAL;
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << spendTx;
bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), nIn, libconsensus_flags, &err);
BOOST_CHECK_EQUAL(result, 0);
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_TX_INDEX);
}
/* Test bitcoinconsensus_verify_script returns tx size mismatch err*/
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_size)
{
unsigned int libconsensus_flags = 0;
int nIn = 0;
CScript scriptPubKey;
CScript scriptSig;
CScriptWitness wit;
scriptPubKey << OP_EQUAL;
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << spendTx;
bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size() * 2, nIn, libconsensus_flags, &err);
BOOST_CHECK_EQUAL(result, 0);
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
}
/* Test bitcoinconsensus_verify_script returns invalid tx serialization error */
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_serialization)
{
unsigned int libconsensus_flags = 0;
int nIn = 0;
CScript scriptPubKey;
CScript scriptSig;
CScriptWitness wit;
scriptPubKey << OP_EQUAL;
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << 0xffffffff;
bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), nIn, libconsensus_flags, &err);
BOOST_CHECK_EQUAL(result, 0);
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_TX_DESERIALIZE);
}
/* Test bitcoinconsensus_verify_script returns amount required error */
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_amount_required_err)
{
unsigned int libconsensus_flags = bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS;
int nIn = 0;
CScript scriptPubKey;
CScript scriptSig;
CScriptWitness wit;
scriptPubKey << OP_EQUAL;
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << spendTx;
bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), nIn, libconsensus_flags, &err);
BOOST_CHECK_EQUAL(result, 0);
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
}
/* Test bitcoinconsensus_verify_script returns invalid flags err */
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_invalid_flags)
{
unsigned int libconsensus_flags = 1 << 3;
int nIn = 0;
CScript scriptPubKey;
CScript scriptSig;
CScriptWitness wit;
scriptPubKey << OP_EQUAL;
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
stream << spendTx;
bitcoinconsensus_error err;
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), nIn, libconsensus_flags, &err);
BOOST_CHECK_EQUAL(result, 0);
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_INVALID_FLAGS);
}
#endif
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

Loading…
Cancel
Save