Skip to content

Commit

Permalink
Update CBlockIndex for PoS. Fix coinbase maturity in consensus rules
Browse files Browse the repository at this point in the history
  • Loading branch information
akyo8 committed Feb 18, 2021
1 parent b840689 commit 40b2167
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
70 changes: 70 additions & 0 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

#include <vector>

#define BLOCK_PROOF_OF_STAKE 0x01 // is proof-of-stake block
#define BLOCK_STAKE_ENTROPY 0x02 // entropy bit for stake modifier
#define BLOCK_STAKE_MODIFIER 0x04

/**
* Maximum amount of time that a block timestamp is allowed to exceed the
* current network-adjusted time before the block will be accepted.
Expand Down Expand Up @@ -63,6 +67,11 @@ class CBlockFileInfo
nBlocks = 0;
nSize = 0;
nUndoSize = 0;
nFlags = 0;
nStakeModifier = 0;
hashProofOfStake = arith_uint256();
prevoutStake.SetNull();
nStakeTime = 0;
nHeightFirst = 0;
nHeightLast = 0;
nTimeFirst = 0;
Expand Down Expand Up @@ -180,14 +189,26 @@ class CBlockIndex
uint32_t nBits{0};
uint32_t nNonce{0};

unsigned int nFlags; // ppcoin: block index flags

uint64_t nStakeModifier; // hash modifier for proof-of-stake

// proof-of-stake specific fields
COutPoint prevoutStake;
unsigned int nStakeTime;
arith_uint256 hashProofOfStake;

//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
int32_t nSequenceId{0};

//! (memory only) Maximum nTime in the chain up to and including this block.
unsigned int nTimeMax{0};



CBlockIndex()
{

}

explicit CBlockIndex(const CBlockHeader& block)
Expand Down Expand Up @@ -301,6 +322,47 @@ class CBlockIndex
return false;
}

bool IsProofOfWork() const
{
return !(nFlags & BLOCK_PROOF_OF_STAKE);
}

bool IsProofOfStake() const
{
return (nFlags & BLOCK_PROOF_OF_STAKE);
}

void SetProofOfStake()
{
nFlags |= BLOCK_PROOF_OF_STAKE;
}

unsigned int GetStakeEntropyBit() const
{
return ((nFlags & BLOCK_STAKE_ENTROPY) >> 1);
}

bool SetStakeEntropyBit(unsigned int nEntropyBit)
{
if (nEntropyBit > 1)
return false;
nFlags |= (nEntropyBit ? BLOCK_STAKE_ENTROPY : 0);
return true;
}

void SetStakeModifier(uint64_t nModifier, bool fGeneratedStakeModifier)
{
nStakeModifier = nModifier;
if (fGeneratedStakeModifier)
nFlags |= BLOCK_STAKE_MODIFIER;
}

bool GeneratedStakeModifier() const
{
return (nFlags & BLOCK_STAKE_MODIFIER);
}


//! Build the skiplist pointer for this entry.
void BuildSkip();

Expand All @@ -319,11 +381,15 @@ const CBlockIndex* LastCommonAncestor(const CBlockIndex* pa, const CBlockIndex*
/** Used to marshal pointers into hashes for db storage. */
class CDiskBlockIndex : public CBlockIndex
{
private:
uint256 blockHash;

public:
uint256 hashPrev;

CDiskBlockIndex() {
hashPrev = uint256();

}

explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
Expand All @@ -349,6 +415,8 @@ class CDiskBlockIndex : public CBlockIndex
READWRITE(obj.nTime);
READWRITE(obj.nBits);
READWRITE(obj.nNonce);
READWRITE(blockHash);

}

uint256 GetBlockHash() const
Expand All @@ -360,6 +428,8 @@ class CDiskBlockIndex : public CBlockIndex
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
const_cast<CDiskBlockIndex*>(this)->blockHash = block.GetHash();

return block.GetHash();
}

Expand Down
7 changes: 4 additions & 3 deletions src/consensus/consensus.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#ifndef BITCOIN_CONSENSUS_CONSENSUS_H
#define BITCOIN_CONSENSUS_CONSENSUS_H

#include <stdlib.h>
#include <stdint.h>
#include <stdlib.h>

/** The maximum allowed size for a serialized block, in bytes (only for buffer size limits) */
static const unsigned int MAX_BLOCK_SERIALIZED_SIZE = 4000000;
Expand All @@ -16,11 +16,12 @@ 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;
static const int COINBASE_MATURITY = 40;
static const int COINBASE_MATURITY_TESTNET = 10;

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
static const size_t MIN_TRANSACTION_WEIGHT = WITNESS_SCALE_FACTOR * 60; // 60 is the lower bound for the size of a valid serialized CTransaction
static const size_t MIN_SERIALIZABLE_TRANSACTION_WEIGHT = WITNESS_SCALE_FACTOR * 10; // 10 is the lower bound for the size of a serialized CTransaction

/** Flags for nSequence and nLockTime locks */
Expand Down

0 comments on commit 40b2167

Please sign in to comment.