Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/proof_tests.cpp
Show First 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(proofbuilder) { | ||||
for (int i = 0; i < 3; i++) { | for (int i = 0; i < 3; i++) { | ||||
key.MakeNewKey(true); | key.MakeNewKey(true); | ||||
BOOST_CHECK(pb.addUTXO(COutPoint(TxId(GetRandHash()), InsecureRand32()), | BOOST_CHECK(pb.addUTXO(COutPoint(TxId(GetRandHash()), InsecureRand32()), | ||||
int64_t(InsecureRand32()) * COIN / 100, | int64_t(InsecureRand32()) * COIN / 100, | ||||
InsecureRand32(), InsecureRandBool(), key)); | InsecureRand32(), InsecureRandBool(), key)); | ||||
} | } | ||||
Proof p = pb.build(); | ProofRef p = pb.build(); | ||||
ProofValidationState state; | ProofValidationState state; | ||||
BOOST_CHECK(p.verify(state)); | BOOST_CHECK(p->verify(state)); | ||||
BOOST_CHECK_EQUAL(p.getSequence(), sequence); | BOOST_CHECK_EQUAL(p->getSequence(), sequence); | ||||
BOOST_CHECK_EQUAL(p.getExpirationTime(), expiration); | BOOST_CHECK_EQUAL(p->getExpirationTime(), expiration); | ||||
BOOST_CHECK(p.getMaster() == master); | BOOST_CHECK(p->getMaster() == master); | ||||
BOOST_CHECK(state.GetResult() == ProofValidationResult::NONE); | BOOST_CHECK(state.GetResult() == ProofValidationResult::NONE); | ||||
} | } | ||||
struct TestVector { | struct TestVector { | ||||
std::string name; | std::string name; | ||||
std::string hex; | std::string hex; | ||||
ProofId proofid; | ProofId proofid; | ||||
▲ Show 20 Lines • Show All 838 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(verify) { | ||||
const auto runCheck = [&](const ProofValidationResult result, | const auto runCheck = [&](const ProofValidationResult result, | ||||
const COutPoint &o, const Amount v, | const COutPoint &o, const Amount v, | ||||
const uint32_t h, const bool is_coinbase, | const uint32_t h, const bool is_coinbase, | ||||
const CKey &k) { | const CKey &k) { | ||||
// Generate a proof that match the UTXO. | // Generate a proof that match the UTXO. | ||||
ProofBuilder pb(0, 0, key); | ProofBuilder pb(0, 0, key); | ||||
BOOST_CHECK(pb.addUTXO(o, v, h, is_coinbase, k)); | BOOST_CHECK(pb.addUTXO(o, v, h, is_coinbase, k)); | ||||
Proof p = pb.build(); | ProofRef p = pb.build(); | ||||
ProofValidationState state; | ProofValidationState state; | ||||
BOOST_CHECK(p.verify(state)); | BOOST_CHECK(p->verify(state)); | ||||
BOOST_CHECK(p.verify(state, coins) == | BOOST_CHECK(p->verify(state, coins) == | ||||
(result == ProofValidationResult::NONE)); | (result == ProofValidationResult::NONE)); | ||||
BOOST_CHECK(state.GetResult() == result); | BOOST_CHECK(state.GetResult() == result); | ||||
}; | }; | ||||
// Valid proof | // Valid proof | ||||
runCheck(ProofValidationResult::NONE, pkh_outpoint, value, height, false, | runCheck(ProofValidationResult::NONE, pkh_outpoint, value, height, false, | ||||
key); | key); | ||||
Show All 28 Lines | BOOST_AUTO_TEST_CASE(verify) { | ||||
// Mismatching key | // Mismatching key | ||||
{ | { | ||||
runCheck(ProofValidationResult::DESTINATION_MISMATCH, pkh_outpoint, | runCheck(ProofValidationResult::DESTINATION_MISMATCH, pkh_outpoint, | ||||
value, height, false, CKey::MakeCompressedKey()); | value, height, false, CKey::MakeCompressedKey()); | ||||
} | } | ||||
// No stake | // No stake | ||||
{ | { | ||||
Proof p = ProofBuilder(0, 0, key).build(); | ProofRef p = ProofBuilder(0, 0, key).build(); | ||||
ProofValidationState state; | ProofValidationState state; | ||||
BOOST_CHECK(!p.verify(state, coins)); | BOOST_CHECK(!p->verify(state, coins)); | ||||
BOOST_CHECK(state.GetResult() == ProofValidationResult::NO_STAKE); | BOOST_CHECK(state.GetResult() == ProofValidationResult::NO_STAKE); | ||||
} | } | ||||
// Dust thresold | // Dust thresold | ||||
{ | { | ||||
ProofBuilder pb(0, 0, key); | ProofBuilder pb(0, 0, key); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pb.addUTXO(pkh_outpoint, Amount::zero(), height, false, key)); | pb.addUTXO(pkh_outpoint, Amount::zero(), height, false, key)); | ||||
Proof p = pb.build(); | ProofRef p = pb.build(); | ||||
ProofValidationState state; | ProofValidationState state; | ||||
BOOST_CHECK(!p.verify(state, coins)); | BOOST_CHECK(!p->verify(state, coins)); | ||||
BOOST_CHECK(state.GetResult() == ProofValidationResult::DUST_THRESOLD); | BOOST_CHECK(state.GetResult() == ProofValidationResult::DUST_THRESOLD); | ||||
} | } | ||||
{ | { | ||||
ProofBuilder pb(0, 0, key); | ProofBuilder pb(0, 0, key); | ||||
BOOST_CHECK(pb.addUTXO(pkh_outpoint, PROOF_DUST_THRESHOLD - 1 * SATOSHI, | BOOST_CHECK(pb.addUTXO(pkh_outpoint, PROOF_DUST_THRESHOLD - 1 * SATOSHI, | ||||
height, false, key)); | height, false, key)); | ||||
Proof p = pb.build(); | ProofRef p = pb.build(); | ||||
ProofValidationState state; | ProofValidationState state; | ||||
BOOST_CHECK(!p.verify(state, coins)); | BOOST_CHECK(!p->verify(state, coins)); | ||||
BOOST_CHECK(state.GetResult() == ProofValidationResult::DUST_THRESOLD); | BOOST_CHECK(state.GetResult() == ProofValidationResult::DUST_THRESOLD); | ||||
} | } | ||||
// Duplicated input | // Duplicated input | ||||
{ | { | ||||
ProofBuilder pb(0, 0, key); | ProofBuilder pb(0, 0, key); | ||||
BOOST_CHECK(pb.addUTXO(pkh_outpoint, value, height, false, key)); | BOOST_CHECK(pb.addUTXO(pkh_outpoint, value, height, false, key)); | ||||
Proof p = TestProofBuilder::buildDuplicatedStakes(pb); | ProofRef p = TestProofBuilder::buildDuplicatedStakes(pb); | ||||
ProofValidationState state; | ProofValidationState state; | ||||
BOOST_CHECK(!p.verify(state, coins)); | BOOST_CHECK(!p->verify(state, coins)); | ||||
BOOST_CHECK(state.GetResult() == | BOOST_CHECK(state.GetResult() == | ||||
ProofValidationResult::DUPLICATE_STAKE); | ProofValidationResult::DUPLICATE_STAKE); | ||||
} | } | ||||
// Wrong stake ordering | // Wrong stake ordering | ||||
{ | { | ||||
COutPoint other_pkh_outpoint(TxId(InsecureRand256()), InsecureRand32()); | COutPoint other_pkh_outpoint(TxId(InsecureRand256()), InsecureRand32()); | ||||
CTxOut other_pkh_output(value, GetScriptForRawPubKey(pubkey)); | CTxOut other_pkh_output(value, GetScriptForRawPubKey(pubkey)); | ||||
coins.AddCoin(other_pkh_outpoint, Coin(other_pkh_output, height, false), | coins.AddCoin(other_pkh_outpoint, Coin(other_pkh_output, height, false), | ||||
false); | false); | ||||
ProofBuilder pb(0, 0, key); | ProofBuilder pb(0, 0, key); | ||||
BOOST_CHECK(pb.addUTXO(pkh_outpoint, value, height, false, key)); | BOOST_CHECK(pb.addUTXO(pkh_outpoint, value, height, false, key)); | ||||
BOOST_CHECK(pb.addUTXO(other_pkh_outpoint, value, height, false, key)); | BOOST_CHECK(pb.addUTXO(other_pkh_outpoint, value, height, false, key)); | ||||
Proof p = TestProofBuilder::buildWithReversedOrderStakes(pb); | ProofRef p = TestProofBuilder::buildWithReversedOrderStakes(pb); | ||||
ProofValidationState state; | ProofValidationState state; | ||||
BOOST_CHECK(!p.verify(state, coins)); | BOOST_CHECK(!p->verify(state, coins)); | ||||
BOOST_CHECK(state.GetResult() == | BOOST_CHECK(state.GetResult() == | ||||
ProofValidationResult::WRONG_STAKE_ORDERING); | ProofValidationResult::WRONG_STAKE_ORDERING); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(deterministic_proofid) { | BOOST_AUTO_TEST_CASE(deterministic_proofid) { | ||||
CCoinsView coinsDummy; | CCoinsView coinsDummy; | ||||
CCoinsViewCache coins(&coinsDummy); | CCoinsViewCache coins(&coinsDummy); | ||||
auto key = CKey::MakeCompressedKey(); | auto key = CKey::MakeCompressedKey(); | ||||
const Amount value = 12345 * COIN; | const Amount value = 12345 * COIN; | ||||
const uint32_t height = 10; | const uint32_t height = 10; | ||||
std::vector<COutPoint> outpoints(10); | std::vector<COutPoint> outpoints(10); | ||||
for (size_t i = 0; i < 10; i++) { | for (size_t i = 0; i < 10; i++) { | ||||
outpoints[i] = COutPoint(TxId(InsecureRand256()), InsecureRand32()); | outpoints[i] = COutPoint(TxId(InsecureRand256()), InsecureRand32()); | ||||
} | } | ||||
auto computeProofId = [&]() { | auto computeProofId = [&]() { | ||||
ProofBuilder pb(0, 0, key); | ProofBuilder pb(0, 0, key); | ||||
for (const COutPoint &outpoint : outpoints) { | for (const COutPoint &outpoint : outpoints) { | ||||
BOOST_CHECK(pb.addUTXO(outpoint, value, height, false, key)); | BOOST_CHECK(pb.addUTXO(outpoint, value, height, false, key)); | ||||
} | } | ||||
Proof p = pb.build(); | ProofRef p = pb.build(); | ||||
return p.getId(); | return p->getId(); | ||||
}; | }; | ||||
const ProofId proofid = computeProofId(); | const ProofId proofid = computeProofId(); | ||||
Shuffle(outpoints.begin(), outpoints.end(), FastRandomContext()); | Shuffle(outpoints.begin(), outpoints.end(), FastRandomContext()); | ||||
BOOST_CHECK_EQUAL(proofid, computeProofId()); | BOOST_CHECK_EQUAL(proofid, computeProofId()); | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |