nBlockTrust cleanup.
[NovacoinLibrary.git] / Novacoin / DatabaseObjects.cs
index b49d63e..b12d681 100644 (file)
@@ -405,26 +405,14 @@ namespace Novacoin
         {
             get
             {
-                uint256 nTarget = 0;
-                nTarget.Compact = nBits;
-
-                /* Old protocol */
-                if (nTime < NetInfo.nChainChecksSwitchTime)
+                // Return 1 for the first 12 blocks
+                if (prev == null || prev.nHeight < 12)
                 {
-                    return IsProofOfStake ? (new uint256(1) << 256) / (nTarget + 1) : 1;
+                    return 1;
                 }
 
-                /* New protocol */
-
-                // Calculate work amount for block
-                var nPoWTrust = NetInfo.nPoWBase / (nTarget + 1);
-
-                // Set nPowTrust to 1 if we are checking PoS block or PoW difficulty is too low
-                nPoWTrust = (IsProofOfStake || !nPoWTrust) ? 1 : nPoWTrust;
-
-                // Return nPoWTrust for the first 12 blocks
-                if (prev == null || prev.nHeight < 12)
-                    return nPoWTrust;
+                uint256 nTarget = 0;
+                nTarget.Compact = nBits;
 
                 CBlockStoreItem currentIndex = prev;
 
@@ -460,6 +448,12 @@ namespace Novacoin
                 }
                 else
                 {
+                    // Calculate work amount for block
+                    var nPoWTrust = NetInfo.nPoWBase / (nTarget + 1);
+
+                    // Set nPowTrust to 1 if we are checking PoS block or PoW difficulty is too low
+                    nPoWTrust = (IsProofOfStake || !nPoWTrust) ? 1 : nPoWTrust;
+
                     var nLastBlockTrust = prev.nChainTrust - prev.prev.nChainTrust;
 
                     // Return nPoWTrust + 2/3 of previous block score if two parent blocks are not PoS blocks
@@ -502,7 +496,7 @@ namespace Novacoin
         }
 
         /// <summary>
-        /// Stake modifier checksum.
+        /// Stake modifier checksum
         /// </summary>
         public uint nStakeModifierChecksum;
 
@@ -566,6 +560,12 @@ namespace Novacoin
         public long nParentBlockID { get; set; }
 
         /// <summary>
+        /// Transaction timestamp
+        /// </summary>
+        [Column("nTime")]
+        public uint nTime { get; set; }
+
+        /// <summary>
         /// Transaction type flag
         /// </summary>
         [Column("TransactionFlags")]
@@ -606,7 +606,7 @@ namespace Novacoin
             {
                 reader.Seek(nBlockPos + nTxOffset, SeekOrigin.Begin); // Seek to transaction offset
 
-                if (nTxSize != reader.Read(buffer, 0, nTxSize))
+                if (nTxSize != reader.Read(buffer, 0, (int)nTxSize))
                 {
                     return false;
                 }
@@ -641,14 +641,32 @@ namespace Novacoin
         /// Transaction size accessor
         /// </summary>
         [Ignore]
-        public int nTxSize
+        public uint nTxSize
         {
-            get { return (int)VarInt.DecodeVarInt(TxSize); }
+            get { return (uint)VarInt.DecodeVarInt(TxSize); }
             private set { TxSize = VarInt.EncodeVarInt(value); }
         }
 
+        [Ignore]
+        public bool IsCoinBase
+        {
+            get { return TransactionFlags == TxFlags.TX_COINBASE; }
+        }
+
+        [Ignore]
+        public bool IsCoinStake
+        {
+            get { return TransactionFlags == TxFlags.TX_COINSTAKE; }
+        }
+
+        public CMerkleNode()
+        {
+        }
+
         public CMerkleNode(CTransaction tx)
         {
+            nTime = tx.nTime;
+
             nTxOffset = -1;
             nParentBlockID = -1;
 
@@ -657,20 +675,22 @@ namespace Novacoin
 
             if (tx.IsCoinBase)
             {
-                TransactionFlags |= TxFlags.TX_COINBASE;
+                TransactionFlags = TxFlags.TX_COINBASE;
             }
             else if (tx.IsCoinStake)
             {
-                TransactionFlags |= TxFlags.TX_COINSTAKE;
+                TransactionFlags = TxFlags.TX_COINSTAKE;
             }
             else
             {
-                TransactionFlags |= TxFlags.TX_USER;
+                TransactionFlags = TxFlags.TX_USER;
             }
         }
 
         public CMerkleNode(long nBlockId, long nOffset, CTransaction tx)
         {
+            nTime = tx.nTime;
+
             nParentBlockID = nBlockId;
 
             nTxOffset = nOffset;
@@ -697,6 +717,12 @@ namespace Novacoin
     public class TxOutItem
     {
         /// <summary>
+        /// Outpoint identifier.
+        /// </summary>
+        [PrimaryKey, AutoIncrement]
+        public long nOutpointID { get; set; }
+
+        /// <summary>
         /// Reference to transaction item.
         /// </summary>
         [ForeignKey(typeof(CMerkleNode), Name = "nMerkleNodeID")]
@@ -736,7 +762,7 @@ namespace Novacoin
         /// Getter for output value.
         /// </summary>
         [Ignore]
-        public ulong nValue
+        public long nValue
         {
             get { return VarInt.DecodeVarInt(OutputValue); }
             set { OutputValue = VarInt.EncodeVarInt(value); }
@@ -760,6 +786,23 @@ namespace Novacoin
     public class InputsJoin : TxOutItem
     {
         public byte[] TransactionHash { get; set; }
+
+        /// <summary>
+        /// To avoid awkwardness of sqlite wrapper.
+        /// </summary>
+        /// <returns></returns>
+        public TxOutItem getTxOutItem()
+        {
+            return new TxOutItem()
+            {
+                nOutpointID = nOutpointID,
+                nMerkleNodeID = nMerkleNodeID,
+                outputFlags = outputFlags,
+                OutputNumber = OutputNumber,
+                OutputValue = OutputValue,
+                scriptPubKey = scriptPubKey
+            };
+        }
     }