added unit test for signing claims

This commit is contained in:
Brannon King 2019-08-30 13:54:19 -06:00 committed by Anthony Fieroni
parent b908bf48a1
commit ea032a2ad7
3 changed files with 89 additions and 6 deletions

View file

@ -28,18 +28,22 @@ uint32_t vch_to_uint32_t(std::vector<unsigned char>& vchN)
return n; return n;
} }
CScript ClaimNameScript(std::string name, std::string value) CScript ClaimNameScript(std::string name, std::string value, bool fakeSuffix)
{ {
std::vector<unsigned char> vchName(name.begin(), name.end()); std::vector<unsigned char> vchName(name.begin(), name.end());
std::vector<unsigned char> vchValue(value.begin(), value.end()); std::vector<unsigned char> vchValue(value.begin(), value.end());
return CScript() << OP_CLAIM_NAME << vchName << vchValue << OP_2DROP << OP_DROP << OP_TRUE; auto ret = CScript() << OP_CLAIM_NAME << vchName << vchValue << OP_2DROP << OP_DROP;
if (fakeSuffix) ret.push_back(OP_TRUE);
return ret;
} }
CScript SupportClaimScript(std::string name, uint160 claimId) CScript SupportClaimScript(std::string name, uint160 claimId, bool fakeSuffix)
{ {
std::vector<unsigned char> vchName(name.begin(), name.end()); std::vector<unsigned char> vchName(name.begin(), name.end());
std::vector<unsigned char> vchClaimId(claimId.begin(), claimId.end()); std::vector<unsigned char> vchClaimId(claimId.begin(), claimId.end());
return CScript() << OP_SUPPORT_CLAIM << vchName << vchClaimId << OP_2DROP << OP_DROP << OP_TRUE; auto ret = CScript() << OP_SUPPORT_CLAIM << vchName << vchClaimId << OP_2DROP << OP_DROP;
if (fakeSuffix) ret.push_back(OP_TRUE);
return ret;
} }
CScript UpdateClaimScript(std::string name, uint160 claimId, std::string value) CScript UpdateClaimScript(std::string name, uint160 claimId, std::string value)

View file

@ -25,8 +25,8 @@
// Scripts exceeding this size are rejected in CheckTransaction in main.cpp // Scripts exceeding this size are rejected in CheckTransaction in main.cpp
#define MAX_CLAIM_NAME_SIZE 255 #define MAX_CLAIM_NAME_SIZE 255
CScript ClaimNameScript(std::string name, std::string value); CScript ClaimNameScript(std::string name, std::string value, bool fakeSuffix=true);
CScript SupportClaimScript(std::string name, uint160 claimId); CScript SupportClaimScript(std::string name, uint160 claimId, bool fakeSuffix=true);
CScript UpdateClaimScript(std::string name, uint160 claimId, std::string value); CScript UpdateClaimScript(std::string name, uint160 claimId, std::string value);
bool DecodeClaimScript(const CScript& scriptIn, int& op, std::vector<std::vector<unsigned char> >& vvchParams); bool DecodeClaimScript(const CScript& scriptIn, int& op, std::vector<std::vector<unsigned char> >& vvchParams);
bool DecodeClaimScript(const CScript& scriptIn, int& op, std::vector<std::vector<unsigned char> >& vvchParams, CScript::const_iterator& pc); bool DecodeClaimScript(const CScript& scriptIn, int& op, std::vector<std::vector<unsigned char> >& vvchParams, CScript::const_iterator& pc);

View file

@ -7,6 +7,8 @@
#include <wallet/wallet.h> #include <wallet/wallet.h>
#include <consensus/validation.h> #include <consensus/validation.h>
#include <core_io.h>
#include <key_io.h>
#include <rpc/server.h> #include <rpc/server.h>
#include <test/test_bitcoin.h> #include <test/test_bitcoin.h>
#include <validation.h> #include <validation.h>
@ -275,4 +277,81 @@ BOOST_AUTO_TEST_CASE(claim_op_runthrough_bech32)
AddClaimSupportThenRemove(); AddClaimSupportThenRemove();
} }
std::vector<COutPoint> SpendableCoins() {
rpcfn_type rpc_method = tableRPC["listunspent"]->actor;
JSONRPCRequest req;
req.URI = "/wallet/tester_wallet";
req.params = UniValue(UniValue::VARR);
UniValue unspents = rpc_method(req);
std::vector<COutPoint> ret;
for (uint32_t i = 0; i < unspents.size(); ++i)
ret.emplace_back(uint256S(unspents[i]["txid"].get_str()), unspents[i]["vout"].get_int());
return ret;
}
CTxDestination NewAddress(OutputType t) {
rpcfn_type rpc_method = tableRPC["getnewaddress"]->actor;
JSONRPCRequest req;
req.URI = "/wallet/tester_wallet";
req.params = UniValue(UniValue::VARR);
req.params.push_back("");
req.params.push_back(FormatOutputType(t));
UniValue address = rpc_method(req);
return DecodeDestination(address.get_str());
}
std::string SignRawTransaction(const std::string& tx) {
rpcfn_type rpc_method = tableRPC["signrawtransactionwithwallet"]->actor;
JSONRPCRequest req;
req.URI = "/wallet/tester_wallet";
req.params = UniValue(UniValue::VARR);
req.params.push_back(tx);
UniValue results = rpc_method(req);
BOOST_CHECK(results["complete"].get_bool());
BOOST_CHECK(!results.exists("errors"));
return results["hex"].get_str();
}
std::string SendRawTransaction(const std::string& tx) {
rpcfn_type rpc_method = tableRPC["sendrawtransaction"]->actor;
JSONRPCRequest req;
req.URI = "/wallet/tester_wallet";
req.params = UniValue(UniValue::VARR);
req.params.push_back(tx);
req.params.push_back(UniValue(true)); // allow high fees
UniValue results = rpc_method(req);
return results.get_str();
}
BOOST_AUTO_TEST_CASE(can_sign_all_addr)
{
generateBlock(110);
BOOST_CHECK_EQUAL(AvailableBalance(), 10.0);
auto coins = SpendableCoins();
std::vector<std::string> txids;
for (std::size_t i = 0; i < 3; ++i) {
CMutableTransaction rawTx;
CTxIn in(coins[i], CScript(), 0);
rawTx.vin.push_back(in);
auto destination = NewAddress(OutputType(i));
CScript scriptPubKey = ClaimNameScript("name" + std::to_string(i), "value", false)
+ GetScriptForDestination(destination);
CTxOut out(100000, scriptPubKey);
rawTx.vout.push_back(out);
auto hex = EncodeHexTx(rawTx);
hex = SignRawTransaction(hex);
txids.push_back(SendRawTransaction(hex));
}
generateBlock(1);
auto looked = LookupAllNames().get_array();
for (std::size_t i = 0; i < 3; ++i) {
BOOST_CHECK_EQUAL(looked[i]["name"].get_str(), "name" + std::to_string(i));
BOOST_CHECK_EQUAL(looked[i]["value"].get_str(), HexStr(std::string("value")));
BOOST_CHECK_EQUAL(looked[i]["txid"].get_str(), txids[i]);
}
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()