-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ahmed Hilali
committed
Sep 9, 2019
1 parent
54467fb
commit 7818581
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |