Skip to content

Commit

Permalink
Port log events storage
Browse files Browse the repository at this point in the history
  • Loading branch information
timemarkovqtum committed Jun 20, 2024
1 parent 1202459 commit 953066d
Show file tree
Hide file tree
Showing 15 changed files with 705 additions and 50 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ libbitcoin_consensus_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
libbitcoin_consensus_a_SOURCES = \
arith_uint256.cpp \
arith_uint256.h \
consensus/consensus.cpp \
consensus/amount.h \
consensus/merkle.cpp \
consensus/merkle.h \
Expand Down
38 changes: 30 additions & 8 deletions src/coins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ void CCoinsViewCache::AddCoin(const COutPoint &outpoint, Coin&& coin, bool possi
it->second.coin = std::move(coin);
it->second.flags |= CCoinsCacheEntry::DIRTY | (fresh ? CCoinsCacheEntry::FRESH : 0);
cachedCoinsUsage += it->second.coin.DynamicMemoryUsage();
TRACE5(utxocache, add,
TRACE6(utxocache, add,
outpoint.hash.data(),
(uint32_t)outpoint.n,
(uint32_t)it->second.coin.nHeight,
(int64_t)it->second.coin.out.nValue,
(bool)it->second.coin.IsCoinBase());
(bool)it->second.coin.IsCoinBase(),
(bool)it->second.coin.IsCoinStake());
}

void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coin) {
Expand All @@ -116,25 +117,27 @@ void CCoinsViewCache::EmplaceCoinInternalDANGER(COutPoint&& outpoint, Coin&& coi

void AddCoins(CCoinsViewCache& cache, const CTransaction &tx, int nHeight, bool check_for_overwrite) {
bool fCoinbase = tx.IsCoinBase();
bool fCoinstake = tx.IsCoinStake();
const Txid& txid = tx.GetHash();
for (size_t i = 0; i < tx.vout.size(); ++i) {
bool overwrite = check_for_overwrite ? cache.HaveCoin(COutPoint(txid, i)) : fCoinbase;
// Coinbase transactions can always be overwritten, in order to correctly
// deal with the pre-BIP30 occurrences of duplicate coinbase transactions.
cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase), overwrite);
cache.AddCoin(COutPoint(txid, i), Coin(tx.vout[i], nHeight, fCoinbase, fCoinstake), overwrite);
}
}

bool CCoinsViewCache::SpendCoin(const COutPoint &outpoint, Coin* moveout) {
CCoinsMap::iterator it = FetchCoin(outpoint);
if (it == cacheCoins.end()) return false;
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
TRACE5(utxocache, spent,
TRACE6(utxocache, spent,
outpoint.hash.data(),
(uint32_t)outpoint.n,
(uint32_t)it->second.coin.nHeight,
(int64_t)it->second.coin.out.nValue,
(bool)it->second.coin.IsCoinBase());
(bool)it->second.coin.IsCoinBase(),
(bool)it->second.coin.IsCoinStake());
if (moveout) {
*moveout = std::move(it->second.coin);
}
Expand Down Expand Up @@ -285,12 +288,13 @@ void CCoinsViewCache::Uncache(const COutPoint& hash)
CCoinsMap::iterator it = cacheCoins.find(hash);
if (it != cacheCoins.end() && it->second.flags == 0) {
cachedCoinsUsage -= it->second.coin.DynamicMemoryUsage();
TRACE5(utxocache, uncache,
TRACE6(utxocache, uncache,
hash.hash.data(),
(uint32_t)hash.n,
(uint32_t)it->second.coin.nHeight,
(int64_t)it->second.coin.out.nValue,
(bool)it->second.coin.IsCoinBase());
(bool)it->second.coin.IsCoinBase(),
(bool)it->second.coin.IsCoinStake());
cacheCoins.erase(it);
}
}
Expand All @@ -299,6 +303,18 @@ unsigned int CCoinsViewCache::GetCacheSize() const {
return cacheCoins.size();
}

CAmount CCoinsViewCache::GetValueIn(const CTransaction& tx) const
{
if (tx.IsCoinBase())
return 0;

CAmount nResult = 0;
for (unsigned int i = 0; i < tx.vin.size(); i++)
nResult += AccessCoin(tx.vin[i].prevout).out.nValue;

return nResult;
}

bool CCoinsViewCache::HaveInputs(const CTransaction& tx) const
{
if (!tx.IsCoinBase()) {
Expand Down Expand Up @@ -339,7 +355,7 @@ void CCoinsViewCache::SanityCheck() const
}

static const size_t MIN_TRANSACTION_OUTPUT_WEIGHT = WITNESS_SCALE_FACTOR * ::GetSerializeSize(CTxOut());
static const size_t MAX_OUTPUTS_PER_BLOCK = MAX_BLOCK_WEIGHT / MIN_TRANSACTION_OUTPUT_WEIGHT;
static const size_t MAX_OUTPUTS_PER_BLOCK = dgpMaxBlockWeight / MIN_TRANSACTION_OUTPUT_WEIGHT;

const Coin& AccessByTxid(const CCoinsViewCache& view, const Txid& txid)
{
Expand Down Expand Up @@ -377,3 +393,9 @@ bool CCoinsViewErrorCatcher::GetCoin(const COutPoint &outpoint, Coin &coin) cons
bool CCoinsViewErrorCatcher::HaveCoin(const COutPoint &outpoint) const {
return ExecuteBackedWrapper([&]() { return CCoinsViewBacked::HaveCoin(outpoint); }, m_err_callbacks);
}

const CTxOut &CCoinsViewCache::GetOutputFor(const CTxIn& input) const
{
const Coin& coins = AccessCoin(input.prevout);
return coins.out;
}
92 changes: 85 additions & 7 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,72 @@
#include <functional>
#include <unordered_map>

////////////////////////////////////////////////////////////////// // qtum
struct CSpentIndexKey {
uint256 txid;
unsigned int outputIndex;

SERIALIZE_METHODS(CSpentIndexKey, obj) { READWRITE(obj.txid, obj.outputIndex); }

CSpentIndexKey(uint256 t, unsigned int i) {
txid = t;
outputIndex = i;
}

CSpentIndexKey() {
SetNull();
}

void SetNull() {
txid.SetNull();
outputIndex = 0;
}
};

struct CSpentIndexValue {
uint256 txid;
unsigned int inputIndex;
int blockHeight;
CAmount satoshis;
int addressType;
uint256 addressHash;

SERIALIZE_METHODS(CSpentIndexValue, obj) { READWRITE(obj.txid, obj.inputIndex, obj.blockHeight, obj.satoshis, obj.addressType, obj.addressHash); }

CSpentIndexValue(uint256 t, unsigned int i, int h, CAmount s, int type, uint256 a) {
txid = t;
inputIndex = i;
blockHeight = h;
satoshis = s;
addressType = type;
addressHash = a;
}

CSpentIndexValue() {
SetNull();
}

void SetNull() {
txid.SetNull();
inputIndex = 0;
blockHeight = 0;
satoshis = 0;
addressType = 0;
addressHash.SetNull();
}

bool IsNull() const {
return txid.IsNull();
}
};
//////////////////////////////////////////////////////////////////

/**
* A UTXO entry.
*
* Serialized format:
* - VARINT((coinbase ? 1 : 0) | (height << 1))
* - VARINT((coinbase ? 1 : 0) | (height << 2))
* - VARINT((coinstake ? 2 : 0) | (height << 2))
* - the non-spent CTxOut (via TxOutCompression)
*/
class Coin
Expand All @@ -39,29 +100,33 @@ class Coin
unsigned int fCoinStake : 1;

//! at which height this containing transaction was included in the active block chain
uint32_t nHeight : 31;
uint32_t nHeight : 30;

//! construct a Coin from a CTxOut and height/coinbase information.
Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), nHeight(nHeightIn) {}
Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn) : out(outIn), fCoinBase(fCoinBaseIn),nHeight(nHeightIn) {}
Coin(CTxOut&& outIn, int nHeightIn, bool fCoinBaseIn, bool fCoinStakeIn) : out(std::move(outIn)), fCoinBase(fCoinBaseIn), fCoinStake(fCoinStakeIn), nHeight(nHeightIn) {}
Coin(const CTxOut& outIn, int nHeightIn, bool fCoinBaseIn, bool fCoinStakeIn) : out(outIn), fCoinBase(fCoinBaseIn), fCoinStake(fCoinStakeIn), nHeight(nHeightIn) {}

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

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

bool IsCoinBase() const {
return fCoinBase;
}
bool IsCoinStake() const {
return fCoinStake;
}

template<typename Stream>
void Serialize(Stream &s) const {
assert(!IsSpent());
uint32_t code = nHeight * uint32_t{2} + fCoinBase;
uint32_t code = (nHeight << 2) + (fCoinBase ? 1 : 0) + (fCoinStake ? 2 : 0);
::Serialize(s, VARINT(code));
::Serialize(s, Using<TxOutCompression>(out));
}
Expand All @@ -70,8 +135,9 @@ class Coin
void Unserialize(Stream &s) {
uint32_t code = 0;
::Unserialize(s, VARINT(code));
nHeight = code >> 1;
nHeight = code >> 2;
fCoinBase = code & 1;
fCoinStake = (code >> 1) & 1;
::Unserialize(s, Using<TxOutCompression>(out));
}

Expand Down Expand Up @@ -331,6 +397,16 @@ class CCoinsViewCache : public CCoinsViewBacked
//! Calculate the size of the cache (in bytes)
size_t DynamicMemoryUsage() const;

/**
* Amount of bitcoins coming in to a transaction
* Note that lightweight clients may not know anything besides the hash of previous transactions,
* so may not be able to calculate this.
*
* @param[in] tx transaction for which we are checking input total
* @return Sum of value of all inputs (scriptSigs)
*/
CAmount GetValueIn(const CTransaction& tx) const;

//! Check whether all prevouts of the transaction are present in the UTXO set represented by this view
bool HaveInputs(const CTransaction& tx) const;

Expand All @@ -344,6 +420,8 @@ class CCoinsViewCache : public CCoinsViewBacked
//! Run an internal sanity check on the cache data structure. */
void SanityCheck() const;

const CTxOut &GetOutputFor(const CTxIn& input) const;

private:
/**
* @note this is marked const, but may actually append to `cacheCoins`, increasing
Expand Down
12 changes: 12 additions & 0 deletions src/consensus/consensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@
#include <stdint.h>

/** The maximum allowed size for a serialized block, in bytes (only for buffer size limits) */
extern unsigned int dgpMaxBlockSerSize;
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE = 4000000;
/** The maximum allowed weight for a block, see BIP 141 (network rule) */
extern unsigned int dgpMaxBlockWeight;

extern unsigned int dgpMaxBlockSize; // qtum
static const unsigned int MAX_BLOCK_WEIGHT = 4000000;
/** The maximum allowed number of signature check operations in a block (network rule) */
static const int64_t MAX_BLOCK_SIGOPS_COST = 80000;
/** Coinbase transaction outputs can only be spent after this number of new blocks (network rule) */
static const int COINBASE_MATURITY = 100;
extern int64_t dgpMaxBlockSigOps;

extern unsigned int dgpMaxProtoMsgLength;

extern unsigned int dgpMaxTxSigOps;

static const int MAX_TRANSACTION_BASE_SIZE = 1000000;
static const int WITNESS_SCALE_FACTOR = 4;

static const size_t MIN_TRANSACTION_WEIGHT = WITNESS_SCALE_FACTOR * 60; // 60 is the lower bound for the size of a valid serialized CTransaction
Expand All @@ -27,4 +37,6 @@ static const size_t MIN_SERIALIZABLE_TRANSACTION_WEIGHT = WITNESS_SCALE_FACTOR *
/** Interpret sequence numbers as relative lock-time constraints. */
static constexpr unsigned int LOCKTIME_VERIFY_SEQUENCE = (1 << 0);

void updateBlockSizeParams(unsigned int newBlockSize);

#endif // BITCOIN_CONSENSUS_CONSENSUS_H
12 changes: 8 additions & 4 deletions src/core_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
const bool have_undo = txundo != nullptr;
CAmount amt_total_in = 0;
CAmount amt_total_out = 0;
bool isCoinStake = tx.IsCoinStake();

for (unsigned int i = 0; i < tx.vin.size(); i++) {
const CTxIn& txin = tx.vin[i];
Expand All @@ -214,14 +215,17 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
const Coin& prev_coin = txundo->vprevout[i];
const CTxOut& prev_txout = prev_coin.out;

amt_total_in += prev_txout.nValue;
if(!isCoinStake)
{
amt_total_in += prev_txout.nValue;
}

if (verbosity == TxVerbosity::SHOW_DETAILS_AND_PREVOUT) {
UniValue o_script_pub_key(UniValue::VOBJ);
ScriptToUniv(prev_txout.scriptPubKey, /*out=*/o_script_pub_key, /*include_hex=*/true, /*include_address=*/true);

UniValue p(UniValue::VOBJ);
p.pushKV("generated", bool(prev_coin.fCoinBase));
p.pushKV("generated", bool(prev_coin.fCoinBase || prev_coin.fCoinStake));
p.pushKV("height", uint64_t(prev_coin.nHeight));
p.pushKV("value", ValueFromAmount(prev_txout.nValue));
p.pushKV("scriptPubKey", o_script_pub_key);
Expand All @@ -247,13 +251,13 @@ void TxToUniv(const CTransaction& tx, const uint256& block_hash, UniValue& entry
out.pushKV("scriptPubKey", o);
vout.push_back(out);

if (have_undo) {
if (have_undo && !isCoinStake) {
amt_total_out += txout.nValue;
}
}
entry.pushKV("vout", vout);

if (have_undo) {
if (have_undo && !isCoinStake) {
const CAmount fee = amt_total_in - amt_total_out;
CHECK_NONFATAL(MoneyRange(fee));
entry.pushKV("fee", ValueFromAmount(fee));
Expand Down
4 changes: 2 additions & 2 deletions src/index/coinstatsindex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)

for (uint32_t j = 0; j < tx->vout.size(); ++j) {
const CTxOut& out{tx->vout[j]};
Coin coin{out, block.height, tx->IsCoinBase()};
Coin coin{out, block.height, tx->IsCoinBase(), tx->IsCoinStake()};
COutPoint outpoint{tx->GetHash(), j};

// Skip unspendable coins
Expand Down Expand Up @@ -435,7 +435,7 @@ bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex
for (uint32_t j = 0; j < tx->vout.size(); ++j) {
const CTxOut& out{tx->vout[j]};
COutPoint outpoint{tx->GetHash(), j};
Coin coin{out, pindex->nHeight, tx->IsCoinBase()};
Coin coin{out, pindex->nHeight, tx->IsCoinBase(), tx->IsCoinStake()};

// Skip unspendable coins
if (coin.out.scriptPubKey.IsUnspendable()) {
Expand Down
Loading

0 comments on commit 953066d

Please sign in to comment.