Skip to content

Commit

Permalink
impl_wasm_map/list macro rewrite + clippy fixes + CI
Browse files Browse the repository at this point in the history
impl_wasm_map is now aware of whether the keys/values are WASM-exposable
and copy or not. There is now a away to specify the backing map e.g.
BTreeMap for preserve-encodings=false i.e. Byron, CIP25

Use of this macro now replaces hard-generated WASM Map (and list) types
to reduce boilerplate + allow a single common place to fix linting
errors (one of the main reasons for this change to be done in this PR -
but it was planned at some point anyway)

Fixes #234

The rest of clippy linting errors were also fixed and a check in the CI
for fmt + linting.
  • Loading branch information
rooooooooob committed Aug 16, 2023
1 parent 9279d1d commit 38a82c3
Show file tree
Hide file tree
Showing 74 changed files with 1,195 additions and 3,368 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ jobs:
- name: rust:build-browser
run: |
npm run rust:build-browser
- name: cargo-fmt
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
- name: cargo-clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: --workspace --all-features --all-targets -- --deny "clippy::all"
- name: all:test
run: cargo test
- name: chain:install
Expand Down
17 changes: 7 additions & 10 deletions chain/rust/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,12 @@ impl Address {
data: &[u8],
addr_size: usize,
) -> Result<Option<Vec<u8>>, DeserializeFailure> {
if data.len() < addr_size {
Err(DeserializeFailure::CBOR(cbor_event::Error::NotEnough(
data.len(),
addr_size,
)))
} else if data.len() > addr_size {
Ok(Some(data[addr_size..].to_vec()))
} else {
Ok(None)
match data.len().cmp(&addr_size) {
std::cmp::Ordering::Less => Err(DeserializeFailure::CBOR(
cbor_event::Error::NotEnough(data.len(), addr_size),
)),
std::cmp::Ordering::Greater => Ok(Some(data[addr_size..].to_vec())),
std::cmp::Ordering::Equal => Ok(None),
}
}
match (header & 0xF0) >> 4 {
Expand Down Expand Up @@ -1433,7 +1430,7 @@ mod tests {
255, 255, 255, 255, 255, 255, 255, 255, 127, 129, 255, 255, 255, 255, 255, 255, 255,
255, 127, 129, 255, 255, 255, 255, 255, 255, 255, 255, 127,
];
let addr = Address::from_raw_bytes(&addr_bytes.clone()).unwrap();
let addr = Address::from_raw_bytes(&addr_bytes).unwrap();
assert_eq!(addr_bytes, addr.to_raw_bytes());
}
}
6 changes: 6 additions & 0 deletions chain/rust/src/auxdata/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,9 @@ impl AuxiliaryData {
}
}
}

impl Default for AuxiliaryData {
fn default() -> Self {
Self::new()
}
}
1 change: 1 addition & 0 deletions chain/rust/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub struct HeaderBody {
}

impl HeaderBody {
#[allow(clippy::too_many_arguments)]
pub fn new(
block_number: u64,
slot: u64,
Expand Down
16 changes: 10 additions & 6 deletions chain/rust/src/builders/input_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use crate::{
#[derive(Debug, thiserror::Error)]
pub enum InputBuilderError {
#[error("UTXO address was not a payment key: {0:?}")]
UTXOAddressNotPayment(Address),
UTXOAddressNotPayment(Box<Address>),
#[error("Missing the following witnesses for the input: {0:?}")]
MissingWitnesses(RequiredWitnessSet),
MissingWitnesses(Box<RequiredWitnessSet>),
}

pub fn input_required_wits(
Expand Down Expand Up @@ -71,9 +71,9 @@ impl SingleInputBuilder {
let required_wits_left = required_wits.clone();

if !required_wits_left.scripts.is_empty() {
return Err(InputBuilderError::UTXOAddressNotPayment(
return Err(InputBuilderError::UTXOAddressNotPayment(Box::new(
self.utxo_info.address().clone(),
));
)));
}

Ok(InputBuilderResult {
Expand All @@ -99,7 +99,9 @@ impl SingleInputBuilder {
required_wits_left.scripts.remove(script_hash);

if !required_wits_left.scripts.is_empty() {
return Err(InputBuilderError::MissingWitnesses(required_wits_left));
return Err(InputBuilderError::MissingWitnesses(Box::new(
required_wits_left,
)));
}

Ok(InputBuilderResult {
Expand Down Expand Up @@ -138,7 +140,9 @@ impl SingleInputBuilder {
.remove(&hash_plutus_data(&datum));

if required_wits_left.len() > 0 {
return Err(InputBuilderError::MissingWitnesses(required_wits_left));
return Err(InputBuilderError::MissingWitnesses(Box::new(
required_wits_left,
)));
}

Ok(InputBuilderResult {
Expand Down
6 changes: 3 additions & 3 deletions chain/rust/src/builders/redeemer_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl RedeemerSetBuilder {
)));
}
RedeemerTag::Cert => {
let entry = self.cert.iter_mut().nth(key.index as usize).unwrap();
let entry = self.cert.get_mut(key.index as usize).unwrap();
*entry = Some(UntaggedRedeemerPlaceholder::Full(UntaggedRedeemer::new(
entry.as_ref().unwrap().data().clone(),
ex_units,
Expand Down Expand Up @@ -359,8 +359,8 @@ mod tests {

let input_result = InputBuilderResult {
input: TransactionInput::new(TransactionHash::from([0; 32]), 0),
utxo_info: ShelleyTxOut::new(address.clone(), Value::zero()).into(),
aggregate_witness: Some(data.clone()),
utxo_info: ShelleyTxOut::new(address, Value::zero()).into(),
aggregate_witness: Some(data),
required_wits: RequiredWitnessSet::new(),
};

Expand Down
Loading

0 comments on commit 38a82c3

Please sign in to comment.