Skip to content

Commit

Permalink
add proofOfStake
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Hilali committed Sep 11, 2019
1 parent a8cd45f commit 19e52cc
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
18 changes: 17 additions & 1 deletion src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ class CBlockIndex
nSequenceId = 0;
nTimeMax = 0;

// PoS
proofOfStakeBody = boost::optional<CBlockHeader::PoS>{};
stakeModifier = uint256{};

nVersion = 0;
hashMerkleRoot = uint256();
nTime = 0;
Expand All @@ -230,6 +234,8 @@ class CBlockIndex
nTime = block.nTime;
nBits = block.nBits;
nNonce = block.nNonce;
stakeModifier = block.stakeModifier;
proofOfStakeBody = block.proofOfStakeBody;
}

FlatFilePos GetBlockPos() const {
Expand Down Expand Up @@ -260,6 +266,8 @@ class CBlockIndex
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.stakeModifier = stakeModifier;
block.proofOfStakeBody = proofOfStakeBody;
return block;
}

Expand Down Expand Up @@ -305,8 +313,9 @@ class CBlockIndex

std::string ToString() const
{
return strprintf("CBlockIndex(pprev=%p, nHeight=%d, merkle=%s, hashBlock=%s)",
return strprintf("CBlockIndex(pprev=%p, nHeight=%d, stakeModifier=(%s), merkle=%s, hashBlock=%s)",
pprev, nHeight,
stakeModifier.ToString(),
hashMerkleRoot.ToString(),
GetBlockHash().ToString());
}
Expand Down Expand Up @@ -386,6 +395,11 @@ class CDiskBlockIndex : public CBlockIndex
if (nStatus & BLOCK_HAVE_UNDO)
READWRITE(VARINT(nUndoPos));

//PoS serialization
CBlockHeader::PoS loc_proofOfStake = proofOfStakeBody ? *proofOfStakeBody : CBlockHeader::PoS{};
READWRITE(loc_proofOfStake);
proofOfStakeBody = loc_proofOfStake;

// block header
READWRITE(this->nVersion);
READWRITE(hashPrev);
Expand All @@ -404,6 +418,8 @@ class CDiskBlockIndex : public CBlockIndex
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.stakeModifier = stakeModifier;
block.proofOfStakeBody = proofOfStakeBody;
return block.GetHash();
}

Expand Down
3 changes: 3 additions & 0 deletions src/miner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <policy/feerate.h>
#include <policy/policy.h>
#include <pow.h>
#include <pos_kernel.h>
#include <primitives/transaction.h>
#include <script/standard.h>
#include <timedata.h>
Expand Down Expand Up @@ -162,6 +163,8 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
pblock->nNonce = 0;
pblock->stakeModifier = uint256{}; // SS

pblocktemplate->vTxSigOpsCost[0] = WITNESS_SCALE_FACTOR * GetLegacySigOpCount(*pblock->vtx[0]);

CValidationState state;
Expand Down
6 changes: 0 additions & 6 deletions src/pos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,4 @@ namespace pos {
}
return CheckHeaderSignature(block);
}

uint32_t GetNextTargetRequired(const CBlockIndex* pindexLast, const CBlockHeader* pblock,
const Consensus::Params& params) {
return ::GetNextWorkRequired(pindexLast, pblock, params);
}

}
4 changes: 0 additions & 4 deletions src/pos.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,5 @@ namespace pos {
bool CheckProofOfStake(const CBlockIndex* pindexPrev, const CBlock& block, CCoinsViewCache& view,
const Consensus::Params& params);

/// @return nBits
uint32_t GetNextTargetRequired(const CBlockIndex* pindexLast, const CBlockHeader* pblock,
const Consensus::Params& params);

}

2 changes: 2 additions & 0 deletions src/primitives/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <hash.h>
#include <tinyformat.h>
#include <util/strencodings.h>
#include <crypto/common.h>

uint256 CBlockHeader::GetHash() const
Expand All @@ -28,6 +29,7 @@ std::string CBlock::ToString() const
hashPrevBlock.ToString(),
hashMerkleRoot.ToString(),
nTime, nBits, nNonce,
proofOfStakeBody ? HexStr(proofOfStakeBody->sig.begin(), proofOfStakeBody->sig.end()) : "Empty",
vtx.size());
for (const auto& tx : vtx) {
s << " " << tx->ToString() << "\n";
Expand Down
18 changes: 14 additions & 4 deletions src/primitives/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class CBlockHeader
READWRITE(coinstakePrevout);
READWRITE(pubKeyHash);
READWRITE(coinstakeAmount);
bool hashToSignAction = s.GetType() & SER_GETSIGNHASH;
if (!hashToSignAction) {
READWRITE(sig);
}
// bool hashToSignAction = s.GetType() & SER_GETSIGNHASH;
// if (!hashToSignAction) {
// READWRITE(sig);
// }
}
};

Expand Down Expand Up @@ -73,6 +73,12 @@ class CBlockHeader
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
READWRITE(stakeModifier);

//PoS serialization
PoS loc_proofOfStake = proofOfStakeBody ? *proofOfStakeBody : PoS{};
READWRITE(loc_proofOfStake);
proofOfStakeBody = loc_proofOfStake;
}

void SetNull()
Expand All @@ -83,6 +89,8 @@ class CBlockHeader
nTime = 0;
nBits = 0;
nNonce = 0;
stakeModifier.SetNull();
proofOfStakeBody = boost::optional<PoS>{};
}

bool IsNull() const
Expand Down Expand Up @@ -160,6 +168,8 @@ class CBlock : public CBlockHeader
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.stakeModifier = stakeModifier;
block.proofOfStakeBody = proofOfStakeBody;
return block;
}

Expand Down
4 changes: 4 additions & 0 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
pindexNew->nStatus = diskindex.nStatus;
pindexNew->nTx = diskindex.nTx;

//PoS
pindexNew->stakeModifier = diskindex.stakeModifier;
pindexNew->proofOfStakeBody = diskindex.proofOfStakeBody;

if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams))
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());

Expand Down

0 comments on commit 19e52cc

Please sign in to comment.