use crate::interpreter::MemoryRange;
use fuel_asm::Word;
use fuel_tx::{field, ConsensusParameters};
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct RuntimePredicate {
program: MemoryRange,
idx: usize,
}
impl RuntimePredicate {
pub const fn program(&self) -> &MemoryRange {
&self.program
}
pub const fn idx(&self) -> usize {
self.idx
}
pub fn from_tx<T>(params: &ConsensusParameters, tx: &T, idx: usize) -> Option<Self>
where
T: field::Inputs,
{
tx.inputs_predicate_offset_at(idx)
.map(|(ofs, len)| (ofs as Word + params.tx_offset() as Word, len as Word))
.map(|(ofs, len)| MemoryRange::new(ofs, len))
.map(|program| Self { program, idx })
}
}
#[test]
fn from_tx_works() {
use fuel_asm::op;
use fuel_tx::TransactionBuilder;
use fuel_types::bytes;
use fuel_vm::prelude::*;
use rand::rngs::StdRng;
use rand::{Rng, SeedableRng};
use std::iter;
let rng = &mut StdRng::seed_from_u64(2322u64);
let params = ConsensusParameters::default();
let height = 1;
#[rustfmt::skip]
let predicate: Vec<u8> = vec![
op::addi(0x10, 0x00, 0x01),
op::addi(0x10, 0x10, 0x01),
op::ret(0x01),
].into_iter().collect();
let predicate_data = b"If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.".to_vec();
let owner = (*Contract::root_from_code(&predicate)).into();
let a = Input::coin_predicate(
rng.gen(),
owner,
rng.gen(),
rng.gen(),
rng.gen(),
rng.gen(),
predicate.clone(),
predicate_data.clone(),
);
let b = Input::message_predicate(
rng.gen(),
rng.gen(),
rng.gen(),
rng.gen(),
rng.gen(),
vec![],
predicate.clone(),
predicate_data,
);
let inputs = vec![a, b];
for i in inputs {
let tx = TransactionBuilder::script(vec![], vec![])
.add_input(i)
.finalize_checked_basic(height, ¶ms);
let idx = 1;
let runtime = RuntimePredicate::from_tx(¶ms, tx.as_ref(), idx);
assert!(runtime.is_none());
let idx = 0;
let runtime =
RuntimePredicate::from_tx(¶ms, tx.as_ref(), idx).expect("failed to generate predicate from valid tx");
assert_eq!(idx, runtime.idx());
let mut interpreter = Interpreter::without_storage();
assert!(interpreter.init_predicate(tx));
let pad = bytes::padded_len(&predicate) - predicate.len();
assert_ne!(0, pad);
let padded_predicate: Vec<u8> = predicate.iter().copied().chain(iter::repeat(0u8).take(pad)).collect();
let program = runtime.program();
let program = &interpreter.memory()[program.start() as usize..program.end() as usize];
assert_eq!(program, &padded_predicate);
}
}