From 04ac3ad40812b5ff686f16e182a10a78b9816bf4 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Thu, 10 Sep 2015 03:32:18 +0300 Subject: [PATCH] Use GetProofOfStakeHash --- Novacoin/CBlockStore.cs | 39 ++++++++++++++++++--------------------- Novacoin/CTransaction.cs | 4 ++-- Novacoin/StakeModifier.cs | 2 +- 3 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Novacoin/CBlockStore.cs b/Novacoin/CBlockStore.cs index f0c830f..e0f9d1f 100644 --- a/Novacoin/CBlockStore.cs +++ b/Novacoin/CBlockStore.cs @@ -214,7 +214,7 @@ namespace Novacoin } } - public bool GetTxOutCursor(COutPoint outpoint, ref TxOutItem txOutCursor) + public bool GetTxOutCursor(COutPoint outpoint, out TxOutItem txOutCursor) { var queryResults = dbConn.Query("select o.* from [Outputs] o left join [MerkleNodes] m on (m.nMerkleNodeID = o.nMerkleNodeID) where m.[TransactionHash] = ?", (byte[])outpoint.hash); @@ -227,10 +227,12 @@ namespace Novacoin // Tx not found + txOutCursor = null; + return false; } - public bool FetchInputs(CTransaction tx, ref Dictionary queued, ref Dictionary inputs, bool IsBlock, out bool Invalid) + public bool FetchInputs(ref CTransaction tx, ref Dictionary queued, ref Dictionary inputs, bool IsBlock, out bool Invalid) { Invalid = false; @@ -348,17 +350,6 @@ namespace Novacoin return false; // SetStakeEntropyBit() failed } - // Save proof-of-stake hash value - if (itemTemplate.IsProofOfStake) - { - uint256 hashProofOfStake; - if (!GetProofOfStakeHash(blockHash, out hashProofOfStake)) - { - return false; // hashProofOfStake not found - } - itemTemplate.hashProofOfStake = hashProofOfStake; - } - // compute stake modifier long nStakeModifier = 0; bool fGeneratedStakeModifier = false; @@ -368,7 +359,7 @@ namespace Novacoin } itemTemplate.SetStakeModifier(nStakeModifier, fGeneratedStakeModifier); - itemTemplate.nStakeModifierChecksum = StakeModifier.GetStakeModifierChecksum(itemTemplate); + itemTemplate.nStakeModifierChecksum = StakeModifier.GetStakeModifierChecksum(ref itemTemplate); // TODO: verify stake modifier checkpoints @@ -379,7 +370,14 @@ namespace Novacoin itemTemplate.prevoutStake = block.vtx[1].vin[0].prevout; itemTemplate.nStakeTime = block.vtx[1].nTime; - itemTemplate.hashProofOfStake = mapProofOfStake[blockHash]; + + // Save proof-of-stake hash value + uint256 hashProofOfStake; + if (!GetProofOfStakeHash(ref blockHash, out hashProofOfStake)) + { + return false; // hashProofOfStake not found + } + itemTemplate.hashProofOfStake = hashProofOfStake; } if (!itemTemplate.WriteToFile(ref fStreamReadWrite, ref block)) @@ -555,7 +553,6 @@ namespace Novacoin } } - // Connect longer branch var txDelete = new List(); foreach (var cursor in connect) @@ -695,7 +692,7 @@ namespace Novacoin else { bool Invalid; - if (!FetchInputs(tx, ref queuedOutputs, ref inputs, true, out Invalid)) + if (!FetchInputs(ref tx, ref queuedOutputs, ref inputs, true, out Invalid)) { return false; // Unable to fetch some inputs. } @@ -720,7 +717,7 @@ namespace Novacoin nFees += nTxValueIn - nTxValueOut; } - if (!ConnectInputs(tx, ref inputs, ref queuedOutputs, ref cursor, true, fScriptChecks, scriptFlags)) + if (!ConnectInputs(ref tx, ref inputs, ref queuedOutputs, ref cursor, true, fScriptChecks, scriptFlags)) { return false; } @@ -854,7 +851,7 @@ namespace Novacoin return true; } - private bool ConnectInputs(CTransaction tx, ref Dictionary inputs, ref Dictionary queued, ref CBlockStoreItem cursorBlock, bool fBlock, bool fScriptChecks, scriptflag scriptFlags) + private bool ConnectInputs(ref CTransaction tx, ref Dictionary inputs, ref Dictionary queued, ref CBlockStoreItem cursorBlock, bool fBlock, bool fScriptChecks, scriptflag scriptFlags) { // Take over previous transactions' spent items // fBlock is true when this is called from AcceptBlock when a new best-block is added to the blockchain @@ -1032,7 +1029,7 @@ namespace Novacoin /// Block hash /// Proof-of-stake hash /// Proof-of-Stake hash value - private bool GetProofOfStakeHash(uint256 blockHash, out uint256 hashProofOfStake) + private bool GetProofOfStakeHash(ref uint256 blockHash, out uint256 hashProofOfStake) { return mapProofOfStake.TryGetValue(blockHash, out hashProofOfStake); } @@ -1047,7 +1044,7 @@ namespace Novacoin return false; } - CBlockStoreItem prevBlockCursor = null; + CBlockStoreItem prevBlockCursor; if (!blockMap.TryGetValue(block.header.prevHash, out prevBlockCursor)) { // Unable to get the cursor. diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index e0630aa..11d99c9 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -156,12 +156,12 @@ namespace Novacoin return true; } - TxOutItem txOutCursor = null; for (int i = 0; i < vin.Length; i++) { var outpoint = vin[i].prevout; - if (!CBlockStore.Instance.GetTxOutCursor(outpoint, ref txOutCursor)) + TxOutItem txOutCursor; + if (!CBlockStore.Instance.GetTxOutCursor(outpoint, out txOutCursor)) return false; if (!ScriptCode.VerifyScript(vin[i].scriptSig, txOutCursor.scriptPubKey, this, i, (int)scriptflag.SCRIPT_VERIFY_P2SH, 0)) diff --git a/Novacoin/StakeModifier.cs b/Novacoin/StakeModifier.cs index f8aad9d..882a699 100644 --- a/Novacoin/StakeModifier.cs +++ b/Novacoin/StakeModifier.cs @@ -404,7 +404,7 @@ namespace Novacoin return Math.Min(nIntervalEnd - nIntervalBeginning - nStakeMinAge, nStakeMaxAge); } - internal static uint GetStakeModifierChecksum(CBlockStoreItem itemTemplate) + internal static uint GetStakeModifierChecksum(ref CBlockStoreItem itemTemplate) { Contract.Assert(itemTemplate.prev != null || (uint256)itemTemplate.Hash == NetInfo.nHashGenesisBlock); -- 1.7.1