Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/proof.cpp
Show First 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | |||||
Amount Proof::getStakedAmount() const { | Amount Proof::getStakedAmount() const { | ||||
return std::accumulate(stakes.begin(), stakes.end(), Amount::zero(), | return std::accumulate(stakes.begin(), stakes.end(), Amount::zero(), | ||||
[](const Amount current, const SignedStake &ss) { | [](const Amount current, const SignedStake &ss) { | ||||
return current + ss.getStake().getAmount(); | return current + ss.getStake().getAmount(); | ||||
}); | }); | ||||
} | } | ||||
bool Proof::verify(ProofValidationState &state) const { | bool Proof::verify(const ProofParams ¶ms, | ||||
ProofValidationState &state) const { | |||||
if (stakes.empty()) { | if (stakes.empty()) { | ||||
return state.Invalid(ProofValidationResult::NO_STAKE, "no-stake"); | return state.Invalid(ProofValidationResult::NO_STAKE, "no-stake"); | ||||
} | } | ||||
if (stakes.size() > AVALANCHE_MAX_PROOF_STAKES) { | if (stakes.size() > AVALANCHE_MAX_PROOF_STAKES) { | ||||
return state.Invalid( | return state.Invalid( | ||||
ProofValidationResult::TOO_MANY_UTXOS, "too-many-utxos", | ProofValidationResult::TOO_MANY_UTXOS, "too-many-utxos", | ||||
strprintf("%u > %u", stakes.size(), AVALANCHE_MAX_PROOF_STAKES)); | strprintf("%u > %u", stakes.size(), AVALANCHE_MAX_PROOF_STAKES)); | ||||
Show All 11 Lines | if (!useLegacy(gArgs)) { | ||||
"invalid-proof-signature"); | "invalid-proof-signature"); | ||||
} | } | ||||
} | } | ||||
StakeId prevId = uint256::ZERO; | StakeId prevId = uint256::ZERO; | ||||
std::unordered_set<COutPoint, SaltedOutpointHasher> utxos; | std::unordered_set<COutPoint, SaltedOutpointHasher> utxos; | ||||
for (const SignedStake &ss : stakes) { | for (const SignedStake &ss : stakes) { | ||||
const Stake &s = ss.getStake(); | const Stake &s = ss.getStake(); | ||||
if (s.getAmount() < PROOF_DUST_THRESHOLD) { | if (s.getAmount() < params.stakeUtxoDustThreshold) { | ||||
return state.Invalid(ProofValidationResult::DUST_THRESHOLD, | return state.Invalid( | ||||
ProofValidationResult::DUST_THRESHOLD, | |||||
"amount-below-dust-threshold", | "amount-below-dust-threshold", | ||||
strprintf("%s < %s", s.getAmount().ToString(), | strprintf("%s < %s", s.getAmount().ToString(), | ||||
PROOF_DUST_THRESHOLD.ToString())); | params.stakeUtxoDustThreshold.ToString())); | ||||
} | } | ||||
if (s.getId() < prevId) { | if (s.getId() < prevId) { | ||||
return state.Invalid(ProofValidationResult::WRONG_STAKE_ORDERING, | return state.Invalid(ProofValidationResult::WRONG_STAKE_ORDERING, | ||||
"wrong-stake-ordering"); | "wrong-stake-ordering"); | ||||
} | } | ||||
prevId = s.getId(); | prevId = s.getId(); | ||||
if (!utxos.insert(s.getUTXO()).second) { | if (!utxos.insert(s.getUTXO()).second) { | ||||
return state.Invalid(ProofValidationResult::DUPLICATE_STAKE, | return state.Invalid(ProofValidationResult::DUPLICATE_STAKE, | ||||
"duplicated-stake"); | "duplicated-stake"); | ||||
} | } | ||||
if (!ss.verify(getStakeCommitment())) { | if (!ss.verify(getStakeCommitment())) { | ||||
return state.Invalid( | return state.Invalid( | ||||
ProofValidationResult::INVALID_STAKE_SIGNATURE, | ProofValidationResult::INVALID_STAKE_SIGNATURE, | ||||
"invalid-stake-signature", | "invalid-stake-signature", | ||||
strprintf("TxId: %s", s.getUTXO().GetTxId().ToString())); | strprintf("TxId: %s", s.getUTXO().GetTxId().ToString())); | ||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool Proof::verify(ProofValidationState &state, | bool Proof::verify(const ProofParams ¶ms, ProofValidationState &state, | ||||
const ChainstateManager &chainman) const { | const ChainstateManager &chainman) const { | ||||
AssertLockHeld(cs_main); | AssertLockHeld(cs_main); | ||||
if (!verify(state)) { | if (!verify(params, state)) { | ||||
// state is set by verify. | // state is set by verify. | ||||
return false; | return false; | ||||
} | } | ||||
const int64_t activeHeight = chainman.ActiveHeight(); | const int64_t activeHeight = chainman.ActiveHeight(); | ||||
const int64_t stakeUtxoMinConfirmations = | const int64_t stakeUtxoMinConfirmations = | ||||
gArgs.GetArg("-avaproofstakeutxoconfirmations", | gArgs.GetArg("-avaproofstakeutxoconfirmations", | ||||
AVALANCHE_DEFAULT_STAKE_UTXO_CONFIRMATIONS); | AVALANCHE_DEFAULT_STAKE_UTXO_CONFIRMATIONS); | ||||
▲ Show 20 Lines • Show All 72 Lines • Show Last 20 Lines |