iota_cluster_test/test_case/
random_beacon_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::{emit_new_random_u128, publish_basics_package};
9use tracing::info;
10
11use crate::{TestCaseImpl, TestContext};
12
13pub struct RandomBeaconTest;
14
15#[async_trait]
16impl TestCaseImpl for RandomBeaconTest {
17    fn name(&self) -> &'static str {
18        "RandomBeacon"
19    }
20
21    fn description(&self) -> &'static str {
22        "Test publishing basics packages and emitting an event that depends on a random value."
23    }
24
25    async fn run(&self, ctx: &mut TestContext) -> Result<(), anyhow::Error> {
26        let wallet_context: &WalletContext = ctx.get_wallet();
27
28        info!("Testing a transaction that uses Random.");
29
30        let iota_objs = ctx.get_iota_from_faucet(Some(1)).await;
31        assert!(!iota_objs.is_empty());
32
33        let package_ref = publish_basics_package(wallet_context).await;
34
35        let response = emit_new_random_u128(wallet_context, package_ref.0).await;
36        assert_eq!(
37            *response.effects.as_ref().unwrap().status(),
38            IotaExecutionStatus::Success,
39            "Generate new random value txn failed: {:?}",
40            *response.effects.as_ref().unwrap().status()
41        );
42
43        // Check that only the expected event was emitted.
44        let events = response.events.unwrap();
45        assert_eq!(
46            1,
47            events.data.len(),
48            "Expected 1 event, got {:?}",
49            events.data.len()
50        );
51        assert_eq!(
52            "RandomU128Event".to_string(),
53            events.data[0].type_.name.to_string()
54        );
55
56        // Verify fullnode observes the txn
57        ctx.let_fullnode_sync(vec![response.digest], 5).await;
58
59        Ok(())
60    }
61}