-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontract.rs
395 lines (361 loc) · 14.7 KB
/
contract.rs
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
use super::{
slot_info::{LargeStruct, MappingKey, MappingOfMappingsKey, StorageSlotValue},
table_source::DEFAULT_ADDRESS,
};
use crate::common::{
bindings::simple::{
Simple,
Simple::{
MappingChange, MappingOfSingleValueMappingsChange, MappingOfStructMappingsChange,
MappingOperation, MappingStructChange,
},
},
TestContext,
};
use alloy::{
contract::private::Provider,
primitives::{Address, U256},
providers::ProviderBuilder,
};
use itertools::Itertools;
use log::{debug, info};
pub struct Contract {
pub address: Address,
pub chain_id: u64,
}
impl Contract {
/// Deploy the simple contract.
pub(crate) async fn deploy_simple_contract(ctx: &TestContext) -> Self {
// Create a provider with the wallet for contract deployment and interaction.
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(ctx.wallet())
.on_http(ctx.rpc_url.parse().unwrap());
let contract = Simple::deploy(&provider).await.unwrap();
let address = *contract.address();
info!("Deployed Simple contract at address: {address}");
let chain_id = ctx.rpc.get_chain_id().await.unwrap();
Self { address, chain_id }
}
}
/// Common functions for a specific type to interact with the test contract
pub trait ContractController {
/// Get the current values from the contract.
async fn current_values(ctx: &TestContext, contract: &Contract) -> Self;
/// Update the values to the contract.
async fn update_contract(&self, ctx: &TestContext, contract: &Contract);
}
/// Single values collection
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SimpleSingleValues {
pub(crate) s1: bool,
pub(crate) s2: U256,
pub(crate) s3: String,
pub(crate) s4: Address,
}
impl ContractController for SimpleSingleValues {
async fn current_values(ctx: &TestContext, contract: &Contract) -> Self {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(ctx.wallet())
.on_http(ctx.rpc_url.parse().unwrap());
let contract = Simple::new(contract.address, &provider);
SimpleSingleValues {
s1: contract.s1().call().await.unwrap()._0,
s2: contract.s2().call().await.unwrap()._0,
s3: contract.s3().call().await.unwrap()._0,
s4: contract.s4().call().await.unwrap()._0,
}
}
async fn update_contract(&self, ctx: &TestContext, contract: &Contract) {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(ctx.wallet())
.on_http(ctx.rpc_url.parse().unwrap());
let simple_contract = Simple::new(contract.address, &provider);
let call = simple_contract.setSimples(self.s1, self.s2, self.s3.clone(), self.s4);
call.send().await.unwrap().watch().await.unwrap();
log::info!("Updated simple contract single values");
// Sanity check
{
let updated = Self::current_values(ctx, contract).await;
assert_eq!(self, &updated);
}
}
}
impl ContractController for LargeStruct {
async fn current_values(ctx: &TestContext, contract: &Contract) -> Self {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(ctx.wallet())
.on_http(ctx.rpc_url.parse().unwrap());
let contract = Simple::new(contract.address, &provider);
contract.simpleStruct().call().await.unwrap().into()
}
async fn update_contract(&self, ctx: &TestContext, contract: &Contract) {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(ctx.wallet())
.on_http(ctx.rpc_url.parse().unwrap());
let simple_contract = Simple::new(contract.address, &provider);
let call = simple_contract.setSimpleStruct(self.field1, self.field2, self.field3);
call.send().await.unwrap().watch().await.unwrap();
// Sanity check
{
let updated = Self::current_values(ctx, contract).await;
assert_eq!(self, &updated);
}
log::info!("Updated simple contract for LargeStruct");
}
}
#[derive(Clone, Debug)]
pub enum MappingUpdate<K, V> {
// key and value
Insertion(K, V),
// key and value
Deletion(K, V),
// key, previous value and new value
Update(K, V, V),
}
impl<K, V> From<&MappingUpdate<K, V>> for MappingOperation {
fn from(update: &MappingUpdate<K, V>) -> Self {
Self::from(match update {
MappingUpdate::Deletion(_, _) => 0,
MappingUpdate::Update(_, _, _) => 1,
MappingUpdate::Insertion(_, _) => 2,
})
}
}
impl ContractController for Vec<MappingUpdate<MappingKey, Address>> {
async fn current_values(_ctx: &TestContext, _contract: &Contract) -> Self {
unimplemented!("Unimplemented for fetching the all mapping values")
}
async fn update_contract(&self, ctx: &TestContext, contract: &Contract) {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(ctx.wallet())
.on_http(ctx.rpc_url.parse().unwrap());
let contract = Simple::new(contract.address, &provider);
let changes = self
.iter()
.map(|tuple| {
let operation: MappingOperation = tuple.into();
let operation = operation.into();
let (key, value) = match tuple {
MappingUpdate::Deletion(k, _) => (*k, *DEFAULT_ADDRESS),
MappingUpdate::Update(k, _, v) | MappingUpdate::Insertion(k, v) => (*k, *v),
};
MappingChange {
operation,
key,
value,
}
})
.collect_vec();
let call = contract.changeMapping(changes);
call.send().await.unwrap().watch().await.unwrap();
// Sanity check
for update in self.iter() {
match update {
MappingUpdate::Deletion(k, _) => {
let res = contract.m1(*k).call().await.unwrap();
let v: U256 = res._0.into_word().into();
assert_eq!(v, U256::ZERO, "Key deletion is wrong on contract");
}
MappingUpdate::Insertion(k, v) => {
let res = contract.m1(*k).call().await.unwrap();
let new_value: U256 = res._0.into_word().into();
let new_value = Address::from_u256_slice(&[new_value]);
assert_eq!(&new_value, v, "Key insertion is wrong on contract");
}
MappingUpdate::Update(k, _, v) => {
let res = contract.m1(*k).call().await.unwrap();
let new_value: U256 = res._0.into_word().into();
let new_value = Address::from_u256_slice(&[new_value]);
assert_eq!(&new_value, v, "Key update is wrong on contract");
}
}
}
log::info!("Updated simple contract single values");
}
}
impl ContractController for Vec<MappingUpdate<MappingKey, LargeStruct>> {
async fn current_values(_ctx: &TestContext, _contract: &Contract) -> Self {
unimplemented!("Unimplemented for fetching the all mapping values")
}
async fn update_contract(&self, ctx: &TestContext, contract: &Contract) {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(ctx.wallet())
.on_http(ctx.rpc_url.parse().unwrap());
let contract = Simple::new(contract.address, &provider);
let changes = self
.iter()
.map(|tuple| {
let operation: MappingOperation = tuple.into();
let operation = operation.into();
let (key, field1, field2, field3) = match tuple {
MappingUpdate::Insertion(k, v)
| MappingUpdate::Deletion(k, v)
| MappingUpdate::Update(k, _, v) => (*k, v.field1, v.field2, v.field3),
};
MappingStructChange {
operation,
key,
field1,
field2,
field3,
}
})
.collect_vec();
let call = contract.changeMappingStruct(changes);
call.send().await.unwrap().watch().await.unwrap();
// Sanity check
for update in self.iter() {
match update {
MappingUpdate::Deletion(k, _) => {
let res = contract.structMapping(*k).call().await.unwrap();
assert_eq!(
LargeStruct::from(res),
LargeStruct::new(U256::from(0), 0, 0)
);
}
MappingUpdate::Insertion(k, v) | MappingUpdate::Update(k, _, v) => {
let res = contract.structMapping(*k).call().await.unwrap();
debug!("Set mapping struct: key = {k}, value = {v:?}");
assert_eq!(&LargeStruct::from(res), v);
}
}
}
log::info!("Updated simple contract for mapping values of LargeStruct");
}
}
impl ContractController for Vec<MappingUpdate<MappingOfMappingsKey, U256>> {
async fn current_values(_ctx: &TestContext, _contract: &Contract) -> Self {
unimplemented!("Unimplemented for fetching the all mapping of mappings")
}
async fn update_contract(&self, ctx: &TestContext, contract: &Contract) {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(ctx.wallet())
.on_http(ctx.rpc_url.parse().unwrap());
let contract = Simple::new(contract.address, &provider);
let changes = self
.iter()
.map(|tuple| {
let operation: MappingOperation = tuple.into();
let operation = operation.into();
let (k, v) = match tuple {
MappingUpdate::Insertion(k, v)
| MappingUpdate::Deletion(k, v)
| MappingUpdate::Update(k, _, v) => (k, v),
};
MappingOfSingleValueMappingsChange {
operation,
outerKey: k.outer_key,
innerKey: k.inner_key,
value: *v,
}
})
.collect_vec();
let call = contract.changeMappingOfSingleValueMappings(changes);
call.send().await.unwrap().watch().await.unwrap();
// Sanity check
for update in self.iter() {
match update {
MappingUpdate::Insertion(k, v) => {
let res = contract
.mappingOfSingleValueMappings(k.outer_key, k.inner_key)
.call()
.await
.unwrap();
assert_eq!(&res._0, v, "Insertion is wrong on contract");
}
MappingUpdate::Deletion(k, _) => {
let res = contract
.mappingOfSingleValueMappings(k.outer_key, k.inner_key)
.call()
.await
.unwrap();
assert_eq!(res._0, U256::ZERO, "Deletion is wrong on contract");
}
MappingUpdate::Update(k, _, v) => {
let res = contract
.mappingOfSingleValueMappings(k.outer_key, k.inner_key)
.call()
.await
.unwrap();
assert_eq!(&res._0, v, "Update is wrong on contract");
}
}
}
log::info!("Updated simple contract for mapping of single value mappings");
}
}
impl ContractController for Vec<MappingUpdate<MappingOfMappingsKey, LargeStruct>> {
async fn current_values(_ctx: &TestContext, _contract: &Contract) -> Self {
unimplemented!("Unimplemented for fetching the all mapping of mappings")
}
async fn update_contract(&self, ctx: &TestContext, contract: &Contract) {
let provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(ctx.wallet())
.on_http(ctx.rpc_url.parse().unwrap());
let contract = Simple::new(contract.address, &provider);
let changes = self
.iter()
.map(|tuple| {
let operation: MappingOperation = tuple.into();
let operation = operation.into();
let (k, v) = match tuple {
MappingUpdate::Insertion(k, v)
| MappingUpdate::Deletion(k, v)
| MappingUpdate::Update(k, _, v) => (k, v),
};
MappingOfStructMappingsChange {
operation,
outerKey: k.outer_key,
innerKey: k.inner_key,
field1: v.field1,
field2: v.field2,
field3: v.field3,
}
})
.collect_vec();
let call = contract.changeMappingOfStructMappings(changes);
call.send().await.unwrap().watch().await.unwrap();
// Sanity check
for update in self.iter() {
match update {
MappingUpdate::Insertion(k, v) => {
let res = contract
.mappingOfStructMappings(k.outer_key, k.inner_key)
.call()
.await
.unwrap();
let res = LargeStruct::from(res);
assert_eq!(&res, v, "Insertion is wrong on contract");
}
MappingUpdate::Deletion(k, _) => {
let res = contract
.mappingOfStructMappings(k.outer_key, k.inner_key)
.call()
.await
.unwrap();
let res = LargeStruct::from(res);
assert_eq!(res, LargeStruct::default(), "Deletion is wrong on contract");
}
MappingUpdate::Update(k, _, v) => {
let res = contract
.mappingOfStructMappings(k.outer_key, k.inner_key)
.call()
.await
.unwrap();
let res = LargeStruct::from(res);
assert_eq!(&res, v, "Update is wrong on contract");
}
}
}
log::info!("Updated simple contract for mapping of LargeStruct mappings");
}
}