Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/proof_tests.cpp
Show All 21 Lines | |||||
BOOST_AUTO_TEST_CASE(proof_random) { | BOOST_AUTO_TEST_CASE(proof_random) { | ||||
for (int i = 0; i < 1000; i++) { | for (int i = 0; i < 1000; i++) { | ||||
const uint32_t score = InsecureRand32(); | const uint32_t score = InsecureRand32(); | ||||
const Proof p = buildRandomProof(score); | const Proof p = buildRandomProof(score); | ||||
BOOST_CHECK_EQUAL(p.getScore(), score); | BOOST_CHECK_EQUAL(p.getScore(), score); | ||||
ProofValidationState state; | ProofValidationState state; | ||||
if (score >= MIN_SCORE) { | |||||
if (score <= MAX_SCORE) { | |||||
BOOST_CHECK(p.verify(state)); | BOOST_CHECK(p.verify(state)); | ||||
BOOST_CHECK(state.GetResult() == ProofValidationResult::NONE); | BOOST_CHECK(state.GetResult() == ProofValidationResult::NONE); | ||||
} else { | |||||
BOOST_CHECK(!p.verify(state)); | |||||
BOOST_CHECK(state.GetResult() == | |||||
ProofValidationResult::INVALID_TOTAL_AMOUNT); | |||||
} | |||||
} else { | |||||
BOOST_CHECK(!p.verify(state)); | |||||
if (score >= MIN_SCORE_DUST) { | |||||
BOOST_CHECK(state.GetResult() == | |||||
ProofValidationResult::DUST_THRESOLD); | |||||
} else { | |||||
BOOST_CHECK(state.GetResult() == | |||||
ProofValidationResult::INSUFFICIENT_TOTAL_AMOUNT); | |||||
} | |||||
} | |||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(proofbuilder) { | BOOST_AUTO_TEST_CASE(proofbuilder) { | ||||
CKey key; | CKey key; | ||||
// Master key. | // Master key. | ||||
key.MakeNewKey(true); | key.MakeNewKey(true); | ||||
const CPubKey master = key.GetPubKey(); | const CPubKey master = key.GetPubKey(); | ||||
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); | ||||
const Amount v = int64_t(InsecureRand32()) * COIN / 100; | |||||
pb.addUTXO(COutPoint(TxId(GetRandHash()), InsecureRand32()), | pb.addUTXO(COutPoint(TxId(GetRandHash()), InsecureRand32()), | ||||
int64_t(InsecureRand32()) * COIN / 100, InsecureRand32(), | int64_t(InsecureRand32()) * COIN / 100, InsecureRand32(), | ||||
InsecureRandBool(), key); | InsecureRandBool(), key); | ||||
} | } | ||||
Proof p = pb.build(); | Proof p = pb.build(); | ||||
ProofValidationState state; | ProofValidationState state; | ||||
▲ Show 20 Lines • Show All 370 Lines • ▼ Show 20 Lines | // Dust thresold | ||||
pb.addUTXO(pkh_outpoint, Amount::zero(), height, false, key); | 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); | ||||
} | } | ||||
// Dust thresold | |||||
{ | |||||
ProofBuilder pb(0, 0, pubkey); | |||||
pb.addUTXO(pkh_outpoint, Amount::zero(), height, false, key); | |||||
Proof p = pb.build(); | |||||
ProofValidationState state; | |||||
BOOST_CHECK(!p.verify(state, coins)); | |||||
BOOST_CHECK(state.GetResult() == ProofValidationResult::DUST_THRESOLD); | |||||
} | |||||
{ | |||||
ProofBuilder pb(0, 0, pubkey); | |||||
pb.addUTXO(pkh_outpoint, PROOF_DUST_THRESOLD - 1 * Amount::satoshi(), | |||||
height, false, key); | |||||
Proof p = pb.build(); | |||||
ProofValidationState state; | |||||
BOOST_CHECK(!p.verify(state, coins)); | |||||
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); | pb.addUTXO(pkh_outpoint, value, height, false, key); | ||||
pb.addUTXO(pkh_outpoint, value, height, false, key); | pb.addUTXO(pkh_outpoint, value, 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() == | BOOST_CHECK(state.GetResult() == | ||||
ProofValidationResult::DUPLICATE_STAKE); | ProofValidationResult::DUPLICATE_STAKE); | ||||
} | } | ||||
// Too low total amount for all stakes | |||||
{ | |||||
ProofBuilder pb(0, 0, pubkey); | |||||
pb.addUTXO(pkh_outpoint, PROOF_MIN_TOTAL_AMOUNT - 1 * SATOSHI, height, | |||||
false, key); | |||||
Proof p = pb.build(); | |||||
ProofValidationState state; | |||||
BOOST_CHECK(!p.verify(state, coins)); | |||||
BOOST_CHECK(state.GetResult() == | |||||
ProofValidationResult::INSUFFICIENT_TOTAL_AMOUNT); | |||||
} | |||||
// Invalid amount | |||||
{ | |||||
ProofBuilder pb(0, 0, pubkey); | |||||
pb.addUTXO(pkh_outpoint, MAX_MONEY + 1 * SATOSHI, height, false, key); | |||||
Proof p = pb.build(); | |||||
ProofValidationState state; | |||||
BOOST_CHECK(!p.verify(state, coins)); | |||||
BOOST_CHECK(state.GetResult() == | |||||
ProofValidationResult::INVALID_TOTAL_AMOUNT); | |||||
} | |||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |