fuels_core/utils/
offsets.rs

1use fuel_asm::Instruction;
2use fuel_tx::{field::Script, ConsensusParameters};
3use fuel_types::bytes::padded_len_usize;
4
5use crate::{error, types::errors::Result};
6
7/// Gets the base offset for a script or a predicate. The offset depends on the `max_inputs`
8/// field of the `ConsensusParameters` and the static offset.
9pub fn base_offset_script(consensus_parameters: &ConsensusParameters) -> usize {
10    consensus_parameters.tx_params().tx_offset() + fuel_tx::Script::script_offset_static()
11}
12
13/// Calculates the length of the script based on the number of contract calls it
14/// has to make and returns the offset at which the script data begins
15pub fn call_script_data_offset(
16    consensus_parameters: &ConsensusParameters,
17    calls_instructions_len: usize,
18) -> Result<usize> {
19    // Instruction::SIZE is a placeholder for the RET instruction which is added later for returning
20    // from the script. This doesn't happen in the predicate.
21    let opcode_len = Instruction::SIZE;
22
23    let padded_len = padded_len_usize(calls_instructions_len + opcode_len).ok_or_else(|| {
24        error!(
25            Other,
26            "call script data len overflow: {calls_instructions_len}"
27        )
28    })?;
29    Ok(base_offset_script(consensus_parameters) + padded_len)
30}