Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/proof_tests.cpp
Show First 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(proofbuilder) { | ||||
const uint64_t sequence = InsecureRandBits(64); | const uint64_t sequence = InsecureRandBits(64); | ||||
const int64_t expiration = InsecureRandBits(64); | const int64_t expiration = InsecureRandBits(64); | ||||
ProofBuilder pb(sequence, expiration, master); | ProofBuilder pb(sequence, expiration, master); | ||||
for (int i = 0; i < 3; i++) { | for (int i = 0; i < 3; i++) { | ||||
key.MakeNewKey(true); | key.MakeNewKey(true); | ||||
pb.addUTXO(COutPoint(TxId(GetRandHash()), InsecureRand32()), | BOOST_CHECK(pb.addUTXO(COutPoint(TxId(GetRandHash()), InsecureRand32()), | ||||
int64_t(InsecureRand32()) * COIN / 100, InsecureRand32(), | int64_t(InsecureRand32()) * COIN / 100, | ||||
InsecureRandBool(), key); | InsecureRand32(), InsecureRandBool(), key)); | ||||
} | } | ||||
Proof p = pb.build(); | Proof 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); | ||||
▲ Show 20 Lines • Show All 283 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(verify) { | ||||
coins.AddCoin(p2sh_outpoint, Coin(p2sh_output, height, false), false); | coins.AddCoin(p2sh_outpoint, Coin(p2sh_output, height, false), false); | ||||
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, pubkey); | ProofBuilder pb(0, 0, pubkey); | ||||
pb.addUTXO(o, v, h, is_coinbase, k); | BOOST_CHECK(pb.addUTXO(o, v, h, is_coinbase, k)); | ||||
Proof p = pb.build(); | Proof 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); | ||||
}; | }; | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | // No stake | ||||
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, pubkey); | ProofBuilder pb(0, 0, pubkey); | ||||
pb.addUTXO(pkh_outpoint, Amount::zero(), height, false, key); | BOOST_CHECK( | ||||
pb.addUTXO(pkh_outpoint, Amount::zero(), height, false, key)); | |||||
Proof p = pb.build(); | Proof 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, pubkey); | ProofBuilder pb(0, 0, pubkey); | ||||
pb.addUTXO(pkh_outpoint, PROOF_DUST_THRESHOLD - 1 * SATOSHI, height, | BOOST_CHECK(pb.addUTXO(pkh_outpoint, PROOF_DUST_THRESHOLD - 1 * SATOSHI, | ||||
false, key); | height, false, key)); | ||||
Proof p = pb.build(); | Proof 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, pubkey); | ProofBuilder pb(0, 0, pubkey); | ||||
pb.addUTXO(pkh_outpoint, value, height, false, key); | BOOST_CHECK(pb.addUTXO(pkh_outpoint, value, height, false, key)); | ||||
Proof p = TestProofBuilder::buildDuplicatedStakes(pb); | Proof 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, pubkey); | ProofBuilder pb(0, 0, pubkey); | ||||
pb.addUTXO(pkh_outpoint, value, height, false, key); | BOOST_CHECK(pb.addUTXO(pkh_outpoint, value, height, false, key)); | ||||
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); | Proof 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); | ||||
} | } | ||||
} | } | ||||
Show All 11 Lines | BOOST_AUTO_TEST_CASE(deterministic_proofid) { | ||||
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, pubkey); | ProofBuilder pb(0, 0, pubkey); | ||||
for (const COutPoint &outpoint : outpoints) { | for (const COutPoint &outpoint : outpoints) { | ||||
pb.addUTXO(outpoint, value, height, false, key); | BOOST_CHECK(pb.addUTXO(outpoint, value, height, false, key)); | ||||
} | } | ||||
Proof p = pb.build(); | Proof 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() |