-\feffusing System;
+\feff/**
+* 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 <http://www.gnu.org/licenses/>.
+*/
+
+
+using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
-using System.Numerics;
-using System.Text;
-using System.Threading.Tasks;
namespace Novacoin
{
+ /// <summary>
+ /// Stake modifier calculation. Doesn't work properly, for now.
+ /// </summary>
public class StakeModifier
{
/// <summary>
/// Get stake modifier selection interval (in seconds)
/// </summary>
/// <returns></returns>
- static long GetStakeModifierSelectionInterval()
+ internal static long GetStakeModifierSelectionInterval()
{
long nSelectionInterval = 0;
for (int nSection = 0; nSection < 64; nSection++)
/// <param name="nStakeModifierPrev">Previous value of stake modifier.</param>
/// <param name="selectedCursor">Selection result.</param>
/// <returns></returns>
- static bool SelectBlockFromCandidates(List<Tuple<uint, uint256>> sortedByTimestamp, Dictionary<uint256, CBlockStoreItem> mapSelectedBlocks, long nSelectionIntervalStop, long nStakeModifierPrev, ref CBlockStoreItem selectedCursor)
+ internal static bool SelectBlockFromCandidates(List<Tuple<uint, uint256>> sortedByTimestamp, Dictionary<uint256, CBlockStoreItem> 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)
{
// compute the selection hash by hashing its proof-hash and the
// previous proof-of-stake modifier
- uint256 hashProof = cursor.IsProofOfStake ? (uint256)cursor.hashProofOfStake : selectedBlockHash;
+ var hashProof = cursor.IsProofOfStake ? (uint256)cursor.hashProofOfStake : selectedBlockHash;
uint256 hashSelection;
var s = new MemoryStream();
/// additional bits in the stake modifier, even after generating a chain of
/// blocks.
/// </summary>
- 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;
}
// 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);
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
return true;
}
- bool GetKernelStakeModifier(uint256 hashBlockFrom, ref long nStakeModifier)
+ public static bool GetKernelStakeModifier(uint256 hashBlockFrom, ref long nStakeModifier)
{
uint nStakeModifierHeight = 0;
uint nStakeModifierTime = 0;
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
}
- uint256 nTargetPerCoinDay = new uint256();
+ uint256 nTargetPerCoinDay = 0;
nTargetPerCoinDay.Compact = nBits;
ulong nValueIn = txPrev.vout[prevout.n].nValue;
- uint256 hashBlockFrom = blockFrom.header.Hash;
- BigInteger bnCoinDayWeight = (long)nValueIn * GetWeight(txPrev.nTime, nTimeTx) / (long)CTransaction.nCoin / (24 * 60 * 60);
+ uint256 nCoinDayWeight = new uint256(nValueIn) * GetWeight(txPrev.nTime, nTimeTx) / CTransaction.nCoin / (24 * 60 * 60);
- targetProofOfStake = (bnCoinDayWeight * new BigInteger(nTargetPerCoinDay)).ToByteArray();
+ targetProofOfStake = nCoinDayWeight * nTargetPerCoinDay;
// Calculate hash
long nStakeModifier = 0;
}
// Get time weight using supplied timestamps
- static long GetWeight(long nIntervalBeginning, long nIntervalEnd)
+ static ulong GetWeight(ulong nIntervalBeginning, ulong nIntervalEnd)
{
// Kernel hash weight starts from 0 at the 30-day min age
// this change increases active coins participating the hash and helps
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
+
+ long nBlockPos = 0;
+ CBlock block = null;
+ if (!CBlockStore.Instance.GetBlockByTransactionID(txin.prevout.hash, ref block, ref nBlockPos))
+ {
+ return false; // unable to read block of previous transaction
+ }
+
+ long nTxPos = 0;
+ CTransaction txPrev = null;
+
+ // Iterate through vtx array
+ for (var i = 0; i < block.vtx.Length; i++)
+ {
+ if (block.vtx[i].Hash == txin.prevout.hash)
+ {
+ txPrev = block.vtx[i];
+ nTxPos = nBlockPos + block.GetTxOffset(i);
+
+ break;
+ }
+ }
+
+ if (txPrev == null)
+ {
+ return false; // No such transaction found in the block
+ }
+
+ 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;
+ }
}
}