Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more test cases for transfer and transfer_output #535

Merged
merged 13 commits into from
Aug 4, 2023

Conversation

MitchTurner
Copy link
Member

@MitchTurner
Copy link
Member Author

The existing test for for transfer_output was simply wrong. It just happened to work because so many of the test parameters were the same :(

@MitchTurner MitchTurner changed the title Draft: Add more test cases for transfer and transfer_output Add more test cases for transfer and transfer_output Jul 28, 2023
@MitchTurner MitchTurner requested a review from a team July 28, 2023 20:45
@@ -365,6 +345,63 @@ impl<'vm, S, Tx> TransferCtx<'vm, S, Tx> {
}
}

fn get_contract_id_from_memory(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually replace all new functions with CheckedMemConstLen and read, it should be shorter and more readable and will unify the code

image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with

fn get_address_from_memory(
    offset: Word,
    memory: &[u8; MEM_SIZE],
) -> Result<Address, RuntimeError> {
    get_from_memory::<{ Address::LEN }, Address>(offset, memory)
}

fn get_from_memory<const SIZE: usize, T>(
    offset: Word,
    memory: &[u8; MEM_SIZE],
) -> Result<T, RuntimeError>
where
    T: From<[u8; SIZE]>,
{
    let range = CheckedMemConstLen::<SIZE>::new(offset)?;
    let bytes = range.read(memory);
    let value = T::from(*bytes);
    Ok(value)
}

Which I'm happy with.

BUT

I wish that LEN was a associated const for some trait and then we could let the type checker fill in the generics for us:

fn get_from_memory<T>(
    offset: Word,
    memory: &[u8; MEM_SIZE],
) -> Result<T, RuntimeError>
where
    T: WithSize
    T: From<[u8; <T as WithSize>::SIZE]>,
{
    let range = CheckedMemConstLen::<<T as WithSize>::SIZE>::new(offset)?;
    let bytes = range.read(memory);
    let value = T::from(*bytes);
    Ok(value)
}

I would add a trait for it, but then we'd need to import that trait everywhere we want the LEN and that's not worth it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this even cleaner, as it's modular yet still readable:

let contract_id = ContractId::from(read_bytes(self.memory, contract_id_addr)?);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I added that change. I'm keeping the helper functions though because I prefer my code to read declaratively.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, but I think it is already descriptive enough: "Read bytes from memory with offset as ContractId"=)

let contract_id = ContractId::from(read_bytes(memory, offset)?);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. 2v1. I'll change it.

) -> Result<(), RuntimeError> {
// Given

const ASSET_ID: [u8; AssetId::LEN] = [2u8; AssetId::LEN];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can create the type itself. The same is applicable to ContractId and Adress=) And instead of &ASSET_ID[..] you can use ASSET_ID.as_ref()

Suggested change
const ASSET_ID: [u8; AssetId::LEN] = [2u8; AssetId::LEN];
const ASSET_ID: AssetId = AssetId::new([2u8; AssetId::LEN]);

Comment on lines 63 to 67
contract_id_offset: Word,
transfer_amount: Word,
asset_id_offset: Word,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if you tested something with contract_id_offset and asset_id_offset arguments. Otherwise, it is strange why do we have them=D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some simple tests that swap them. I think that's good enough because it shows that it actually uses memory. There are infinite other tests we could write including the offsets and none of them seem particularly interesting off the top of my head.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to test some bad cases like memory overflow=)

@MitchTurner MitchTurner added this pull request to the merge queue Aug 4, 2023
Merged via the queue into master with commit 376989e Aug 4, 2023
31 checks passed
@MitchTurner MitchTurner deleted the more-test-cases branch August 4, 2023 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants