Skip to content

Commit 417b6c1

Browse files
author
Thomas Kerin
committed
bitcoinconsensus: invalid flags should be set to bitcoinconsensus_error type, add test cases covering bitcoinconsensus error codes
1 parent 2ea7eb6 commit 417b6c1

File tree

2 files changed

+143
-1
lines changed

2 files changed

+143
-1
lines changed

src/script/bitcoinconsensus.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptP
8181
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
8282
{
8383
if (!verify_flags(flags)) {
84-
return bitcoinconsensus_ERR_INVALID_FLAGS;
84+
return set_error(err, bitcoinconsensus_ERR_INVALID_FLAGS);
8585
}
8686
try {
8787
TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);

src/test/script_tests.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,4 +1495,146 @@ BOOST_AUTO_TEST_CASE(script_can_append_self)
14951495
BOOST_CHECK(s == d);
14961496
}
14971497

1498+
1499+
#if defined(HAVE_CONSENSUS_LIB)
1500+
1501+
/* Test simple (successful) usage of bitcoinconsensus_verify_script */
1502+
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_returns_true)
1503+
{
1504+
unsigned int libconsensus_flags = 0;
1505+
int nIn = 0;
1506+
1507+
CScript scriptPubKey;
1508+
CScript scriptSig;
1509+
CScriptWitness wit;
1510+
1511+
scriptPubKey << OP_1;
1512+
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
1513+
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
1514+
1515+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
1516+
stream << spendTx;
1517+
1518+
bitcoinconsensus_error err;
1519+
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), nIn, libconsensus_flags, &err);
1520+
BOOST_CHECK_EQUAL(result, 1);
1521+
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_OK);
1522+
}
1523+
1524+
/* Test bitcoinconsensus_verify_script returns invalid tx index err*/
1525+
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_index_err)
1526+
{
1527+
unsigned int libconsensus_flags = 0;
1528+
int nIn = 3;
1529+
1530+
CScript scriptPubKey;
1531+
CScript scriptSig;
1532+
CScriptWitness wit;
1533+
1534+
scriptPubKey << OP_EQUAL;
1535+
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
1536+
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
1537+
1538+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
1539+
stream << spendTx;
1540+
1541+
bitcoinconsensus_error err;
1542+
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), nIn, libconsensus_flags, &err);
1543+
BOOST_CHECK_EQUAL(result, 0);
1544+
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_TX_INDEX);
1545+
}
1546+
1547+
/* Test bitcoinconsensus_verify_script returns tx size mismatch err*/
1548+
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_size)
1549+
{
1550+
unsigned int libconsensus_flags = 0;
1551+
int nIn = 0;
1552+
1553+
CScript scriptPubKey;
1554+
CScript scriptSig;
1555+
CScriptWitness wit;
1556+
1557+
scriptPubKey << OP_EQUAL;
1558+
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
1559+
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
1560+
1561+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
1562+
stream << spendTx;
1563+
1564+
bitcoinconsensus_error err;
1565+
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size() * 2, nIn, libconsensus_flags, &err);
1566+
BOOST_CHECK_EQUAL(result, 0);
1567+
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
1568+
}
1569+
1570+
/* Test bitcoinconsensus_verify_script returns invalid tx serialization error */
1571+
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_tx_serialization)
1572+
{
1573+
unsigned int libconsensus_flags = 0;
1574+
int nIn = 0;
1575+
1576+
CScript scriptPubKey;
1577+
CScript scriptSig;
1578+
CScriptWitness wit;
1579+
1580+
scriptPubKey << OP_EQUAL;
1581+
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
1582+
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
1583+
1584+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
1585+
stream << 0xffffffff;
1586+
1587+
bitcoinconsensus_error err;
1588+
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), nIn, libconsensus_flags, &err);
1589+
BOOST_CHECK_EQUAL(result, 0);
1590+
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_TX_DESERIALIZE);
1591+
}
1592+
1593+
/* Test bitcoinconsensus_verify_script returns amount required error */
1594+
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_amount_required_err)
1595+
{
1596+
unsigned int libconsensus_flags = bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS;
1597+
int nIn = 0;
1598+
1599+
CScript scriptPubKey;
1600+
CScript scriptSig;
1601+
CScriptWitness wit;
1602+
1603+
scriptPubKey << OP_EQUAL;
1604+
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
1605+
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
1606+
1607+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
1608+
stream << spendTx;
1609+
1610+
bitcoinconsensus_error err;
1611+
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), nIn, libconsensus_flags, &err);
1612+
BOOST_CHECK_EQUAL(result, 0);
1613+
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
1614+
}
1615+
1616+
/* Test bitcoinconsensus_verify_script returns invalid flags err */
1617+
BOOST_AUTO_TEST_CASE(bitcoinconsensus_verify_script_invalid_flags)
1618+
{
1619+
unsigned int libconsensus_flags = 1 << 3;
1620+
int nIn = 0;
1621+
1622+
CScript scriptPubKey;
1623+
CScript scriptSig;
1624+
CScriptWitness wit;
1625+
1626+
scriptPubKey << OP_EQUAL;
1627+
CTransaction creditTx = BuildCreditingTransaction(scriptPubKey, 1);
1628+
CTransaction spendTx = BuildSpendingTransaction(scriptSig, wit, creditTx);
1629+
1630+
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
1631+
stream << spendTx;
1632+
1633+
bitcoinconsensus_error err;
1634+
int result = bitcoinconsensus_verify_script(scriptPubKey.data(), scriptPubKey.size(), (const unsigned char*)&stream[0], stream.size(), nIn, libconsensus_flags, &err);
1635+
BOOST_CHECK_EQUAL(result, 0);
1636+
BOOST_CHECK_EQUAL(err, bitcoinconsensus_ERR_INVALID_FLAGS);
1637+
}
1638+
1639+
#endif
14981640
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)