-
Notifications
You must be signed in to change notification settings - Fork 87
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
Conversation
The existing test for for |
transfer
and transfer_output
transfer
and transfer_output
fuel-vm/src/interpreter/contract.rs
Outdated
@@ -365,6 +345,63 @@ impl<'vm, S, Tx> TransferCtx<'vm, S, Tx> { | |||
} | |||
} | |||
|
|||
fn get_contract_id_from_memory( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)?);
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)?);
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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()
const ASSET_ID: [u8; AssetId::LEN] = [2u8; AssetId::LEN]; | |
const ASSET_ID: AssetId = AssetId::new([2u8; AssetId::LEN]); |
contract_id_offset: Word, | ||
transfer_amount: Word, | ||
asset_id_offset: Word, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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=)
de29d81
to
b140ecd
Compare
#510