<commit_before>// Provides a cache of wallet balances and functionality for determining whether Omni state changes affected anything in the wallet
#include "omnicore/omnicore.h"

#include "init.h"
#include "wallet.h"

#include <boost/algorithm/string.hpp>

#include <map>
#include <string>

using std::map;
using std::string;

using boost::algorithm::token_compress_on;

using namespace mastercore;

std::map<string, CMPTally> walletBalancesCache;

//! Global vector of Omni transactions in the wallet
std::vector<uint256> walletTXIDCache;

/**
 * Adds a txid to the wallet txid cache, performing duplicate detection
 */
void WalletTXIDCacheAdd(uint256 hash)
{
    if (msc_debug_walletcache) PrintToLog("WALLETTXIDCACHE: Adding tx to txid cache : %s\n",hash.GetHex());
    if (std::find(walletTXIDCache.begin(), walletTXIDCache.end(), hash) != walletTXIDCache.end()) {
        PrintToLog("ERROR: Wallet TXID Cache blocked duplicate insertion for %s\n",hash.GetHex());
    } else {
        walletTXIDCache.push_back(hash);
    }
}

/**
 * Performs initial population of the wallet txid cache
 */
void WalletTXIDCacheInit()
{
    if (msc_debug_walletcache) PrintToLog("WALLETTXIDCACHE: WalletTXIDCacheInit requested\n");
    // Prepare a few items and get lock
    CWallet *wallet = pwalletMain;
    LOCK(wallet->cs_wallet);
    std::list<CAccountingEntry> acentries;
    CWallet::TxItems txOrdered = pwalletMain->OrderedTxItems(acentries, "*");

    // STO has no inbound transaction, use STO receipts to insert an STO into the wallet txid cache
    string mySTOReceipts = s_stolistdb->getMySTOReceipts("");
    std::vector<std::string> vecReceipts;
    boost::split(vecReceipts, mySTOReceipts, boost::is_any_of(","), token_compress_on);
    int64_t lastTXBlock = 999999;

    // Iterate through the wallet, checking if each transaction is Omni (via levelDB)
    for (CWallet::TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) {
        CWalletTx *const pwtx = (*it).second.first;
        if (pwtx != 0) {
            uint256 blockHash = pwtx->hashBlock;
            if ((0 == blockHash) || (NULL == GetBlockIndex(blockHash))) continue;
            CBlockIndex* pBlockIndex = GetBlockIndex(blockHash);
            if (NULL == pBlockIndex) continue;
            int blockHeight = pBlockIndex->nHeight;

            // look for an STO receipt to see if we need to insert it into txid cache
            for(uint32_t i = 0; i<vecReceipts.size(); i++) {
                if (!vecReceipts[i].empty()) {
                    std::vector<std::string> svstr;
                    boost::split(svstr, vecReceipts[i], boost::is_any_of(":"), token_compress_on);
                    if (svstr.size() != 4) {
                        PrintToLog("ERROR: Unexpected number of tokens enumerating STO receipt %s\n", vecReceipts[i]);
                        continue;
                    }
                    if ((atoi(svstr[1]) < lastTXBlock) && (atoi(svstr[1]) > blockHeight)) {
                        uint256 hash;
                        hash.SetHex(svstr[0]);
                        if (msc_debug_walletcache) PrintToLog("WALLETTXIDCACHE: Adding STO to txid cache : %s\n",hash.GetHex());
                        walletTXIDCache.push_back(hash);
                    }
                }
            }

            // get the hash of the transaction and check leveldb to see if this is an Omni tx, if so add to cache
            uint256 hash = pwtx->GetHash();
            if (p_txlistdb->exists(hash)) {
                walletTXIDCache.push_back(hash);
                if (msc_debug_walletcache) PrintToLog("WALLETTXIDCACHE: Adding tx to txid cache : %s\n",hash.GetHex());
            }
            lastTXBlock = blockHeight;
        }
    }
}

/**
 * Updates the cache with the latest state, returning true if changes were made to wallet addresses (including watch only)
 * Also prepares a list of addresses that were changed (for future usage)
 */
int WalletCacheUpdate()
{
    if (msc_debug_walletcache) PrintToLog("WALLETCACHE: Update requested\n");
    int numChanges = 0;<commit_msg>WALLETCACHE: Remove STO receipts, not wallet transactions<commit_after>// Provides a cache of wallet balances and functionality for determining whether Omni state changes affected anything in the wallet
#include "omnicore/omnicore.h"

#include "init.h"
#include "wallet.h"

#include <boost/algorithm/string.hpp>

#include <map>
#include <string>

using std::map;
using std::string;

using boost::algorithm::token_compress_on;

using namespace mastercore;

std::map<string, CMPTally> walletBalancesCache;

//! Global vector of Omni transactions in the wallet
std::vector<uint256> walletTXIDCache;

/**
 * Adds a txid to the wallet txid cache, performing duplicate detection
 */
void WalletTXIDCacheAdd(uint256 hash)
{
    if (msc_debug_walletcache) PrintToLog("WALLETTXIDCACHE: Adding tx to txid cache : %s\n",hash.GetHex());
    if (std::find(walletTXIDCache.begin(), walletTXIDCache.end(), hash) != walletTXIDCache.end()) {
        PrintToLog("ERROR: Wallet TXID Cache blocked duplicate insertion for %s\n",hash.GetHex());
    } else {
        walletTXIDCache.push_back(hash);
    }
}

/**
 * Performs initial population of the wallet txid cache
 */
void WalletTXIDCacheInit()
{
    if (msc_debug_walletcache) PrintToLog("WALLETTXIDCACHE: WalletTXIDCacheInit requested\n");
    // Prepare a few items and get lock
    CWallet *wallet = pwalletMain;
    LOCK(wallet->cs_wallet);
    std::list<CAccountingEntry> acentries;
    CWallet::TxItems txOrdered = pwalletMain->OrderedTxItems(acentries, "*");
    // Iterate through the wallet, checking if each transaction is Omni (via levelDB)
    for (CWallet::TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it) {
        CWalletTx *const pwtx = (*it).second.first;
        if (pwtx != 0) {
            // get the hash of the transaction and check leveldb to see if this is an Omni tx, if so add to cache
            uint256 hash = pwtx->GetHash();
            if (p_txlistdb->exists(hash)) {
                walletTXIDCache.push_back(hash);
                if (msc_debug_walletcache) PrintToLog("WALLETTXIDCACHE: Adding tx to txid cache : %s\n",hash.GetHex());
            }
        }
    }
}

/**
 * Updates the cache with the latest state, returning true if changes were made to wallet addresses (including watch only)
 * Also prepares a list of addresses that were changed (for future usage)
 */
int WalletCacheUpdate()
{
    if (msc_debug_walletcache) PrintToLog("WALLETCACHE: Update requested\n");
    int numChanges = 0;
{'fraction_non_alphanumeric': 0.10996667676461679, 'fraction_numerical': 0.006664647076643442, 'mean_word_length': 3.108898568761668, 'pattern_counts': {'":': 1, '<': 19, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6060475', 'n_tokens_mistral': 2061, 'n_tokens_neox': 2025, 'n_words': 572}