Skip to content

Commit

Permalink
test: Added test for RPC Endpoint compressrawtransaction and decompre…
Browse files Browse the repository at this point in the history
…ssrawtransaction
  • Loading branch information
TomBriar committed Dec 24, 2023
1 parent 3b8e0e0 commit 56428ba
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/test/rpc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ BOOST_AUTO_TEST_CASE(rpc_rawparams)
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("decoderawtransaction ")+rawtx+" false"));
BOOST_CHECK_THROW(r = CallRPC(std::string("decoderawtransaction ")+rawtx+" false extra"), std::runtime_error);

BOOST_CHECK_THROW(CallRPC("compressrawtransaction"), std::runtime_error);
BOOST_CHECK_THROW(CallRPC("compressrawtransaction null"), std::runtime_error);
BOOST_CHECK_THROW(CallRPC("compressrawtransaction DEADBEEF"), std::runtime_error);
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("compressrawtransaction ")+rawtx));
std::string compressedrawtx = "158efefefe7fc400a15d57094aa7a21a28cb20b59aab8fc7d1149a3bdbcddba9c622e4f5f6a99ece016e6c493046022100f93bb0e7d8db7bd46e40132d1f8242026e045f03a0efe71bbb8e3f475e970d790221009337cd7f1f929f00cc6ff01f03729b069a7c21b59b1736ddfee5db5946c5da8c0121033b9b137ee87d5a812d6f506efdd37f0affa7ffc310711c06c7f3e097c9447c52000389035a9225b3839e2bbf32d826a1e222031fd8aed6c100";
BOOST_CHECK_EQUAL(r.get_obj().find_value("result").get_str(), compressedrawtx);
BOOST_CHECK_THROW(CallRPC(std::string("compressrawtransaction ")+rawtx+" extra"), std::runtime_error);

BOOST_CHECK_THROW(CallRPC("decompressrawtransaction"), std::runtime_error);
BOOST_CHECK_THROW(CallRPC("decompressrawtransaction null"), std::runtime_error);
BOOST_CHECK_THROW(CallRPC("decompressrawtransaction DEADBEEF"), std::runtime_error);
BOOST_CHECK_NO_THROW(r = CallRPC(std::string("decompressrawtransaction ")+compressedrawtx));
BOOST_CHECK_EQUAL(r.get_str(), rawtx);
BOOST_CHECK_THROW(CallRPC(std::string("decompressrawtransaction ")+compressedrawtx+" extra"), std::runtime_error);

// Only check failure cases for sendrawtransaction, there's no network to send to...
BOOST_CHECK_THROW(CallRPC("sendrawtransaction"), std::runtime_error);
BOOST_CHECK_THROW(CallRPC("sendrawtransaction null"), std::runtime_error);
Expand Down
14 changes: 14 additions & 0 deletions src/test/script_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#include <script/bitcoinconsensus.h>
#endif


#include <iostream>
#include <primitives/compression.h>

#include <cstdint>
#include <fstream>
#include <string>
Expand Down Expand Up @@ -1713,6 +1717,16 @@ static void AssetTest(const UniValue& test)
mtx.vin[idx].scriptSig = ScriptFromHex(test["success"]["scriptSig"].get_str());
mtx.vin[idx].scriptWitness = ScriptWitnessFromJSON(test["success"]["witness"]);
CTransaction tx(mtx);

//Compression Roundtrip
std::vector<CCompressedInput> cinputs;
std::vector<COutPoint> outpoints;
for (size_t index = 0; index < tx.vin.size(); index++) {
outpoints.push_back(tx.vin[index].prevout);
cinputs.push_back(CCompressedInput{CCompressedOutPoint(tx.vin[index].prevout.hash, tx.vin[index].prevout.n), prevouts[index].scriptPubKey});
}
BOOST_CHECK(tx == CTransaction(CMutableTransaction(CCompressedTransaction(tx, 0, cinputs), outpoints, prevouts)));

PrecomputedTransactionData txdata;
txdata.Init(tx, std::vector<CTxOut>(prevouts));
CachingTransactionSignatureChecker txcheck(&tx, idx, prevouts[idx].nValue, true, txdata);
Expand Down
9 changes: 9 additions & 0 deletions test/functional/rpc_rawtransaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Test the following RPCs:
- getrawtransaction
- compressrawtransaction
- createrawtransaction
- signrawtransactionwithwallet
- sendrawtransaction
Expand Down Expand Up @@ -86,6 +87,7 @@ def run_test(self):
self.wallet = MiniWallet(self.nodes[0])

self.getrawtransaction_tests()
self.compressrawtransaction_tests()
self.createrawtransaction_tests()
self.sendrawtransaction_tests()
self.sendrawtransaction_testmempoolaccept_tests()
Expand Down Expand Up @@ -460,6 +462,13 @@ def decoderawtransaction_tests(self):
assert_equal(decrawtx, decrawtx_wit) # the witness interpretation should be chosen
assert_equal(decrawtx['vin'][0]['coinbase'], coinbase)

def compressrawtransaction_tests(self):
self.log.info("Test (de)compressrawtransaction")
tx = self.wallet.send_self_transfer(from_node=self.nodes[0])
ctx = self.nodes[0].compressrawtransaction(tx["hex"])
utx = self.nodes[0].decompressrawtransaction(ctx["result"])
assert_equal(tx["hex"], utx)

def transaction_version_number_tests(self):
self.log.info("Test transaction version numbers")

Expand Down

0 comments on commit 56428ba

Please sign in to comment.