X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fmain.cpp;h=090dd4be1f1eef8e8dc6ffa26cfa9c46f540784a;hb=8d43f1482a14263a76c039c227c4383580dcc1db;hp=c830a8ffadafe694ea104edc00180c5473a4b355;hpb=2c8f88fc67af7916670ce0086e6a9078df8182f0;p=novacoin.git diff --git a/src/main.cpp b/src/main.cpp index c830a8f..090dd4b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,8 +44,8 @@ uint256 nPoWBase = uint256("0x00000000ffff00000000000000000000000000000000000000 CBigNum bnProofOfWorkLimitTestNet(~uint256(0) >> 16); -unsigned int nStakeMinAge = 60 * 60 * 24 * 30; // 30 days as minimum age for coin age -unsigned int nStakeMaxAge = 60 * 60 * 24 * 90; // 90 days as stake age of full weight +unsigned int nStakeMinAge = 60 * 60 * 24 * 30; // 30 days as zero time weight +unsigned int nStakeMaxAge = 60 * 60 * 24 * 90; // 90 days as full weight unsigned int nStakeTargetSpacing = 10 * 60; // 10-minute stakes spacing unsigned int nModifierInterval = 6 * 60 * 60; // time to elapse before new modifier is computed @@ -309,12 +309,18 @@ bool CTransaction::IsStandard() const return false; if (!txin.scriptSig.IsPushOnly()) return false; + if (fEnforceCanonical && !txin.scriptSig.HasCanonicalPushes()) { + return false; + } } BOOST_FOREACH(const CTxOut& txout, vout) { if (!::IsStandard(txout.scriptPubKey)) return false; if (txout.nValue == 0) return false; + if (fEnforceCanonical && !txout.scriptPubKey.HasCanonicalPushes()) { + return false; + } } return true; } @@ -476,10 +482,8 @@ bool CTransaction::CheckTransaction() const if (txout.IsEmpty() && !IsCoinBase() && !IsCoinStake()) return DoS(100, error("CTransaction::CheckTransaction() : txout empty for user transaction")); - // NovaCoin: enforce minimum output amount for user transactions until 1 May 2014 04:00:00 GMT - if (!fTestNet && !IsCoinBase() && !txout.IsEmpty() && nTime < OUTPUT_SWITCH_TIME && txout.nValue < MIN_TXOUT_AMOUNT) - return DoS(100, error("CTransaction::CheckTransaction() : txout.nValue below minimum")); - + if (txout.nValue < 0) + return DoS(100, error("CTransaction::CheckTransaction() : txout.nValue is negative")); if (txout.nValue > MAX_MONEY) return DoS(100, error("CTransaction::CheckTransaction() : txout.nValue too high")); nValueOut += txout.nValue; @@ -511,18 +515,58 @@ bool CTransaction::CheckTransaction() const return true; } -int64 CTransaction::GetMinFee(unsigned int nBlockSize, bool fAllowFree, - enum GetMinFee_mode mode, unsigned int nBytes) const +int64 CTransaction::GetMinFee(unsigned int nBlockSize, bool fAllowFree, enum GetMinFee_mode mode, unsigned int nBytes) const { - // Base fee is either MIN_TX_FEE or MIN_RELAY_TX_FEE - int64 nBaseFee = (mode == GMF_RELAY) ? MIN_RELAY_TX_FEE : MIN_TX_FEE; + // Use new fees approach if we are on test network or + // switch date has been reached + bool fNewApproach = fTestNet || nTime > FEE_SWITCH_TIME; + + int64 nMinTxFee = MIN_TX_FEE, nMinRelayTxFee = MIN_RELAY_TX_FEE; + + if(!fNewApproach || IsCoinStake()) + { + // Enforce 0.01 as minimum fee for old approach or coinstake + nMinTxFee = CENT; + nMinRelayTxFee = CENT; + } + + // Base fee is either nMinTxFee or nMinRelayTxFee + int64 nBaseFee = (mode == GMF_RELAY) ? nMinRelayTxFee : nMinTxFee; unsigned int nNewBlockSize = nBlockSize + nBytes; int64 nMinFee = (1 + (int64)nBytes / 1000) * nBaseFee; - // To limit dust spam, require MIN_TX_FEE/MIN_RELAY_TX_FEE if any output is less than 0.01 - if (nMinFee < nBaseFee) + if (fNewApproach) { + if (fAllowFree) + { + if (nBlockSize == 1) + { + // Transactions under 1K are free + if (nBytes < 1000) + nMinFee = 0; + } + else + { + // Free transaction area + if (nNewBlockSize < 27000) + nMinFee = 0; + } + } + + // To limit dust spam, require additional MIN_TX_FEE/MIN_RELAY_TX_FEE for + // each non empty output which is less than 0.01 + // + // It's safe to ignore empty outputs here, because these inputs are allowed + // only for coinbase and coinstake transactions. + BOOST_FOREACH(const CTxOut& txout, vout) + if (txout.nValue < CENT && !txout.IsEmpty()) + nMinFee += nBaseFee; + } + else if (nMinFee < nBaseFee) + { + // To limit dust spam, require MIN_TX_FEE/MIN_RELAY_TX_FEE if + // any output is less than 0.01 BOOST_FOREACH(const CTxOut& txout, vout) if (txout.nValue < CENT) nMinFee = nBaseFee; @@ -538,6 +582,7 @@ int64 CTransaction::GetMinFee(unsigned int nBlockSize, bool fAllowFree, if (!MoneyRange(nMinFee)) nMinFee = MAX_MONEY; + return nMinFee; } @@ -632,7 +677,7 @@ bool CTxMemPool::accept(CTxDB& txdb, CTransaction &tx, bool fCheckInputs, unsigned int nSize = ::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION); // Don't accept it if it can't get into a block - int64 txMinFee = tx.GetMinFee(1000, false, GMF_RELAY, nSize); + int64 txMinFee = tx.GetMinFee(1000, true, GMF_RELAY, nSize); if (nFees < txMinFee) return error("CTxMemPool::accept() : not enough fees %s, %"PRI64d" < %"PRI64d, hash.ToString().c_str(), @@ -951,7 +996,7 @@ CBigNum inline GetProofOfStakeLimit(int nHeight, unsigned int nTime) } // miner's coin base reward based on nBits -int64 GetProofOfWorkReward(unsigned int nBits) +int64 GetProofOfWorkReward(unsigned int nBits, int64 nFees) { CBigNum bnSubsidyLimit = MAX_MINT_PROOF_OF_WORK; @@ -986,7 +1031,7 @@ int64 GetProofOfWorkReward(unsigned int nBits) if (fDebug && GetBoolArg("-printcreation")) printf("GetProofOfWorkReward() : create=%s nBits=0x%08x nSubsidy=%"PRI64d"\n", FormatMoney(nSubsidy).c_str(), nBits, nSubsidy); - return min(nSubsidy, MAX_MINT_PROOF_OF_WORK); + return min(nSubsidy, MAX_MINT_PROOF_OF_WORK) + nFees; } // miner's coin stake reward based on nBits and coin age spent (coin-days) @@ -1486,11 +1531,13 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs, mapnBits, nTime) - GetMinFee() + MIN_TX_FEE; + unsigned int nTxSize = (nTime > VALIDATION_SWITCH_TIME || fTestNet) ? GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION) : 0; - if (nStakeReward > nCalculatedStakeReward) - return DoS(100, error("ConnectInputs() : coinstake pays too much(actual=%"PRI64d" vs calculated=%"PRI64d")", nStakeReward, nCalculatedStakeReward)); + int64 nReward = GetValueOut() - nValueIn; + int64 nCalculatedReward = GetProofOfStakeReward(nCoinAge, pindexBlock->nBits, nTime) - GetMinFee(1, false, GMF_BLOCK, nTxSize) + CENT; + + if (nReward > nCalculatedReward) + return DoS(100, error("CheckInputs() : coinstake pays too much(actual=%"PRI64d" vs calculated=%"PRI64d")", nReward, nCalculatedReward)); } else { @@ -1502,10 +1549,6 @@ bool CTransaction::ConnectInputs(CTxDB& txdb, MapPrevTx inputs, map nBlockReward) + return error("CheckBlock() : coinbase reward exceeded (actual=%"PRI64d" vs calculated=%"PRI64d")", + vtx[0].GetValueOut(), + nBlockReward); + } + + // track money supply and mint amount info pindex->nMint = nValueOut - nValueIn + nFees; pindex->nMoneySupply = (pindex->pprev? pindex->pprev->nMoneySupply : 0) + nValueOut - nValueIn; if (!txdb.WriteBlockIndex(CDiskBlockIndex(pindex))) return error("Connect() : WriteBlockIndex for pindex failed"); - // ppcoin: fees are not collected by miners as in bitcoin - // ppcoin: fees are destroyed to compensate the entire network - if (fDebug && GetBoolArg("-printcreation")) + // fees are not collected by proof-of-stake miners + // fees are destroyed to compensate the entire network + if (fProtocol048 && fDebug && IsProofOfStake() && GetBoolArg("-printcreation")) printf("ConnectBlock() : destroy=%s nFees=%"PRI64d"\n", FormatMoney(nFees).c_str(), nFees); if (fJustCheck) @@ -2112,6 +2168,8 @@ bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) c if (vtx.empty() || vtx.size() > MAX_BLOCK_SIZE || ::GetSerializeSize(*this, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE) return DoS(100, error("CheckBlock() : size limits failed")); + bool fProtocol048 = fTestNet || VALIDATION_SWITCH_TIME < nTime; + // Check proof of work matches claimed amount if (fCheckPOW && IsProofOfWork() && !CheckProofOfWork(GetHash(), nBits)) return DoS(50, error("CheckBlock() : proof of work failed")); @@ -2123,16 +2181,38 @@ bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) c // First transaction must be coinbase, the rest must not be if (vtx.empty() || !vtx[0].IsCoinBase()) return DoS(100, error("CheckBlock() : first tx is not coinbase")); + + if (!fProtocol048) + { + // Check coinbase timestamp + if (GetBlockTime() < (int64)vtx[0].nTime) + return DoS(100, error("CheckBlock() : coinbase timestamp violation")); + } + else + { + // Check coinbase timestamp + if (GetBlockTime() < PastDrift((int64)vtx[0].nTime)) + return DoS(50, error("CheckBlock() : coinbase timestamp is too late")); + } + for (unsigned int i = 1; i < vtx.size(); i++) + { if (vtx[i].IsCoinBase()) return DoS(100, error("CheckBlock() : more than one coinbase")); - // Check coinbase timestamp - if (GetBlockTime() > FutureDrift((int64)vtx[0].nTime)) - return DoS(50, error("CheckBlock() : coinbase timestamp is too early")); + // Check transaction timestamp + if (GetBlockTime() < (int64)vtx[i].nTime) + return DoS(50, error("CheckBlock() : block timestamp earlier than transaction timestamp")); + } if (IsProofOfStake()) { + if (fProtocol048) + { + if (nNonce != 0) + return DoS(100, error("CheckBlock() : non-zero nonce in proof-of-stake block")); + } + // Coinbase output should be empty if proof-of-stake block if (vtx[0].vout.size() != 1 || !vtx[0].vout[0].IsEmpty()) return DoS(100, error("CheckBlock() : coinbase output not empty for proof-of-stake block")); @@ -2145,7 +2225,7 @@ bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) c return DoS(100, error("CheckBlock() : more than one coinstake")); // Check coinstake timestamp - if (!CheckCoinStakeTimestamp(GetBlockTime(), (int64)vtx[1].nTime)) + if (GetBlockTime() != (int64)vtx[1].nTime) return DoS(50, error("CheckBlock() : coinstake timestamp violation nTimeBlock=%"PRI64d" nTimeTx=%u", GetBlockTime(), vtx[1].nTime)); // NovaCoin: check proof-of-stake block signature @@ -2154,13 +2234,6 @@ bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) c } else { - int64 nReward = GetProofOfWorkReward(nBits); - // Check coinbase reward - if (vtx[0].GetValueOut() > nReward) - return DoS(50, error("CheckBlock() : coinbase reward exceeded (actual=%"PRI64d" vs calculated=%"PRI64d")", - vtx[0].GetValueOut(), - nReward)); - // Should we check proof-of-work block signature or not? // // * Always skip on TestNet @@ -2182,10 +2255,6 @@ bool CBlock::CheckBlock(bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckSig) c { if (!tx.CheckTransaction()) return DoS(tx.nDoS, error("CheckBlock() : CheckTransaction failed")); - - // ppcoin: check transaction timestamp - if (GetBlockTime() < (int64)tx.nTime) - return DoS(50, error("CheckBlock() : block timestamp earlier than transaction timestamp")); } // Check for duplicate txids. This is caught by ConnectInputs(), @@ -2701,10 +2770,9 @@ bool LoadBlockIndex(bool fAllowNew) // // Load block index // - CTxDB txdb("cr"); + CTxDB txdb("cr+"); if (!txdb.LoadBlockIndex()) return false; - txdb.Close(); // // Init with genesis block @@ -2767,25 +2835,19 @@ bool LoadBlockIndex(bool fAllowNew) return error("LoadBlockIndex() : failed to init sync checkpoint"); } - // ppcoin: if checkpoint master key changed must reset sync-checkpoint - { - CTxDB txdb; - string strPubKey = ""; - if (!txdb.ReadCheckpointPubKey(strPubKey) || strPubKey != CSyncCheckpoint::strMasterPubKey) - { - // write checkpoint master key to db - txdb.TxnBegin(); - if (!txdb.WriteCheckpointPubKey(CSyncCheckpoint::strMasterPubKey)) - return error("LoadBlockIndex() : failed to write new checkpoint master key to db"); - if (!txdb.TxnCommit()) - return error("LoadBlockIndex() : failed to commit new checkpoint master key to db"); - if ((!fTestNet) && !Checkpoints::ResetSyncCheckpoint()) - return error("LoadBlockIndex() : failed to reset sync-checkpoint"); - } -#ifndef USE_LEVELDB - txdb.Close(); -#endif + string strPubKey = ""; + // if checkpoint master key changed must reset sync-checkpoint + if (!txdb.ReadCheckpointPubKey(strPubKey) || strPubKey != CSyncCheckpoint::strMasterPubKey) + { + // write checkpoint master key to db + txdb.TxnBegin(); + if (!txdb.WriteCheckpointPubKey(CSyncCheckpoint::strMasterPubKey)) + return error("LoadBlockIndex() : failed to write new checkpoint master key to db"); + if (!txdb.TxnCommit()) + return error("LoadBlockIndex() : failed to commit new checkpoint master key to db"); + if ((!fTestNet) && !Checkpoints::ResetSyncCheckpoint()) + return error("LoadBlockIndex() : failed to reset sync-checkpoint"); } return true; @@ -2947,7 +3009,7 @@ string GetWarnings(string strFor) if (GetBoolArg("-testsafemode")) strRPC = "test"; - // ppcoin: wallet lock warning for minting + // wallet lock warning for minting if (strMintWarning != "") { nPriority = 0; @@ -2961,17 +3023,7 @@ string GetWarnings(string strFor) strStatusBar = strMiscWarning; } - // * Should not enter safe mode for longer invalid chain - // * If sync-checkpoint is too old do not enter safe mode - // * Display warning only in the STRICT mode - if (CheckpointsMode == Checkpoints::STRICT && Checkpoints::IsSyncCheckpointTooOld(60 * 60 * 24 * 10) && - !fTestNet && !IsInitialBlockDownload()) - { - nPriority = 100; - strStatusBar = _("WARNING: Checkpoint is too old. Wait for block chain to download, or notify developers."); - } - - // ppcoin: if detected invalid checkpoint enter safe mode + // if detected invalid checkpoint enter safe mode if (Checkpoints::hashInvalidCheckpoint != 0) { nPriority = 3000; @@ -3105,6 +3157,13 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv) return true; } + if (pfrom->nVersion < 60010) + { + printf("partner %s using a buggy client %d, disconnecting\n", pfrom->addr.ToString().c_str(), pfrom->nVersion); + pfrom->fDisconnect = true; + return true; + } + // record my external IP reported by peer if (addrFrom.IsRoutable() && addrMe.IsRoutable()) addrSeenByPeer = addrMe;