Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions rust/src/base/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
utility::functions::nanomina_to_mina,
};
use anyhow::anyhow;
use log::error;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::ops::{Add, AddAssign, Sub, SubAssign};
Expand Down Expand Up @@ -58,6 +59,9 @@ impl Add<i64> for Amount {
let abs = rhs.unsigned_abs();

if rhs < 0 {
if self.0 < abs {
panic!("Amount: u64 underflow in Add<i64>")
}
Self(self.0 - abs)
} else {
Self(self.0 + abs)
Expand All @@ -66,18 +70,15 @@ impl Add<i64> for Amount {
}

impl Sub<Amount> for Amount {
type Output = Amount;
type Output = Option<Amount>;

fn sub(self, rhs: Amount) -> Self::Output {
Self(self.0.saturating_sub(rhs.0))
}
}

impl Sub<u64> for Amount {
type Output = Amount;

fn sub(self, rhs: u64) -> Self::Output {
Self(self.0 - rhs)
if self.0 < rhs.0 {
error!("Underflow in Sub<Amount>: self.0: {} rhs.0: {}", self.0, rhs.0);
None
} else {
Some(Self(self.0 - rhs.0))
}
}
}

Expand All @@ -98,6 +99,9 @@ impl AddAssign<i64> for Amount {
let abs = rhs.unsigned_abs();

if rhs < 0 {
if self.0 < abs {
panic!("Underflow in AddAssign<i64>")
}
*self -= abs;
} else {
*self += abs;
Expand All @@ -107,6 +111,9 @@ impl AddAssign<i64> for Amount {

impl SubAssign<u64> for Amount {
fn sub_assign(&mut self, rhs: u64) {
if self.0 < rhs {
panic!("Underflow in SubAssign<u64>")
}
*self = Self(self.0 - rhs)
}
}
Expand All @@ -118,6 +125,9 @@ impl SubAssign<i64> for Amount {
if rhs < 0 {
*self += abs;
} else {
if self.0 < abs {
panic!("Underflow in SubAssign<i64>")
}
*self -= abs;
}
}
Expand Down
4 changes: 2 additions & 2 deletions rust/src/block/precomputed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ impl PrecomputedBlock {
// maybe coinbase receiver
if let Some(bal) = self.coinbase_receiver_balance() {
if [
(Amount(MAINNET_COINBASE_REWARD) - MAINNET_ACCOUNT_CREATION_FEE).0,
Amount(MAINNET_COINBASE_REWARD - MAINNET_ACCOUNT_CREATION_FEE_U64).0,
// supercharged
(Amount(2 * MAINNET_COINBASE_REWARD) - MAINNET_ACCOUNT_CREATION_FEE).0,
Amount(2 * MAINNET_COINBASE_REWARD - MAINNET_ACCOUNT_CREATION_FEE_U64).0,
]
.contains(&bal)
{
Expand Down
5 changes: 3 additions & 2 deletions rust/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ pub const MINA_TOKEN_ID: u64 = 1;

pub const MAINNET_BLOCK_SLOT_TIME_MILLIS: u64 = 180000;
pub const MAINNET_TRANSITION_FRONTIER_K: u32 = 290;
pub const MAINNET_ACCOUNT_CREATION_FEE: Amount = Amount(1e9 as u64);
pub const MAINNET_COINBASE_REWARD: u64 = 720000000000;
pub const MAINNET_ACCOUNT_CREATION_FEE_U64: u64 = 1e9 as u64;
pub const MAINNET_ACCOUNT_CREATION_FEE: Amount = Amount(MAINNET_ACCOUNT_CREATION_FEE_U64);
pub const MAINNET_COINBASE_REWARD: u64 = 720_000_000_000;

pub const MAINNET_GENESIS_HASH: &str = "3NKeMoncuHab5ScarV5ViyF16cJPT4taWNSaTLS64Dp67wuXigPZ";
pub const MAINNET_GENESIS_PREV_STATE_HASH: &str =
Expand Down
13 changes: 8 additions & 5 deletions rust/src/ledger/account/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ impl Account {
&& !self.created_by_zkapp
{
return Self {
balance: self.balance - MAINNET_ACCOUNT_CREATION_FEE,
// TODO:
// balance: (self.balance - MAINNET_ACCOUNT_CREATION_FEE).unwrap_or_else(|| panic!("Underflow in Account::display. pk: {:?}", self.public_key)),
balance: (self.balance - MAINNET_ACCOUNT_CREATION_FEE).unwrap_or(Amount(0)),
..self
};
}
Expand Down Expand Up @@ -183,7 +185,7 @@ impl Account {
/// Unapply a coinbase
pub fn coinbase_unapply(self, diff: &CoinbaseDiff) -> Self {
Self {
balance: self.balance - diff.amount,
balance: (self.balance - diff.amount).unwrap_or_else(|| panic!("Underflow in coinbase_unapply. CoinbaseDiff: {:?}", diff)),
..self
}
}
Expand Down Expand Up @@ -219,7 +221,7 @@ impl Account {
pub fn payment_unapply(self, diff: &PaymentDiff) -> Self {
match diff.update_type {
UpdateType::Credit => Self {
balance: self.balance - diff.amount,
balance: (self.balance - diff.amount).unwrap_or_else(|| panic!("Underflow in payment_unapply. PaymentDiff: {:?}", diff)),
..self
},
UpdateType::Debit(nonce) => Self {
Expand Down Expand Up @@ -253,7 +255,8 @@ impl Account {
/// balance.
fn debit(self, amount: Amount, nonce: Option<Nonce>) -> Self {
Self {
balance: self.balance - amount,
balance: (self.balance - amount).unwrap_or_else(||
panic!("Underflow in Account::debit. pk: {} balance: {} amount: {}", self.public_key, self.balance, amount)),
nonce: nonce.or(self.nonce),
..self
}
Expand Down Expand Up @@ -825,7 +828,7 @@ mod tests {
assert_eq!(
after,
Account {
balance: before.balance - amount,
balance: (before.balance - amount).unwrap_or_else(|| panic!("Underflow in zkapp_account_diff_payment. pk: {}", pk)),
..before
}
);
Expand Down