<commit_before>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//
// File contains modifications by: The Gulden developers
// All modifications:
// Copyright (c) 2017-2020 The Gulden developers
// Authored by: <NAME> (<EMAIL>)
// Distributed under the GULDEN software license, see the accompanying
// file COPYING

#ifndef COINS_H
#define COINS_H

#include "primitives/transaction.h"
#include "compressor.h"
#include "core_memusage.h"
#include "hash.h"
#include "memusage.h"
#include "serialize.h"
#include "uint256.h"

#include <assert.h>
#include <stdint.h>

#include <unordered_map>

#include <compat/sys.h>

/**
 * A UTXO entry.
 *
 * Serialized format:
 * - VARINT((height << 2) | (segsig << 1) | (coinbase))
 * - VARINT(index)
 * - the non-spent CTxOut (via CTxOutCompressor)
 */
class Coin
{
public:
    //! unspent transaction output
    CTxOut out;

    //! whether containing transaction was a coinbase
    unsigned int fCoinBase : 1;
    unsigned int fSegSig : 1;

    //! The height this containing transaction was included in the active block chain
    //NB! This limits us to ~5000 years before we need to update serialisation format - so may eventually have to change but not an issue for now.
    //MEMPOOL_HEIGHT indicates it is not yet in the chain - any adjustments to the size of this type should also adjust the value of MEMPOOL_HEIGHT
    uint32_t nHeight : 30;
    
    //! The index of the containing transaction within its parent block
    uint32_t nTxIndex;

    //! construct a Coin from a CTxOut and height/coinbase information.
    Coin(CTxOut&& outIn, uint32_t nHeightIn, uint32_t nTxIndexIn, bool fCoinBaseIn, bool fSegSigIn)
    : out(std::move(outIn))
    , fCoinBase(fCoinBaseIn)
    , fSegSig(fSegSigIn)
    , nHeight(nHeightIn)
    , nTxIndex(nTxIndexIn)
    {}
    Coin(const CTxOut& outIn, uint32_t nHeightIn, uint32_t nTxIndexIn, bool fCoinBaseIn, bool fSegSigIn)
    : out(outIn)
    , fCoinBase(fCoinBaseIn)
    , fSegSig(fSegSigIn)
    , nHeight(nHeightIn)
    , nTxIndex(nTxIndexIn)
    {}

    void Clear() {
        out.SetNull();
        fCoinBase = false;
        fSegSig = false;
        nHeight = 0;
        nTxIndex = 0;
    }

    //! empty constructor
    Coin()
    : fCoinBase(false)
    , fSegSig(false)
    , nHeight(0)
    , nTxIndex(0)
    { }

    bool IsCoinBase() const {
        return fCoinBase;
    }

    template<typename Stream>
    void Serialize(Stream &s) const {
        assert(!IsSpent());
        uint32_t code = (nHeight<<2) + (fSegSig << 1) + fCoinBase;
        ::Serialize(s, VARINT(code));
        ::Serialize(s, VARINT(nTxIndex));
        out.WriteToStream(s, (fSegSig ? CTransaction::SEGSIG_ACTIVATION_VERSION : CTransaction::SEGSIG_ACTIVATION_VERSION-1));
    }

    template<typename Stream>
    void Unserialize(Stream &s) {
        uint32_t code = 0;
        ::Unserialize(s, VARINT(code));
        nHeight = ( ((code  & 0b11111111111111111111111111111100) >> 2) );
        fSegSig = ( (code   & 0b00000000000000000000000000000010) > 0 );
        fCoinBase = ( (code & 0b00000000000000000000000000000001) > 0 );
        ::Unserialize(s, VARINT(nTxIndex));
        out.ReadFromStream(s, (fSegSig ? CTransaction::SEGSIG_ACTIVATION_VERSION : CTransaction::SEGSIG_ACTIVATION_VERSION-1));
    }

    bool IsSpent() const {
        return out.IsNull();
    }

    void Spend() {
        out.SetNull();
    }

    size_t DynamicMemoryUsage() const {
        return RecursiveDynamicUsage(out);
    }
};

class CoinUndo : public Coin
{
    public:
    uint256 prevhash;
    CoinUndo(Coin&& fromCoin, uint256 hashIn)
    : Coin(fromCoin)
    , prevhash(hashIn)
    {
    }
    
    //! empty constructor
    CoinUndo() : Coin() {}
    
    template<typename Stream>
    void Serialize(Stream &s) const
    {
        assert(!IsSpent());<commit_msg>Revert "CORE: Fix Coin::DynamicMemoryUsage() for all output types"<commit_after>// Copyright (c) 2009-2010 <NAME>
// Copyright (c) 2009-2016 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//
// File contains modifications by: The Gulden developers
// All modifications:
// Copyright (c) 2017-2020 The Gulden developers
// Authored by: <NAME> (<EMAIL>)
// Distributed under the GULDEN software license, see the accompanying
// file COPYING

#ifndef COINS_H
#define COINS_H

#include "primitives/transaction.h"
#include "compressor.h"
#include "core_memusage.h"
#include "hash.h"
#include "memusage.h"
#include "serialize.h"
#include "uint256.h"

#include <assert.h>
#include <stdint.h>

#include <unordered_map>

#include <compat/sys.h>

/**
 * A UTXO entry.
 *
 * Serialized format:
 * - VARINT((height << 2) | (segsig << 1) | (coinbase))
 * - VARINT(index)
 * - the non-spent CTxOut (via CTxOutCompressor)
 */
class Coin
{
public:
    //! unspent transaction output
    CTxOut out;

    //! whether containing transaction was a coinbase
    unsigned int fCoinBase : 1;
    unsigned int fSegSig : 1;

    //! The height this containing transaction was included in the active block chain
    //NB! This limits us to ~5000 years before we need to update serialisation format - so may eventually have to change but not an issue for now.
    //MEMPOOL_HEIGHT indicates it is not yet in the chain - any adjustments to the size of this type should also adjust the value of MEMPOOL_HEIGHT
    uint32_t nHeight : 30;
    
    //! The index of the containing transaction within its parent block
    uint32_t nTxIndex;

    //! construct a Coin from a CTxOut and height/coinbase information.
    Coin(CTxOut&& outIn, uint32_t nHeightIn, uint32_t nTxIndexIn, bool fCoinBaseIn, bool fSegSigIn)
    : out(std::move(outIn))
    , fCoinBase(fCoinBaseIn)
    , fSegSig(fSegSigIn)
    , nHeight(nHeightIn)
    , nTxIndex(nTxIndexIn)
    {}
    Coin(const CTxOut& outIn, uint32_t nHeightIn, uint32_t nTxIndexIn, bool fCoinBaseIn, bool fSegSigIn)
    : out(outIn)
    , fCoinBase(fCoinBaseIn)
    , fSegSig(fSegSigIn)
    , nHeight(nHeightIn)
    , nTxIndex(nTxIndexIn)
    {}

    void Clear() {
        out.SetNull();
        fCoinBase = false;
        fSegSig = false;
        nHeight = 0;
        nTxIndex = 0;
    }

    //! empty constructor
    Coin()
    : fCoinBase(false)
    , fSegSig(false)
    , nHeight(0)
    , nTxIndex(0)
    { }

    bool IsCoinBase() const {
        return fCoinBase;
    }

    template<typename Stream>
    void Serialize(Stream &s) const {
        assert(!IsSpent());
        uint32_t code = (nHeight<<2) + (fSegSig << 1) + fCoinBase;
        ::Serialize(s, VARINT(code));
        ::Serialize(s, VARINT(nTxIndex));
        out.WriteToStream(s, (fSegSig ? CTransaction::SEGSIG_ACTIVATION_VERSION : CTransaction::SEGSIG_ACTIVATION_VERSION-1));
    }

    template<typename Stream>
    void Unserialize(Stream &s) {
        uint32_t code = 0;
        ::Unserialize(s, VARINT(code));
        nHeight = ( ((code  & 0b11111111111111111111111111111100) >> 2) );
        fSegSig = ( (code   & 0b00000000000000000000000000000010) > 0 );
        fCoinBase = ( (code & 0b00000000000000000000000000000001) > 0 );
        ::Unserialize(s, VARINT(nTxIndex));
        out.ReadFromStream(s, (fSegSig ? CTransaction::SEGSIG_ACTIVATION_VERSION : CTransaction::SEGSIG_ACTIVATION_VERSION-1));
    }

    bool IsSpent() const {
        return out.IsNull();
    }

    void Spend() {
        out.SetNull();
    }

    size_t DynamicMemoryUsage() const {
        return memusage::DynamicUsage(out.output.scriptPubKey);
    }
};

class CoinUndo : public Coin
{
    public:
    uint256 prevhash;
    CoinUndo(Coin&& fromCoin, uint256 hashIn)
    : Coin(fromCoin)
    , prevhash(hashIn)
    {
    }
    
    //! empty constructor
    CoinUndo() : Coin() {}
    
    template<typename Stream>
    void Serialize(Stream &s) const
    {
        assert(!IsSpent());
{'fraction_non_alphanumeric': 0.11357443646271984, 'fraction_numerical': 0.04211047807778053, 'mean_word_length': 3.0254237288135593, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, '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': '2517690', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2651, 'n_words': 696}