Skip to content

Commit

Permalink
fixed underflow
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrokonrad committed Jan 28, 2025
1 parent d2fa043 commit d0e3739
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"nodeModulesDir": "none",

"name": "@spacebudz/lucid",
"version": "0.10.11",
"version": "0.20.0",
"license": "MIT",
"exports": "./mod.ts",
"author": "Alessandro Konrad",
Expand Down
Binary file modified src/core/libs/lucid_core/pkg/lucid_core_bg.wasm
Binary file not shown.
19 changes: 14 additions & 5 deletions src/core/libs/lucid_core/src/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::{
cmp::Ordering,
collections::{BTreeMap, HashMap},
i128,
net::{Ipv4Addr, Ipv6Addr},
ops::{Add, AddAssign, Deref, DerefMut, Sub, SubAssign},
str::FromStr,
};
Expand Down Expand Up @@ -394,7 +395,7 @@ impl Assets {
}

pub fn get_lovelace(&self) -> u64 {
*self.0.get("lovelace").unwrap_or(&0) as u64
*self.0.get("lovelace").unwrap_or(&0).max(&0) as u64
}

pub fn set_lovelace(&mut self, lovelace: u64) {
Expand Down Expand Up @@ -925,12 +926,20 @@ impl TryFrom<Certificate> for pallas_primitives::conway::Certificate {
Relay::SingleHostIp { ip_v4, ip_v6, port } => {
pallas_primitives::Relay::SingleHostAddr(
port.map_or(Nullable::Null, |p| Nullable::Some(p)),
ip_v6
.map(|ip| ip.parse().map_err(CoreError::msg))
ip_v4
.map(|ip| {
ip.parse::<Ipv4Addr>()
.map(|ip| ip.octets().to_vec().into())
.map_err(CoreError::msg)
})
.transpose()?
.map_or(Nullable::Null, Nullable::Some),
ip_v4
.map(|ip| ip.parse().map_err(CoreError::msg))
ip_v6
.map(|ip| {
ip.parse::<Ipv6Addr>()
.map(|ip| ip.octets().to_vec().into())
.map_err(CoreError::msg)
})
.transpose()?
.map_or(Nullable::Null, Nullable::Some),
)
Expand Down
34 changes: 21 additions & 13 deletions src/core/libs/lucid_core/src/instruction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ impl InstructionBuilder {
self.used_scripts.insert(policy_id);

for (unit, quantity) in assets.iter() {
if *quantity > 0 {
if *quantity >= 0 {
self.implicit_input += Assets::from_unit(unit.clone(), *quantity);
} else {
self.implicit_output += Assets::from_unit(unit.clone(), -*quantity);
self.implicit_output += Assets::from_unit(unit.clone(), -quantity);
}
}

Expand Down Expand Up @@ -485,9 +485,6 @@ impl InstructionBuilder {
bech32::decode(&pool_retirement.pool_id).map_err(CoreError::msg)?;
self.used_keys.insert(pool_id_raw.as_slice().into());

self.implicit_input +=
Assets::from_lovelace(self.protocol_parameters.pool_deposit);

self.certificates
.get_or_insert_with(Vec::new)
.push(BuilderCertificate {
Expand Down Expand Up @@ -647,7 +644,10 @@ impl InstructionBuilder {
// assumes new fee is always higher than previously set fee
let amount_to_subtract = new_fee - old_fee;

change_output.assets -= Assets::from_lovelace(amount_to_subtract);
change_output.assets = change_output
.assets
.clone()
.clamped_sub(Assets::from_lovelace(amount_to_subtract));

if change_output.assets.get_lovelace()
< change_output.required_lovelace(self.protocol_parameters.coins_per_utxo_byte)
Expand Down Expand Up @@ -1212,7 +1212,9 @@ impl InstructionBuilder {
change_output_0
.assets
.set_lovelace(change_value_0.get_lovelace());
total_change_check -= change_output_0.assets.clone();

total_change_check =
total_change_check.clamped_sub(change_output_0.assets.clone());

fee_check +=
change_output_0.get_fee_for_output(builder.protocol_parameters.min_fee_a);
Expand All @@ -1223,9 +1225,12 @@ impl InstructionBuilder {
fee_check +=
change_output_1.get_fee_for_output(builder.protocol_parameters.min_fee_a);

change_output_1
.assets
.set_lovelace(change_output_1.assets.get_lovelace() - fee_check);
change_output_1.assets.set_lovelace(
change_output_1
.assets
.get_lovelace()
.saturating_sub(fee_check),
);

if change_output_1.assets.get_lovelace()
< change_output_1
Expand All @@ -1250,9 +1255,12 @@ impl InstructionBuilder {
fee_check +=
change_output_0.get_fee_for_output(builder.protocol_parameters.min_fee_a);

change_output_0
.assets
.set_lovelace(change_output_0.assets.get_lovelace() - fee_check);
change_output_0.assets.set_lovelace(
change_output_0
.assets
.get_lovelace()
.saturating_sub(fee_check),
);

if change_output_0.assets.get_lovelace()
< change_output_0
Expand Down

0 comments on commit d0e3739

Please sign in to comment.