From 9eae0c2f1007fbe28c2a0f0c2be9780d9ecf4624 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 11:45:56 +0200 Subject: [PATCH 1/5] csnode: BlockVerifier for blocks that come from outside the consensus One verifier for the snapshot importer and backward sync: the block parses, has the expected sequence and hash, and its signatures verify against its own confidants with the real trusted mask, as the node does when it stores a deferred block (block 0 has none). Round confirmations are checked separately against the previous block's confidants, with finalizeBlock's exceptions. Step 3b of snapshots served by community nodes. Co-Authored-By: Claude Opus 5.5 --- csnode/CMakeLists.txt | 2 + csnode/include/csnode/blockverifier.hpp | 34 +++++++ csnode/src/blockverifier.cpp | 73 +++++++++++++ tests/csnode/blockverifier_tests.cpp | 130 ++++++++++++++++++++++++ 4 files changed, 239 insertions(+) create mode 100644 csnode/include/csnode/blockverifier.hpp create mode 100644 csnode/src/blockverifier.cpp create mode 100644 tests/csnode/blockverifier_tests.cpp diff --git a/csnode/CMakeLists.txt b/csnode/CMakeLists.txt index 373b3df8e..42e93c855 100644 --- a/csnode/CMakeLists.txt +++ b/csnode/CMakeLists.txt @@ -69,6 +69,7 @@ add_library(csnode include/csnode/configholder.hpp include/csnode/multiwallets.hpp include/csnode/statedigest.hpp + include/csnode/blockverifier.hpp include/csnode/sendcachedata.hpp include/csnode/eventreport.hpp include/csnode/idatastream.hpp @@ -109,6 +110,7 @@ add_library(csnode src/configholder.cpp src/multiwallets.cpp src/statedigest.cpp + src/blockverifier.cpp src/sendcachedata.cpp src/eventreport.cpp src/poolcache.cpp diff --git a/csnode/include/csnode/blockverifier.hpp b/csnode/include/csnode/blockverifier.hpp new file mode 100644 index 000000000..907159bae --- /dev/null +++ b/csnode/include/csnode/blockverifier.hpp @@ -0,0 +1,34 @@ +#ifndef BLOCKVERIFIER_HPP +#define BLOCKVERIFIER_HPP + +#include + +#include +#include +#include + +namespace cs { + +// Checks a block that comes from outside the consensus (a snapshot, or a block below the lowest +// stored one) before it is stored, with the rules the node applies to blocks it stores itself. +// Used by the snapshot importer and by backward sync. +class BlockVerifier { +public: + // the block parses, has the expected sequence and, when expectedHash is not empty, the hash + // computed from its bytes is expectedHash; for sequence >= 1 its signatures verify against its + // own confidants with the real trusted mask (as tryFlushDeferredBlock does). Block 0 has none. + static bool verify(const csdb::Pool& block, cs::Sequence expectedSequence, const csdb::PoolHash& expectedHash, std::string& reason); + + // the pool signatures alone: outside the hashed part, so the only thing a sender could alter + // without changing the block's hash + static bool verifySignatures(const csdb::Pool& block, std::string& reason); + + // the round confirmations of a block (sequence >= 2) against the confidants of the block before + // it, with finalizeBlock's exceptions: bootstrap blocks and one-entry masks are not checked. + // They are inside the hashed part: a failure is not the sender's doing. + static bool verifyConfirmations(const csdb::Pool& block, const cs::PublicKeys& previousConfidants, std::string& reason); +}; + +} // namespace cs + +#endif // BLOCKVERIFIER_HPP diff --git a/csnode/src/blockverifier.cpp b/csnode/src/blockverifier.cpp new file mode 100644 index 000000000..ecbf8a67a --- /dev/null +++ b/csnode/src/blockverifier.cpp @@ -0,0 +1,73 @@ +#include + +#include + +#include +#include +#include +#include +#include + +namespace cs { + +bool BlockVerifier::verify(const csdb::Pool& block, cs::Sequence expectedSequence, const csdb::PoolHash& expectedHash, std::string& reason) { + if (!block.is_valid()) { + reason = "does not parse"; + return false; + } + if (block.sequence() != expectedSequence) { + reason = "has sequence " + std::to_string(block.sequence()) + ", " + std::to_string(expectedSequence) + " expected"; + return false; + } + if (!expectedHash.is_empty() && block.hash() != expectedHash) { + reason = "has another hash than the chain expects"; + return false; + } + return verifySignatures(block, reason); +} + +bool BlockVerifier::verifySignatures(const csdb::Pool& block, std::string& reason) { + if (block.sequence() == 0) { + return true; + } + const auto& confidants = block.confidants(); + if (confidants.empty() || block.signatures().empty()) { + reason = "has no confidants or no signatures"; + return false; + } + cs::Hash hash{}; + const auto bytes = block.hash().to_binary(); + if (bytes.size() != hash.size()) { + reason = "has no hash"; + return false; + } + std::copy(bytes.begin(), bytes.end(), hash.begin()); + const auto mask = cs::Utils::bitsToMask(block.numberTrusted(), block.realTrusted()); + if (!NodeUtils::checkGroupSignature(confidants, mask, block.signatures(), hash)) { + reason = "has signatures that do not verify against its confidants"; + return false; + } + return true; +} + +bool BlockVerifier::verifyConfirmations(const csdb::Pool& block, const cs::PublicKeys& previousConfidants, std::string& reason) { + if (block.sequence() < 2) { + return true; + } + const auto confMask = cs::Utils::bitsToMask(block.numberConfirmations(), block.roundConfirmationMask()); + if (BlockChain::isBootstrap(block) || confMask.size() <= 1) { + return true; + } + cs::Bytes trustedToHash; + cs::ODataStream stream(trustedToHash); + stream << block.sequence(); + stream << block.confidants(); + const cs::Hash trustedHash = cscrypto::calculateHash(trustedToHash.data(), trustedToHash.size()); + if (!NodeUtils::checkGroupSignature(previousConfidants, confMask, block.roundConfirmations(), trustedHash)) { + reason = "has round confirmations that do not verify against the previous block's confidants"; + return false; + } + return true; +} + +} // namespace cs diff --git a/tests/csnode/blockverifier_tests.cpp b/tests/csnode/blockverifier_tests.cpp new file mode 100644 index 000000000..2bb7c1faa --- /dev/null +++ b/tests/csnode/blockverifier_tests.cpp @@ -0,0 +1,130 @@ +#include + +#include +#include + +#include +#include +#include +#include + +namespace { +struct Keys { + std::vector pub; + std::vector priv; +}; + +Keys makeKeys(size_t n) { + Keys k; + for (size_t i = 0; i < n; ++i) { + cs::PublicKey p; + k.priv.push_back(cscrypto::generateKeyPair(p)); + k.pub.push_back(p); + } + return k; +} + +cs::Hash toHash(const csdb::PoolHash& h) { + cs::Hash out{}; + const auto b = h.to_binary(); + std::copy(b.begin(), b.end(), out.begin()); + return out; +} + +// a block of `seq` whose confidants are `keys`, signed by those in `signers` (bit i = confidant i), +// confirmed by the previous round's confidants `prev` (all of them), parsed back from its bytes +csdb::Pool signedBlock(cs::Sequence seq, const Keys& keys, uint64_t signers, const Keys* prev = nullptr, + const std::string& payload = "payload") { + csdb::Pool p(csdb::PoolHash{}, seq); + p.set_confidants(keys.pub); + p.add_number_trusted(static_cast(keys.pub.size())); + p.add_real_trusted(signers); + p.add_user_field(1, payload); + if (prev) { + cs::Bytes trustedToHash; + cs::ODataStream stream(trustedToHash); + stream << seq; + stream << keys.pub; + const cs::Hash h = cscrypto::calculateHash(trustedToHash.data(), trustedToHash.size()); + std::vector confirmations; + for (const auto& k : prev->priv) { + confirmations.push_back(cscrypto::generateSignature(k, h.data(), h.size())); + } + p.add_number_confirmations(static_cast(prev->pub.size())); + p.add_confirmation_mask((1ull << prev->pub.size()) - 1); + p.add_round_confirmations(confirmations); + } + EXPECT_TRUE(p.compose()); + const cs::Hash hash = toHash(p.hash()); + std::vector sigs; + for (size_t i = 0; i < keys.priv.size(); ++i) { + if (signers >> i & 1) { + sigs.push_back(cscrypto::generateSignature(keys.priv[i], hash.data(), hash.size())); + } + } + p.set_signatures(sigs); + return csdb::Pool::from_binary(p.to_binary_updated()); +} +} // namespace + +TEST(BlockVerifier, ValidBlockPasses) { + const auto keys = makeKeys(5); + const auto block = signedBlock(10, keys, 0b10111); + std::string reason; + EXPECT_TRUE(cs::BlockVerifier::verify(block, 10, block.hash(), reason)) << reason; + EXPECT_TRUE(cs::BlockVerifier::verify(block, 10, csdb::PoolHash{}, reason)) << reason; +} + +TEST(BlockVerifier, WrongSequenceOrHashFails) { + const auto keys = makeKeys(3); + const auto block = signedBlock(10, keys, 0b111); + const auto other = signedBlock(10, keys, 0b111, nullptr, "another payload"); + std::string reason; + EXPECT_FALSE(cs::BlockVerifier::verify(block, 11, block.hash(), reason)); + EXPECT_FALSE(cs::BlockVerifier::verify(block, 10, other.hash(), reason)); +} + +TEST(BlockVerifier, AlteredOrMissingSignatureFails) { + const auto keys = makeKeys(3); + auto bytes = signedBlock(10, keys, 0b111).to_binary(); + // the signatures are the tail of the stored bytes: flip a bit in the last one + bytes.back() ^= 1; + const auto altered = csdb::Pool::from_binary(std::move(bytes)); + std::string reason; + if (altered.is_valid()) { + EXPECT_FALSE(cs::BlockVerifier::verifySignatures(altered, reason)); + } + + // a signature by a key that is not the confidant's + auto wrongKeys = makeKeys(3); + wrongKeys.pub = keys.pub; // the block lists keys' confidants, wrongKeys sign + EXPECT_FALSE(cs::BlockVerifier::verifySignatures(signedBlock(10, wrongKeys, 0b111), reason)); + + // fewer signatures than the real trusted mask announces + auto block = signedBlock(10, keys, 0b111); + csdb::Pool p(csdb::PoolHash{}, 10); + p.set_confidants(keys.pub); + p.add_number_trusted(3); + p.add_real_trusted(0b111); + ASSERT_TRUE(p.compose()); + std::vector one{block.signatures().front()}; + p.set_signatures(one); + EXPECT_FALSE(cs::BlockVerifier::verifySignatures(csdb::Pool::from_binary(p.to_binary_updated()), reason)); +} + +TEST(BlockVerifier, GenesisNeedsNoSignature) { + csdb::Pool genesis(csdb::PoolHash{}, 0); + ASSERT_TRUE(genesis.compose()); + const auto parsed = csdb::Pool::from_binary(genesis.to_binary()); + std::string reason; + EXPECT_TRUE(cs::BlockVerifier::verify(parsed, 0, parsed.hash(), reason)) << reason; +} + +TEST(BlockVerifier, RoundConfirmationsAgainstThePreviousConfidants) { + const auto previous = makeKeys(3); + const auto keys = makeKeys(3); + const auto block = signedBlock(20, keys, 0b111, &previous); + std::string reason; + EXPECT_TRUE(cs::BlockVerifier::verifyConfirmations(block, previous.pub, reason)) << reason; + EXPECT_FALSE(cs::BlockVerifier::verifyConfirmations(block, keys.pub, reason)); +} From 9afc8832e4af2ec0e980d00fdcc5684328a66037 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 11:49:05 +0200 Subject: [PATCH 2/5] tests: initialise the crypto library before generating keys Co-Authored-By: Claude Opus 5.5 --- tests/csnode/blockverifier_tests.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/csnode/blockverifier_tests.cpp b/tests/csnode/blockverifier_tests.cpp index 2bb7c1faa..6e5fc6c4b 100644 --- a/tests/csnode/blockverifier_tests.cpp +++ b/tests/csnode/blockverifier_tests.cpp @@ -15,6 +15,8 @@ struct Keys { }; Keys makeKeys(size_t n) { + // keys live in sodium's guarded memory: the library must be initialised first + EXPECT_TRUE(cscrypto::cryptoInit()); Keys k; for (size_t i = 0; i < n; ++i) { cs::PublicKey p; From 22e8343da3fbc6c652940432fac5b640d7ba731c Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 11:55:44 +0200 Subject: [PATCH 3/5] tests: sign blocks the way the node does (signatures before compose) Co-Authored-By: Claude Opus 5.5 --- tests/csnode/blockverifier_tests.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/csnode/blockverifier_tests.cpp b/tests/csnode/blockverifier_tests.cpp index 6e5fc6c4b..b4e461727 100644 --- a/tests/csnode/blockverifier_tests.cpp +++ b/tests/csnode/blockverifier_tests.cpp @@ -56,8 +56,11 @@ csdb::Pool signedBlock(cs::Sequence seq, const Keys& keys, uint64_t signers, con p.add_confirmation_mask((1ull << prev->pub.size()) - 1); p.add_round_confirmations(confirmations); } - EXPECT_TRUE(p.compose()); - const cs::Hash hash = toHash(p.hash()); + // the signatures are outside the hashed part: the hash of a composed copy is the block's hash, + // and the signatures are set before the block itself is composed (it is read-only after) + auto probe = p.clone(); + EXPECT_TRUE(probe.compose()); + const cs::Hash hash = toHash(probe.hash()); std::vector sigs; for (size_t i = 0; i < keys.priv.size(); ++i) { if (signers >> i & 1) { @@ -65,7 +68,8 @@ csdb::Pool signedBlock(cs::Sequence seq, const Keys& keys, uint64_t signers, con } } p.set_signatures(sigs); - return csdb::Pool::from_binary(p.to_binary_updated()); + EXPECT_TRUE(p.compose()); + return csdb::Pool::from_binary(p.to_binary()); } } // namespace @@ -108,10 +112,10 @@ TEST(BlockVerifier, AlteredOrMissingSignatureFails) { p.set_confidants(keys.pub); p.add_number_trusted(3); p.add_real_trusted(0b111); - ASSERT_TRUE(p.compose()); std::vector one{block.signatures().front()}; p.set_signatures(one); - EXPECT_FALSE(cs::BlockVerifier::verifySignatures(csdb::Pool::from_binary(p.to_binary_updated()), reason)); + ASSERT_TRUE(p.compose()); + EXPECT_FALSE(cs::BlockVerifier::verifySignatures(csdb::Pool::from_binary(p.to_binary()), reason)); } TEST(BlockVerifier, GenesisNeedsNoSignature) { From c25a5b7f011aa490616082f3a05ffb6a415c4313 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 12:02:11 +0200 Subject: [PATCH 4/5] csnode: BlockVerifier refuses a block that does not parse in every check An invalid pool reads as sequence 0 and was taken for the genesis block by verifySignatures, which needs no signature there. Co-Authored-By: Claude Opus 5.5 --- csnode/src/blockverifier.cpp | 9 +++++++++ tests/csnode/blockverifier_tests.cpp | 7 ++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/csnode/src/blockverifier.cpp b/csnode/src/blockverifier.cpp index ecbf8a67a..dae02b909 100644 --- a/csnode/src/blockverifier.cpp +++ b/csnode/src/blockverifier.cpp @@ -27,6 +27,11 @@ bool BlockVerifier::verify(const csdb::Pool& block, cs::Sequence expectedSequenc } bool BlockVerifier::verifySignatures(const csdb::Pool& block, std::string& reason) { + // a block that does not parse reads as sequence 0: never take it for the genesis block + if (!block.is_valid()) { + reason = "does not parse"; + return false; + } if (block.sequence() == 0) { return true; } @@ -51,6 +56,10 @@ bool BlockVerifier::verifySignatures(const csdb::Pool& block, std::string& reaso } bool BlockVerifier::verifyConfirmations(const csdb::Pool& block, const cs::PublicKeys& previousConfidants, std::string& reason) { + if (!block.is_valid()) { + reason = "does not parse"; + return false; + } if (block.sequence() < 2) { return true; } diff --git a/tests/csnode/blockverifier_tests.cpp b/tests/csnode/blockverifier_tests.cpp index b4e461727..fe91843cc 100644 --- a/tests/csnode/blockverifier_tests.cpp +++ b/tests/csnode/blockverifier_tests.cpp @@ -115,7 +115,12 @@ TEST(BlockVerifier, AlteredOrMissingSignatureFails) { std::vector one{block.signatures().front()}; p.set_signatures(one); ASSERT_TRUE(p.compose()); - EXPECT_FALSE(cs::BlockVerifier::verifySignatures(csdb::Pool::from_binary(p.to_binary()), reason)); + const auto fewer = csdb::Pool::from_binary(p.to_binary()); + EXPECT_FALSE(cs::BlockVerifier::verifySignatures(fewer, reason)); + + // a block that does not parse is never taken for the genesis block + EXPECT_FALSE(cs::BlockVerifier::verifySignatures(csdb::Pool{}, reason)); + EXPECT_FALSE(cs::BlockVerifier::verify(csdb::Pool{}, 0, csdb::PoolHash{}, reason)); } TEST(BlockVerifier, GenesisNeedsNoSignature) { From 74d63ae7c68eaa14cbe86d767b0ad3fd63dce957 Mon Sep 17 00:00:00 2001 From: EnzinoBB Date: Sat, 24 Oct 2026 12:19:12 +0200 Subject: [PATCH 5/5] csnode: BlockVerifier review fixes - Signature and confirmation counts are checked with a reason before checkGroupSignature, which logs them as errors (untrusted input). - verifyConfirmations refuses a normal round's block without round confirmations, as storeBlock does. - The header says an empty expectedHash proves only self-consistency: the caller must anchor the block. - Tests: a flipped bit in a real pool signature (it parses, keeps its hash and fails), more signatures than the mask, no confidants, a block without confirmations, confirmations against a set of another size. The two former cases passed without reaching the check. Co-Authored-By: Claude Opus 5.5 --- csnode/include/csnode/blockverifier.hpp | 3 ++ csnode/src/blockverifier.cpp | 30 ++++++++++- tests/csnode/blockverifier_tests.cpp | 68 +++++++++++++++++-------- 3 files changed, 80 insertions(+), 21 deletions(-) diff --git a/csnode/include/csnode/blockverifier.hpp b/csnode/include/csnode/blockverifier.hpp index 907159bae..7355afa60 100644 --- a/csnode/include/csnode/blockverifier.hpp +++ b/csnode/include/csnode/blockverifier.hpp @@ -17,6 +17,9 @@ class BlockVerifier { // the block parses, has the expected sequence and, when expectedHash is not empty, the hash // computed from its bytes is expectedHash; for sequence >= 1 its signatures verify against its // own confidants with the real trusted mask (as tryFlushDeferredBlock does). Block 0 has none. + // An empty expectedHash proves only that the block is consistent with itself (anyone can sign a + // block with confidants of their own): the caller must anchor it, e.g. check that it links to + // a block it already trusts (previous_hash), as storeBlock does. static bool verify(const csdb::Pool& block, cs::Sequence expectedSequence, const csdb::PoolHash& expectedHash, std::string& reason); // the pool signatures alone: outside the hashed part, so the only thing a sender could alter diff --git a/csnode/src/blockverifier.cpp b/csnode/src/blockverifier.cpp index dae02b909..4655ce3fe 100644 --- a/csnode/src/blockverifier.cpp +++ b/csnode/src/blockverifier.cpp @@ -48,6 +48,17 @@ bool BlockVerifier::verifySignatures(const csdb::Pool& block, std::string& reaso } std::copy(bytes.begin(), bytes.end(), hash.begin()); const auto mask = cs::Utils::bitsToMask(block.numberTrusted(), block.realTrusted()); + // the counts are checked here, with a reason: checkGroupSignature logs them as errors, and the + // block comes from an untrusted sender + if (mask.size() != confidants.size()) { + reason = "has a trusted mask of another size than its confidants"; + return false; + } + if (block.signatures().size() != NodeUtils::realTrustedValue(mask)) { + reason = "has " + std::to_string(block.signatures().size()) + " signatures for " + + std::to_string(NodeUtils::realTrustedValue(mask)) + " real trusted"; + return false; + } if (!NodeUtils::checkGroupSignature(confidants, mask, block.signatures(), hash)) { reason = "has signatures that do not verify against its confidants"; return false; @@ -63,10 +74,27 @@ bool BlockVerifier::verifyConfirmations(const csdb::Pool& block, const cs::Publi if (block.sequence() < 2) { return true; } + if (BlockChain::isBootstrap(block)) { + return true; + } + // as storeBlock: a block of a normal round carries the previous round's confirmations + if (block.numberConfirmations() == 0 || block.roundConfirmations().empty()) { + reason = "has no round confirmations"; + return false; + } const auto confMask = cs::Utils::bitsToMask(block.numberConfirmations(), block.roundConfirmationMask()); - if (BlockChain::isBootstrap(block) || confMask.size() <= 1) { + if (confMask.size() <= 1) { return true; } + if (confMask.size() != previousConfidants.size()) { + reason = "has a confirmation mask of another size than the previous block's confidants"; + return false; + } + if (block.roundConfirmations().size() != NodeUtils::realTrustedValue(confMask)) { + reason = "has " + std::to_string(block.roundConfirmations().size()) + " round confirmations for " + + std::to_string(NodeUtils::realTrustedValue(confMask)) + " confirming confidants"; + return false; + } cs::Bytes trustedToHash; cs::ODataStream stream(trustedToHash); stream << block.sequence(); diff --git a/tests/csnode/blockverifier_tests.cpp b/tests/csnode/blockverifier_tests.cpp index fe91843cc..a4c9d073d 100644 --- a/tests/csnode/blockverifier_tests.cpp +++ b/tests/csnode/blockverifier_tests.cpp @@ -92,31 +92,55 @@ TEST(BlockVerifier, WrongSequenceOrHashFails) { TEST(BlockVerifier, AlteredOrMissingSignatureFails) { const auto keys = makeKeys(3); - auto bytes = signedBlock(10, keys, 0b111).to_binary(); - // the signatures are the tail of the stored bytes: flip a bit in the last one - bytes.back() ^= 1; - const auto altered = csdb::Pool::from_binary(std::move(bytes)); + const auto block = signedBlock(10, keys, 0b111); std::string reason; - if (altered.is_valid()) { - EXPECT_FALSE(cs::BlockVerifier::verifySignatures(altered, reason)); - } - // a signature by a key that is not the confidant's + // the pool signatures follow the hashed part and its length (a size_t): flip a bit of the first + auto bytes = block.to_binary(); + bytes[block.hashingLength() + sizeof(size_t)] ^= 1; + const auto altered = csdb::Pool::from_binary(std::move(bytes)); + ASSERT_TRUE(altered.is_valid()); + EXPECT_EQ(altered.hash(), block.hash()); // outside the hash: only the signature check sees it + EXPECT_FALSE(cs::BlockVerifier::verifySignatures(altered, reason)); + + // signatures by keys other than the confidants' auto wrongKeys = makeKeys(3); - wrongKeys.pub = keys.pub; // the block lists keys' confidants, wrongKeys sign + wrongKeys.pub = keys.pub; EXPECT_FALSE(cs::BlockVerifier::verifySignatures(signedBlock(10, wrongKeys, 0b111), reason)); - // fewer signatures than the real trusted mask announces - auto block = signedBlock(10, keys, 0b111); - csdb::Pool p(csdb::PoolHash{}, 10); - p.set_confidants(keys.pub); - p.add_number_trusted(3); - p.add_real_trusted(0b111); - std::vector one{block.signatures().front()}; - p.set_signatures(one); - ASSERT_TRUE(p.compose()); - const auto fewer = csdb::Pool::from_binary(p.to_binary()); - EXPECT_FALSE(cs::BlockVerifier::verifySignatures(fewer, reason)); + // more signatures than the mask has entries: realTrusted with a bit beyond numberTrusted + { + auto four = makeKeys(4); + csdb::Pool p(csdb::PoolHash{}, 10); + p.set_confidants(keys.pub); + p.add_number_trusted(3); + p.add_real_trusted(0b1111); + auto probe = p.clone(); + ASSERT_TRUE(probe.compose()); + const cs::Hash hash = toHash(probe.hash()); + std::vector sigs; + for (size_t i = 0; i < 3; ++i) { + sigs.push_back(cscrypto::generateSignature(keys.priv[i], hash.data(), hash.size())); + } + sigs.push_back(cscrypto::generateSignature(four.priv[3], hash.data(), hash.size())); + p.set_signatures(sigs); + ASSERT_TRUE(p.compose()); + const auto extra = csdb::Pool::from_binary(p.to_binary()); + ASSERT_TRUE(extra.is_valid()); + EXPECT_FALSE(cs::BlockVerifier::verifySignatures(extra, reason)); + } + + // no confidants, no signatures + { + csdb::Pool p(csdb::PoolHash{}, 10); + p.add_number_trusted(0); + p.add_real_trusted(0); + ASSERT_TRUE(p.compose()); + const auto empty = csdb::Pool::from_binary(p.to_binary()); + ASSERT_TRUE(empty.is_valid()); + EXPECT_FALSE(cs::BlockVerifier::verifySignatures(empty, reason)); + EXPECT_NE(reason.find("no confidants"), std::string::npos) << reason; + } // a block that does not parse is never taken for the genesis block EXPECT_FALSE(cs::BlockVerifier::verifySignatures(csdb::Pool{}, reason)); @@ -138,4 +162,8 @@ TEST(BlockVerifier, RoundConfirmationsAgainstThePreviousConfidants) { std::string reason; EXPECT_TRUE(cs::BlockVerifier::verifyConfirmations(block, previous.pub, reason)) << reason; EXPECT_FALSE(cs::BlockVerifier::verifyConfirmations(block, keys.pub, reason)); + // a normal round's block without confirmations is refused, as storeBlock does + EXPECT_FALSE(cs::BlockVerifier::verifyConfirmations(signedBlock(20, keys, 0b111), previous.pub, reason)); + // confirmations from a set of another size + EXPECT_FALSE(cs::BlockVerifier::verifyConfirmations(block, makeKeys(4).pub, reason)); }