1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::interpreter::MemoryRange;
use fuel_asm::Word;
use fuel_tx::{ConsensusParameters, Transaction};
use std::borrow::Borrow;
#[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: Borrow<Transaction>,
{
tx.borrow()
.input_predicate_offset(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_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![
Opcode::ADDI(0x10, 0x00, 0x01),
Opcode::ADDI(0x10, 0x10, 0x01),
Opcode::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(),
owner,
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_without_signature(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);
}
}