iota_cluster_test/
helper.rs1use anyhow::bail;
6use iota_json_rpc_types::{BalanceChange, IotaData, IotaObjectData, IotaObjectDataOptions};
7use iota_sdk::IotaClient;
8use iota_types::{
9 base_types::ObjectID, error::IotaObjectResponseError, gas_coin::GasCoin, object::Owner,
10 parse_iota_type_tag,
11};
12use move_core_types::language_storage::TypeTag;
13use tracing::{debug, trace};
14
15#[derive(Debug)]
23pub struct ObjectChecker {
24 object_id: ObjectID,
25 owner: Option<Owner>,
26 is_deleted: bool,
27 is_iota_coin: Option<bool>,
28}
29
30impl ObjectChecker {
31 pub fn new(object_id: ObjectID) -> ObjectChecker {
32 Self {
33 object_id,
34 owner: None,
35 is_deleted: false, is_iota_coin: None,
37 }
38 }
39
40 pub fn owner(mut self, owner: Owner) -> Self {
41 self.owner = Some(owner);
42 self
43 }
44
45 pub fn deleted(mut self) -> Self {
46 self.is_deleted = true;
47 self
48 }
49
50 pub fn is_iota_coin(mut self, is_iota_coin: bool) -> Self {
51 self.is_iota_coin = Some(is_iota_coin);
52 self
53 }
54
55 pub async fn check_into_gas_coin(self, client: &IotaClient) -> GasCoin {
56 if self.is_iota_coin == Some(false) {
57 panic!("'check_into_gas_coin' shouldn't be called with 'is_iota_coin' set as false");
58 }
59 self.is_iota_coin(true)
60 .check(client)
61 .await
62 .unwrap()
63 .into_gas_coin()
64 }
65
66 pub async fn check_into_object(self, client: &IotaClient) -> IotaObjectData {
67 self.check(client).await.unwrap().into_object()
68 }
69
70 pub async fn check(self, client: &IotaClient) -> Result<CheckerResultObject, anyhow::Error> {
71 debug!(?self);
72
73 let object_id = self.object_id;
74 let object_info = client
75 .read_api()
76 .get_object_with_options(
77 object_id,
78 IotaObjectDataOptions::new()
79 .with_type()
80 .with_owner()
81 .with_bcs(),
82 )
83 .await
84 .or_else(|err| bail!("Failed to get object info (id: {}), err: {err}", object_id))?;
85
86 trace!("getting object {object_id}, info :: {object_info:?}");
87
88 match (object_info.data, object_info.error) {
89 (None, Some(IotaObjectResponseError::NotExists { object_id })) => {
90 panic!(
91 "Node can't find gas object {} with client {:?}",
92 object_id,
93 client.read_api()
94 )
95 }
96 (
97 None,
98 Some(IotaObjectResponseError::DynamicFieldNotFound {
99 parent_object_id: object_id,
100 }),
101 ) => {
102 panic!(
103 "Node can't find dynamic field for {} with client {:?}",
104 object_id,
105 client.read_api()
106 )
107 }
108 (
109 None,
110 Some(IotaObjectResponseError::Deleted {
111 object_id,
112 version: _,
113 digest: _,
114 }),
115 ) => {
116 if !self.is_deleted {
117 panic!("Gas object {object_id} was deleted");
118 }
119 Ok(CheckerResultObject::new(None, None))
120 }
121 (Some(object), _) => {
122 if self.is_deleted {
123 panic!("Expect Gas object {object_id} deleted, but it is not");
124 }
125 if let Some(owner) = self.owner {
126 let object_owner = object
127 .owner
128 .unwrap_or_else(|| panic!("Object {object_id} does not have owner"));
129 assert_eq!(
130 object_owner, owner,
131 "Gas coin {object_id} does not belong to {owner}, but {object_owner}"
132 );
133 }
134 if self.is_iota_coin == Some(true) {
135 let move_obj = object
136 .bcs
137 .as_ref()
138 .unwrap_or_else(|| panic!("Object {object_id} does not have bcs data"))
139 .try_as_move()
140 .unwrap_or_else(|| panic!("Object {object_id} is not a move object"));
141
142 let gas_coin = move_obj.deserialize()?;
143 return Ok(CheckerResultObject::new(Some(gas_coin), Some(object)));
144 }
145 Ok(CheckerResultObject::new(None, Some(object)))
146 }
147 (None, Some(IotaObjectResponseError::Display { error })) => {
148 panic!("Display Error: {error:?}");
149 }
150 (None, None) | (None, Some(IotaObjectResponseError::Unknown)) => {
151 panic!("Unexpected response: object not found and no specific error provided");
152 }
153 }
154 }
155}
156
157pub struct CheckerResultObject {
158 gas_coin: Option<GasCoin>,
159 object: Option<IotaObjectData>,
160}
161
162impl CheckerResultObject {
163 pub fn new(gas_coin: Option<GasCoin>, object: Option<IotaObjectData>) -> Self {
164 Self { gas_coin, object }
165 }
166 pub fn into_gas_coin(self) -> GasCoin {
167 self.gas_coin.unwrap()
168 }
169 pub fn into_object(self) -> IotaObjectData {
170 self.object.unwrap()
171 }
172}
173
174#[macro_export]
175macro_rules! assert_eq_if_present {
176 ($left:expr, $right:expr, $($arg:tt)+) => {
177 match (&$left, &$right) {
178 (Some(left_val), right_val) => {
179 if !(&left_val == right_val) {
180 panic!("{} does not match, left: {:?}, right: {:?}", $($arg)+, left_val, right_val);
181 }
182 }
183 _ => ()
184 }
185 };
186}
187
188#[derive(Default, Debug)]
189pub struct BalanceChangeChecker {
190 owner: Option<Owner>,
191 coin_type: Option<TypeTag>,
192 amount: Option<i128>,
193}
194
195impl BalanceChangeChecker {
196 pub fn new() -> Self {
197 Default::default()
198 }
199
200 pub fn owner(mut self, owner: Owner) -> Self {
201 self.owner = Some(owner);
202 self
203 }
204 pub fn coin_type(mut self, coin_type: &str) -> Self {
205 self.coin_type = Some(parse_iota_type_tag(coin_type).unwrap());
206 self
207 }
208
209 pub fn amount(mut self, amount: i128) -> Self {
210 self.amount = Some(amount);
211 self
212 }
213
214 pub fn check(self, event: &BalanceChange) {
215 let BalanceChange {
216 owner,
217 coin_type,
218 amount,
219 } = event;
220
221 assert_eq_if_present!(self.owner, owner, "owner");
222 assert_eq_if_present!(self.coin_type, coin_type, "coin_type");
223 assert_eq_if_present!(self.amount, amount, "version");
224 }
225}