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: {object_id}), err: {err}"))?;
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 {object_id} with client {:?}",
92 client.read_api()
93 )
94 }
95 (
96 None,
97 Some(IotaObjectResponseError::DynamicFieldNotFound {
98 parent_object_id: object_id,
99 }),
100 ) => {
101 panic!(
102 "node can't find dynamic field for {object_id} with client {:?}",
103 client.read_api()
104 )
105 }
106 (
107 None,
108 Some(IotaObjectResponseError::Deleted {
109 object_id,
110 version: _,
111 digest: _,
112 }),
113 ) => {
114 if !self.is_deleted {
115 panic!("gas object {object_id} was deleted");
116 }
117 Ok(CheckerResultObject::new(None, None))
118 }
119 (Some(object), _) => {
120 if self.is_deleted {
121 panic!("expect gas object {object_id} deleted, but it is not");
122 }
123 if let Some(owner) = self.owner {
124 let object_owner = object
125 .owner
126 .unwrap_or_else(|| panic!("object {object_id} does not have owner"));
127 assert_eq!(
128 object_owner, owner,
129 "gas coin {object_id} does not belong to {owner}, but {object_owner}"
130 );
131 }
132 if self.is_iota_coin == Some(true) {
133 let move_obj = object
134 .bcs
135 .as_ref()
136 .unwrap_or_else(|| panic!("object {object_id} does not have bcs data"))
137 .try_as_move()
138 .unwrap_or_else(|| panic!("object {object_id} is not a move object"));
139
140 let gas_coin = move_obj.deserialize()?;
141 return Ok(CheckerResultObject::new(Some(gas_coin), Some(object)));
142 }
143 Ok(CheckerResultObject::new(None, Some(object)))
144 }
145 (None, Some(IotaObjectResponseError::Display { error })) => {
146 panic!("display error: {error:?}");
147 }
148 (None, None) | (None, Some(IotaObjectResponseError::Unknown)) => {
149 panic!("unexpected response: object not found and no specific error provided");
150 }
151 }
152 }
153}
154
155pub struct CheckerResultObject {
156 gas_coin: Option<GasCoin>,
157 object: Option<IotaObjectData>,
158}
159
160impl CheckerResultObject {
161 pub fn new(gas_coin: Option<GasCoin>, object: Option<IotaObjectData>) -> Self {
162 Self { gas_coin, object }
163 }
164 pub fn into_gas_coin(self) -> GasCoin {
165 self.gas_coin.unwrap()
166 }
167 pub fn into_object(self) -> IotaObjectData {
168 self.object.unwrap()
169 }
170}
171
172#[macro_export]
173macro_rules! assert_eq_if_present {
174 ($left:expr, $right:expr, $($arg:tt)+) => {
175 match (&$left, &$right) {
176 (Some(left_val), right_val) => {
177 if !(&left_val == right_val) {
178 panic!("{} does not match, left: {left_val:?}, right: {right_val:?}", $($arg)+);
179 }
180 }
181 _ => ()
182 }
183 };
184}
185
186#[derive(Default, Debug)]
187pub struct BalanceChangeChecker {
188 owner: Option<Owner>,
189 coin_type: Option<TypeTag>,
190 amount: Option<i128>,
191}
192
193impl BalanceChangeChecker {
194 pub fn new() -> Self {
195 Default::default()
196 }
197
198 pub fn owner(mut self, owner: Owner) -> Self {
199 self.owner = Some(owner);
200 self
201 }
202 pub fn coin_type(mut self, coin_type: &str) -> Self {
203 self.coin_type = Some(parse_iota_type_tag(coin_type).unwrap());
204 self
205 }
206
207 pub fn amount(mut self, amount: i128) -> Self {
208 self.amount = Some(amount);
209 self
210 }
211
212 pub fn check(self, event: &BalanceChange) {
213 let BalanceChange {
214 owner,
215 coin_type,
216 amount,
217 } = event;
218
219 assert_eq_if_present!(self.owner, owner, "owner");
220 assert_eq_if_present!(self.coin_type, coin_type, "coin_type");
221 assert_eq_if_present!(self.amount, amount, "version");
222 }
223}