Skip to content

Commit

Permalink
Merge pull request #31 from cevap/Avoid-failed-zPIV-spend
Browse files Browse the repository at this point in the history
Avoid failed xion spend
  • Loading branch information
ioncoincore authored Jan 17, 2019
2 parents df3f639 + 0077213 commit 8bdc4bd
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 18 deletions.
4 changes: 2 additions & 2 deletions COPYING
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ The MIT License (MIT)

Copyright (c) 2009-2015 Bitcoin Developers
Copyright (c) 2014-2015 Dash Developers
Copyright (c) 2015-2018 PIVX Developers
Copyright (c) 2018 Ion Developers
Copyright (c) 2015-2019 PIVX Developers
Copyright (c) 2018-2019 Ion Developers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ MIT License

Copyright (c) 2009-2015 Bitcoin Developers
Copyright (c) 2014-2015 Dash Developers
Copyright (c) 2015-2018 PIVX Developers
Copyright (c) 2018 Ion Developers
Copyright (c) 2015-2019 PIVX Developers
Copyright (c) 2018-2019 Ion Developers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 1 addition & 0 deletions src/primitives/zerocoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct CMintMeta
bool isUsed;
bool isArchived;
bool isDeterministic;
bool isSeedCorrect;

bool operator <(const CMintMeta& a) const;
};
Expand Down
4 changes: 2 additions & 2 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,10 +772,10 @@ void WalletModel::listLockedCoins(std::vector<COutPoint>& vOutpts)
}


void WalletModel::listZerocoinMints(std::set<CMintMeta>& setMints, bool fUnusedOnly, bool fMaturedOnly, bool fUpdateStatus)
void WalletModel::listZerocoinMints(std::set<CMintMeta>& setMints, bool fUnusedOnly, bool fMaturedOnly, bool fUpdateStatus, bool fWrongSeed)
{
setMints.clear();
setMints = pwalletMain->xionTracker->ListMints(fUnusedOnly, fMaturedOnly, fUpdateStatus);
setMints = pwalletMain->xionTracker->ListMints(fUnusedOnly, fMaturedOnly, fUpdateStatus, fWrongSeed);
}

void WalletModel::loadReceiveRequests(std::vector<std::string>& vReceiveRequests)
Expand Down
2 changes: 1 addition & 1 deletion src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class WalletModel : public QObject
void unlockCoin(COutPoint& output);
void listLockedCoins(std::vector<COutPoint>& vOutpts);

void listZerocoinMints(std::set<CMintMeta>& setMints, bool fUnusedOnly = false, bool fMaturedOnly = false, bool fUpdateStatus = false);
void listZerocoinMints(std::set<CMintMeta>& setMints, bool fUnusedOnly = false, bool fMaturedOnly = false, bool fUpdateStatus = false, bool fWrongSeed = false);

string GetUniqueWalletBackupName();
void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
Expand Down
8 changes: 5 additions & 3 deletions src/qt/xioncontroldialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ void XIonControlDialog::updateList()
itemDenom->setData(COLUMN_DENOMINATION, Qt::UserRole, QVariant((qlonglong) denom));
}

// select all unused coins - including not mature. Update status of coins too.
// select all unused coins - including not mature and mismatching seed. Update status of coins too.
std::set<CMintMeta> set;
model->listZerocoinMints(set, true, false, true);
model->listZerocoinMints(set, true, false, true, true);
this->setMints = set;

//populate rows with mint info
Expand Down Expand Up @@ -116,7 +116,7 @@ void XIonControlDialog::updateList()
isMature = mint.nHeight < mapMaturityHeight.at(denom);

// disable selecting this mint if it is not spendable - also display a reason why
bool fSpendable = isMature && nConfirmations >= Params().Zerocoin_MintRequiredConfirmations();
bool fSpendable = isMature && nConfirmations >= Params().Zerocoin_MintRequiredConfirmations() && mint.isSeedCorrect;
if(!fSpendable) {
itemMint->setDisabled(true);
itemMint->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
Expand All @@ -128,6 +128,8 @@ void XIonControlDialog::updateList()
string strReason = "";
if(nConfirmations < Params().Zerocoin_MintRequiredConfirmations())
strReason = strprintf("Needs %d more confirmations", Params().Zerocoin_MintRequiredConfirmations() - nConfirmations);
else if (!mint.isSeedCorrect)
strReason = "The xION seed used to mint this xION is not the same as currently hold in the wallet";
else
strReason = strprintf("Needs %d more mints added to network", Params().Zerocoin_RequiredAccumulation());

Expand Down
24 changes: 20 additions & 4 deletions src/xiontracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "main.h"
#include "txdb.h"
#include "walletdb.h"
#include "xionwallet.h"
#include "accumulators.h"

using namespace std;
Expand Down Expand Up @@ -277,8 +278,9 @@ bool CxIONTracker::UpdateState(const CMintMeta& meta)
return true;
}

void CxIONTracker::Add(const CDeterministicMint& dMint, bool isNew, bool isArchived)
void CxIONTracker::Add(const CDeterministicMint& dMint, bool isNew, bool isArchived, CxIONWallet* xIONWallet)
{
bool isxIONWalletInitialized = (NULL != xIONWallet);
CMintMeta meta;
meta.hashPubcoin = dMint.GetPubcoinHash();
meta.nHeight = dMint.GetHeight();
Expand All @@ -290,6 +292,11 @@ void CxIONTracker::Add(const CDeterministicMint& dMint, bool isNew, bool isArchi
meta.denom = dMint.GetDenomination();
meta.isArchived = isArchived;
meta.isDeterministic = true;
if (! isxIONWalletInitialized)
xIONWallet = new CxIONWallet(strWalletFile);
meta.isSeedCorrect = xIONWallet->CheckSeed(dMint);
if (! isxIONWalletInitialized)
delete xIONWallet;
mapSerialHashes[meta.hashSerial] = meta;

if (isNew)
Expand All @@ -310,6 +317,7 @@ void CxIONTracker::Add(const CZerocoinMint& mint, bool isNew, bool isArchived)
meta.denom = mint.GetDenomination();
meta.isArchived = isArchived;
meta.isDeterministic = false;
meta.isSeedCorrect = true;
mapSerialHashes[meta.hashSerial] = meta;

if (isNew)
Expand Down Expand Up @@ -429,7 +437,7 @@ bool CxIONTracker::UpdateStatusInternal(const std::set<uint256>& setMempool, CMi
return false;
}

std::set<CMintMeta> CxIONTracker::ListMints(bool fUnusedOnly, bool fMatureOnly, bool fUpdateStatus)
std::set<CMintMeta> CxIONTracker::ListMints(bool fUnusedOnly, bool fMatureOnly, bool fUpdateStatus, bool fWrongSeed)
{
CWalletDB walletdb(strWalletFile);
if (fUpdateStatus) {
Expand All @@ -439,8 +447,12 @@ std::set<CMintMeta> CxIONTracker::ListMints(bool fUnusedOnly, bool fMatureOnly,
LogPrint("zero", "%s: added %d zerocoinmints from DB\n", __func__, listMintsDB.size());

std::list<CDeterministicMint> listDeterministicDB = walletdb.ListDeterministicMints();
for (auto& dMint : listDeterministicDB)
Add(dMint);

CxIONWallet* xIONWallet = new CxIONWallet(strWalletFile);
for (auto& dMint : listDeterministicDB) {
Add(dMint, false, false, xIONWallet);
}
delete xIONWallet;
LogPrint("zero", "%s: added %d dxion from DB\n", __func__, listDeterministicDB.size());
}

Expand Down Expand Up @@ -479,6 +491,10 @@ std::set<CMintMeta> CxIONTracker::ListMints(bool fUnusedOnly, bool fMatureOnly,
if (mint.nHeight >= mapMaturity.at(mint.denom))
continue;
}

if (!fWrongSeed && !mint.isSeedCorrect)
continue;

setMints.insert(mint);
}

Expand Down
5 changes: 3 additions & 2 deletions src/xiontracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <list>

class CDeterministicMint;
class CxIONWallet;

class CxIONTracker
{
Expand All @@ -22,7 +23,7 @@ class CxIONTracker
public:
CxIONTracker(std::string strWalletFile);
~CxIONTracker();
void Add(const CDeterministicMint& dMint, bool isNew = false, bool isArchived = false);
void Add(const CDeterministicMint& dMint, bool isNew = false, bool isArchived = false, CxIONWallet* xIONWallet = NULL);
void Add(const CZerocoinMint& mint, bool isNew = false, bool isArchived = false);
bool Archive(CMintMeta& meta);
bool HasPubcoin(const CBigNum& bnValue) const;
Expand All @@ -39,7 +40,7 @@ class CxIONTracker
std::vector<uint256> GetSerialHashes();
std::vector<CMintMeta> GetMints(bool fConfirmedOnly) const;
CAmount GetUnconfirmedBalance() const;
std::set<CMintMeta> ListMints(bool fUnusedOnly, bool fMatureOnly, bool fUpdateStatus);
std::set<CMintMeta> ListMints(bool fUnusedOnly, bool fMatureOnly, bool fUpdateStatus, bool fWrongSeed = false);
void RemovePending(const uint256& txid);
void SetPubcoinUsed(const uint256& hashPubcoin, const uint256& txid);
void SetPubcoinNotUsed(const uint256& hashPubcoin);
Expand Down
11 changes: 9 additions & 2 deletions src/xionwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,12 +446,19 @@ void CxIONWallet::GenerateMint(const uint32_t& nCount, const CoinDenomination de
dMint.SetDenomination(denom);
}

bool CxIONWallet::RegenerateMint(const CDeterministicMint& dMint, CZerocoinMint& mint)
bool CxIONWallet::CheckSeed(const CDeterministicMint& dMint)
{
//Check that the seed is correct todo:handling of incorrect, or multiple seeds
uint256 hashSeed = Hash(seedMaster.begin(), seedMaster.end());
if (hashSeed != dMint.GetSeedHash())
return hashSeed == dMint.GetSeedHash();
}

bool CxIONWallet::RegenerateMint(const CDeterministicMint& dMint, CZerocoinMint& mint)
{
if (!CheckSeed(dMint)) {
uint256 hashSeed = Hash(seedMaster.begin(), seedMaster.end());
return error("%s: master seed does not match!\ndmint:\n %s \nhashSeed: %s\nseed: %s", __func__, dMint.ToString(), hashSeed.GetHex(), seedMaster.GetHex());
}

//Generate the coin
PrivateCoin coin(Params().Zerocoin_Params(false), dMint.GetDenomination(), false);
Expand Down
1 change: 1 addition & 0 deletions src/xionwallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CxIONWallet
void UpdateCount();
void Lock();
void SeedToXION(const uint512& seed, CBigNum& bnValue, CBigNum& bnSerial, CBigNum& bnRandomness, CKey& key);
bool CheckSeed(const CDeterministicMint& dMint);

private:
uint512 GetZerocoinSeed(uint32_t n);
Expand Down

0 comments on commit 8bdc4bd

Please sign in to comment.