iota_cluster_test/test_case/
shared_object_test.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use async_trait::async_trait;
6use iota_json_rpc_types::{IotaExecutionStatus, IotaTransactionBlockEffectsAPI};
7use iota_sdk::wallet_context::WalletContext;
8use iota_test_transaction_builder::{increment_counter, publish_basics_package_and_make_counter};
9use iota_types::object::Owner;
10use tracing::info;
11
12use crate::{TestCaseImpl, TestContext, helper::ObjectChecker};
13
14pub struct SharedCounterTest;
15
16#[async_trait]
17impl TestCaseImpl for SharedCounterTest {
18    fn name(&self) -> &'static str {
19        "SharedCounter"
20    }
21
22    fn description(&self) -> &'static str {
23        "Test publishing basics packages and incrementing Counter (shared object)"
24    }
25
26    async fn run(&self, ctx: &mut TestContext) -> Result<(), anyhow::Error> {
27        info!("Testing shared object transactions.");
28
29        let iota_objs = ctx.get_iota_from_faucet(Some(1)).await;
30        assert!(!iota_objs.is_empty());
31
32        let wallet_context: &WalletContext = ctx.get_wallet();
33        let address = ctx.get_wallet_address();
34        let (package_ref, (counter_id, initial_counter_version, _)) =
35            publish_basics_package_and_make_counter(wallet_context).await;
36        let response = increment_counter(
37            wallet_context,
38            address,
39            None,
40            package_ref.0,
41            counter_id,
42            initial_counter_version,
43        )
44        .await;
45        assert_eq!(
46            *response.effects.as_ref().unwrap().status(),
47            IotaExecutionStatus::Success,
48            "Increment counter txn failed: {:?}",
49            *response.effects.as_ref().unwrap().status()
50        );
51
52        response
53            .effects
54            .as_ref()
55            .unwrap()
56            .shared_objects()
57            .iter()
58            .find(|o| o.object_id == counter_id)
59            .unwrap_or_else(|| panic!("Expect obj {counter_id} in shared_objects"));
60
61        let counter_version = response
62            .effects
63            .as_ref()
64            .unwrap()
65            .mutated()
66            .iter()
67            .find_map(|obj| {
68                let Owner::Shared {
69                    initial_shared_version,
70                } = obj.owner
71                else {
72                    return None;
73                };
74
75                if obj.reference.object_id == counter_id
76                    && initial_shared_version == initial_counter_version
77                {
78                    Some(obj.reference.version)
79                } else {
80                    None
81                }
82            })
83            .unwrap_or_else(|| panic!("Expect obj {counter_id} in mutated"));
84
85        // Verify fullnode observes the txn
86        ctx.let_fullnode_sync(vec![response.digest], 5).await;
87
88        let counter_object = ObjectChecker::new(counter_id)
89            .owner(Owner::Shared {
90                initial_shared_version: initial_counter_version,
91            })
92            .check_into_object(ctx.get_fullnode_client())
93            .await;
94
95        assert_eq!(
96            counter_object.version, counter_version,
97            "Expect sequence number to be 2"
98        );
99
100        Ok(())
101    }
102}