<commit_before>// Copyright (c) 2017 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "zpivwallet.h"
#include "main.h"
#include "txdb.h"
#include "walletdb.h"
#include "init.h"
#include "wallet.h"
#include "primitives/deterministicmint.h"

using namespace libzerocoin;

CzPIVWallet::CzPIVWallet(std::string strWalletFile, bool fFirstRun)
{
    this->strWalletFile = strWalletFile;
    CWalletDB walletdb(strWalletFile);

    uint256 seed;
    if (!walletdb.ReadZPIVSeed(seed))
        fFirstRun = true;

    //First time running, generate master seed
    if (fFirstRun)
        seed = CBigNum::randBignum(CBigNum(~uint256(0))).getuint256();

    SetMasterSeed(seed);
    this->mintPool = CMintPool(nCountLastUsed);
}

bool CzPIVWallet::SetMasterSeed(const uint256& seedMaster, bool fResetCount)
{
    this->seedMaster = seedMaster;

    CWalletDB walletdb(strWalletFile);
    if (!walletdb.WriteZPIVSeed(seedMaster))
        return false;

    nCountLastUsed = 0;
    if (fResetCount)
        walletdb.WriteZPIVCount(nCountLastUsed);
    else if (!walletdb.ReadZPIVCount(nCountLastUsed))
        nCountLastUsed = 0;

    //todo fix to sync with count above
    mintPool.Reset();

    return true;
}

//Add the next 20 mints to the mint pool
void CzPIVWallet::GenerateMintPool(uint32_t nCountStart, uint32_t nCountEnd)
{
    uint32_t n = nCountLastUsed + 1;
    if (nCountStart > 0)
        n = nCountStart;

    uint32_t nStop = n + 20;
    if (nCountEnd > 0)
        nStop = std::max(n, n + nCountEnd);

    bool fFound;

    uint256 hashSeed = Hash(seedMaster.begin(), seedMaster.end());
    LogPrintf("%s : n=%d nStop=%d\n", __func__, n, nStop);
    for (uint i = n; i < nStop; ++i) {
        fFound = false;

        // Prevent unnecessary repeated minted
        for (auto& pair : mintPool) {
            if(pair.second == i) {
                fFound = true;
                break;
            }
        }

        if(fFound)
            continue;

        uint512 seedZerocoin = GetZerocoinSeed(i);
        CBigNum bnSerial;
        CBigNum bnRandomness;
        CKey key;
        SeedToZPIV(seedZerocoin, bnSerial, bnRandomness, key);

        PrivateCoin coin(Params().Zerocoin_Params(false), CoinDenomination::ZQ_ONE, bnSerial, bnRandomness);
        coin.setVersion(PrivateCoin::CURRENT_VERSION);
        coin.setPrivKey(key.GetPrivKey());
        mintPool.Add(coin.getPublicCoin().getValue(), i);
        CWalletDB(strWalletFile).WriteMintPoolPair(hashSeed, GetPubCoinHash(coin.getPublicCoin().getValue()), i);
        LogPrintf("%s : %s count=%d\n", __func__, coin.getPublicCoin().getValue().GetHex().substr(0, 6), i);
    }
}<commit_msg>Fix MintPool logprint.<commit_after>// Copyright (c) 2017 The PIVX developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include "zpivwallet.h"
#include "main.h"
#include "txdb.h"
#include "walletdb.h"
#include "init.h"
#include "wallet.h"
#include "primitives/deterministicmint.h"

using namespace libzerocoin;

CzPIVWallet::CzPIVWallet(std::string strWalletFile, bool fFirstRun)
{
    this->strWalletFile = strWalletFile;
    CWalletDB walletdb(strWalletFile);

    uint256 seed;
    if (!walletdb.ReadZPIVSeed(seed))
        fFirstRun = true;

    //First time running, generate master seed
    if (fFirstRun)
        seed = CBigNum::randBignum(CBigNum(~uint256(0))).getuint256();

    SetMasterSeed(seed);
    this->mintPool = CMintPool(nCountLastUsed);
}

bool CzPIVWallet::SetMasterSeed(const uint256& seedMaster, bool fResetCount)
{
    this->seedMaster = seedMaster;

    CWalletDB walletdb(strWalletFile);
    if (!walletdb.WriteZPIVSeed(seedMaster))
        return false;

    nCountLastUsed = 0;
    if (fResetCount)
        walletdb.WriteZPIVCount(nCountLastUsed);
    else if (!walletdb.ReadZPIVCount(nCountLastUsed))
        nCountLastUsed = 0;

    mintPool.Reset();

    return true;
}

//Add the next 20 mints to the mint pool
void CzPIVWallet::GenerateMintPool(uint32_t nCountStart, uint32_t nCountEnd)
{
    uint32_t n = nCountLastUsed + 1;
    if (nCountStart > 0)
        n = nCountStart;

    uint32_t nStop = n + 20;
    if (nCountEnd > 0)
        nStop = std::max(n, n + nCountEnd);

    bool fFound;

    uint256 hashSeed = Hash(seedMaster.begin(), seedMaster.end());
    LogPrintf("%s : n=%d nStop=%d\n", __func__, n, nStop - 1);
    for (uint i = n; i < nStop; ++i) {
        fFound = false;

        // Prevent unnecessary repeated minted
        for (auto& pair : mintPool) {
            if(pair.second == i) {
                fFound = true;
                break;
            }
        }

        if(fFound)
            continue;

        uint512 seedZerocoin = GetZerocoinSeed(i);
        CBigNum bnSerial;
        CBigNum bnRandomness;
        CKey key;
        SeedToZPIV(seedZerocoin, bnSerial, bnRandomness, key);

        PrivateCoin coin(Params().Zerocoin_Params(false), CoinDenomination::ZQ_ONE, bnSerial, bnRandomness);
        coin.setVersion(PrivateCoin::CURRENT_VERSION);
        coin.setPrivKey(key.GetPrivKey());
        mintPool.Add(coin.getPublicCoin().getValue(), i);
        CWalletDB(strWalletFile).WriteMintPoolPair(hashSeed, GetPubCoinHash(coin.getPublicCoin().getValue()), i);
        LogPrintf("%s : %s count=%d\n", __func__, coin.getPublicCoin().getValue().GetHex().substr(0, 6), i);
    }
}
{'fraction_non_alphanumeric': 0.1211681480137856, 'fraction_numerical': 0.015418102666424814, 'mean_word_length': 3.1087928464977646, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1259886', 'n_tokens_mistral': 1962, 'n_tokens_neox': 1904, 'n_words': 396}