mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 06:43:45 +01:00
6ad0328f1c Don't assert(foo()) where foo has side effects (practicalswift)
Pull request description:
Don't `assert(foo())` where `foo` has side effects.
From `assert(3)`:
> If the macro `NDEBUG` is defined at the moment `<assert.h>` was last included, the macro `assert()` generates no code, and hence does nothing at all.
Bitcoin currently cannot be compiled without assertions, but we shouldn't rely on that.
Tree-SHA512: 28cff0c6d1c2fb612ca58c9c94142ed01c5cfd0a2fecb8e59cdb6c270374b215d952ed3491d921d84dc1b439fa49da4f0e75e080f6adcbc6b0e08be14e54c170
60 lines
2.0 KiB
C++
60 lines
2.0 KiB
C++
// Copyright (c) 2016-2018 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <bench/bench.h>
|
|
|
|
#include <chainparams.h>
|
|
#include <validation.h>
|
|
#include <streams.h>
|
|
#include <consensus/validation.h>
|
|
|
|
namespace block_bench {
|
|
#include <bench/data/block413567.raw.h>
|
|
} // namespace block_bench
|
|
|
|
// These are the two major time-sinks which happen after we have fully received
|
|
// a block off the wire, but before we can relay the block on to peers using
|
|
// compact block relay.
|
|
|
|
static void DeserializeBlockTest(benchmark::State& state)
|
|
{
|
|
CDataStream stream((const char*)block_bench::block413567,
|
|
(const char*)&block_bench::block413567[sizeof(block_bench::block413567)],
|
|
SER_NETWORK, PROTOCOL_VERSION);
|
|
char a = '\0';
|
|
stream.write(&a, 1); // Prevent compaction
|
|
|
|
while (state.KeepRunning()) {
|
|
CBlock block;
|
|
stream >> block;
|
|
bool rewound = stream.Rewind(sizeof(block_bench::block413567));
|
|
assert(rewound);
|
|
}
|
|
}
|
|
|
|
static void DeserializeAndCheckBlockTest(benchmark::State& state)
|
|
{
|
|
CDataStream stream((const char*)block_bench::block413567,
|
|
(const char*)&block_bench::block413567[sizeof(block_bench::block413567)],
|
|
SER_NETWORK, PROTOCOL_VERSION);
|
|
char a = '\0';
|
|
stream.write(&a, 1); // Prevent compaction
|
|
|
|
const auto chainParams = CreateChainParams(CBaseChainParams::MAIN);
|
|
|
|
while (state.KeepRunning()) {
|
|
CBlock block; // Note that CBlock caches its checked state, so we need to recreate it here
|
|
stream >> block;
|
|
bool rewound = stream.Rewind(sizeof(block_bench::block413567));
|
|
assert(rewound);
|
|
|
|
CValidationState validationState;
|
|
bool checked = CheckBlock(block, validationState, chainParams->GetConsensus());
|
|
assert(checked);
|
|
}
|
|
}
|
|
|
|
BENCHMARK(DeserializeBlockTest, 130);
|
|
BENCHMARK(DeserializeAndCheckBlockTest, 160);
|