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

TRO opcode does not revert when amount=0 #522

Closed
Dentosal opened this issue Jul 25, 2023 · 0 comments · Fixed by #532
Closed

TRO opcode does not revert when amount=0 #522

Dentosal opened this issue Jul 25, 2023 · 0 comments · Fixed by #532
Labels
breaking A breaking api change fuel-vm Related to the `fuel-vm` crate. testing

Comments

@Dentosal
Copy link
Member

The specification states that TRO should panic if the amount ($rC) is zero but the implementation does not perform this validation. TR performs this validation.

pub(crate) fn transfer_output(
self,
a: Word,
b: Word,
c: Word,
d: Word,
) -> Result<(), RuntimeError>
where
Tx: ExecutableTransaction,
S: ContractsAssetsStorage,
<S as StorageInspect<ContractsAssets>>::Error: Into<std::io::Error>,
{
let ax = a
.checked_add(ContractId::LEN as Word)
.ok_or(PanicReason::ArithmeticOverflow)?;
let dx = d
.checked_add(AssetId::LEN as Word)
.ok_or(PanicReason::ArithmeticOverflow)?;
// if above usize::MAX then it cannot be safely cast to usize,
// check the tighter bound between VM_MAX_RAM and usize::MAX
if ax > MIN_VM_MAX_RAM_USIZE_MAX || dx > MIN_VM_MAX_RAM_USIZE_MAX {
return Err(PanicReason::MemoryOverflow.into())
}
let out_idx = b as usize;
let to = Address::try_from(&self.memory[a as usize..ax as usize])
.expect("Unreachable! Checked memory range");
let asset_id = AssetId::try_from(&self.memory[d as usize..dx as usize])
.expect("Unreachable! Checked memory range");
let amount = c;
let internal_context = match internal_contract(self.context, self.fp, self.memory)
{
// optimistically attempt to load the internal contract id
Ok(source_contract) => Some(*source_contract),
// revert to external context if no internal contract is set
Err(RuntimeError::Recoverable(PanicReason::ExpectedInternalContext)) => None,
// bubble up any other kind of errors
Err(e) => return Err(e),
};
if let Some(source_contract) = internal_context {
// debit funding source (source contract balance)
balance_decrease(self.storage, &source_contract, &asset_id, amount)?;
} else {
// debit external funding source (i.e. UTXOs)
external_asset_id_balance_sub(self.balances, self.memory, &asset_id, amount)?;
}
// credit variable output
let variable = Output::variable(to, amount, asset_id);
set_variable_output(self.tx, self.memory, self.tx_offset, out_idx, variable)?;
let receipt = Receipt::transfer_out(
internal_context.unwrap_or_default(),
to,
amount,
asset_id,
*self.pc,
*self.is,
);
append_receipt(
AppendReceipt {
receipts: self.receipts,
script: self.tx.as_script_mut(),
tx_offset: self.tx_offset,
memory: self.memory,
},
receipt,
);
inc_pc(self.pc)
}
}

Either update the specification, or add validation behavior. This should have a test case as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking A breaking api change fuel-vm Related to the `fuel-vm` crate. testing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant