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_sdk::wallet_context::WalletContext;
7use sui_test_transaction_builder::{emit_new_random_u128, publish_basics_package};
8use sui_types::effects::TransactionEffectsAPI;
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!(
40            response.effects.status().is_ok(),
41            "Generate new random value txn failed: {:?}",
42            *response.effects.status()
43        );
44
45        // Check that only the expected event was emitted.
46        let events = response.events.unwrap();
47        assert_eq!(
48            1,
49            events.data.len(),
50            "Expected 1 event, got {:?}",
51            events.data.len()
52        );
53        assert_eq!(
54            "RandomU128Event".to_string(),
55            events.data[0].type_.name.to_string()
56        );
57
58        // Verify fullnode observes the txn
59        ctx.let_fullnode_sync(vec![response.transaction.digest()], 5)
60            .await;
61
62        Ok(())
63    }
64}
65
66impl RandomBeaconTest {
67    async fn is_beacon_enabled(wallet_context: &WalletContext) -> bool {
68        let config = wallet_context
69            .grpc_client()
70            .unwrap()
71            .get_protocol_config(None)
72            .await
73            .unwrap();
74        *config.feature_flags().get("random_beacon").unwrap()
75    }
76}