|
1 | 1 | use super::{
|
2 |
| - indexing::MappingUpdate, |
3 |
| - storage_slot_value::{LargeStruct, StorageSlotValue}, |
| 2 | + slot_info::{LargeStruct, MappingKey, MappingOfMappingsKey, StorageSlotValue}, |
4 | 3 | table_source::DEFAULT_ADDRESS,
|
5 | 4 | };
|
6 | 5 | use crate::common::{
|
7 | 6 | bindings::simple::{
|
8 | 7 | Simple,
|
9 |
| - Simple::{MappingChange, MappingOperation, MappingStructChange}, |
| 8 | + Simple::{ |
| 9 | + MappingChange, MappingOfSingleValueMappingsChange, MappingOfStructMappingsChange, |
| 10 | + MappingOperation, MappingStructChange, |
| 11 | + }, |
10 | 12 | },
|
11 | 13 | TestContext,
|
12 | 14 | };
|
@@ -121,7 +123,27 @@ impl ContractController for LargeStruct {
|
121 | 123 | }
|
122 | 124 | }
|
123 | 125 |
|
124 |
| -impl ContractController for Vec<MappingUpdate<Address>> { |
| 126 | +#[derive(Clone, Debug)] |
| 127 | +pub enum MappingUpdate<K, V> { |
| 128 | + // key and value |
| 129 | + Insertion(K, V), |
| 130 | + // key and value |
| 131 | + Deletion(K, V), |
| 132 | + // key, previous value and new value |
| 133 | + Update(K, V, V), |
| 134 | +} |
| 135 | + |
| 136 | +impl<K, V> From<&MappingUpdate<K, V>> for MappingOperation { |
| 137 | + fn from(update: &MappingUpdate<K, V>) -> Self { |
| 138 | + Self::from(match update { |
| 139 | + MappingUpdate::Deletion(_, _) => 0, |
| 140 | + MappingUpdate::Update(_, _, _) => 1, |
| 141 | + MappingUpdate::Insertion(_, _) => 2, |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl ContractController for Vec<MappingUpdate<MappingKey, Address>> { |
125 | 147 | async fn current_values(_ctx: &TestContext, _contract: &Contract) -> Self {
|
126 | 148 | unimplemented!("Unimplemented for fetching the all mapping values")
|
127 | 149 | }
|
@@ -178,7 +200,7 @@ impl ContractController for Vec<MappingUpdate<Address>> {
|
178 | 200 | }
|
179 | 201 | }
|
180 | 202 |
|
181 |
| -impl ContractController for Vec<MappingUpdate<LargeStruct>> { |
| 203 | +impl ContractController for Vec<MappingUpdate<MappingKey, LargeStruct>> { |
182 | 204 | async fn current_values(_ctx: &TestContext, _contract: &Contract) -> Self {
|
183 | 205 | unimplemented!("Unimplemented for fetching the all mapping values")
|
184 | 206 | }
|
@@ -233,3 +255,142 @@ impl ContractController for Vec<MappingUpdate<LargeStruct>> {
|
233 | 255 | log::info!("Updated simple contract for mapping values of LargeStruct");
|
234 | 256 | }
|
235 | 257 | }
|
| 258 | + |
| 259 | +impl ContractController for Vec<MappingUpdate<MappingOfMappingsKey, U256>> { |
| 260 | + async fn current_values(_ctx: &TestContext, _contract: &Contract) -> Self { |
| 261 | + unimplemented!("Unimplemented for fetching the all mapping of mappings") |
| 262 | + } |
| 263 | + |
| 264 | + async fn update_contract(&self, ctx: &TestContext, contract: &Contract) { |
| 265 | + let provider = ProviderBuilder::new() |
| 266 | + .with_recommended_fillers() |
| 267 | + .wallet(ctx.wallet()) |
| 268 | + .on_http(ctx.rpc_url.parse().unwrap()); |
| 269 | + let contract = Simple::new(contract.address, &provider); |
| 270 | + |
| 271 | + let changes = self |
| 272 | + .iter() |
| 273 | + .map(|tuple| { |
| 274 | + let operation: MappingOperation = tuple.into(); |
| 275 | + let operation = operation.into(); |
| 276 | + let (k, v) = match tuple { |
| 277 | + MappingUpdate::Insertion(k, v) |
| 278 | + | MappingUpdate::Deletion(k, v) |
| 279 | + | MappingUpdate::Update(k, _, v) => (k, v), |
| 280 | + }; |
| 281 | + |
| 282 | + MappingOfSingleValueMappingsChange { |
| 283 | + operation, |
| 284 | + outerKey: k.outer_key, |
| 285 | + innerKey: k.inner_key, |
| 286 | + value: v.clone(), |
| 287 | + } |
| 288 | + }) |
| 289 | + .collect_vec(); |
| 290 | + |
| 291 | + let call = contract.changeMappingOfSingleValueMappings(changes); |
| 292 | + call.send().await.unwrap().watch().await.unwrap(); |
| 293 | + // Sanity check |
| 294 | + for update in self.iter() { |
| 295 | + match update { |
| 296 | + MappingUpdate::Insertion(k, v) => { |
| 297 | + let res = contract |
| 298 | + .mappingOfSingleValueMappings(k.outer_key, k.inner_key) |
| 299 | + .call() |
| 300 | + .await |
| 301 | + .unwrap(); |
| 302 | + assert_eq!(&res._0, v, "Insertion is wrong on contract"); |
| 303 | + } |
| 304 | + MappingUpdate::Deletion(k, _) => { |
| 305 | + let res = contract |
| 306 | + .mappingOfSingleValueMappings(k.outer_key, k.inner_key) |
| 307 | + .call() |
| 308 | + .await |
| 309 | + .unwrap(); |
| 310 | + assert_eq!(res._0, U256::ZERO, "Deletion is wrong on contract"); |
| 311 | + } |
| 312 | + MappingUpdate::Update(k, _, v) => { |
| 313 | + let res = contract |
| 314 | + .mappingOfSingleValueMappings(k.outer_key, k.inner_key) |
| 315 | + .call() |
| 316 | + .await |
| 317 | + .unwrap(); |
| 318 | + assert_eq!(&res._0, v, "Update is wrong on contract"); |
| 319 | + } |
| 320 | + } |
| 321 | + } |
| 322 | + log::info!("Updated simple contract for mapping of single value mappings"); |
| 323 | + } |
| 324 | +} |
| 325 | + |
| 326 | +impl ContractController for Vec<MappingUpdate<MappingOfMappingsKey, LargeStruct>> { |
| 327 | + async fn current_values(_ctx: &TestContext, _contract: &Contract) -> Self { |
| 328 | + unimplemented!("Unimplemented for fetching the all mapping of mappings") |
| 329 | + } |
| 330 | + |
| 331 | + async fn update_contract(&self, ctx: &TestContext, contract: &Contract) { |
| 332 | + let provider = ProviderBuilder::new() |
| 333 | + .with_recommended_fillers() |
| 334 | + .wallet(ctx.wallet()) |
| 335 | + .on_http(ctx.rpc_url.parse().unwrap()); |
| 336 | + let contract = Simple::new(contract.address, &provider); |
| 337 | + |
| 338 | + let changes = self |
| 339 | + .iter() |
| 340 | + .map(|tuple| { |
| 341 | + let operation: MappingOperation = tuple.into(); |
| 342 | + let operation = operation.into(); |
| 343 | + let (k, v) = match tuple { |
| 344 | + MappingUpdate::Insertion(k, v) |
| 345 | + | MappingUpdate::Deletion(k, v) |
| 346 | + | MappingUpdate::Update(k, _, v) => (k, v), |
| 347 | + }; |
| 348 | + |
| 349 | + MappingOfStructMappingsChange { |
| 350 | + operation, |
| 351 | + outerKey: k.outer_key, |
| 352 | + innerKey: k.inner_key, |
| 353 | + field1: v.field1, |
| 354 | + field2: v.field2, |
| 355 | + field3: v.field3, |
| 356 | + } |
| 357 | + }) |
| 358 | + .collect_vec(); |
| 359 | + |
| 360 | + let call = contract.changeMappingOfStructMappings(changes); |
| 361 | + call.send().await.unwrap().watch().await.unwrap(); |
| 362 | + // Sanity check |
| 363 | + for update in self.iter() { |
| 364 | + match update { |
| 365 | + MappingUpdate::Insertion(k, v) => { |
| 366 | + let res = contract |
| 367 | + .mappingOfStructMappings(k.outer_key, k.inner_key) |
| 368 | + .call() |
| 369 | + .await |
| 370 | + .unwrap(); |
| 371 | + let res = LargeStruct::from(res); |
| 372 | + assert_eq!(&res, v, "Insertion is wrong on contract"); |
| 373 | + } |
| 374 | + MappingUpdate::Deletion(k, _) => { |
| 375 | + let res = contract |
| 376 | + .mappingOfStructMappings(k.outer_key, k.inner_key) |
| 377 | + .call() |
| 378 | + .await |
| 379 | + .unwrap(); |
| 380 | + let res = LargeStruct::from(res); |
| 381 | + assert_eq!(res, LargeStruct::default(), "Deletion is wrong on contract"); |
| 382 | + } |
| 383 | + MappingUpdate::Update(k, _, v) => { |
| 384 | + let res = contract |
| 385 | + .mappingOfStructMappings(k.outer_key, k.inner_key) |
| 386 | + .call() |
| 387 | + .await |
| 388 | + .unwrap(); |
| 389 | + let res = LargeStruct::from(res); |
| 390 | + assert_eq!(&res, v, "Update is wrong on contract"); |
| 391 | + } |
| 392 | + } |
| 393 | + } |
| 394 | + log::info!("Updated simple contract for mapping of LargeStruct mappings"); |
| 395 | + } |
| 396 | +} |
0 commit comments