-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmod.rs
More file actions
231 lines (207 loc) · 6.92 KB
/
mod.rs
File metadata and controls
231 lines (207 loc) · 6.92 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use anyhow::anyhow;
use std::collections::HashMap;
use std::hash::Hash;
use std::iter::FromIterator;
use graph::{
data::value::Word,
runtime::{
asc_get, asc_new, gas::GasCounter, AscHeap, AscIndexId, AscPtr, AscType, AscValue,
DeterministicHostError, FromAscObj, HostExportError, ToAscObj,
},
};
use crate::asc_abi::class::*;
///! Implementations of `ToAscObj` and `FromAscObj` for Rust types.
///! Standard Rust types go in `mod.rs` and external types in `external.rs`.
mod external;
impl<T: AscValue> ToAscObj<TypedArray<T>> for [T] {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<TypedArray<T>, HostExportError> {
TypedArray::new(self, heap, gas)
}
}
impl<T: AscValue> FromAscObj<TypedArray<T>> for Vec<T> {
fn from_asc_obj<H: AscHeap + ?Sized>(
typed_array: TypedArray<T>,
heap: &H,
gas: &GasCounter,
_depth: usize,
) -> Result<Self, DeterministicHostError> {
typed_array.to_vec(heap, gas)
}
}
impl<T: AscValue + Send + Sync, const LEN: usize> FromAscObj<TypedArray<T>> for [T; LEN] {
fn from_asc_obj<H: AscHeap + ?Sized>(
typed_array: TypedArray<T>,
heap: &H,
gas: &GasCounter,
_depth: usize,
) -> Result<Self, DeterministicHostError> {
let v = typed_array.to_vec(heap, gas)?;
let array = <[T; LEN]>::try_from(v)
.map_err(|v| anyhow!("expected array of length {}, found length {}", LEN, v.len()))?;
Ok(array)
}
}
impl ToAscObj<AscString> for str {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
heap: &mut H,
_gas: &GasCounter,
) -> Result<AscString, HostExportError> {
Ok(AscString::new(
&self.encode_utf16().collect::<Vec<_>>(),
heap.api_version(),
)?)
}
}
impl ToAscObj<AscString> for &str {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
heap: &mut H,
_gas: &GasCounter,
) -> Result<AscString, HostExportError> {
Ok(AscString::new(
&self.encode_utf16().collect::<Vec<_>>(),
heap.api_version(),
)?)
}
}
impl ToAscObj<AscString> for String {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscString, HostExportError> {
self.as_str().to_asc_obj(heap, gas)
}
}
impl ToAscObj<AscString> for Word {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscString, HostExportError> {
self.as_str().to_asc_obj(heap, gas)
}
}
impl FromAscObj<AscString> for String {
fn from_asc_obj<H: AscHeap + ?Sized>(
asc_string: AscString,
_: &H,
_gas: &GasCounter,
_depth: usize,
) -> Result<Self, DeterministicHostError> {
let utf16_content = asc_string.content();
let mut string = String::from_utf16(utf16_content)
.map_err(|e| DeterministicHostError::from(anyhow::Error::from(e)))?;
// Strip null characters since they are not accepted by Postgres.
// But be more careful with address-like strings to avoid corruption
if string.contains('\u{0000}') {
// For address-like strings, be more conservative about null removal
if string.starts_with("0x") && string.len() >= 42 {
// This looks like an Ethereum address - check if nulls are just padding
let without_nulls = string.replace('\u{0000}', "");
// If removing nulls significantly shortens the string, something is wrong
if without_nulls.len() < 10 || (string.len() - without_nulls.len()) > 10 {
return Err(DeterministicHostError::from(anyhow::anyhow!(
"String contains suspicious null characters that would corrupt address: original length {}, after removal: {}",
string.len(), without_nulls.len()
)));
}
string = without_nulls;
} else {
// For non-address strings, proceed with normal null removal
string = string.replace('\u{0000}', "");
}
}
Ok(string)
}
}
impl FromAscObj<AscString> for Word {
fn from_asc_obj<H: AscHeap + ?Sized>(
asc_string: AscString,
heap: &H,
gas: &GasCounter,
depth: usize,
) -> Result<Self, DeterministicHostError> {
let string = String::from_asc_obj(asc_string, heap, gas, depth)?;
Ok(Word::from(string))
}
}
impl<C: AscType + AscIndexId, T: ToAscObj<C>> ToAscObj<Array<AscPtr<C>>> for [T] {
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<Array<AscPtr<C>>, HostExportError> {
let content: Result<Vec<_>, _> = self.iter().map(|x| asc_new(heap, x, gas)).collect();
let content = content?;
Array::new(&content, heap, gas)
}
}
impl<C: AscType + AscIndexId, T: FromAscObj<C>> FromAscObj<Array<AscPtr<C>>> for Vec<T> {
fn from_asc_obj<H: AscHeap + ?Sized>(
array: Array<AscPtr<C>>,
heap: &H,
gas: &GasCounter,
depth: usize,
) -> Result<Self, DeterministicHostError> {
array
.to_vec(heap, gas)?
.into_iter()
.map(|x| asc_get(heap, x, gas, depth))
.collect()
}
}
impl<K: AscType + AscIndexId, V: AscType + AscIndexId, T: FromAscObj<K>, U: FromAscObj<V>>
FromAscObj<AscTypedMapEntry<K, V>> for (T, U)
{
fn from_asc_obj<H: AscHeap + ?Sized>(
asc_entry: AscTypedMapEntry<K, V>,
heap: &H,
gas: &GasCounter,
depth: usize,
) -> Result<Self, DeterministicHostError> {
Ok((
asc_get(heap, asc_entry.key, gas, depth)?,
asc_get(heap, asc_entry.value, gas, depth)?,
))
}
}
impl<K: AscType + AscIndexId, V: AscType + AscIndexId, T: ToAscObj<K>, U: ToAscObj<V>>
ToAscObj<AscTypedMapEntry<K, V>> for (T, U)
{
fn to_asc_obj<H: AscHeap + ?Sized>(
&self,
heap: &mut H,
gas: &GasCounter,
) -> Result<AscTypedMapEntry<K, V>, HostExportError> {
Ok(AscTypedMapEntry {
key: asc_new(heap, &self.0, gas)?,
value: asc_new(heap, &self.1, gas)?,
})
}
}
impl<
K: AscType + AscIndexId,
V: AscType + AscIndexId,
T: FromAscObj<K> + Hash + Eq,
U: FromAscObj<V>,
> FromAscObj<AscTypedMap<K, V>> for HashMap<T, U>
where
Array<AscPtr<AscTypedMapEntry<K, V>>>: AscIndexId,
AscTypedMapEntry<K, V>: AscIndexId,
{
fn from_asc_obj<H: AscHeap + ?Sized>(
asc_map: AscTypedMap<K, V>,
heap: &H,
gas: &GasCounter,
depth: usize,
) -> Result<Self, DeterministicHostError> {
let entries: Vec<(T, U)> = asc_get(heap, asc_map.entries, gas, depth)?;
Ok(HashMap::from_iter(entries.into_iter()))
}
}