From: CryptoManiac Date: Fri, 11 Sep 2015 17:03:05 +0000 (+0300) Subject: Use signed 64 bit integers for better compatibility. X-Git-Url: https://git.novaco.in/?p=NovacoinLibrary.git;a=commitdiff_plain;h=8cd2a7c7073eeeafd88c1745e4886fb940c02c91 Use signed 64 bit integers for better compatibility. --- diff --git a/Novacoin/BigNum.cs b/Novacoin/BigNum.cs index af8ae4a..9e49922 100644 --- a/Novacoin/BigNum.cs +++ b/Novacoin/BigNum.cs @@ -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); diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index b616fa5..923b47a 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -477,7 +477,7 @@ namespace Novacoin /// Packed difficulty representation. /// Amount of fees. /// Reward value. - 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) { diff --git a/Novacoin/CBlockStore.cs b/Novacoin/CBlockStore.cs index 8548972..25ea3bb 100644 --- a/Novacoin/CBlockStore.cs +++ b/Novacoin/CBlockStore.cs @@ -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(); @@ -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 diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index 11d99c9..c5c2b90 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -51,21 +51,21 @@ namespace Novacoin /// /// One cent = 10000 satoshis. /// - public const ulong nCent = 10000; + public const long nCent = 10000; /// /// One coin = 1000000 satoshis. /// - public const ulong nCoin = 1000000; + public const long nCoin = 1000000; /// /// Sanity checking threshold. /// - 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; /// /// 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 /// /// Amount of novacoins spent by this transaction. /// - 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); } /// /// Get total sigops. @@ -523,14 +523,14 @@ namespace Novacoin /// /// Reference to innputs map. /// Sum of inputs. - public ulong GetValueIn(ref Dictionary inputs) + public long GetValueIn(ref Dictionary 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 /// Inputs set. /// Coin age calculation result. /// Result - public bool GetCoinAge(ref Dictionary inputs, out ulong nCoinAge) + public bool GetCoinAge(ref Dictionary 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) { diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs index 36f4817..756938f 100644 --- a/Novacoin/CTxOut.cs +++ b/Novacoin/CTxOut.cs @@ -16,22 +16,20 @@ * along with this program. If not, see . */ -using System; using System.Text; -using System.Collections.Generic; using System.IO; namespace Novacoin { - /// - /// Transaction output. - /// - public class CTxOut + /// + /// Transaction output. + /// + public class CTxOut { /// /// Input value. /// - public ulong nValue = ulong.MaxValue; + public long nValue = unchecked((long)0xffffffffffffffff); /// /// Second half of script which contains spending instructions. @@ -43,7 +41,7 @@ namespace Novacoin /// /// Input value /// Spending instructions. - 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 /// 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 diff --git a/Novacoin/DatabaseObjects.cs b/Novacoin/DatabaseObjects.cs index d9668f5..df58f35 100644 --- a/Novacoin/DatabaseObjects.cs +++ b/Novacoin/DatabaseObjects.cs @@ -768,7 +768,7 @@ namespace Novacoin /// Getter for output value. /// [Ignore] - public ulong nValue + public long nValue { get { return VarInt.DecodeVarInt(OutputValue); } set { OutputValue = VarInt.EncodeVarInt(value); } diff --git a/Novacoin/NetInfo.cs b/Novacoin/NetInfo.cs index 6db6c22..96cf321 100644 --- a/Novacoin/NetInfo.cs +++ b/Novacoin/NetInfo.cs @@ -75,12 +75,12 @@ /// /// Maximum possible proof-of-work reward. /// - public const ulong nMaxMintProofOfWork = CTransaction.nCoin * 100; + public const long nMaxMintProofOfWork = CTransaction.nCoin * 100; /// /// Maximum possible proof-of-stake reward per coin*year. /// - public const ulong nMaxMintProofOfStake = CTransaction.nCoin * 100; + public const long nMaxMintProofOfStake = CTransaction.nCoin * 100; public static uint GetAdjustedTime() { diff --git a/Novacoin/StakeModifier.cs b/Novacoin/StakeModifier.cs index 882a699..5d5f819 100644 --- a/Novacoin/StakeModifier.cs +++ b/Novacoin/StakeModifier.cs @@ -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; diff --git a/Novacoin/VarInt.cs b/Novacoin/VarInt.cs index ba174e9..c3520ba 100644 --- a/Novacoin/VarInt.cs +++ b/Novacoin/VarInt.cs @@ -59,7 +59,7 @@ namespace Novacoin } else { - // ulong flag + // long flag prefix = 0xff; valueBytes = BitConverter.GetBytes(n); } @@ -110,7 +110,7 @@ namespace Novacoin /// /// Byte sequence /// Integer value - 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; }