Skip to content

Commit

Permalink
Merge pull request #5 from cakedefi/feature_PoS_staker
Browse files Browse the repository at this point in the history
PoS staker thread
  • Loading branch information
uzyn authored Sep 26, 2019
2 parents c74bde1 + 070b238 commit 7ee1c81
Show file tree
Hide file tree
Showing 39 changed files with 601 additions and 468 deletions.
2 changes: 0 additions & 2 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ BITCOIN_CORE_H = \
policy/settings.h \
pos.h \
pos_kernel.h \
pow.h \
protocol.h \
psbt.h \
random.h \
Expand Down Expand Up @@ -290,7 +289,6 @@ libbitcoin_server_a_SOURCES = \
policy/settings.cpp \
pos.cpp \
pos_kernel.cpp \
pow.cpp \
rest.cpp \
rpc/blockchain.cpp \
rpc/mining.cpp \
Expand Down
6 changes: 3 additions & 3 deletions src/bench/duplicate_inputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <coins.h>
#include <consensus/merkle.h>
#include <consensus/validation.h>
#include <pow.h>
#include <pos.h>
#include <txmempool.h>
#include <validation.h>

Expand All @@ -28,8 +28,8 @@ static void DuplicateInputs(benchmark::State& state)
LOCK(cs_main);
CBlockIndex* pindexPrev = ::ChainActive().Tip();
assert(pindexPrev != nullptr);
block.nBits = GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus());
block.nNonce = 0;
block.nBits = pos::GetNextWorkRequired(pindexPrev, &block, chainparams.GetConsensus().pos);
// block.nNonce = 0;
auto nHeight = pindexPrev->nHeight + 1;

// Make a coinbase TX
Expand Down
2 changes: 1 addition & 1 deletion src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& fr
r = from.nChainWork - to.nChainWork;
sign = -1;
}
r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
r = r * arith_uint256(params.pos.nTargetSpacing) / GetBlockProof(tip);
if (r.bits() > 63) {
return sign * std::numeric_limits<int64_t>::max();
}
Expand Down
55 changes: 30 additions & 25 deletions src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <consensus/params.h>
#include <flatfile.h>
#include <primitives/block.h>
#include <streams.h>
#include <tinyformat.h>
#include <uint256.h>

Expand Down Expand Up @@ -181,11 +182,13 @@ class CBlockIndex
uint256 hashMerkleRoot;
uint32_t nTime;
uint32_t nBits;
uint32_t nNonce;
boost::optional<CBlockHeader::PoS> proofOfStakeBody;

// proof-of-stake specific fields
uint64_t height;
uint64_t mintedBlocks;
uint256 stakeModifier; // hash modifier for proof-of-stake
std::vector<unsigned char> sig;
CKeyID minter; // memory only

//! (memory only) Sequential id assigned to distinguish order in which blocks are received.
int32_t nSequenceId;
Expand All @@ -209,15 +212,14 @@ class CBlockIndex
nSequenceId = 0;
nTimeMax = 0;

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

nVersion = 0;
hashMerkleRoot = uint256();
nTime = 0;
nBits = 0;
nNonce = 0;
stakeModifier = uint256{};
height = 0;
mintedBlocks = 0;
sig = {};
}

CBlockIndex()
Expand All @@ -233,9 +235,10 @@ class CBlockIndex
hashMerkleRoot = block.hashMerkleRoot;
nTime = block.nTime;
nBits = block.nBits;
nNonce = block.nNonce;
height = block.height;
mintedBlocks = block.mintedBlocks;
stakeModifier = block.stakeModifier;
proofOfStakeBody = block.proofOfStakeBody;
sig = block.sig;
}

FlatFilePos GetBlockPos() const {
Expand Down Expand Up @@ -265,9 +268,10 @@ class CBlockIndex
block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.stakeModifier = stakeModifier;
block.proofOfStakeBody = proofOfStakeBody;
block.height = height;
block.mintedBlocks = mintedBlocks;
block.sig = sig;
return block;
}

Expand Down Expand Up @@ -343,14 +347,16 @@ class CBlockIndex
return false;
}

bool IsProofOfStake() const
{
return (bool) proofOfStakeBody;
}

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

uint256 GetHashToSign() const
{
CDataStream ss(SER_GETHASH, 0);
ss << nVersion << pprev->GetBlockHash() << hashMerkleRoot << nTime << nBits << height << mintedBlocks << stakeModifier;
return Hash(ss.begin(), ss.end());
}

//! Efficiently find an ancestor of this block.
CBlockIndex* GetAncestor(int height);
const CBlockIndex* GetAncestor(int height) const;
Expand Down Expand Up @@ -395,18 +401,15 @@ 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);
READWRITE(hashMerkleRoot);
READWRITE(nTime);
READWRITE(nBits);
READWRITE(nNonce);
READWRITE(height);
READWRITE(mintedBlocks);
READWRITE(sig);
}

uint256 GetBlockHash() const
Expand All @@ -417,9 +420,11 @@ class CDiskBlockIndex : public CBlockIndex
block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime;
block.nBits = nBits;
block.nNonce = nNonce;
block.stakeModifier = stakeModifier;
block.proofOfStakeBody = proofOfStakeBody;
block.stakeModifier = stakeModifier;
block.height = height;
block.mintedBlocks = mintedBlocks;
block.sig = sig;

return block.GetHash();
}

Expand Down
Loading

0 comments on commit 7ee1c81

Please sign in to comment.