Skip to main content

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_sdk_types::Owner;
9use iota_test_transaction_builder::{increment_counter, publish_basics_package_and_make_counter};
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_ref) =
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.object_id,
41            counter_ref.object_id,
42            counter_ref.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_ref.object_id)
59            .unwrap_or_else(|| panic!("expect obj {} in shared_objects", counter_ref.object_id));
60
61        let counter_version = response
62            .effects
63            .as_ref()
64            .unwrap()
65            .mutated()
66            .iter()
67            .find_map(|obj| {
68                let initial_shared_version = obj.owner.as_opt_shared()?;
69                (obj.reference.object_id == counter_ref.object_id
70                    && *initial_shared_version == counter_ref.version)
71                    .then_some(obj.reference.version)
72            })
73            .unwrap_or_else(|| panic!("expect obj {} in mutated", counter_ref.object_id));
74
75        // Verify fullnode observes the txn
76        ctx.let_fullnode_sync(vec![response.digest], 5).await;
77
78        let counter_object = ObjectChecker::new(counter_ref.object_id)
79            .owner(Owner::Shared(counter_ref.version))
80            .check_into_object(ctx.get_fullnode_client())
81            .await;
82
83        assert_eq!(
84            counter_object.version, counter_version,
85            "expect sequence number to be 2"
86        );
87
88        Ok(())
89    }
90}