Skip to content

Commit

Permalink
Bumped version. Removed creating of legacy contracts. Enabled creatin…
Browse files Browse the repository at this point in the history
…g of new contracts by default
  • Loading branch information
durkmurder committed Oct 12, 2020
1 parent e8688cd commit 72b3020
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 144 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class WalletImpl : public Wallet
{
LOCK2(cs_main, m_wallet.cs_wallet);
auto pending = MakeUnique<PendingWalletTxImpl>(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);
Expand Down
15 changes: 5 additions & 10 deletions src/qt/tpospage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static QString PrepareCreateContractQuestionString(const CBitcoinAddress &tposAd
}

std::unique_ptr<interfaces::PendingWalletTx> TPoSPage::CreateContractTransaction(QWidget *widget,
const CBitcoinAddress &tposAddress,
const CTxDestination &tposAddress,
const CBitcoinAddress &merchantAddress,
int merchantCommission)
{
Expand All @@ -68,7 +68,7 @@ std::unique_ptr<interfaces::PendingWalletTx> 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;
}

Expand Down Expand Up @@ -154,19 +154,18 @@ 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;
if (!_walletModel->wallet().getKeyFromPool(false, newKey))
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) :
Expand Down Expand Up @@ -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())
{
Expand Down
4 changes: 2 additions & 2 deletions src/qt/tpospage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<interfaces::PendingWalletTx> CreateContractTransaction(QWidget *widget,
const CBitcoinAddress &tposAddress,
const CTxDestination &tposAddress,
const CBitcoinAddress &merchantAddress,
int merchantCommission);

Expand Down
8 changes: 2 additions & 6 deletions src/rpc/merchantnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down
100 changes: 4 additions & 96 deletions src/test/tpos_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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]);
Expand All @@ -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));
Expand Down Expand Up @@ -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());
Expand Down
37 changes: 11 additions & 26 deletions src/tpos/tposutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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<CKeyID>(&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<unsigned char> 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);
Expand Down
3 changes: 1 addition & 2 deletions src/tpos/tposutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -104,7 +104,6 @@ class TPoSUtils
const CTxDestination &tposAddress,
const CTxDestination &merchantAddress,
int merchantCommission,
bool createLegacyContract,
std::string &strError);

static bool CreateCancelContractTransaction(CWallet *wallet,
Expand Down

0 comments on commit 72b3020

Please sign in to comment.