Add new block cursor helper properties, start implementation of StakeModifier calcula...
[NovacoinLibrary.git] / Novacoin / CBlockStore.cs
index cf6c515..a9c504c 100644 (file)
@@ -16,7 +16,7 @@ namespace Novacoin
     /// Block headers table
     /// </summary>
     [Table("BlockStorage")]
-    class CBlockStoreItem
+    public class CBlockStoreItem
     {
         /// <summary>
         /// Item ID in the database
@@ -76,11 +76,6 @@ namespace Novacoin
         public byte nEntropyBit { get; set; }
 
         /// <summary>
-        /// Next block hash
-        /// </summary>
-        public byte[] NextHash { get; set; }
-
-        /// <summary>
         /// Block height
         /// </summary>
         public uint nHeight { get; set; }
@@ -100,9 +95,9 @@ namespace Novacoin
         /// </summary>
         /// <param name="header">Block header</param>
         /// <returns>Header hash</returns>
-        public ScryptHash256 FillHeader(CBlockHeader header)
+        public uint256 FillHeader(CBlockHeader header)
         {
-            ScryptHash256 _hash;
+            uint256 _hash;
             Hash = _hash = header.Hash;
 
             nVersion = header.nVersion;
@@ -125,8 +120,8 @@ namespace Novacoin
                 CBlockHeader header = new CBlockHeader();
 
                 header.nVersion = nVersion;
-                header.prevHash = new ScryptHash256(prevHash);
-                header.merkleRoot = new Hash256(merkleRoot);
+                header.prevHash = prevHash;
+                header.merkleRoot = merkleRoot;
                 header.nTime = nTime;
                 header.nBits = nBits;
                 header.nNonce = nNonce;
@@ -212,6 +207,72 @@ namespace Novacoin
                 return false;
             }
         }
+
+        /// <summary>
+        /// Previous block cursor
+        /// </summary>
+        public public CBlockStoreItem prev {
+            get { return CBlockStore.Instance.GetCursor(prevHash); }
+        }
+
+        /// <summary>
+        /// STake modifier generation flag
+        /// </summary>
+        public bool GeneratedStakeModifier
+        {
+            get { return (BlockTypeFlag & BlockType.BLOCK_STAKE_MODIFIER) != 0; }
+        }
+
+        /// <summary>
+        /// Sets stake modifier and flag.
+        /// </summary>
+        /// <param name="nModifier">New stake modifier.</param>
+        /// <param name="fGeneratedStakeModifier">Set generation flag?</param>
+        public void SetStakeModifier(long nModifier, bool fGeneratedStakeModifier)
+        {
+            nStakeModifier = nModifier;
+            if (fGeneratedStakeModifier)
+                BlockTypeFlag |= BlockType.BLOCK_STAKE_MODIFIER;
+        }
+
+        /// <summary>
+        /// Set entropy bit.
+        /// </summary>
+        /// <param name="nEntropyBit">Entropy bit value (0 or 1).</param>
+        /// <returns>False if value is our of range.</returns>
+        public bool SetStakeEntropyBit(byte nEntropyBit)
+        {
+            if (nEntropyBit > 1)
+                return false;
+            BlockTypeFlag |= (nEntropyBit != 0 ? BlockType.BLOCK_STAKE_ENTROPY : 0);
+            return true;
+        }
+
+        /// <summary>
+        /// Set proof-of-stake flag.
+        /// </summary>
+        public void SetProofOfStake()
+        {
+            BlockTypeFlag |= BlockType.BLOCK_PROOF_OF_STAKE;
+        }
+
+        /// <summary>
+        /// Block has no proof-of-stake flag.
+        /// </summary>
+        public bool IsProofOfWork
+        {
+            get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) != 0; }
+        }
+
+        /// <summary>
+        /// Block has proof-of-stake flag set.
+        /// </summary>
+        public bool IsProofOfStake 
+        {
+            get { return (BlockTypeFlag & BlockType.BLOCK_PROOF_OF_STAKE) == 0; }
+        }
+
+
     }
 
     /// <summary>
@@ -219,10 +280,9 @@ namespace Novacoin
     /// </summary>
     public enum BlockType
     {
-        PROOF_OF_WORK,
-        PROOF_OF_WORK_MODIFIER,
-        PROOF_OF_STAKE,
-        PROOF_OF_STAKE_MODIFIER
+        BLOCK_PROOF_OF_STAKE = (1 << 0), // is proof-of-stake block
+        BLOCK_STAKE_ENTROPY = (1 << 1), // entropy bit for stake modifier
+        BLOCK_STAKE_MODIFIER = (1 << 2), // regenerated stake modifier
     };
 
     /// <summary>
@@ -327,18 +387,18 @@ namespace Novacoin
         /// <summary>
         /// Map of block tree nodes.
         /// </summary>
-        private ConcurrentDictionary<ScryptHash256, CBlockStoreItem> blockMap = new ConcurrentDictionary<ScryptHash256, CBlockStoreItem>();
+        private ConcurrentDictionary<uint256, CBlockStoreItem> blockMap = new ConcurrentDictionary<uint256, CBlockStoreItem>();
 
         /// <summary>
         /// Orphaned blocks map.
         /// </summary>
-        private ConcurrentDictionary<ScryptHash256, CBlock> orphanMap = new ConcurrentDictionary<ScryptHash256, CBlock>();
-        private ConcurrentDictionary<ScryptHash256, CBlock> orphanMapByPrev = new ConcurrentDictionary<ScryptHash256, CBlock>();
+        private ConcurrentDictionary<uint256, CBlock> orphanMap = new ConcurrentDictionary<uint256, CBlock>();
+        private ConcurrentDictionary<uint256, CBlock> orphanMapByPrev = new ConcurrentDictionary<uint256, CBlock>();
 
         /// <summary>
         /// Map of unspent items.
         /// </summary>
-        private ConcurrentDictionary<Hash256, CTransactionStoreItem> txMap = new ConcurrentDictionary<Hash256, CTransactionStoreItem>();
+        private ConcurrentDictionary<uint256, CTransactionStoreItem> txMap = new ConcurrentDictionary<uint256, CTransactionStoreItem>();
 
         public static CBlockStore Instance;
 
@@ -415,14 +475,14 @@ namespace Novacoin
                 // Init list of block items
                 foreach (var item in blockTreeItems)
                 {
-                    blockMap.TryAdd(new ScryptHash256(item.Hash), item);
+                    blockMap.TryAdd(item.Hash, item);
                 }
             }
 
             Instance = this;
         }
 
-        public bool GetTransaction(Hash256 TxID, ref CTransaction tx)
+        public bool GetTransaction(uint256 TxID, ref CTransaction tx)
         {
             var reader = new BinaryReader(fStreamReadWrite).BaseStream;
             var QueryTx = dbConn.Query<CTransactionStoreItem>("select * from [TransactionStorage] where [TransactionHash] = ?", (byte[])TxID);
@@ -440,7 +500,7 @@ namespace Novacoin
         private bool AddItemToIndex(ref CBlockStoreItem itemTemplate, ref CBlock block)
         {
             var writer = new BinaryWriter(fStreamReadWrite).BaseStream;
-            var blockHash = new ScryptHash256(itemTemplate.Hash);
+            uint256 blockHash = itemTemplate.Hash;
 
             if (blockMap.ContainsKey(blockHash))
             {
@@ -501,9 +561,9 @@ namespace Novacoin
 
         public bool AcceptBlock(ref CBlock block)
         {
-            ScryptHash256 hash = block.header.Hash;
+            uint256 nHash = block.header.Hash;
 
-            if (blockMap.ContainsKey(hash))
+            if (blockMap.ContainsKey(nHash))
             {
                 // Already have this block.
                 return false;
@@ -542,7 +602,8 @@ namespace Novacoin
             // Write block to file.
             var itemTemplate = new CBlockStoreItem()
             {
-                nHeight = nHeight
+                nHeight = nHeight,
+                nEntropyBit = Entropy.GetStakeEntropyBit(nHeight, nHash)
             };
 
             itemTemplate.FillHeader(block.header);
@@ -555,7 +616,7 @@ namespace Novacoin
             return true;
         }
 
-        public bool GetBlock(ScryptHash256 blockHash, ref CBlock block)
+        public bool GetBlock(uint256 blockHash, ref CBlock block)
         {
             var reader = new BinaryReader(fStreamReadWrite).BaseStream;
 
@@ -571,9 +632,41 @@ namespace Novacoin
             return false;
         }
 
+        /// <summary>
+        /// Get block cursor from map.
+        /// </summary>
+        /// <param name="blockHash">block hash</param>
+        /// <returns>Cursor or null</returns>
+        public CBlockStoreItem GetCursor(uint256 blockHash)
+        {
+            if (blockHash == 0)
+            {
+                // Genesis block has zero prevHash and no parent.
+                return null;
+            }
+
+            // First, check our block map.
+            CBlockStoreItem item = null;
+            if (blockMap.TryGetValue(blockHash, out item))
+            {
+                return item;
+            }
+
+            // Trying to get cursor from the database.
+            var QueryBlockCursor = dbConn.Query<CBlockStoreItem>("select * from [BlockStorage] where [Hash] = ?", (byte[])blockHash);
+
+            if (QueryBlockCursor.Count == 1)
+            {
+                return QueryBlockCursor[0];
+            }
+
+            // Nothing found.
+            return null;
+        }
+
         public bool ProcessBlock(ref CBlock block)
         {
-            ScryptHash256 blockHash = block.header.Hash;
+            var blockHash = block.header.Hash;
 
             if (blockMap.ContainsKey(blockHash))
             {
@@ -631,12 +724,12 @@ namespace Novacoin
             }
 
             // Recursively process any orphan blocks that depended on this one
-            var orphansQueue = new List<ScryptHash256>();
+            var orphansQueue = new List<uint256>();
             orphansQueue.Add(blockHash);
 
             for (int i = 0; i < orphansQueue.Count; i++)
             {
-                ScryptHash256 hashPrev = orphansQueue[i];
+                var hashPrev = orphansQueue[i];
 
                 foreach (var pair in orphanMap)
                 {