Skip to main content

sui_surfer/
surfer_state.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use indexmap::IndexSet;
5use move_binary_format::file_format::Visibility;
6use move_binary_format::normalized;
7use move_core_types::identifier::IdentStr;
8use move_core_types::language_storage::StructTag;
9use mysten_common::fatal;
10use rand::rngs::StdRng;
11use std::collections::{HashMap, HashSet};
12use std::path::Path;
13use std::sync::Arc;
14use std::time::{Duration, Instant};
15use sui_move_build::BuildConfig;
16use sui_protocol_config::{Chain, ProtocolConfig};
17use sui_types::base_types::{ConsensusObjectSequenceKey, ObjectID, ObjectRef, SuiAddress};
18use sui_types::effects::{TransactionEffects, TransactionEffectsAPI};
19use sui_types::object::{Object, Owner};
20use sui_types::storage::WriteKind;
21use sui_types::transaction::{
22    CallArg, ObjectArg, TEST_ONLY_GAS_UNIT_FOR_GENERIC, TEST_ONLY_GAS_UNIT_FOR_PUBLISH,
23    TransactionData,
24};
25use sui_types::{Identifier, SUI_FRAMEWORK_ADDRESS};
26use test_cluster::TestCluster;
27use tokio::sync::RwLock;
28use tracing::{debug, error, info};
29
30type Type = normalized::Type<normalized::ArcIdentifier>;
31
32#[derive(Debug, Clone)]
33pub struct EntryFunction {
34    pub package: ObjectID,
35    pub module: String,
36    pub function: String,
37    pub parameters: Vec<Type>,
38}
39
40#[derive(Debug, Default)]
41pub struct SurfStatistics {
42    pub num_successful_transactions: u64,
43    pub num_failed_transactions: u64,
44    pub num_owned_obj_transactions: u64,
45    pub num_shared_obj_transactions: u64,
46    pub unique_move_functions_called: HashSet<(ObjectID, String, String)>,
47}
48
49impl SurfStatistics {
50    pub fn record_transaction(
51        &mut self,
52        has_shared_object: bool,
53        tx_succeeded: bool,
54        package: ObjectID,
55        module: String,
56        function: String,
57    ) {
58        if tx_succeeded {
59            self.num_successful_transactions += 1;
60        } else {
61            self.num_failed_transactions += 1;
62        }
63        if has_shared_object {
64            self.num_shared_obj_transactions += 1;
65        } else {
66            self.num_owned_obj_transactions += 1;
67        }
68        self.unique_move_functions_called
69            .insert((package, module, function));
70    }
71
72    pub fn aggregate(stats: Vec<Self>) -> Self {
73        let mut result = Self::default();
74        for stat in stats {
75            result.num_successful_transactions += stat.num_successful_transactions;
76            result.num_failed_transactions += stat.num_failed_transactions;
77            result.num_owned_obj_transactions += stat.num_owned_obj_transactions;
78            result.num_shared_obj_transactions += stat.num_shared_obj_transactions;
79            result
80                .unique_move_functions_called
81                .extend(stat.unique_move_functions_called);
82        }
83        result
84    }
85
86    pub fn print_stats(&self) {
87        info!(
88            "Executed {} transactions, {} succeeded, {} failed",
89            self.num_successful_transactions + self.num_failed_transactions,
90            self.num_successful_transactions,
91            self.num_failed_transactions
92        );
93        info!(
94            "{} are owned object transactions, {} are shared object transactions",
95            self.num_owned_obj_transactions, self.num_shared_obj_transactions
96        );
97        info!(
98            "Unique move functions called: {}",
99            self.unique_move_functions_called.len()
100        );
101    }
102}
103
104pub type OwnedObjects = HashMap<StructTag, IndexSet<ObjectRef>>;
105
106pub type ImmObjects = Arc<RwLock<HashMap<StructTag, Vec<ObjectRef>>>>;
107
108/// Map from StructTag to a vector of shared objects, where each shared object is a tuple of
109/// (object ID, initial shared version).
110pub type SharedObjects = Arc<RwLock<HashMap<StructTag, Vec<ConsensusObjectSequenceKey>>>>;
111
112pub struct SurferState {
113    pub pool: Arc<RwLock<normalized::ArcPool>>,
114    pub id: usize,
115    pub cluster: Arc<TestCluster>,
116    pub rng: StdRng,
117
118    pub address: SuiAddress,
119    pub gas_object: ObjectRef,
120    pub owned_objects: OwnedObjects,
121    pub immutable_objects: ImmObjects,
122    pub shared_objects: SharedObjects,
123    pub entry_functions: Arc<RwLock<Vec<EntryFunction>>>,
124
125    pub stats: SurfStatistics,
126}
127
128impl SurferState {
129    pub fn new(
130        id: usize,
131        cluster: Arc<TestCluster>,
132        rng: StdRng,
133        address: SuiAddress,
134        gas_object: ObjectRef,
135        owned_objects: OwnedObjects,
136        immutable_objects: ImmObjects,
137        shared_objects: SharedObjects,
138        entry_functions: Arc<RwLock<Vec<EntryFunction>>>,
139    ) -> Self {
140        Self {
141            pool: Arc::new(RwLock::new(normalized::ArcPool::new())),
142            id,
143            cluster,
144            rng,
145            address,
146            gas_object,
147            owned_objects,
148            immutable_objects,
149            shared_objects,
150            entry_functions,
151            stats: Default::default(),
152        }
153    }
154
155    #[tracing::instrument(skip_all, fields(surfer_id = self.id))]
156    pub async fn execute_move_transaction(
157        &mut self,
158        package: ObjectID,
159        module: String,
160        function: String,
161        args: Vec<CallArg>,
162    ) {
163        let rgp = self.cluster.get_reference_gas_price().await;
164        let use_shared_object = args
165            .iter()
166            .any(|arg| matches!(arg, CallArg::Object(ObjectArg::SharedObject { .. })));
167        let tx_data = TransactionData::new_move_call(
168            self.address,
169            package,
170            Identifier::new(module.as_str()).unwrap(),
171            Identifier::new(function.as_str()).unwrap(),
172            vec![],
173            self.gas_object,
174            args,
175            TEST_ONLY_GAS_UNIT_FOR_GENERIC * rgp,
176            rgp,
177        )
178        .unwrap();
179        let tx = self.cluster.wallet.sign_transaction(&tx_data).await;
180        let response = loop {
181            debug!("Executing transaction {:?}", tx.digest());
182            match self
183                .cluster
184                .wallet
185                .execute_transaction_may_fail(tx.clone())
186                .await
187            {
188                Ok(effects) => break effects,
189                Err(e) => {
190                    error!("Error executing transaction {:?}: {e:?}", tx.digest());
191                    tokio::time::sleep(Duration::from_secs(1)).await;
192                }
193            }
194        };
195        debug!(
196            "Successfully executed transaction {:?} with response {:?}",
197            tx, response
198        );
199        let effects = response.effects;
200        info!(
201            "[{:?}] Calling Move function {:?}::{:?} returned {:?}",
202            self.address,
203            module,
204            function,
205            effects.status()
206        );
207        self.stats.record_transaction(
208            use_shared_object,
209            effects.status().is_ok(),
210            package,
211            module,
212            function,
213        );
214        self.process_tx_effects(&effects).await;
215    }
216
217    #[tracing::instrument(skip_all, fields(surfer_id = self.id))]
218    async fn process_tx_effects(&mut self, effects: &TransactionEffects) {
219        for (obj_ref, owner, write_kind) in effects.all_changed_objects() {
220            if matches!(owner, Owner::ObjectOwner(_)) {
221                // For object owned objects, we don't need to do anything.
222                // We also cannot read them because in the case of shared objects, there can be
223                // races and the child object may no longer exist.
224                continue;
225            }
226            // let obj_ref = owned_ref.reference.to_object_ref();
227            let object = self
228                .cluster
229                .get_object_from_fullnode_store(&obj_ref.0)
230                .await
231                .unwrap();
232            if object.is_package() {
233                self.discover_entry_functions(object).await;
234                continue;
235            }
236            let struct_tag = object.struct_tag().unwrap();
237            match owner {
238                Owner::Immutable => {
239                    self.immutable_objects
240                        .write()
241                        .await
242                        .entry(struct_tag)
243                        .or_default()
244                        .push(obj_ref);
245                }
246                Owner::AddressOwner(address) => {
247                    if address == self.address {
248                        self.owned_objects
249                            .entry(struct_tag)
250                            .or_default()
251                            .insert(obj_ref);
252                    }
253                }
254                Owner::ObjectOwner(_) => (),
255                // TODO(Party WIP) Implement full support for Party objects in sui-surfer.
256                Owner::Shared {
257                    initial_shared_version,
258                }
259                | Owner::ConsensusAddressOwner {
260                    start_version: initial_shared_version,
261                    ..
262                }
263                | Owner::Party {
264                    start_version: initial_shared_version,
265                    ..
266                } => {
267                    if write_kind != WriteKind::Mutate {
268                        self.shared_objects
269                            .write()
270                            .await
271                            .entry(struct_tag)
272                            .or_default()
273                            .push((obj_ref.0, initial_shared_version));
274                    }
275                    // We do not need to insert it if it's a Mutate, because it means
276                    // we should already have it in the inventory.
277                }
278            }
279            if obj_ref.0 == self.gas_object.0 {
280                self.gas_object = obj_ref;
281            }
282        }
283    }
284
285    async fn discover_entry_functions(&self, package: Object) {
286        let package_id = package.id();
287        let move_package = package.into_inner().data.try_into_package().unwrap();
288        let proto_version = self.cluster.highest_protocol_version();
289        let config = ProtocolConfig::get_for_version(proto_version, Chain::Unknown);
290        let binary_config = config.binary_config(None);
291        let pool: &mut normalized::ArcPool = &mut *self.pool.write().await;
292        let entry_functions: Vec<_> = move_package
293            .normalize(pool, &binary_config, /* include code */ false)
294            .unwrap()
295            .into_iter()
296            .flat_map(|(module_name, module)| {
297                module
298                    .functions
299                    .into_iter()
300                    .filter_map(|(func_name, func)| {
301                        // Either public function or entry function is callable.
302                        if !matches!(func.visibility, Visibility::Public) && !func.is_entry {
303                            return None;
304                        }
305                        // Surfer doesn't support chaining transactions in a programmable transaction yet.
306                        if !func.return_.is_empty() {
307                            return None;
308                        }
309                        // Surfer doesn't support type parameter yet.
310                        if !func.type_parameters.is_empty() {
311                            return None;
312                        }
313                        let mut parameters = (*func.parameters).clone();
314                        if let Some(last_param) = parameters.last().as_ref()
315                            && is_type_tx_context(last_param)
316                        {
317                            parameters.pop();
318                        }
319                        Some(EntryFunction {
320                            package: package_id,
321                            module: module_name.clone(),
322                            function: func_name.to_string(),
323                            parameters: parameters
324                                .into_iter()
325                                .map(|rc_ty| (*rc_ty).clone())
326                                .collect(),
327                        })
328                    })
329                    .collect::<Vec<_>>()
330            })
331            .collect();
332        info!(
333            "Number of entry functions discovered: {:?}",
334            entry_functions.len()
335        );
336        debug!("Entry functions: {:?}", entry_functions);
337        self.entry_functions.write().await.extend(entry_functions);
338    }
339
340    #[tracing::instrument(skip_all, fields(surfer_id = self.id))]
341    pub async fn publish_package(&mut self, path: &Path) {
342        let rgp = self.cluster.get_reference_gas_price().await;
343        let package = BuildConfig::new_for_testing()
344            .build_async(path)
345            .await
346            .unwrap();
347        let modules = package.get_package_bytes(false);
348        let tx_data = TransactionData::new_module(
349            self.address,
350            self.gas_object,
351            modules,
352            package
353                .dependency_ids
354                .published
355                .values()
356                .map(|dep| dep.published_at)
357                .collect(),
358            TEST_ONLY_GAS_UNIT_FOR_PUBLISH * rgp,
359            rgp,
360        );
361        let tx = self.cluster.wallet.sign_transaction(&tx_data).await;
362        let tx_digest = *tx.digest();
363        info!(?tx_digest, "Publishing package");
364        let start = Instant::now();
365        let response = loop {
366            match self
367                .cluster
368                .wallet
369                .execute_transaction_may_fail(tx.clone())
370                .await
371            {
372                Ok(response) => {
373                    break response;
374                }
375                Err(err) => {
376                    if start.elapsed() > Duration::from_secs(120) {
377                        fatal!(
378                            "Failed to publish package after 120 seconds: {} {}",
379                            err,
380                            tx.digest()
381                        );
382                    }
383                    error!(?tx_digest, "Failed to publish package: {}", err);
384                    tokio::time::sleep(Duration::from_secs(1)).await;
385                }
386            }
387        };
388        info!("Successfully published package in {:?}", path);
389        self.process_tx_effects(&response.effects).await;
390    }
391
392    pub fn matching_owned_objects_count(&self, type_tag: &StructTag) -> usize {
393        self.owned_objects
394            .get(type_tag)
395            .map(|objects| objects.len())
396            .unwrap_or(0)
397    }
398
399    pub async fn matching_immutable_objects_count(&self, type_tag: &StructTag) -> usize {
400        self.immutable_objects
401            .read()
402            .await
403            .get(type_tag)
404            .map(|objects| objects.len())
405            .unwrap_or(0)
406    }
407
408    pub async fn matching_shared_objects_count(&self, type_tag: &StructTag) -> usize {
409        self.shared_objects
410            .read()
411            .await
412            .get(type_tag)
413            .map(|objects| objects.len())
414            .unwrap_or(0)
415    }
416
417    pub fn choose_nth_owned_object(&mut self, type_tag: &StructTag, n: usize) -> ObjectRef {
418        self.owned_objects
419            .get_mut(type_tag)
420            .unwrap()
421            .swap_remove_index(n)
422            .unwrap()
423    }
424
425    pub async fn choose_nth_immutable_object(&self, type_tag: &StructTag, n: usize) -> ObjectRef {
426        self.immutable_objects.read().await.get(type_tag).unwrap()[n]
427    }
428
429    pub async fn choose_nth_shared_object(
430        &self,
431        type_tag: &StructTag,
432        n: usize,
433    ) -> ConsensusObjectSequenceKey {
434        self.shared_objects.read().await.get(type_tag).unwrap()[n]
435    }
436}
437
438fn is_type_tx_context(ty: &Type) -> bool {
439    match ty {
440        Type::Reference(_, inner) => match inner.as_ref() {
441            Type::Datatype(dt) => {
442                dt.module.address == SUI_FRAMEWORK_ADDRESS
443                    && dt.module.name.as_ident_str() == IdentStr::new("tx_context").unwrap()
444                    && dt.name.as_ident_str() == IdentStr::new("TxContext").unwrap()
445                    && dt.type_arguments.is_empty()
446            }
447            _ => false,
448        },
449        _ => false,
450    }
451}