Use signed 64 bit integers for better compatibility.
authorCryptoManiac <balthazar@yandex.ru>
Fri, 11 Sep 2015 17:03:05 +0000 (20:03 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Fri, 11 Sep 2015 17:03:05 +0000 (20:03 +0300)
Novacoin/BigNum.cs
Novacoin/CBlock.cs
Novacoin/CBlockStore.cs
Novacoin/CTransaction.cs
Novacoin/CTxOut.cs
Novacoin/DatabaseObjects.cs
Novacoin/NetInfo.cs
Novacoin/StakeModifier.cs
Novacoin/VarInt.cs

index af8ae4a..9e49922 100644 (file)
@@ -34,6 +34,11 @@ namespace Novacoin
             bn = new BigInteger(BitConverter.GetBytes(ulongValue));
         }
 
+        public BigNum(long longValue)
+        {
+            bn = new BigInteger(Math.Sign(longValue), BitConverter.GetBytes(longValue));
+        }
+
         public BigNum(uint256 uint256Value)
         {
             bn = new BigInteger(uint256Value);
@@ -47,11 +52,22 @@ namespace Novacoin
             return new BigNum(a.bn.Add(bnValueToAdd));
         }
 
+        public static BigNum operator +(BigNum a, long b)
+        {
+            var bnValueToAdd = new BigInteger(BitConverter.GetBytes(b));
+            return new BigNum(a.bn.Add(bnValueToAdd));
+        }
+
         public static BigNum operator -(BigNum a, ulong b)
         {
             var bnValueToSubstract = new BigInteger(BitConverter.GetBytes(b));
             return new BigNum(a.bn.Subtract(bnValueToSubstract));
         }
+        public static BigNum operator -(BigNum a, long b)
+        {
+            var bnValueToSubstract = new BigInteger(BitConverter.GetBytes(b));
+            return new BigNum(a.bn.Subtract(bnValueToSubstract));
+        }
 
         public static BigNum operator +(BigNum a, uint256 b)
         {
@@ -81,6 +97,12 @@ namespace Novacoin
             return new BigNum(a.bn.Divide(bnDivider));
         }
 
+        public static BigNum operator /(BigNum a, long b)
+        {
+            var bnDivider = new BigInteger(BitConverter.GetBytes(b));
+            return new BigNum(a.bn.Divide(bnDivider));
+        }
+
         public static BigNum operator /(BigNum a, uint256 b)
         {
             var bnDivider = new BigInteger(b);
@@ -98,6 +120,12 @@ namespace Novacoin
             return new BigNum(a.bn.Multiply(bnMultiplier));
         }
 
+        public static BigNum operator *(BigNum a, long b)
+        {
+            var bnMultiplier = new BigInteger(BitConverter.GetBytes(b));
+            return new BigNum(a.bn.Multiply(bnMultiplier));
+        }
+
         public static BigNum operator *(BigNum a, uint256 b)
         {
             var bnMultiplier = new BigInteger(b);
@@ -153,6 +181,11 @@ namespace Novacoin
             return new BigNum(ulongValue);
         }
 
+        public static implicit operator BigNum(long ulongValue)
+        {
+            return new BigNum(ulongValue);
+        }
+
         public static implicit operator BigNum(uint256 uint256Value)
         {
             return new BigNum(uint256Value);
@@ -163,6 +196,11 @@ namespace Novacoin
             return (ulong)a.bn.LongValue;
         }
 
+        public static implicit operator long (BigNum a)
+        {
+            return a.bn.LongValue;
+        }
+
         public int CompareTo(BigNum other)
         {
             return bn.CompareTo(other.bn);
index b616fa5..923b47a 100644 (file)
@@ -477,7 +477,7 @@ namespace Novacoin
         /// <param name="nBits">Packed difficulty representation.</param>
         /// <param name="nFees">Amount of fees.</param>
         /// <returns>Reward value.</returns>
-        public static ulong GetProofOfWorkReward(uint nBits, ulong nFees)
+        public static long GetProofOfWorkReward(uint nBits, long nFees)
         {
             // NovaCoin: subsidy is cut in half every 64x multiply of PoW difficulty
             // A reasonably continuous curve is used to avoid shock to market
@@ -509,15 +509,15 @@ namespace Novacoin
                     bnLowerBound = bnMidValue;
             }
 
-            ulong nSubsidy = bnUpperBound;
+            long nSubsidy = (long)bnUpperBound;
             nSubsidy = (nSubsidy / CTransaction.nCent) * CTransaction.nCent;
 
             return Math.Min(nSubsidy, NetInfo.nMaxMintProofOfWork) + nFees;
         }
 
-        public static ulong GetProofOfStakeReward(ulong nCoinAge, uint nBits, uint nTime)
+        public static long GetProofOfStakeReward(long nCoinAge, uint nBits, uint nTime)
         {
-            ulong nRewardCoinYear, nSubsidy, nSubsidyLimit = 10 * CTransaction.nCoin;
+            long nRewardCoinYear, nSubsidy, nSubsidyLimit = 10 * CTransaction.nCoin;
 
             if (nTime > NetInfo.nDynamicStakeRewardTime)
             {
index 8548972..25ea3bb 100644 (file)
@@ -648,9 +648,9 @@ namespace Novacoin
             bool fScriptChecks = cursor.nHeight >= Checkpoints.TotalBlocksEstimate;
             var scriptFlags = scriptflag.SCRIPT_VERIFY_NOCACHE | scriptflag.SCRIPT_VERIFY_P2SH;
 
-            ulong nFees = 0;
-            ulong nValueIn = 0;
-            ulong nValueOut = 0;
+            long nFees = 0;
+            long nValueIn = 0;
+            long nValueOut = 0;
             uint nSigOps = 0;
 
             var queuedMerkleNodes = new Dictionary<uint256, CMerkleNode>();
@@ -706,8 +706,8 @@ namespace Novacoin
                         return false; // too many sigops
                     }
 
-                    ulong nTxValueIn = tx.GetValueIn(ref inputs);
-                    ulong nTxValueOut = tx.nValueOut;
+                    long nTxValueIn = tx.GetValueIn(ref inputs);
+                    long nTxValueOut = tx.nValueOut;
 
                     nValueIn += nTxValueIn;
                     nValueOut += nTxValueOut;
@@ -741,7 +741,7 @@ namespace Novacoin
 
             if (!block.IsProofOfStake)
             {
-                ulong nBlockReward = CBlock.GetProofOfWorkReward(cursor.nBits, nFees);
+                long nBlockReward = CBlock.GetProofOfWorkReward(cursor.nBits, nFees);
 
                 // Check coinbase reward
                 if (block.vtx[0].nValueOut > nBlockReward)
@@ -750,8 +750,8 @@ namespace Novacoin
                 }
             }
 
-            cursor.nMint = (long)(nValueOut - nValueIn + nFees);
-            cursor.nMoneySupply = (cursor.prev != null ? cursor.prev.nMoneySupply : 0) + (long)nValueOut - (long)nValueIn;
+            cursor.nMint = nValueOut - nValueIn + nFees;
+            cursor.nMoneySupply = (cursor.prev != null ? cursor.prev.nMoneySupply : 0) + nValueOut - nValueIn;
 
             if (!UpdateDBCursor(ref cursor))
             {
@@ -858,8 +858,8 @@ namespace Novacoin
 
             if (!tx.IsCoinBase)
             {
-                ulong nValueIn = 0;
-                ulong nFees = 0;
+                long nValueIn = 0;
+                long nFees = 0;
                 for (uint i = 0; i < tx.vin.Length; i++)
                 {
                     var prevout = tx.vin[i].prevout;
@@ -966,15 +966,15 @@ namespace Novacoin
                 if (tx.IsCoinStake)
                 {
                     // ppcoin: coin stake tx earns reward instead of paying fee
-                    ulong nCoinAge;
+                    long nCoinAge;
                     if (!tx.GetCoinAge(ref inputs, out nCoinAge))
                     {
                         return false; // unable to get coin age for coinstake
                     }
 
-                    ulong nReward = tx.nValueOut - nValueIn;
+                    long nReward = tx.nValueOut - nValueIn;
 
-                    ulong nCalculatedReward = CBlock.GetProofOfStakeReward(nCoinAge, cursorBlock.nBits, tx.nTime) - tx.GetMinFee(1, false, CTransaction.MinFeeMode.GMF_BLOCK) + CTransaction.nCent;
+                    long nCalculatedReward = CBlock.GetProofOfStakeReward(nCoinAge, cursorBlock.nBits, tx.nTime) - tx.GetMinFee(1, false, CTransaction.MinFeeMode.GMF_BLOCK) + CTransaction.nCent;
 
                     if (nReward > nCalculatedReward)
                     {
@@ -989,7 +989,7 @@ namespace Novacoin
                     }
 
                     // Tally transaction fees
-                    ulong nTxFee = nValueIn - tx.nValueOut;
+                    long nTxFee = nValueIn - tx.nValueOut;
                     if (nTxFee < 0)
                     {
                         return false; // nTxFee < 0
index 11d99c9..c5c2b90 100644 (file)
@@ -51,21 +51,21 @@ namespace Novacoin
         /// <summary>
         /// One cent = 10000 satoshis.
         /// </summary>
-        public const ulong nCent = 10000;
+        public const long nCent = 10000;
 
         /// <summary>
         /// One coin = 1000000 satoshis.
         /// </summary>
-        public const ulong nCoin = 1000000;
+        public const long nCoin = 1000000;
         
         /// <summary>
         /// Sanity checking threshold.
         /// </summary>
-        public const ulong nMaxMoney = 2000000000 * nCoin;
+        public const long nMaxMoney = 2000000000 * nCoin;
 
-        public const ulong nMinTxFee = nCent / 10;
-        public const ulong nMinRelayTxFee = nCent / 50;
-        public const ulong nMinTxoutAmount = nCent / 100;        
+        public const long nMinTxFee = nCent / 10;
+        public const long nMinRelayTxFee = nCent / 50;
+        public const long nMinTxoutAmount = nCent / 100;        
 
         /// <summary>
         /// Maximum transaction size is 250Kb
@@ -204,7 +204,7 @@ namespace Novacoin
             }
 
             // Check for empty or overflow output values
-            ulong nValueOut = 0;
+            long nValueOut = 0;
             for (int i = 0; i < vout.Length; i++)
             {
                 CTxOut txout = vout[i];
@@ -322,7 +322,7 @@ namespace Novacoin
                 {
                     // Fill outputs array
                     vout[nCurrentOutput] = new CTxOut();
-                    vout[nCurrentOutput].nValue = reader.ReadUInt64();
+                    vout[nCurrentOutput].nValue = reader.ReadInt64();
 
                     int nScriptPKLen = (int)VarInt.ReadVarInt(ref reader);
                     vout[nCurrentOutput].scriptPubKey = new CScript(reader.ReadBytes(nScriptPKLen));
@@ -425,11 +425,11 @@ namespace Novacoin
         /// <summary>
         /// Amount of novacoins spent by this transaction.
         /// </summary>
-        public ulong nValueOut
+        public long nValueOut
         {
             get
             {
-                ulong nValueOut = 0;
+                long nValueOut = 0;
                 foreach (var txout in vout)
                 {
                     nValueOut += txout.nValue;
@@ -491,7 +491,7 @@ namespace Novacoin
             return sb.ToString();
         }
 
-        public static bool MoneyRange(ulong nValue) { return (nValue <= nMaxMoney); }
+        public static bool MoneyRange(long nValue) { return (nValue <= nMaxMoney); }
 
         /// <summary>
         /// Get total sigops.
@@ -523,14 +523,14 @@ namespace Novacoin
         /// </summary>
         /// <param name="inputs">Reference to innputs map.</param>
         /// <returns>Sum of inputs.</returns>
-        public ulong GetValueIn(ref Dictionary<COutPoint, TxOutItem> inputs)
+        public long GetValueIn(ref Dictionary<COutPoint, TxOutItem> inputs)
         {
             if (IsCoinBase)
             {
                 return 0;
             }
 
-            ulong nResult = 0;
+            long nResult = 0;
             for (int i = 0; i < vin.Length; i++)
             {
                 nResult += GetOutputFor(vin[i], ref inputs).nValue;
@@ -565,7 +565,7 @@ namespace Novacoin
         /// <param name="inputs">Inputs set.</param>
         /// <param name="nCoinAge">Coin age calculation result.</param>
         /// <returns>Result</returns>
-        public bool GetCoinAge(ref Dictionary<COutPoint, TxOutItem> inputs, out ulong nCoinAge)
+        public bool GetCoinAge(ref Dictionary<COutPoint, TxOutItem> inputs, out long nCoinAge)
         {
             BigInteger bnCentSecond = 0;  // coin age in the unit of cent-seconds
             nCoinAge = 0;
@@ -600,19 +600,19 @@ namespace Novacoin
                     continue; // only count coins meeting min age requirement
                 }
 
-                ulong nValueIn = input.nValue;
+                long nValueIn = input.nValue;
                 bnCentSecond += new BigInteger(nValueIn) * (nTime - merkleItem.nTime) / nCent;
             }
 
             BigInteger bnCoinDay = bnCentSecond * nCent / nCoin / (24 * 60 * 60);
-            nCoinAge = (ulong)bnCoinDay;
+            nCoinAge = (long)bnCoinDay;
 
             return true;
         }
 
-        public ulong GetMinFee(uint nBlockSize, bool fAllowFree, MinFeeMode mode)
+        public long GetMinFee(uint nBlockSize, bool fAllowFree, MinFeeMode mode)
         {
-            ulong nMinTxFee = CTransaction.nMinTxFee, nMinRelayTxFee = CTransaction.nMinRelayTxFee;
+            long nMinTxFee = CTransaction.nMinTxFee, nMinRelayTxFee = CTransaction.nMinRelayTxFee;
             uint nBytes = Size;
 
             if (IsCoinStake)
@@ -629,10 +629,10 @@ namespace Novacoin
             }
 
             // Base fee is either nMinTxFee or nMinRelayTxFee
-            ulong nBaseFee = (mode == MinFeeMode.GMF_RELAY) ? nMinRelayTxFee : nMinTxFee;
+            long nBaseFee = (mode == MinFeeMode.GMF_RELAY) ? nMinRelayTxFee : nMinTxFee;
 
             uint nNewBlockSize = nBlockSize + nBytes;
-            ulong nMinFee = (1 + (ulong)nBytes / 1000) * nBaseFee;
+            long nMinFee = (1 + (long)nBytes / 1000) * nBaseFee;
 
             if (fAllowFree)
             {
index 36f4817..756938f 100644 (file)
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-using System;
 using System.Text;
-using System.Collections.Generic;
 using System.IO;
 
 namespace Novacoin
 {
-       /// <summary>
-       /// Transaction output.
-       /// </summary>
-       public class CTxOut
+    /// <summary>
+    /// Transaction output.
+    /// </summary>
+    public class CTxOut
        {
                /// <summary>
                /// Input value.
                /// </summary>
-        public ulong nValue = ulong.MaxValue;
+        public long nValue = unchecked((long)0xffffffffffffffff);
 
                /// <summary>
                /// Second half of script which contains spending instructions.
@@ -43,7 +41,7 @@ namespace Novacoin
         /// </summary>
         /// <param name="nValue">Input value</param>
         /// <param name="scriptPubKey">Spending instructions.</param>
-        public CTxOut(ulong nValue, CScript scriptPubKey)
+        public CTxOut(long nValue, CScript scriptPubKey)
         {
             this.nValue = nValue;
             this.scriptPubKey = scriptPubKey;
@@ -81,7 +79,7 @@ namespace Novacoin
             {
                 // Fill outputs array
                 vout[nIndex] = new CTxOut();
-                vout[nIndex].nValue = reader.ReadUInt64();
+                vout[nIndex].nValue = reader.ReadInt64();
 
                 int nScriptPKLen = (int)VarInt.ReadVarInt(ref reader);
                 vout[nIndex].scriptPubKey = new CScript(reader.ReadBytes(nScriptPKLen));
@@ -157,7 +155,7 @@ namespace Novacoin
         /// </summary>
         public void SetNull()
         {
-            nValue = ulong.MaxValue;
+            nValue = unchecked((long)0xffffffffffffffff);
             scriptPubKey = new CScript();
         }
 
@@ -172,7 +170,7 @@ namespace Novacoin
 
         public bool IsNull
         {
-            get { return (nValue == ulong.MaxValue); }
+            get { return nValue == unchecked((long)0xffffffffffffffff); }
         }
 
         public bool IsEmpty
index d9668f5..df58f35 100644 (file)
@@ -768,7 +768,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); }
index 6db6c22..96cf321 100644 (file)
         /// <summary>
         /// Maximum possible proof-of-work reward.
         /// </summary>
-        public const ulong nMaxMintProofOfWork = CTransaction.nCoin * 100;
+        public const long nMaxMintProofOfWork = CTransaction.nCoin * 100;
 
         /// <summary>
         /// Maximum possible proof-of-stake reward per coin*year.
         /// </summary>
-        public const ulong nMaxMintProofOfStake = CTransaction.nCoin * 100;
+        public const long nMaxMintProofOfStake = CTransaction.nCoin * 100;
 
         public static uint GetAdjustedTime()
         {
index 882a699..5d5f819 100644 (file)
@@ -358,8 +358,8 @@ namespace Novacoin
             uint256 nTargetPerCoinDay = 0;
             nTargetPerCoinDay.Compact = nBits;
 
-            ulong nValueIn = txPrev.vout[prevout.n].nValue;
-            uint256 nCoinDayWeight = new uint256(nValueIn) * GetWeight(txPrev.nTime, nTimeTx) / CTransaction.nCoin / (24 * 60 * 60);
+            long nValueIn = txPrev.vout[prevout.n].nValue;
+            uint256 nCoinDayWeight = new uint256((ulong)nValueIn) * GetWeight(txPrev.nTime, nTimeTx) / CTransaction.nCoin / (24 * 60 * 60);
 
             targetProofOfStake = nCoinDayWeight * nTargetPerCoinDay;
 
index ba174e9..c3520ba 100644 (file)
@@ -59,7 +59,7 @@ namespace Novacoin
                 }
                 else
                 {
-                    // ulong flag
+                    // long flag
                     prefix = 0xff;
                     valueBytes = BitConverter.GetBytes(n);
                 }
@@ -110,7 +110,7 @@ namespace Novacoin
         /// </summary>
         /// <param name="bytes">Byte sequence</param>
         /// <returns>Integer value</returns>
-        public static ulong DecodeVarInt(byte[] bytes)
+        public static long DecodeVarInt(byte[] bytes)
         {
             var prefix = bytes[0];
 
@@ -125,15 +125,15 @@ namespace Novacoin
                         return BitConverter.ToUInt16(bytesArray, 0);
                     case 0xfe: // uint flag
                         return BitConverter.ToUInt32(bytesArray, 0);
-                    case 0xff: // ulong flag
-                        return BitConverter.ToUInt64(bytesArray, 0);
+                    case 0xff: // long flag
+                        return BitConverter.ToInt64(bytesArray, 0);
                 }
             }
             
             return prefix; // Values lower than 0xfd are stored directly
         }
 
-        public static ulong ReadVarInt(ref BinaryReader reader)
+        public static long ReadVarInt(ref BinaryReader reader)
         {
             byte prefix = reader.ReadByte();
 
@@ -143,8 +143,8 @@ namespace Novacoin
                     return reader.ReadUInt16();
                 case 0xfe: // uint
                     return reader.ReadUInt32();
-                case 0xff: // ulong
-                    return reader.ReadUInt64();
+                case 0xff: // long
+                    return reader.ReadInt64();
                 default:
                     return prefix;
             }