Skip to content

Commit

Permalink
Implement sane wrapper over all the plutus integer types
Browse files Browse the repository at this point in the history
CDDL defines:
```
int = uint / nint
biguint = #6.2(bstr)
bignint = #6.3(bstr)
bigint = biguint / bignint
integer = int / bigint
unsigned = uint / biguint
```

which is very unwieldly to use and not to mention to have to interpret
the bytes correctly and everything so this commit introduces a single
wrapper type `BigInt` that covers all of these cases and tries to encode
the integer in the smallest possible type.
  • Loading branch information
rooooooooob committed Jul 8, 2021
1 parent d6619be commit 3b68540
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 396 deletions.
55 changes: 46 additions & 9 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ hex = "0.4.0"
cfg-if = "1"
linked-hash-map = "0.5.3"
serde_json = "1.0.57"
num-bigint = "0.4.0"
# The default can't be compiled to wasm, so it's necessary to use either the 'nightly'
# feature or this one
clear_on_drop = { version = "0.2", features = ["no_cc"] }
Expand Down
57 changes: 3 additions & 54 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,7 @@ pub struct ProtocolParamUpdate {
execution_costs: Option<ExUnitPrices>,
max_tx_ex_units: Option<ExUnits>,
max_block_ex_units: Option<ExUnits>,
max_value_size: Option<PreludeUnsigned>,
max_value_size: Option<u32>,
}

to_from_bytes!(ProtocolParamUpdate);
Expand Down Expand Up @@ -2093,11 +2093,11 @@ impl ProtocolParamUpdate {
self.max_block_ex_units.clone()
}

pub fn set_max_value_size(&mut self, max_value_size: &PreludeUnsigned) {
pub fn set_max_value_size(&mut self, max_value_size: u32) {
self.max_value_size = Some(max_value_size.clone())
}

pub fn max_value_size(&self) -> Option<PreludeUnsigned> {
pub fn max_value_size(&self) -> Option<u32> {
self.max_value_size.clone()
}

Expand Down Expand Up @@ -2678,57 +2678,6 @@ impl NetworkId {
}
}

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum PreludeUnsignedKind {
U64,
PreludeBiguint,
}

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
enum PreludeUnsignedEnum {
U64(BigNum),
PreludeBiguint(PreludeBiguint),
}

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct PreludeUnsigned(PreludeUnsignedEnum);

to_from_bytes!(PreludeUnsigned);

#[wasm_bindgen]
impl PreludeUnsigned {
pub fn new_u64(uint: BigNum) -> Self {
Self(PreludeUnsignedEnum::U64(uint))
}

pub fn new_prelude_biguint(prelude_biguint: &PreludeBiguint) -> Self {
Self(PreludeUnsignedEnum::PreludeBiguint(prelude_biguint.clone()))
}

pub fn kind(&self) -> PreludeUnsignedKind {
match &self.0 {
PreludeUnsignedEnum::U64(_) => PreludeUnsignedKind::U64,
PreludeUnsignedEnum::PreludeBiguint(_) => PreludeUnsignedKind::PreludeBiguint,
}
}

pub fn as_u64(&self) -> Option<BigNum> {
match &self.0 {
PreludeUnsignedEnum::U64(x) => Some(x.clone()),
_ => None,
}
}

pub fn as_prelude_biguint(&self) -> Option<PreludeBiguint> {
match &self.0 {
PreludeUnsignedEnum::PreludeBiguint(x) => Some(x.clone()),
_ => None,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 3b68540

Please sign in to comment.