Skip to content

Commit

Permalink
add pos_kernel base functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Hilali committed Sep 9, 2019
1 parent 54467fb commit 7818581
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ BITCOIN_CORE_H = \
policy/policy.h \
policy/rbf.h \
policy/settings.h \
pos_kernel.h \
pow.h \
protocol.h \
psbt.h \
Expand Down Expand Up @@ -286,6 +287,7 @@ libbitcoin_server_a_SOURCES = \
policy/fees.cpp \
policy/rbf.cpp \
policy/settings.cpp \
pos_kernel.cpp \
pow.cpp \
rest.cpp \
rpc/blockchain.cpp \
Expand Down
53 changes: 53 additions & 0 deletions src/pos_kernel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include <pos_kernel.h>
#include <wallet/wallet.h>
#include <txdb.h>
#include <validation.h>
#include <arith_uint256.h>

namespace pos {

uint256 CalcKernelHash(uint256 stakeModifier, int64_t coinstakeTime, const COutPoint& prevout,
const Consensus::Params& params) {
// Calculate hash
CDataStream ss(SER_GETHASH, 0);
ss << stakeModifier << coinstakeTime << prevout.hash << prevout.n;
return Hash(ss.begin(), ss.end());
}

CheckKernelHashRes
CheckKernelHash(uint256 stakeModifier, uint32_t nBits, int64_t coinstakeTime, CAmount coinstakeAmount,
const COutPoint& prevout, const Consensus::Params& params) {
if (prevout.IsNull() || coinstakeAmount <= 0) {
return {false, arith_uint256{}};
}

// Base target
arith_uint256 targetProofOfStake;
targetProofOfStake.SetCompact(nBits);

const arith_uint256 hashProofOfStake = UintToArith256(
CalcKernelHash(stakeModifier, coinstakeTime, prevout, params));

// Now check if proof-of-stake hash meets target protocol
if ((hashProofOfStake / (uint64_t) coinstakeAmount) > targetProofOfStake) {
return {false, hashProofOfStake};
}

return {true, hashProofOfStake};
}

uint256 ComputeStakeModifier_PoS(uint256 prevStakeModifier, const COutPoint& prevout) {
// Calculate hash
CDataStream ss(SER_GETHASH, 0);
ss << prevStakeModifier << prevout.hash << prevout.n;
return Hash(ss.begin(), ss.end());
}

uint256 ComputeStakeModifier_PoW(uint256 prevStakeModifier, const uint256& prevBlockHash) {
// Calculate hash
CDataStream ss(SER_GETHASH, 0);
ss << prevStakeModifier << prevBlockHash;
return Hash(ss.begin(), ss.end());
}

}
47 changes: 47 additions & 0 deletions src/pos_kernel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include <uint256.h>
#include <arith_uint256.h>
#include <consensus/params.h>
#include <streams.h>
#include <amount.h>

class CWallet;

class COutPoint;

class CBlock;

class CTransaction;

class CCoinsViewCache;

namespace pos {

struct CheckKernelHashRes {
bool hashOk;
arith_uint256 hashProofOfStake;
};

/// Calculate PoS kernel hash
uint256
CalcKernelHash(uint256 stakeModifier, int64_t coinstakeTime, const COutPoint& prevout, const Consensus::Params& params);

/// Check whether stake kernel meets hash target
/// Sets hashProofOfStake, hashOk is true of the kernel meets hash target
CheckKernelHashRes
CheckKernelHash(uint256 stakeModifier, uint32_t nBits, int64_t coinstakeTime, CAmount coinstakeAmount,
const COutPoint& prevout, const Consensus::Params& params);

/// Stake Modifier (hash modifier of proof-of-stake):
/// The purpose of stake modifier is to prevent a txout (coin) owner from
/// computing future proof-of-stake generated by this txout at the time
/// of transaction confirmation. To meet kernel protocol, the txout
/// must hash with a future stake modifier to generate the proof.
uint256 ComputeStakeModifier_PoS(uint256 prevStakeModifier, const COutPoint& prevout);

/// Theoretically, blockHash can be used by miner to make next PoS block easier for him.
/// However, PoW blocks are not supported in mainnet.
uint256 ComputeStakeModifier_PoW(uint256 prevStakeModifier, const uint256& prevBlockHash);

}

0 comments on commit 7818581

Please sign in to comment.