sui_cluster_test/test_case/
random_beacon_test.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{TestCaseImpl, TestContext};
5use async_trait::async_trait;
6use sui_json_rpc_types::{SuiExecutionStatus, SuiTransactionBlockEffectsAPI};
7use sui_sdk::wallet_context::WalletContext;
8use sui_test_transaction_builder::{emit_new_random_u128, publish_basics_package};
9use tracing::info;
10
11pub struct RandomBeaconTest;
12
13#[async_trait]
14impl TestCaseImpl for RandomBeaconTest {
15    fn name(&self) -> &'static str {
16        "RandomBeacon"
17    }
18
19    fn description(&self) -> &'static str {
20        "Test publishing basics packages and emitting an event that depends on a random value."
21    }
22
23    async fn run(&self, ctx: &mut TestContext) -> Result<(), anyhow::Error> {
24        let wallet_context: &WalletContext = ctx.get_wallet();
25        // Test only if the beacon is enabled.
26        if !Self::is_beacon_enabled(wallet_context).await {
27            info!("Random beacon is not enabled. Skipping test.");
28            return Ok(());
29        }
30
31        info!("Testing a transaction that uses Random.");
32
33        let sui_objs = ctx.get_sui_from_faucet(Some(1)).await;
34        assert!(!sui_objs.is_empty());
35
36        let package_ref = publish_basics_package(wallet_context).await;
37
38        let response = emit_new_random_u128(wallet_context, package_ref.0).await;
39        assert_eq!(
40            *response.effects.as_ref().unwrap().status(),
41            SuiExecutionStatus::Success,
42            "Generate new random value txn failed: {:?}",
43            *response.effects.as_ref().unwrap().status()
44        );
45
46        // Check that only the expected event was emitted.
47        let events = response.events.unwrap();
48        assert_eq!(
49            1,
50            events.data.len(),
51            "Expected 1 event, got {:?}",
52            events.data.len()
53        );
54        assert_eq!(
55            "RandomU128Event".to_string(),
56            events.data[0].type_.name.to_string()
57        );
58
59        // Verify fullnode observes the txn
60        ctx.let_fullnode_sync(vec![response.digest], 5).await;
61
62        Ok(())
63    }
64}
65
66impl RandomBeaconTest {
67    async fn is_beacon_enabled(wallet_context: &WalletContext) -> bool {
68        let client = wallet_context.get_client().await.unwrap();
69        let config = client.read_api().get_protocol_config(None).await.unwrap();
70        *config.feature_flags.get("random_beacon").unwrap()
71    }
72}