diff --git a/src/consensus/consensus.h b/src/consensus/consensus.h --- a/src/consensus/consensus.h +++ b/src/consensus/consensus.h @@ -16,14 +16,20 @@ static const uint64_t LEGACY_MAX_BLOCK_SIZE = ONE_MEGABYTE; /** Default setting for maximum allowed size for a block, in bytes */ static const uint64_t DEFAULT_MAX_BLOCK_SIZE = 32 * ONE_MEGABYTE; -/** The maximum allowed number of signature check operations per MB in a block - * (network rule) */ +/** + * The maximum allowed number of signature check operations per MB in a block + * (network rule). + */ static const int64_t MAX_BLOCK_SIGOPS_PER_MB = 20000; /** allowed number of signature check operations per transaction. */ static const uint64_t MAX_TX_SIGOPS_COUNT = 20000; -/** Coinbase transaction outputs can only be spent after this number of new - * blocks (network rule) */ +/** + * Coinbase transaction outputs can only be spent after this number of new + * blocks (network rule). + */ static const int COINBASE_MATURITY = 100; +/** Activation time for P2SH (April 1st 2012) */ +static const int64_t P2SH_ACTIVATION_TIME = 1333234914; /** Flags for nSequence and nLockTime locks */ enum { diff --git a/src/test/miner_tests.cpp b/src/test/miner_tests.cpp --- a/src/test/miner_tests.cpp +++ b/src/test/miner_tests.cpp @@ -292,7 +292,7 @@ pblock->nNonce = blockinfo[i].nonce; std::shared_ptr shared_pblock = std::make_shared(*pblock); - BOOST_CHECK(ProcessNewBlock(GetConfig(), shared_pblock, true, nullptr)); + BOOST_CHECK(ProcessNewBlock(config, shared_pblock, true, nullptr)); pblock->hashPrevBlock = pblock->GetHash(); } @@ -357,8 +357,10 @@ tx.vin[0].scriptSig = CScript(); // 18 * (520char + DROP) + OP_1 = 9433 bytes std::vector vchData(520); - for (unsigned int i = 0; i < 18; ++i) + for (unsigned int i = 0; i < 18; ++i) { tx.vin[0].scriptSig << vchData << OP_DROP; + } + tx.vin[0].scriptSig << OP_1; tx.vin[0].prevout.hash = txFirst[0]->GetId(); tx.vout[0].nValue = BLOCKSUBSIDY; @@ -423,6 +425,16 @@ mempool.clear(); // Invalid (pre-p2sh) txn in mempool, template creation fails. + std::array times; + for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++) { + // Trick the MedianTimePast. + times[i] = chainActive.Tip() + ->GetAncestor(chainActive.Tip()->nHeight - i) + ->nTime; + chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime = + P2SH_ACTIVATION_TIME; + } + tx.vin[0].prevout.hash = txFirst[0]->GetId(); tx.vin[0].prevout.n = 0; tx.vin[0].scriptSig = CScript() << OP_1; @@ -444,6 +456,11 @@ BOOST_CHECK_THROW(BlockAssembler(config).CreateNewBlock(scriptPubKey), std::runtime_error); mempool.clear(); + for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++) { + // Restore the MedianTimePast. + chainActive.Tip()->GetAncestor(chainActive.Tip()->nHeight - i)->nTime = + times[i]; + } // Double spend txn pair in mempool, template creation fails. tx.vin[0].prevout.hash = txFirst[0]->GetId(); diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -1908,9 +1908,9 @@ const Consensus::Params &consensusparams = config.GetChainParams().GetConsensus(); - // BIP16 didn't become active until Apr 1 2012 - int64_t nBIP16SwitchTime = 1333238400; - bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime); + // P2SH didn't become active until Apr 1 2012 + bool fStrictPayToScriptHash = + (pindex->pprev->GetMedianTimePast() >= P2SH_ACTIVATION_TIME); uint32_t flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE; @@ -3361,8 +3361,9 @@ pos.nPos); fclose(file); } - } else + } else { return state.Error("out of disk space"); + } } } @@ -3950,7 +3951,9 @@ bool fForceProcessing, bool *fNewBlock) { { CBlockIndex *pindex = nullptr; - if (fNewBlock) *fNewBlock = false; + if (fNewBlock) { + *fNewBlock = false; + } const CChainParams &chainparams = config.GetChainParams();