X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FStakeModifier.cs;h=b3f7876712255143d28efc617c698ca1f37ba527;hb=624ac1021490395614a0cbee619c79860c22061a;hp=e19a8f05d84a49cb179857142d3fd9ea4fe50e30;hpb=f8e0e5777cdd3fbc2a0c7148731d301899dfea5f;p=NovacoinLibrary.git
diff --git a/Novacoin/StakeModifier.cs b/Novacoin/StakeModifier.cs
index e19a8f0..b3f7876 100644
--- a/Novacoin/StakeModifier.cs
+++ b/Novacoin/StakeModifier.cs
@@ -1,4 +1,23 @@
-using System;
+/**
+* Novacoin classes library
+* Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+* This program is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see .
+*/
+
+
+using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
@@ -6,6 +25,9 @@ using System.Linq;
namespace Novacoin
{
+ ///
+ /// Stake modifier calculation. Doesn't work properly, for now.
+ ///
public class StakeModifier
{
///
@@ -95,7 +117,7 @@ namespace Novacoin
/// Get stake modifier selection interval (in seconds)
///
///
- static long GetStakeModifierSelectionInterval()
+ internal static long GetStakeModifierSelectionInterval()
{
long nSelectionInterval = 0;
for (int nSection = 0; nSection < 64; nSection++)
@@ -115,14 +137,14 @@ namespace Novacoin
/// Previous value of stake modifier.
/// Selection result.
///
- static bool SelectBlockFromCandidates(List> sortedByTimestamp, Dictionary mapSelectedBlocks, long nSelectionIntervalStop, long nStakeModifierPrev, ref CBlockStoreItem selectedCursor)
+ internal static bool SelectBlockFromCandidates(List> sortedByTimestamp, Dictionary mapSelectedBlocks, long nSelectionIntervalStop, long nStakeModifierPrev, ref CBlockStoreItem selectedCursor)
{
bool fSelected = false;
uint256 hashBest = 0;
selectedCursor = null;
foreach (var item in sortedByTimestamp)
{
- CBlockStoreItem cursor = CBlockStore.Instance.GetCursor(item.Item2);
+ CBlockStoreItem cursor = CBlockStore.Instance.GetMapCursor(item.Item2);
if (cursor == null)
{
@@ -191,7 +213,7 @@ namespace Novacoin
/// additional bits in the stake modifier, even after generating a chain of
/// blocks.
///
- bool ComputeNextStakeModifier(CBlockStoreItem cursorCurrent, ref long nStakeModifier, ref bool fGeneratedStakeModifier)
+ public static bool ComputeNextStakeModifier(CBlockStoreItem cursorCurrent, ref long nStakeModifier, ref bool fGeneratedStakeModifier)
{
nStakeModifier = 0;
fGeneratedStakeModifier = false;
@@ -258,7 +280,7 @@ namespace Novacoin
}
// write the entropy bit of the selected block
- nStakeModifierNew |= ((cursor.StakeEntropyBit) << nRound);
+ nStakeModifierNew |= (((long)cursor.StakeEntropyBit) << nRound);
// add the selected block from candidates to selected list
mapSelectedBlocks.Add(cursor.Hash, cursor);
@@ -276,7 +298,7 @@ namespace Novacoin
static bool GetKernelStakeModifier(uint256 hashBlockFrom, ref long nStakeModifier, ref uint nStakeModifierHeight, ref uint nStakeModifierTime)
{
nStakeModifier = 0;
- var cursorFrom = CBlockStore.Instance.GetCursor(hashBlockFrom);
+ var cursorFrom = CBlockStore.Instance.GetMapCursor(hashBlockFrom);
if (cursorFrom == null)
{
return false; // Block not indexed
@@ -308,7 +330,7 @@ namespace Novacoin
return true;
}
- bool GetKernelStakeModifier(uint256 hashBlockFrom, ref long nStakeModifier)
+ public static bool GetKernelStakeModifier(uint256 hashBlockFrom, ref long nStakeModifier)
{
uint nStakeModifierHeight = 0;
uint nStakeModifierTime = 0;
@@ -316,14 +338,13 @@ namespace Novacoin
return GetKernelStakeModifier(hashBlockFrom, ref nStakeModifier, ref nStakeModifierHeight, ref nStakeModifierTime);
}
- bool CheckStakeKernelHash(uint nBits, CBlock blockFrom, uint nTxPrevOffset, CTransaction txPrev, COutPoint prevout, uint nTimeTx, ref uint256 hashProofOfStake, ref uint256 targetProofOfStake)
+ public static bool CheckStakeKernelHash(uint nBits, uint256 hashBlockFrom, uint nTimeBlockFrom, uint nTxPrevOffset, CTransaction txPrev, COutPoint prevout, uint nTimeTx, ref uint256 hashProofOfStake, ref uint256 targetProofOfStake)
{
if (nTimeTx < txPrev.nTime)
{
return false; // Transaction timestamp violation
}
- uint nTimeBlockFrom = blockFrom.header.nTime;
if (nTimeBlockFrom + nStakeMinAge > nTimeTx) // Min age requirement
{
return false; // Min age violation
@@ -333,7 +354,6 @@ namespace Novacoin
nTargetPerCoinDay.Compact = nBits;
ulong nValueIn = txPrev.vout[prevout.n].nValue;
- uint256 hashBlockFrom = blockFrom.header.Hash;
uint256 nCoinDayWeight = new uint256(nValueIn) * GetWeight(txPrev.nTime, nTimeTx) / CTransaction.nCoin / (24 * 60 * 60);
targetProofOfStake = nCoinDayWeight * nTargetPerCoinDay;
@@ -378,5 +398,72 @@ namespace Novacoin
return Math.Min(nIntervalEnd - nIntervalBeginning - nStakeMinAge, nStakeMaxAge);
}
+
+ internal static uint GetStakeModifierChecksum(CBlockStoreItem itemTemplate)
+ {
+ Contract.Assert(itemTemplate.prev != null || (uint256)itemTemplate.Hash == NetInfo.nHashGenesisBlock);
+
+ // Hash previous checksum with flags, hashProofOfStake and nStakeModifier
+ MemoryStream ss = new MemoryStream();
+ BinaryWriter writer = new BinaryWriter(ss);
+
+ if (itemTemplate.prev != null)
+ {
+ writer.Write(itemTemplate.prev.nStakeModifierChecksum);
+ }
+
+ writer.Write((uint)itemTemplate.BlockTypeFlag);
+
+ if (itemTemplate.IsProofOfStake)
+ {
+ writer.Write(itemTemplate.hashProofOfStake);
+ }
+ else
+ {
+ writer.Write(new uint256(0));
+ }
+ writer.Write(itemTemplate.nStakeModifier);
+
+ uint256 hashChecksum = CryptoUtils.ComputeHash256(ss.ToArray());
+ writer.Close();
+
+ hashChecksum >>= (256 - 32);
+
+ return (uint)hashChecksum.Low64;
+ }
+
+ internal static bool CheckProofOfStake(CTransaction tx, uint nBits, ref uint256 hashProofOfStake, ref uint256 targetProofOfStake)
+ {
+ if (!tx.IsCoinStake)
+ {
+ return false; // called on non-coinstake
+ }
+
+ // Kernel (input 0) must match the stake hash target per coin age (nBits)
+ CTxIn txin = tx.vin[0];
+
+ // Read block header
+
+ CBlock block = null;
+ CTransaction txPrev = null;
+ long nBlockPos = 0, nTxPos = 0;
+
+ if (!CBlockStore.Instance.GetBlockByTransactionID(txin.prevout.hash, ref block, ref txPrev, ref nBlockPos, ref nTxPos))
+ {
+ return false; // unable to read block of previous transaction
+ }
+
+ if (!ScriptCode.VerifyScript(txin.scriptSig, txPrev.vout[txin.prevout.n].scriptPubKey, tx, 0, (int)scriptflag.SCRIPT_VERIFY_P2SH, 0))
+ {
+ return false; // vin[0] signature check failed
+ }
+
+ if (!CheckStakeKernelHash(nBits, block.header.Hash, block.header.nTime, (uint)(nTxPos - nBlockPos), txPrev, txin.prevout, tx.nTime, ref hashProofOfStake, ref targetProofOfStake))
+ {
+ return false; // check kernel failed on coinstake
+ }
+
+ return true;
+ }
}
}