forked from hyperledger-iroha/iroha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset.rs
442 lines (385 loc) · 16 KB
/
asset.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use eyre::Result;
use iroha::{
crypto::KeyPair,
data_model::{
asset::{AssetId, AssetType, AssetValue},
isi::error::{InstructionEvaluationError, InstructionExecutionError, Mismatch, TypeError},
prelude::*,
transaction::error::TransactionRejectionReason,
},
};
use iroha_executor_data_model::permission::asset::CanTransferAsset;
use iroha_test_network::*;
use iroha_test_samples::{gen_account_in, ALICE_ID, BOB_ID};
#[test]
// This test is also covered at the UI level in the iroha_cli tests
// in test_register_asset_definitions.py
fn client_register_asset_should_add_asset_once_but_not_twice() -> Result<()> {
let (network, _rt) = NetworkBuilder::new().start_blocking()?;
let test_client = network.client();
// Given
let account_id = ALICE_ID.clone();
let asset_definition_id = "test_asset#wonderland"
.parse::<AssetDefinitionId>()
.expect("Valid");
let create_asset =
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone()));
let register_asset = Register::asset(Asset::new(
AssetId::new(asset_definition_id.clone(), account_id.clone()),
0_u32,
));
test_client.submit_all_blocking::<InstructionBox>([
create_asset.into(),
register_asset.clone().into(),
])?;
// Registering an asset to an account which doesn't have one
// should result in asset being created
let asset = test_client
.query(FindAssets::new())
.filter_with(|asset| asset.id.account.eq(account_id))
.execute_all()?
.into_iter()
.find(|asset| *asset.id().definition() == asset_definition_id)
.unwrap();
assert_eq!(*asset.value(), AssetValue::Numeric(Numeric::ZERO));
// But registering an asset to account already having one should fail
assert!(test_client.submit_blocking(register_asset).is_err());
Ok(())
}
#[test]
fn unregister_asset_should_remove_asset_from_account() -> Result<()> {
let (network, _rt) = NetworkBuilder::new().start_blocking()?;
let test_client = network.client();
// Given
let account_id = ALICE_ID.clone();
let asset_definition_id = "test_asset#wonderland"
.parse::<AssetDefinitionId>()
.expect("Valid");
let asset_id = AssetId::new(asset_definition_id.clone(), account_id.clone());
let create_asset: InstructionBox =
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone())).into();
let register_asset = Register::asset(Asset::new(asset_id.clone(), 0_u32)).into();
let unregister_asset = Unregister::asset(asset_id);
test_client.submit_all_blocking([create_asset, register_asset])?;
// Check for asset to be registered
let assets = test_client
.query(FindAssets::new())
.filter_with(|asset| asset.id.account.eq(account_id.clone()))
.execute_all()?;
assert!(assets
.iter()
.any(|asset| *asset.id().definition() == asset_definition_id));
test_client.submit_blocking(unregister_asset)?;
// ... and check that it is removed after Unregister
let assets = test_client
.query(FindAssets::new())
.filter_with(|asset| asset.id.account.eq(account_id.clone()))
.execute_all()?;
assert!(assets
.iter()
.all(|asset| *asset.id().definition() != asset_definition_id));
Ok(())
}
#[test]
// This test is also covered at the UI level in the iroha_cli tests
// in test_mint_assets.py
fn client_add_asset_quantity_to_existing_asset_should_increase_asset_amount() -> Result<()> {
let (network, _rt) = NetworkBuilder::new().start_blocking()?;
let test_client = network.client();
// Given
let account_id = ALICE_ID.clone();
let asset_definition_id = "xor#wonderland"
.parse::<AssetDefinitionId>()
.expect("Valid");
let create_asset =
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone()));
let metadata = iroha::data_model::metadata::Metadata::default();
//When
let quantity = numeric!(200);
let mint = Mint::asset_numeric(
quantity,
AssetId::new(asset_definition_id.clone(), account_id.clone()),
);
let instructions: [InstructionBox; 2] = [create_asset.into(), mint.into()];
let tx = test_client.build_transaction(instructions, metadata);
test_client.submit_transaction_blocking(&tx)?;
let asset = test_client
.query(FindAssets::new())
.filter_with(|asset| asset.id.account.eq(account_id))
.execute_all()?
.into_iter()
.find(|asset| *asset.id().definition() == asset_definition_id)
.unwrap();
assert_eq!(*asset.value(), AssetValue::Numeric(quantity));
Ok(())
}
#[test]
fn client_add_big_asset_quantity_to_existing_asset_should_increase_asset_amount() -> Result<()> {
let (network, _rt) = NetworkBuilder::new().start_blocking()?;
let test_client = network.client();
// Given
let account_id = ALICE_ID.clone();
let asset_definition_id = "xor#wonderland"
.parse::<AssetDefinitionId>()
.expect("Valid");
let create_asset =
Register::asset_definition(AssetDefinition::numeric(asset_definition_id.clone()));
let metadata = iroha::data_model::metadata::Metadata::default();
// When
let quantity = Numeric::new(2_u128.pow(65), 0);
let mint = Mint::asset_numeric(
quantity,
AssetId::new(asset_definition_id.clone(), account_id.clone()),
);
let instructions: [InstructionBox; 2] = [create_asset.into(), mint.into()];
let tx = test_client.build_transaction(instructions, metadata);
test_client.submit_transaction_blocking(&tx)?;
let asset = test_client
.query(FindAssets::new())
.filter_with(|asset| asset.id.account.eq(account_id))
.execute_all()?
.into_iter()
.find(|asset| *asset.id().definition() == asset_definition_id)
.unwrap();
assert_eq!(*asset.value(), AssetValue::Numeric(quantity));
Ok(())
}
#[test]
fn client_add_asset_with_decimal_should_increase_asset_amount() -> Result<()> {
let (network, _rt) = NetworkBuilder::new().start_blocking()?;
let test_client = network.client();
// Given
let account_id = ALICE_ID.clone();
let asset_definition_id = "xor#wonderland"
.parse::<AssetDefinitionId>()
.expect("Valid");
let asset_definition = AssetDefinition::numeric(asset_definition_id.clone());
let create_asset = Register::asset_definition(asset_definition);
let metadata = iroha::data_model::metadata::Metadata::default();
//When
let quantity = numeric!(123.456);
let mint = Mint::asset_numeric(
quantity,
AssetId::new(asset_definition_id.clone(), account_id.clone()),
);
let instructions: [InstructionBox; 2] = [create_asset.into(), mint.into()];
let tx = test_client.build_transaction(instructions, metadata);
test_client.submit_transaction_blocking(&tx)?;
let asset = test_client
.query(FindAssets::new())
.filter_with(|asset| asset.id.account.eq(account_id.clone()))
.execute_all()?
.into_iter()
.find(|asset| *asset.id().definition() == asset_definition_id)
.unwrap();
assert_eq!(*asset.value(), AssetValue::Numeric(quantity));
// Add some fractional part
let quantity2 = numeric!(0.55);
let mint = Mint::asset_numeric(
quantity2,
AssetId::new(asset_definition_id.clone(), account_id.clone()),
);
// and check that it is added without errors
let sum = quantity
.checked_add(quantity2)
.ok_or_else(|| eyre::eyre!("overflow"))?;
test_client.submit_blocking(mint)?;
let asset = test_client
.query(FindAssets::new())
.filter_with(|asset| asset.id.account.eq(account_id))
.execute_all()?
.into_iter()
.find(|asset| *asset.id().definition() == asset_definition_id)
.unwrap();
assert_eq!(*asset.value(), AssetValue::Numeric(sum));
Ok(())
}
#[allow(unused_must_use)]
#[allow(clippy::too_many_lines)]
#[allow(clippy::expect_fun_call)]
#[test]
fn find_rate_and_make_exchange_isi_should_succeed() {
let (network, _rt) = NetworkBuilder::new().start_blocking().unwrap();
let test_client = network.client();
let (dex_id, _dex_keypair) = gen_account_in("exchange");
let (seller_id, seller_keypair) = gen_account_in("company");
let (buyer_id, buyer_keypair) = gen_account_in("company");
let rate: AssetId = format!("btc/eth##{}", &dex_id)
.parse()
.expect("should be valid");
let seller_btc: AssetId = format!("btc#crypto#{}", &seller_id)
.parse()
.expect("should be valid");
let buyer_eth: AssetId = format!("eth#crypto#{}", &buyer_id)
.parse()
.expect("should be valid");
test_client
.submit_all_blocking::<InstructionBox>([
register::domain("exchange").into(),
register::domain("company").into(),
register::domain("crypto").into(),
register::account(dex_id.clone()).into(),
register::account(seller_id.clone()).into(),
register::account(buyer_id.clone()).into(),
register::asset_definition_numeric("btc/eth#exchange").into(),
register::asset_definition_numeric("btc#crypto").into(),
register::asset_definition_numeric("eth#crypto").into(),
Mint::asset_numeric(20_u32, rate.clone()).into(),
Mint::asset_numeric(10_u32, seller_btc.clone()).into(),
Mint::asset_numeric(200_u32, buyer_eth.clone()).into(),
])
.expect("transaction should be committed");
let alice_id = ALICE_ID.clone();
let alice_can_transfer_asset = |asset_id: AssetId, owner_key_pair: KeyPair| {
let permission = CanTransferAsset {
asset: asset_id.clone(),
};
let instruction = Grant::account_permission(permission, alice_id.clone());
let transaction = TransactionBuilder::new(
ChainId::from("00000000-0000-0000-0000-000000000000"),
asset_id.account().clone(),
)
.with_instructions([instruction])
.sign(owner_key_pair.private_key());
test_client
.submit_transaction_blocking(&transaction)
.expect("transaction should be committed");
};
alice_can_transfer_asset(seller_btc.clone(), seller_keypair);
alice_can_transfer_asset(buyer_eth.clone(), buyer_keypair);
let assert_balance = |asset_id: AssetId, expected: Numeric| {
let got = test_client
.query_single(FindAssetQuantityById::new(asset_id))
.expect("query should succeed");
assert_eq!(got, expected);
};
// before: seller has $BTC10 and buyer has $ETH200
assert_balance(seller_btc.clone(), numeric!(10));
assert_balance(buyer_eth.clone(), numeric!(200));
let rate: u32 = test_client
.query_single(FindAssetQuantityById::new(rate))
.expect("query should succeed")
.try_into()
.expect("numeric should be u32 originally");
test_client
.submit_all_blocking([
Transfer::asset_numeric(seller_btc.clone(), 10_u32, buyer_id.clone()),
Transfer::asset_numeric(buyer_eth.clone(), 10_u32 * rate, seller_id.clone()),
])
.expect("transaction should be committed");
let assert_purged = |asset_id: AssetId| {
let _err = test_client
.query_single(FindAssetQuantityById::new(asset_id))
.expect_err("query should fail, as zero assets are purged from accounts");
};
let seller_eth: AssetId = format!("eth#crypto#{}", &seller_id)
.parse()
.expect("should be valid");
let buyer_btc: AssetId = format!("btc#crypto#{}", &buyer_id)
.parse()
.expect("should be valid");
// after: seller has $ETH200 and buyer has $BTC10
assert_purged(seller_btc);
assert_purged(buyer_eth);
assert_balance(seller_eth, numeric!(200));
assert_balance(buyer_btc, numeric!(10));
}
#[test]
fn transfer_asset_definition() {
let (network, _rt) = NetworkBuilder::new().start_blocking().unwrap();
let test_client = network.client();
let alice_id = ALICE_ID.clone();
let bob_id = BOB_ID.clone();
let asset_definition_id: AssetDefinitionId = "asset#wonderland".parse().expect("Valid");
test_client
.submit_blocking(Register::asset_definition(AssetDefinition::numeric(
asset_definition_id.clone(),
)))
.expect("Failed to submit transaction");
let asset_definition = test_client
.query(FindAssetsDefinitions::new())
.filter_with(|asset_definition| asset_definition.id.eq(asset_definition_id.clone()))
.execute_single()
.expect("Failed to execute Iroha Query");
assert_eq!(asset_definition.owned_by(), &alice_id);
test_client
.submit_blocking(Transfer::asset_definition(
alice_id,
asset_definition_id.clone(),
bob_id.clone(),
))
.expect("Failed to submit transaction");
let asset_definition = test_client
.query(FindAssetsDefinitions::new())
.filter_with(|asset_definition| asset_definition.id.eq(asset_definition_id))
.execute_single()
.expect("Failed to execute Iroha Query");
assert_eq!(asset_definition.owned_by(), &bob_id);
}
#[test]
fn fail_if_dont_satisfy_spec() {
let (network, _rt) = NetworkBuilder::new().start_blocking().unwrap();
let test_client = network.client();
let alice_id = ALICE_ID.clone();
let bob_id = BOB_ID.clone();
let asset_definition_id: AssetDefinitionId = "asset#wonderland".parse().expect("Valid");
let asset_id: AssetId = AssetId::new(asset_definition_id.clone(), alice_id.clone());
// Create asset definition which accepts only integers
let asset_definition = AssetDefinition::new(
asset_definition_id.clone(),
AssetType::Numeric(NumericSpec::integer()),
);
test_client
.submit_blocking(Register::asset_definition(asset_definition))
.expect("Failed to submit transaction");
let isi = |value: Numeric| {
[
Register::asset(Asset::new(asset_id.clone(), value)).into(),
Mint::asset_numeric(value, asset_id.clone()).into(),
Burn::asset_numeric(value, asset_id.clone()).into(),
Transfer::asset_numeric(asset_id.clone(), value, bob_id.clone()).into(),
]
};
// Fail if submitting fractional value
let fractional_value = numeric!(0.01);
for isi in isi(fractional_value) {
let err = test_client
.submit_blocking::<InstructionBox>(isi)
.expect_err("Should be rejected due to non integer value");
let rejection_reason = err
.downcast_ref::<TransactionRejectionReason>()
.unwrap_or_else(|| panic!("Error {err} is not TransactionRejectionReason"));
assert_eq!(
rejection_reason,
&TransactionRejectionReason::Validation(ValidationFail::InstructionFailed(
InstructionExecutionError::Evaluate(InstructionEvaluationError::Type(
TypeError::from(Mismatch {
expected: AssetType::Numeric(NumericSpec::integer()),
actual: AssetType::Numeric(NumericSpec::fractional(2))
})
))
))
);
}
// Everything works fine when submitting proper integer value
let integer_value = numeric!(1);
for isi in isi(integer_value) {
test_client
.submit_blocking(isi)
.expect("Should be accepted since submitting integer value");
}
}
mod register {
use super::*;
pub fn domain(id: &str) -> Register<Domain> {
Register::domain(Domain::new(id.parse().expect("should parse to DomainId")))
}
pub fn account(id: AccountId) -> Register<Account> {
Register::account(Account::new(id))
}
pub fn asset_definition_numeric(id: &str) -> Register<AssetDefinition> {
Register::asset_definition(AssetDefinition::numeric(
id.parse().expect("should parse to AssetDefinitionId"),
))
}
}