diff --git a/configure.ac b/configure.ac index bc7912eba..1ed0e7660 100644 --- a/configure.ac +++ b/configure.ac @@ -2,7 +2,7 @@ dnl require autoconf 2.60 (AS_ECHO/AS_ECHO_N) AC_PREREQ([2.60]) define(_CLIENT_VERSION_MAJOR, 1) define(_CLIENT_VERSION_MINOR, 0) -define(_CLIENT_VERSION_REVISION, 24) +define(_CLIENT_VERSION_REVISION, 25) define(_CLIENT_VERSION_BUILD, 0) define(_CLIENT_VERSION_IS_RELEASE, true) define(_COPYRIGHT_YEAR, 2020) diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp index 953a201bf..92cb5b0ee 100644 --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -242,7 +242,7 @@ class WalletImpl : public Wallet { LOCK2(cs_main, m_wallet.cs_wallet); auto pending = MakeUnique(m_wallet); - if(!TPoSUtils::CreateTPoSTransaction(&m_wallet, pending->m_tx, pending->m_key, tpos_address, merchant_address, merchant_commission, true, fail_reason)) { + if(!TPoSUtils::CreateTPoSTransaction(&m_wallet, pending->m_tx, pending->m_key, tpos_address, merchant_address, merchant_commission, fail_reason)) { return {}; } return std::move(pending); diff --git a/src/qt/tpospage.cpp b/src/qt/tpospage.cpp index 4075b5db2..a3a797d6b 100644 --- a/src/qt/tpospage.cpp +++ b/src/qt/tpospage.cpp @@ -53,7 +53,7 @@ static QString PrepareCreateContractQuestionString(const CBitcoinAddress &tposAd } std::unique_ptr TPoSPage::CreateContractTransaction(QWidget *widget, - const CBitcoinAddress &tposAddress, + const CTxDestination &tposAddress, const CBitcoinAddress &merchantAddress, int merchantCommission) { @@ -68,7 +68,7 @@ std::unique_ptr TPoSPage::CreateContractTransaction { return {}; } - if(auto walletTx = _walletModel->wallet().createTPoSContractTransaction(tposAddress.Get(), merchantAddress.Get(), merchantCommission, strError)) { + if(auto walletTx = _walletModel->wallet().createTPoSContractTransaction(tposAddress, merchantAddress.Get(), merchantCommission, strError)) { return walletTx; } @@ -154,7 +154,7 @@ void TPoSPage::SendToAddress(const CTxDestination &address, CAmount nValue, int SendPendingTransaction(penWalletTx.get()); } -CBitcoinAddress TPoSPage::GetNewAddress() +CTxDestination TPoSPage::GetNewAddress() { // Generate a new key that is added to wallet CPubKey newKey; @@ -162,11 +162,10 @@ CBitcoinAddress TPoSPage::GetNewAddress() throw std::runtime_error("Error: Keypool ran out, please call keypoolrefill first"); CKeyID keyID = newKey.GetID(); - _walletModel->wallet().setAddressBook(keyID, std::string(), "tpos address"); //pwalletMain->SetAddressBook(keyID, std::string(), "tpos address"); - return CBitcoinAddress(keyID); + return GetDestinationForKey(newKey, OutputType::BECH32); } TPoSPage::TPoSPage(QWidget *parent) : @@ -208,11 +207,7 @@ void TPoSPage::onStakeClicked() { auto worker = [this]() { // CReserveKey reserveKey(pwalletMain); - CBitcoinAddress tposAddress = GetNewAddress(); - if(!tposAddress.IsValid()) - { - throw std::runtime_error("Critical error, TPoS address is empty"); - } + auto tposAddress = GetNewAddress(); CBitcoinAddress merchantAddress(ui->merchantAddress->text().toStdString()); if(!merchantAddress.IsValid()) { diff --git a/src/qt/tpospage.h b/src/qt/tpospage.h index d835cafa3..df78b6c7e 100644 --- a/src/qt/tpospage.h +++ b/src/qt/tpospage.h @@ -48,10 +48,10 @@ private Q_SLOTS: void onStakeError(); void SendToAddress(const CTxDestination &address, CAmount nValue, int splitCount); void sendToTPoSAddress(const CBitcoinAddress &tposAddress); - CBitcoinAddress GetNewAddress(); + CTxDestination GetNewAddress(); std::unique_ptr CreateContractTransaction(QWidget *widget, - const CBitcoinAddress &tposAddress, + const CTxDestination &tposAddress, const CBitcoinAddress &merchantAddress, int merchantCommission); diff --git a/src/rpc/merchantnode.cpp b/src/rpc/merchantnode.cpp index a000323b2..c8ac251c1 100644 --- a/src/rpc/merchantnode.cpp +++ b/src/rpc/merchantnode.cpp @@ -528,15 +528,11 @@ static UniValue tposcontract(const JSONRPCRequest& request) { if (request.params.size() < 4) throw JSONRPCError(RPC_INVALID_PARAMETER, - "Expected format: tposcontract create tpos_address merchant_address commission use_legacy_contract"); + "Expected format: tposcontract create tpos_address merchant_address commission"); CTxDestination tposAddress = DecodeDestination(request.params[1].get_str()); CTxDestination merchantAddress = DecodeDestination(request.params[2].get_str()); int commission = std::stoi(request.params[3].get_str()); - bool use_legacy_contract = true; - if(request.params.size() > 4) { - use_legacy_contract = std::stoi(request.params[4].get_str()) > 0; - } CReserveKey reserveKey(pwallet); @@ -545,7 +541,7 @@ static UniValue tposcontract(const JSONRPCRequest& request) if(TPoSUtils::CreateTPoSTransaction(pwallet, transaction, reserveKey, tposAddress, - merchantAddress, commission, use_legacy_contract, strError)) + merchantAddress, commission, strError)) { return EncodeHexTx(*transaction); } diff --git a/src/test/tpos_tests.cpp b/src/test/tpos_tests.cpp index 807d2fe8a..975e94499 100644 --- a/src/test/tpos_tests.cpp +++ b/src/test/tpos_tests.cpp @@ -102,11 +102,11 @@ static void SignTransaction(CMutableTransaction& tx, const CKey& coinbaseKey) } } -static CMutableTransaction CreateContractTx(const CTxDestination &tposAddress, const CTxDestination &merchantAddress, uint16_t nOperatorReward, bool legacyContract) +static CMutableTransaction CreateContractTx(const CTxDestination &tposAddress, const CTxDestination &merchantAddress, uint16_t nOperatorReward) { CMutableTransaction tx; std::string strError; - TPoSUtils::CreateTPoSTransaction(tx, tposAddress, merchantAddress, nOperatorReward, legacyContract, strError); + TPoSUtils::CreateTPoSTransaction(tx, tposAddress, merchantAddress, nOperatorReward, strError); return tx; } @@ -121,98 +121,6 @@ static void SignContract(CMutableTransaction &tx, const CKey &key, bool legacyCo BOOST_AUTO_TEST_SUITE(tpos_tests) -BOOST_FIXTURE_TEST_CASE(tpos_create_legacy_contract, TestChain100Setup) -{ - m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]); - m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]); - - CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG; - auto utxos = BuildSimpleUtxoMap(m_coinbase_txns); - - CKey tposAddressKey; - tposAddressKey.MakeNewKey(true); - CKey merchantAddressKey; - merchantAddressKey.MakeNewKey(true); - - auto unsignedContract = CreateContractTx(tposAddressKey.GetPubKey().GetID(), - merchantAddressKey.GetPubKey().GetID(), 1, true); - TPoSContract tmp; - std::string strError; - BOOST_ASSERT(TPoSUtils::CheckContract(MakeTransactionRef(unsignedContract), tmp, 1, false, false, strError)); - BOOST_ASSERT(!TPoSUtils::CheckContract(MakeTransactionRef(unsignedContract), tmp, 1, true, true, strError)); - - FundTransaction(unsignedContract, utxos, unsignedContract.vout[1].scriptPubKey); - SignContract(unsignedContract, tposAddressKey, true); - SignTransaction(unsignedContract, coinbaseKey); - BOOST_ASSERT(TPoSUtils::CheckContract(MakeTransactionRef(unsignedContract), tmp, 1, true, false, strError)); - BOOST_ASSERT(TPoSUtils::CheckContract(MakeTransactionRef(unsignedContract), tmp, Params().GetConsensus().nTPoSSignatureUpgradeHFHeight, true, false, strError)); - - auto contract = TPoSContract::FromTPoSContractTx(MakeTransactionRef(unsignedContract)); - BOOST_ASSERT(contract.IsValid()); - - CValidationState state; - LOCK(cs_main); - - BOOST_CHECK_EQUAL( - true, - AcceptToMemoryPool(mempool, state, contract.txContract, - nullptr /* pfMissingInputs */, - nullptr /* plTxnReplaced */, - true /* bypass_limits */, - 0 /* nAbsurdFee */)); -} - -BOOST_FIXTURE_TEST_CASE(tpos_create_invalid_legacy_contract, TestChain100Setup) -{ - m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]); - m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]); - m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]); - - auto utxos = BuildSimpleUtxoMap(m_coinbase_txns); - - CKey tposAddressKey; - tposAddressKey.MakeNewKey(true); - CKey merchantAddressKey; - merchantAddressKey.MakeNewKey(true); - - auto createContract = [&](int nOperatorReward) { - return CreateContractTx(tposAddressKey.GetPubKey().GetID(), - merchantAddressKey.GetPubKey().GetID(), nOperatorReward, true); - }; - - TPoSContract tmp; - std::string strError; - auto invalidContract = MakeTransactionRef(createContract(101)); - BOOST_ASSERT(!TPoSContract::FromTPoSContractTx(invalidContract).IsValid()); - BOOST_ASSERT(!TPoSUtils::CheckContract(invalidContract, tmp, 1, false, false, strError)); - - auto unsignedContract = createContract(1); - - FundTransaction(unsignedContract, utxos, unsignedContract.vout[1].scriptPubKey); - unsignedContract.vin[0].prevout = COutPoint(m_coinbase_txns[2]->GetHash(), 0); - SignContract(unsignedContract, tposAddressKey, true); - BOOST_ASSERT(TPoSUtils::CheckContract(MakeTransactionRef(unsignedContract), tmp, 1, true, false, strError)); - // replace input invalidating the signature - unsignedContract.vin[0].prevout = COutPoint(m_coinbase_txns[3]->GetHash(), 0); - BOOST_ASSERT(!TPoSUtils::CheckContract(MakeTransactionRef(unsignedContract), tmp, 1, true, false, strError)); - - SignTransaction(unsignedContract, coinbaseKey); - - auto contract = TPoSContract::FromTPoSContractTx(MakeTransactionRef(unsignedContract)); - BOOST_ASSERT(contract.IsValid()); - - CValidationState state; - LOCK(cs_main); - - BOOST_CHECK_EQUAL( - false, - AcceptToMemoryPool(mempool, state, contract.txContract, - nullptr /* pfMissingInputs */, - nullptr /* plTxnReplaced */, - true /* bypass_limits */, - 0 /* nAbsurdFee */)); -} - BOOST_FIXTURE_TEST_CASE(tpos_create_new_contract, TestChain100Setup) { m_coinbase_txns.emplace_back(CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())).vtx[0]); @@ -224,7 +132,7 @@ BOOST_FIXTURE_TEST_CASE(tpos_create_new_contract, TestChain100Setup) tposAddressKey.MakeNewKey(true); CKey merchantAddressKey; merchantAddressKey.MakeNewKey(true); - auto unsignedContract = CreateContractTx(WitnessV0KeyHash(tposAddressKey.GetPubKey().GetID()), WitnessV0KeyHash(merchantAddressKey.GetPubKey().GetID()), 1, false); + auto unsignedContract = CreateContractTx(WitnessV0KeyHash(tposAddressKey.GetPubKey().GetID()), WitnessV0KeyHash(merchantAddressKey.GetPubKey().GetID()), 1); TPoSContract tmp; std::string strError; BOOST_ASSERT(TPoSUtils::CheckContract(MakeTransactionRef(unsignedContract), tmp, 1, false, false, strError)); @@ -357,7 +265,7 @@ BOOST_FIXTURE_TEST_CASE(tpos_contract_adjust_mn_payment, TestChain100Setup) auto merchantAddress = WitnessV0KeyHash(keyMerhchant.GetPubKey().GetID()); auto scriptMerchant = GetScriptForDestination(merchantAddress); - auto txContract = CreateContractTx(coinbaseKey.GetPubKey().GetID(), merchantAddress, 50, false); + auto txContract = CreateContractTx(coinbaseKey.GetPubKey().GetID(), merchantAddress, 50); auto contract = TPoSContract::FromTPoSContractTx(MakeTransactionRef(txContract)); BOOST_ASSERT(contract.IsValid()); diff --git a/src/tpos/tposutils.cpp b/src/tpos/tposutils.cpp index 053eec0fa..72a2040f2 100644 --- a/src/tpos/tposutils.cpp +++ b/src/tpos/tposutils.cpp @@ -118,7 +118,6 @@ bool TPoSUtils::CreateTPoSTransaction(CWallet *wallet, const CTxDestination &tposAddress, const CTxDestination &merchantAddress, int merchantCommission, - bool createLegacyContract, std::string &strError) { if (wallet->IsLocked()) { @@ -129,7 +128,7 @@ bool TPoSUtils::CreateTPoSTransaction(CWallet *wallet, CAmount nFeeRequired; CMutableTransaction baseTx; - if(!CreateTPoSTransaction(baseTx, tposAddress, merchantAddress, merchantCommission, createLegacyContract, strError)) { + if(!CreateTPoSTransaction(baseTx, tposAddress, merchantAddress, merchantCommission, strError)) { return false; } @@ -464,40 +463,26 @@ bool TPoSUtils::SignTPoSContract(CMutableTransaction &tx, CKeyStore *keystore, T return false; } -bool TPoSUtils::CreateTPoSTransaction(CMutableTransaction &txOut, const CTxDestination &tposAddress, const CTxDestination &merchantAddress, int nOperatorReward, bool createLegacyContract, std::string &strError) +bool TPoSUtils::CreateTPoSTransaction(CMutableTransaction &txOut, const CTxDestination &tposAddress, + const CTxDestination &merchantAddress, int nOperatorReward, std::string &strError) { CKey key; CKeyID keyID; - if (createLegacyContract) { - if(auto tmpKeyID = boost::get(&tposAddress)) { - keyID = *tmpKeyID; - } else { - strError = "Error: TPoS Address is not P2PKH"; - return false; - } - } else { - keyID = GetKeyForDestination(CBasicKeyStore{}, tposAddress); - if (keyID.IsNull()) { - strError = "Error: TPoS Address is not supported"; - return false; - } + keyID = GetKeyForDestination(CBasicKeyStore{}, tposAddress); + if (keyID.IsNull()) { + strError = "Error: TPoS Address is not supported"; + return false; } // dummy signature, just to know size std::vector vchSignature(CPubKey::COMPACT_SIGNATURE_SIZE, '0'); CScript metadataScriptPubKey; - - if (createLegacyContract) { - metadataScriptPubKey = GenerateLegacyContractScript(tposAddress, merchantAddress, - nOperatorReward, vchSignature); - } else { - metadataScriptPubKey << OP_RETURN - << GenerateContractPayload(TPoSContract({}, merchantAddress, - tposAddress, nOperatorReward, - vchSignature)); - } + metadataScriptPubKey << OP_RETURN + << GenerateContractPayload(TPoSContract({}, merchantAddress, + tposAddress, nOperatorReward, + vchSignature)); txOut = CMutableTransaction{}; txOut.vout.emplace_back(0, metadataScriptPubKey); diff --git a/src/tpos/tposutils.h b/src/tpos/tposutils.h index 8ca2f68b4..9621c18e5 100644 --- a/src/tpos/tposutils.h +++ b/src/tpos/tposutils.h @@ -83,7 +83,7 @@ class TPoSUtils const CTxDestination &tposAddress, const CTxDestination &merchantAddress, int nOperatorReward, - bool createLegacyContract, std::string &strError); + std::string &strError); static CAmount GetOperatorPayment(CAmount basePayment, int nOperatorReward); static CAmount GetOwnerPayment(CAmount basePayment, int nOperatorReward); @@ -104,7 +104,6 @@ class TPoSUtils const CTxDestination &tposAddress, const CTxDestination &merchantAddress, int merchantCommission, - bool createLegacyContract, std::string &strError); static bool CreateCancelContractTransaction(CWallet *wallet,