forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rs
More file actions
61 lines (53 loc) · 1.82 KB
/
Copy pathutils.rs
File metadata and controls
61 lines (53 loc) · 1.82 KB
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
use fe_analyzer::namespace::types::{AbiArraySize, AbiEncoding, AbiType, AbiUintSize};
/// Returns the offset at which each head is located in the static section
/// of an encoding and the total size of the static section.
pub fn abi_head_offsets<T: AbiEncoding>(types: &[T]) -> (Vec<usize>, usize) {
let mut offsets = vec![];
let mut curr_offset = 0;
for typ in types {
offsets.push(curr_offset);
curr_offset += match typ.abi_type() {
AbiType::Array {
size: AbiArraySize::Dynamic { .. },
..
} => 32,
AbiType::Array {
size: AbiArraySize::Static { size },
inner,
} => match *inner {
AbiType::Array { .. } => todo!(),
AbiType::Tuple { .. } => todo!(),
AbiType::Uint {
size: AbiUintSize { padded_size, .. },
} => ceil_32(padded_size * size),
},
AbiType::Tuple { elems } => elems.len() * 32,
AbiType::Uint {
size: AbiUintSize { padded_size, .. },
} => padded_size,
};
}
(offsets, curr_offset)
}
/// Rounds up to nearest multiple of 32.
pub fn ceil_32(n: usize) -> usize {
((n + 31) / 32) * 32
}
#[cfg(test)]
mod tests {
use crate::yul::utils::abi_head_offsets;
use fe_analyzer::namespace::types::{Array, Base, FeString, FixedSize, U256};
#[test]
fn test_head_offsets() {
let types = vec![
FixedSize::Array(Array {
inner: U256,
size: 42,
}),
FixedSize::Base(U256),
FixedSize::String(FeString { max_size: 26 }),
FixedSize::Base(Base::Address),
];
assert_eq!(abi_head_offsets(&types), (vec![0, 1344, 1376, 1408], 1440))
}
}