Skip to content

Commit

Permalink
Add testcase to verify transaction compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ShengguangXiao committed Sep 18, 2020
1 parent 60195a2 commit 2b4c86d
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/test/txvalidation_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,54 @@ BOOST_FIXTURE_TEST_CASE(tx_check_transaction_size, TestChain100Setup)
tx.vout[0].scriptPubKey = scriptPubKey;

int size = ::GetSerializeSize(tx, PROTOCOL_VERSION | (tx.nVersion < CTransaction::TOKENS_MIN_VERSION ? SERIALIZE_TRANSACTION_NO_TOKENS : 0));
std::cout << "size = " << size << std::endl;
BOOST_CHECK(size == 97);

CTxOut::SERIALIZE_FORCED_TO_OLD_IN_TESTS = false;
tx.nVersion = 3;
tx.vout[0].nTokenId.v = 1;
size = ::GetSerializeSize(tx, PROTOCOL_VERSION | (tx.nVersion < CTransaction::TOKENS_MIN_VERSION ? SERIALIZE_TRANSACTION_NO_TOKENS : 0));
std::cout << "size = " << size << std::endl;
BOOST_CHECK(size == 98);

CTxOut::SERIALIZE_FORCED_TO_OLD_IN_TESTS = true;
}

BOOST_FIXTURE_TEST_CASE(tx_transaction_compatibility, TestChain100Setup)
{
CTxOut::SERIALIZE_FORCED_TO_OLD_IN_TESTS = false;

CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
CMutableTransaction tx;

tx.nVersion = 2;
tx.vin.resize(1);
tx.vout.resize(1);
tx.vin[0].scriptSig = CScript() << OP_11 << OP_EQUAL;
tx.vout[0].nValue = 1 * CENT;
tx.vout[0].scriptPubKey = scriptPubKey;

// Serialize the transaction and put to a buffer with SERIALIZE_TRANSACTION_NO_TOKENS flag,
// So the TokenId will not be serialized.
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_TOKENS);
stream << tx;
const auto txStr = stream.str();
CSerializeData txBinary(txStr.begin(), txStr.end());

// Use the DataStream without SERIALIZE_TRANSACTION_NO_TOKENS to load, it should pass
CDataStream loadStreamNew(txBinary.begin(), txBinary.end(), SER_NETWORK, PROTOCOL_VERSION);
CMutableTransaction txLoadNew;
loadStreamNew >> txLoadNew;

// Load fail, so hash not match
BOOST_CHECK(CTransaction(tx) == CTransaction(txLoadNew));

// Use the DataStream with SERIALIZE_TRANSACTION_NO_TOKENS to load, it must pass
CDataStream loadStreamGood(txBinary.begin(), txBinary.end(), SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_TOKENS);
CMutableTransaction txLoad;
loadStreamGood >> txLoad;

BOOST_CHECK(CTransaction(tx) == CTransaction(txLoad));

CTxOut::SERIALIZE_FORCED_TO_OLD_IN_TESTS = true;
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 2b4c86d

Please sign in to comment.