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

#ifndef BITCOIN_MAIN_COMMON_H
#define BITCOIN_MAIN_COMMON_H

#if defined(HAVE_CONFIG_H)
#include "bitcredit-config.h"
#endif

#include "chainparams.h"
#include "net.h"
#include "sync.h"
#include "uint256.h"

#include <algorithm>
#include <exception>
#include <map>
#include <set>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>

#include <boost/filesystem.hpp>

class CBlockFileInfo
{
public:
    unsigned int nBlocks;      // number of blocks stored in file
    unsigned int nSize;        // number of used bytes of block file
    unsigned int nSizeCompressed;        // number of used bytes of compressed block file
    unsigned int nUndoSize;    // number of used bytes in the undo file
    unsigned int nUndoSizeClaim;    // number of used bytes in the undo claim file
    unsigned int nHeightFirst; // lowest height of block in file
    unsigned int nHeightLast;  // highest height of block in file
    uint64_t nTimeFirst;         // earliest time of block in file
    uint64_t nTimeLast;          // latest time of block in file

    IMPLEMENT_SERIALIZE(
        READWRITE(VARINT(nBlocks));
        READWRITE(VARINT(nSize));
        READWRITE(VARINT(nSizeCompressed));
        READWRITE(VARINT(nUndoSize));
        READWRITE(VARINT(nUndoSizeClaim));
        READWRITE(VARINT(nHeightFirst));
        READWRITE(VARINT(nHeightLast));
        READWRITE(VARINT(nTimeFirst));
        READWRITE(VARINT(nTimeLast));
     )

     void SetNull() {
         nBlocks = 0;
         nSize = 0;
         nSizeCompressed = 0;
         nUndoSize = 0;
         nUndoSizeClaim = 0;
         nHeightFirst = 0;
         nHeightLast = 0;
         nTimeFirst = 0;
         nTimeLast = 0;
     }

    CBlockFileInfo() {
         SetNull();
     }

     std::string ToString() const {
         return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst).c_str(), DateTimeStrFormat("%Y-%m-%d", nTimeLast).c_str());
     }

     // update statistics (does not update nSize)
     void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {
         if (nBlocks==0 || nHeightFirst > nHeightIn)
             nHeightFirst = nHeightIn;
         if (nBlocks==0 || nTimeFirst > nTimeIn)
             nTimeFirst = nTimeIn;
         nBlocks++;
         if (nHeightIn > nHeightLast)
             nHeightLast = nHeightIn;
         if (nTimeIn > nTimeLast)
             nTimeLast = nTimeIn;
     }
};

class COrphanBlock {
public:
    uint256 hashBlock;
    uint256 hashPrev;
    uint256 hashLinkedBitcoinBlock;
    vector<unsigned char> vchBlock;
    bool fStoredInMemory;

    void SetNull() {
        hashBlock = 0;
        hashPrev = 0;
        hashLinkedBitcoinBlock = 0;
        vchBlock.clear();
        fStoredInMemory = false;
    }

    COrphanBlock() {
        SetNull();
    }
};

class COrphanIndex {
public:
	map<uint256, COrphanBlock*> mapOrphanBlocks;
	multimap<uint256, COrphanBlock*> mapOrphanBlocksByPrev;
	multimap<uint256, COrphanBlock*> mapOrphanBlocksByLinkedBitcoinBlock;
	int nStoredInMemory;
	std::string orphanDir;

    void SetNull() {
        mapOrphanBlocks.clear();
        mapOrphanBlocksByPrev.clear();
        mapOrphanBlocksByLinkedBitcoinBlock.clear();
        nStoredInMemory = 0;
        orphanDir = "";
    }

    COrphanIndex(std::string orphanDirIn) {
        SetNull();
        orphanDir = orphanDirIn;
    }

    uint256 GetOrphanRoot(const uint256& hash) {
        map<uint256, COrphanBlock*>::iterator it = mapOrphanBlocks.find(hash);
        if (it == mapOrphanBlocks.end())
            return hash;

        // Work back to the first block in the orphan chain
        do {
            map<uint256, COrphanBlock*>::iterator it2 = mapOrphanBlocks.find(it->second->hashPrev);
            if (it2 == mapOrphanBlocks.end())
                return it->first;
            it = it2;
        } while(true);
    }

    void DeletePrevPartial(const uint256& hash, std::vector <COrphanBlock*> &deleteOrphans) {
    	typedef multimap<uint256, COrphanBlock*>::iterator multiIter;
		std::pair<multiIter, multiIter> inneriterpair = mapOrphanBlocksByPrev.equal_range(hash);
		for (multiIter innerit = inneriterpair.first; innerit != inneriterpair.second; ++innerit) {
			if(std::find(deleteOrphans.begin(), deleteOrphans.end(), innerit->second) != deleteOrphans.end()) {
				delete innerit->second;
				mapOrphanBlocksByPrev.erase(innerit);
			}
		}
    }

    void RemoveOrpan(COrphanBlock * orphan) {
        const bool fStoredInMemory = orphan->fStoredInMemory;<commit_msg>Trying to get around nullpointer.<commit_after>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2014 The Bitcoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_MAIN_COMMON_H
#define BITCOIN_MAIN_COMMON_H

#if defined(HAVE_CONFIG_H)
#include "bitcredit-config.h"
#endif

#include "chainparams.h"
#include "net.h"
#include "sync.h"
#include "uint256.h"

#include <algorithm>
#include <exception>
#include <map>
#include <set>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>

#include <boost/filesystem.hpp>

class CBlockFileInfo
{
public:
    unsigned int nBlocks;      // number of blocks stored in file
    unsigned int nSize;        // number of used bytes of block file
    unsigned int nSizeCompressed;        // number of used bytes of compressed block file
    unsigned int nUndoSize;    // number of used bytes in the undo file
    unsigned int nUndoSizeClaim;    // number of used bytes in the undo claim file
    unsigned int nHeightFirst; // lowest height of block in file
    unsigned int nHeightLast;  // highest height of block in file
    uint64_t nTimeFirst;         // earliest time of block in file
    uint64_t nTimeLast;          // latest time of block in file

    IMPLEMENT_SERIALIZE(
        READWRITE(VARINT(nBlocks));
        READWRITE(VARINT(nSize));
        READWRITE(VARINT(nSizeCompressed));
        READWRITE(VARINT(nUndoSize));
        READWRITE(VARINT(nUndoSizeClaim));
        READWRITE(VARINT(nHeightFirst));
        READWRITE(VARINT(nHeightLast));
        READWRITE(VARINT(nTimeFirst));
        READWRITE(VARINT(nTimeLast));
     )

     void SetNull() {
         nBlocks = 0;
         nSize = 0;
         nSizeCompressed = 0;
         nUndoSize = 0;
         nUndoSizeClaim = 0;
         nHeightFirst = 0;
         nHeightLast = 0;
         nTimeFirst = 0;
         nTimeLast = 0;
     }

    CBlockFileInfo() {
         SetNull();
     }

     std::string ToString() const {
         return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst).c_str(), DateTimeStrFormat("%Y-%m-%d", nTimeLast).c_str());
     }

     // update statistics (does not update nSize)
     void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {
         if (nBlocks==0 || nHeightFirst > nHeightIn)
             nHeightFirst = nHeightIn;
         if (nBlocks==0 || nTimeFirst > nTimeIn)
             nTimeFirst = nTimeIn;
         nBlocks++;
         if (nHeightIn > nHeightLast)
             nHeightLast = nHeightIn;
         if (nTimeIn > nTimeLast)
             nTimeLast = nTimeIn;
     }
};

class COrphanBlock {
public:
    uint256 hashBlock;
    uint256 hashPrev;
    uint256 hashLinkedBitcoinBlock;
    vector<unsigned char> vchBlock;
    bool fStoredInMemory;

    void SetNull() {
        hashBlock = 0;
        hashPrev = 0;
        hashLinkedBitcoinBlock = 0;
        vchBlock.clear();
        fStoredInMemory = false;
    }

    COrphanBlock() {
        SetNull();
    }
};

class COrphanIndex {
public:
	map<uint256, COrphanBlock*> mapOrphanBlocks;
	multimap<uint256, COrphanBlock*> mapOrphanBlocksByPrev;
	multimap<uint256, COrphanBlock*> mapOrphanBlocksByLinkedBitcoinBlock;
	int nStoredInMemory;
	std::string orphanDir;

    void SetNull() {
        mapOrphanBlocks.clear();
        mapOrphanBlocksByPrev.clear();
        mapOrphanBlocksByLinkedBitcoinBlock.clear();
        nStoredInMemory = 0;
        orphanDir = "";
    }

    COrphanIndex(std::string orphanDirIn) {
        SetNull();
        orphanDir = orphanDirIn;
    }

    uint256 GetOrphanRoot(const uint256& hash) {
        map<uint256, COrphanBlock*>::iterator it = mapOrphanBlocks.find(hash);
        if (it == mapOrphanBlocks.end())
            return hash;

        // Work back to the first block in the orphan chain
        do {
            map<uint256, COrphanBlock*>::iterator it2 = mapOrphanBlocks.find(it->second->hashPrev);
            if (it2 == mapOrphanBlocks.end())
                return it->first;
            it = it2;
        } while(true);
    }

    void DeletePrevPartial(const uint256& hash, std::vector <COrphanBlock*> &deleteOrphans) {
    	typedef multimap<uint256, COrphanBlock*>::iterator multiIter;
    	for(unsigned int i = 0; i < deleteOrphans.size(); i++) {
    		COrphanBlock * orphan = deleteOrphans[i];

			std::pair<multiIter, multiIter> inneriterpair = mapOrphanBlocksByLinkedBitcoinBlock.equal_range(hash); //
			for (multiIter innerit = inneriterpair.first; innerit != inneriterpair.second; ++innerit) {
				if (innerit->second == orphan) {
					mapOrphanBlocksByLinkedBitcoinBlock.erase(innerit);
					break;
				}
			}
    	}
    }

    void RemoveOrpan(COrphanBlock * orphan) {
        const bool fStoredInMemory = orphan->fStoredInMemory;
{'fraction_non_alphanumeric': 0.10757297407634211, 'fraction_numerical': 0.01663604817309655, 'mean_word_length': 2.801008533747091, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 60, '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': '1309026', 'n_tokens_mistral': 3266, 'n_tokens_neox': 3055, 'n_words': 731}