Skip to content

Commit

Permalink
Adding minimum stake restriction to the add stake.
Browse files Browse the repository at this point in the history
  • Loading branch information
ii-cruz committed Jan 7, 2025
1 parent b160775 commit 7217e53
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
17 changes: 11 additions & 6 deletions primitives/account/src/account/staking_contract/staker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,17 @@ impl StakingContract {
// Get the staker.
let mut staker = store.expect_staker(staker_address)?;

// Fail if the minimum stake would be violated for the non-retired funds (invariant 1).
Staker::enforce_min_stake(
staker.active_balance + value,
staker.inactive_balance,
staker.retired_balance,
)?;
// Add stake txs never violate minimum stake for the non-retired funds (invariant 1),
// because the intrinsic tx checks that value is >= min stake.
assert!(
Staker::enforce_min_stake(
staker.active_balance + value,
staker.inactive_balance,
staker.retired_balance,
)
.is_ok(),
"Add stake should never violate the min stake invariants"
);

// All checks passed, not allowed to fail from here on!

Expand Down
15 changes: 5 additions & 10 deletions primitives/account/tests/staking_contract/staker.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use nimiq_account::*;
use nimiq_database::{mdbx::MdbxDatabase, traits::Database};
use nimiq_keys::Address;
use nimiq_primitives::{account::AccountError, coin::Coin, policy::Policy};
use nimiq_primitives::{
account::AccountError, coin::Coin, policy::Policy, transaction::TransactionError,
};
use nimiq_test_log::test;
use nimiq_transaction::{
account::staking_contract::{IncomingStakingTransactionData, OutgoingStakingTransactionData},
Expand Down Expand Up @@ -473,16 +475,9 @@ fn add_stake_enforces_minimum_stake() {
Policy::MINIMUM_STAKE - 1,
&staker_keypair,
);

let mut tx_logs = TransactionLog::empty();
assert_eq!(
staker_setup.staking_contract.commit_incoming_transaction(
&tx,
&staker_setup.before_release_block_state,
data_store.write(&mut db_txn),
&mut tx_logs,
),
Err(AccountError::InvalidCoinValue)
tx.verify(NetworkId::UnitAlbatross),
Err(TransactionError::InvalidValue)
);

// Can add in the valid case.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ impl IncomingStakingTransactionData {
verify_transaction_signature(transaction, proof)?
}
IncomingStakingTransactionData::AddStake { .. } => {
// Adding stake should be at least greater than 0.
if transaction.value.is_zero() {
warn!("Add stake transactions must have positive value. The offending transaction is the following:\n{:?}", transaction);
return Err(TransactionError::ZeroValue);
// Adding stake should be greater than 0.
if transaction.value < Coin::from_u64_unchecked(Policy::MINIMUM_STAKE) {
warn!("Add stake must increment stake by at least minimum stake. The offending transaction is the following:\n{:?}", transaction);
return Err(TransactionError::InvalidValue);
}

// No more checks needed.
Expand Down
4 changes: 2 additions & 2 deletions primitives/transaction/tests/staking_contract_verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,12 @@ fn stake() {
IncomingStakingTransactionData::AddStake {
staker_address: STAKER_ADDRESS.parse().unwrap(),
},
100,
Policy::MINIMUM_STAKE,
&keypair,
None,
);

let tx_hex = "018c551fabc6e6e00c609c3f0313257ad7e835643c000000000000000000000000000000000000000000010315068c551fabc6e6e00c609c3f0313257ad7e835643c000000000000006400000000000000640000000107006200b3adb13fe6887f6cdcb8c82c429f718fcdbbb27b2a19df7c1ea9814f19cd910500f5d801f531117483118108b30cd606301a424ba63147f3f0a2e085bd655fd15c8eafb971a39883fc9da3711d7ac474cd6047eed7791ec6e00c6ed1a464fddb01";
let tx_hex = "018c551fabc6e6e00c609c3f0313257ad7e835643c000000000000000000000000000000000000000000010315068c551fabc6e6e00c609c3f0313257ad7e835643c000000000098968000000000000000640000000107006200b3adb13fe6887f6cdcb8c82c429f718fcdbbb27b2a19df7c1ea9814f19cd910500f6318c0aa54b87dc8da554a2f5cdab566d69d09d5f4b148fe98a547cfb4d2032623a5aa11d3150cb716160f5d1ba87adda0ae203f5659d60490366e8a020f10a";
let tx_size = 187;

let mut ser_tx: Vec<u8> = Vec::with_capacity(tx_size);
Expand Down
2 changes: 1 addition & 1 deletion test-utils/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl<R: Rng + CryptoRng> TransactionsGenerator<R> {
IncomingType::SetActiveStake => {
value = Coin::ZERO;
}
IncomingType::CreateStaker | IncomingType::RetireStake => {
IncomingType::CreateStaker | IncomingType::AddStake | IncomingType::RetireStake => {
value = Coin::from_u64_unchecked(Policy::MINIMUM_STAKE);
}
_ => {}
Expand Down

0 comments on commit 7217e53

Please sign in to comment.