forked from Gravity-Bridge/Gravity-Bridge
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhappy_path.rs
658 lines (611 loc) · 22.7 KB
/
happy_path.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
use crate::get_fee;
use crate::utils::check_erc20_balance;
use crate::utils::*;
use crate::MINER_ADDRESS;
use crate::MINER_PRIVATE_KEY;
use crate::OPERATION_TIMEOUT;
use crate::TOTAL_TIMEOUT;
use bytes::BytesMut;
use clarity::{Address as EthAddress, Uint256};
use cosmos_gravity::query::get_attestations;
use cosmos_gravity::send::send_to_eth;
use cosmos_gravity::{query::get_oldest_unsigned_transaction_batches, send::send_ethereum_claims};
use deep_space::address::Address as CosmosAddress;
use deep_space::coin::Coin;
use deep_space::private_key::{CosmosPrivateKey, PrivateKey};
use deep_space::Contact;
use ethereum_gravity::utils::get_valset_nonce;
use ethereum_gravity::{send_to_cosmos::send_to_cosmos, utils::get_tx_batch_nonce};
use gravity_proto::gravity::query_client::QueryClient as GravityQueryClient;
use gravity_proto::gravity::MsgSendToCosmosClaim;
use gravity_proto::gravity::MsgValsetUpdatedClaim;
use gravity_utils::error::GravityError;
use gravity_utils::types::SendToCosmosEvent;
use num::CheckedAdd;
use prost::Message;
use std::any::type_name;
use std::time::Duration;
use std::time::Instant;
use tokio::time::sleep as delay_for;
use tonic::transport::Channel;
use web30::client::Web3;
pub async fn happy_path_test(
web30: &Web3,
grpc_client: GravityQueryClient<Channel>,
contact: &Contact,
keys: Vec<ValidatorKeys>,
gravity_address: EthAddress,
erc20_address: EthAddress,
validator_out: bool,
) {
let mut grpc_client = grpc_client;
let no_relay_market_config = create_default_test_config();
start_orchestrators(
keys.clone(),
gravity_address,
validator_out,
no_relay_market_config,
)
.await;
// bootstrapping tests finish here and we move into operational tests
// send 3 valset updates to make sure the process works back to back
// don't do this in the validator out test because it changes powers
// randomly and may actually make it impossible for that test to pass
// by random re-allocation of powers. If we had 5 or 10 validators
// instead of 3 this wouldn't be a problem. But with 3 not even 1% of
// power can be reallocated to the down validator before things stop
// working. We'll settle for testing that the initial valset (generated
// with the first block) is successfully updated
if !validator_out {
for _ in 0u32..2 {
test_valset_update(web30, contact, &mut grpc_client, &keys, gravity_address).await;
}
} else {
wait_for_nonzero_valset(web30, gravity_address).await;
}
// generate an address for coin sending tests, this ensures test imdepotency
let user_keys = get_user_key(None);
info!("testing erc20 deposit");
// the denom and amount of the token bridged from Ethereum -> Cosmos
// so the denom is the gravity<hash> token name
// Send a token 3 times
for _ in 0u32..3 {
test_erc20_deposit_panic(
web30,
contact,
&mut grpc_client,
user_keys.cosmos_address,
gravity_address,
erc20_address,
100u64.into(),
None,
None,
)
.await;
}
let event_nonce = get_event_nonce_safe(gravity_address, web30, *MINER_ADDRESS)
.await
.unwrap();
// We are going to submit a duplicate tx with nonce 1
// This had better not increase the balance again
// this test may have false positives if the timeout is not
// long enough. TODO check for an error on the cosmos send response
submit_duplicate_erc20_send(
event_nonce, // Duplicate the current nonce
contact,
erc20_address,
1u64.into(),
user_keys.cosmos_address,
&keys,
)
.await;
// we test a batch by sending a transaction
test_batch(
contact,
&mut grpc_client,
web30,
user_keys.eth_address,
gravity_address,
keys[0].validator_key,
user_keys.cosmos_key,
erc20_address,
)
.await;
}
// Iterates each attestation known by the grpc endpoint by calling `get_attestations()`
// Executes the input closure `f` against each attestation's decoded claim
// This is useful for testing that certain attestations exist in the oracle,
// see the consumers of this function (check_valset_update_attestation(),
// check_send_to_cosmos_attestation(), etc.) for examples of usage
//
// `F` is the type of the closure, a state mutating function which may be called multiple times
// `F` functions take a single parameter of type `T`, which is some sort of `Message`
// The type `T` is very important as it dictates how we decode the message
pub async fn iterate_attestations<F: FnMut(T), T: Message + Default>(
grpc_client: &mut GravityQueryClient<Channel>,
f: &mut F,
) {
let attestations = get_attestations(grpc_client, None)
.await
.expect("Something happened while getting attestations after delegating to validator");
for (i, att) in attestations.into_iter().enumerate() {
let claim = att.clone().claim;
trace!("Processing attestation {}", i);
if claim.is_none() {
trace!("Attestation returned with no claim: {:?}", att);
continue;
}
let claim = claim.unwrap();
let mut buf = BytesMut::with_capacity(claim.value.len());
buf.extend_from_slice(&claim.value);
// Here we use the `T` type to decode whatever type of message this attestation holds
// for use in the `f` function
let decoded = T::decode(buf);
// Decoding errors indicate there is some other attestation we don't care about
if decoded.is_err() {
debug!(
"Found an attestation which is not a {}: {:?}",
type_name::<T>(),
att,
);
continue;
}
let decoded = decoded.unwrap();
f(decoded);
}
}
pub async fn wait_for_nonzero_valset(web30: &Web3, gravity_address: EthAddress) {
let start = Instant::now();
let mut current_eth_valset_nonce = get_valset_nonce(gravity_address, *MINER_ADDRESS, web30)
.await
.expect("Failed to get current eth valset");
while 0 == current_eth_valset_nonce {
info!("Validator set is not yet updated to 0>, waiting",);
current_eth_valset_nonce = get_valset_nonce(gravity_address, *MINER_ADDRESS, web30)
.await
.expect("Failed to get current eth valset");
delay_for(Duration::from_secs(4)).await;
if Instant::now() - start > TOTAL_TIMEOUT {
panic!("Failed to update validator set");
}
}
}
pub async fn test_valset_update(
web30: &Web3,
contact: &Contact,
grpc_client: &mut GravityQueryClient<Channel>,
keys: &[ValidatorKeys],
gravity_address: EthAddress,
) {
get_valset_nonce(gravity_address, keys[0].eth_key.to_address(), web30)
.await
.expect("Incorrect Gravity Address or otherwise unable to contact Gravity");
let mut grpc_client = grpc_client.clone();
// if we don't do this the orchestrators may run ahead of us and we'll be stuck here after
// getting credit for two loops when we did one
let starting_eth_valset_nonce = get_valset_nonce(gravity_address, *MINER_ADDRESS, web30)
.await
.expect("Failed to get starting eth valset");
let start = Instant::now();
let (delegate_address, amount) = get_validator_to_delegate_to(contact).await;
info!(
"Delegating {} to {} in order to generate a validator set update",
amount, delegate_address
);
contact
.delegate_to_validator(
delegate_address,
amount,
get_fee(None),
keys[1].validator_key,
Some(TOTAL_TIMEOUT),
)
.await
.unwrap();
check_valset_update_attestation(&mut grpc_client, keys).await;
let mut current_eth_valset_nonce = get_valset_nonce(gravity_address, *MINER_ADDRESS, web30)
.await
.expect("Failed to get current eth valset");
while starting_eth_valset_nonce == current_eth_valset_nonce {
info!(
"Validator set is not yet updated to {}>, waiting",
starting_eth_valset_nonce
);
current_eth_valset_nonce = get_valset_nonce(gravity_address, *MINER_ADDRESS, web30)
.await
.expect("Failed to get current eth valset");
delay_for(Duration::from_secs(4)).await;
if Instant::now() - start > TOTAL_TIMEOUT {
panic!("Failed to update validator set");
}
}
assert!(starting_eth_valset_nonce != current_eth_valset_nonce);
info!("Validator set successfully updated!");
}
// Checks for a MsgValsetUpdatedClaim attestation where every validator is represented
async fn check_valset_update_attestation(
grpc_client: &mut GravityQueryClient<Channel>,
keys: &[ValidatorKeys],
) {
let mut found = true;
iterate_attestations(grpc_client, &mut |decoded: MsgValsetUpdatedClaim| {
// Check that each bridge validator is one of the addresses in our keys
for bridge_val in decoded.members {
let found_val = keys.iter().any(|key: &ValidatorKeys| {
let eth_pub_key = key.eth_key.to_address().to_string();
bridge_val.ethereum_address == eth_pub_key
});
if !found_val {
warn!(
"Could not find BridgeValidator eth pub key {} in keys",
bridge_val.ethereum_address
);
}
found &= found_val;
}
})
.await;
assert!(
found,
"Could not find the valset updated attestation we were looking for!"
);
info!("Found the expected MsgValsetUpdatedClaim attestation");
}
// Tests an ERC20 deposit and panics on failure
#[allow(clippy::too_many_arguments)]
pub async fn test_erc20_deposit_panic(
web30: &Web3,
contact: &Contact,
grpc_client: &mut GravityQueryClient<Channel>,
dest: CosmosAddress,
gravity_address: EthAddress,
erc20_address: EthAddress,
amount: Uint256,
timeout: Option<Duration>, // how long to wait for balance on cosmos to change
expected_change: Option<Uint256>, // provide an expected change when multiple transactions will take place at once
) {
match test_erc20_deposit_result(
web30,
contact,
grpc_client,
dest,
gravity_address,
erc20_address,
amount,
timeout,
expected_change,
)
.await
{
Ok(_) => {
info!("Successfully bridged ERC20!")
}
Err(_) => {
panic!("Failed to bridge ERC20!")
}
}
}
/// this function tests Ethereum -> Cosmos deposits of ERC20 tokens
/// it validates the that the contract being called is the gravity contract
/// the erc20 provided is a valid erc20 implementation, sends the prescribed
/// amount of tokens, and then finds the attestations submitted by orchestrators
/// on the Cosmos chain. Finally it checks that the balance has changed by the
/// correct amount, which is either the deposit amount or the 'expected change'
/// provided by the caller
#[allow(clippy::too_many_arguments)]
pub async fn test_erc20_deposit_result(
web30: &Web3,
contact: &Contact,
grpc_client: &mut GravityQueryClient<Channel>,
dest: CosmosAddress,
gravity_address: EthAddress,
erc20_address: EthAddress,
amount: Uint256,
timeout: Option<Duration>,
expected_change: Option<Uint256>, // provide an expected change when multiple transactions will take place at once
) -> Result<(), GravityError> {
let start_coin = contact
.get_balance(dest, format!("gravity{}", erc20_address))
.await
.unwrap();
send_erc20_deposit(
web30,
grpc_client,
dest,
gravity_address,
erc20_address,
amount.clone(),
)
.await?;
let start = Instant::now();
let duration = match timeout {
Some(w) => w,
None => TOTAL_TIMEOUT,
};
while Instant::now() - start < duration {
match (
start_coin.clone(),
contact
.get_balance(dest, format!("gravity{}", erc20_address))
.await
.unwrap(),
) {
(Some(start_coin), Some(end_coin)) => {
// When a bridge governance vote happens, the orchestrator will replay all incomplete
// sends to cosmos on the next send to cosmos transaction, so we need to use expected_change
if let Some(expected) = expected_change.clone() {
if end_coin.amount.clone() - start_coin.amount.clone() == expected
&& start_coin.denom == end_coin.denom
{
info!(
"Successfully bridged ERC20 {}{} to Cosmos! Balance is now {}{}",
amount, start_coin.denom, end_coin.amount, end_coin.denom
);
return Ok(());
}
} else {
let expected_end = start_coin.amount.checked_add(&amount.clone());
if expected_end.is_none() {
info!(
"Expecting overflow from addition of {:?} + {:?}!",
start_coin.amount,
amount.clone()
);
} else if start_coin.amount + amount.clone() == end_coin.amount
&& start_coin.denom == end_coin.denom
{
info!(
"Successfully bridged ERC20 {}{} to Cosmos! Balance is now {}{}",
amount, start_coin.denom, end_coin.amount, end_coin.denom
);
return Ok(());
}
}
}
(None, Some(end_coin)) => {
// When a bridge governance vote happens, the orchestrator will replay all incomplete
// sends to cosmos on the next send to cosmos transaction, so we need to use expected_change
if let Some(expected) = expected_change.clone() {
if end_coin.amount == expected {
info!(
"Successfully bridged ERC20 {}{} to Cosmos! Balance is now {}{}",
amount, end_coin.denom, end_coin.amount, end_coin.denom,
);
return Ok(());
}
} else if amount == end_coin.amount {
info!(
"Successfully bridged ERC20 {}{} to Cosmos! Balance is now {}{}",
amount, end_coin.denom, end_coin.amount, end_coin.denom
);
return Ok(());
} else {
panic!("Failed to bridge ERC20!")
}
}
_ => {}
}
info!("Waiting for ERC20 deposit");
contact.wait_for_next_block(TOTAL_TIMEOUT).await.unwrap();
}
Err(GravityError::InvalidBridgeStateError(
"Did not complete deposit!".to_string(),
))
}
pub async fn send_erc20_deposit(
web30: &Web3,
grpc_client: &mut GravityQueryClient<Channel>,
dest: CosmosAddress,
gravity_address: EthAddress,
erc20_address: EthAddress,
amount: Uint256,
) -> Result<(), GravityError> {
get_valset_nonce(gravity_address, *MINER_ADDRESS, web30)
.await
.expect("Incorrect Gravity Address or otherwise unable to contact Gravity");
web30
.get_erc20_name(erc20_address, *MINER_ADDRESS)
.await
.expect("Not a valid ERC20 contract address");
let mut grpc_client = grpc_client.clone();
info!(
"Sending to Cosmos from {} to {} with amount {}",
*MINER_ADDRESS, dest, amount
);
// we send some erc20 tokens to the gravity contract to register a deposit
let tx_id = send_to_cosmos(
erc20_address,
gravity_address,
amount.clone(),
dest,
*MINER_PRIVATE_KEY,
None,
web30,
vec![],
)
.await
.expect("Failed to send tokens to Cosmos");
info!("Send to Cosmos txid: {:#066x}", tx_id);
let _tx_res = web30
.wait_for_transaction(tx_id, OPERATION_TIMEOUT, None)
.await
.expect("Send to cosmos transaction failed to be included into ethereum side");
check_send_to_cosmos_attestation(&mut grpc_client, erc20_address, dest, *MINER_ADDRESS).await?;
Ok(())
}
// Tries up to TOTAL_TIMEOUT time to find a MsgSendToCosmosClaim attestation created in the
// test_erc20_deposit test
async fn check_send_to_cosmos_attestation(
grpc_client: &mut GravityQueryClient<Channel>,
erc20_address: EthAddress,
receiver: CosmosAddress,
sender: EthAddress,
) -> Result<(), GravityError> {
let start = Instant::now();
let mut found = false;
loop {
iterate_attestations(grpc_client, &mut |decoded: MsgSendToCosmosClaim| {
let right_contract = decoded.token_contract == erc20_address.to_string();
let right_destination = decoded.cosmos_receiver == receiver.to_string();
let right_sender = decoded.ethereum_sender == sender.to_string();
found = right_contract && right_destination && right_sender;
})
.await;
if found {
break;
} else if Instant::now() - start > TOTAL_TIMEOUT {
return Err(GravityError::InvalidBridgeStateError(
"Could not find the send_to_cosmos attestation we were looking for!".to_string(),
));
}
info!("Looking for send_to_cosmos attestations");
delay_for(Duration::from_secs(10)).await;
}
info!("Found the expected MsgSendToCosmosClaim attestation");
Ok(())
}
#[allow(clippy::too_many_arguments)]
async fn test_batch(
contact: &Contact,
grpc_client: &mut GravityQueryClient<Channel>,
web30: &Web3,
dest_eth_address: EthAddress,
gravity_address: EthAddress,
requester_cosmos_private_key: CosmosPrivateKey,
dest_cosmos_private_key: CosmosPrivateKey,
erc20_contract: EthAddress,
) {
get_valset_nonce(gravity_address, *MINER_ADDRESS, web30)
.await
.expect("Incorrect Gravity Address or otherwise unable to contact Gravity");
let mut grpc_client = grpc_client.clone();
let dest_cosmos_address = dest_cosmos_private_key
.to_address(&contact.get_prefix())
.unwrap();
let coin = contact
.get_balance(dest_cosmos_address, format!("gravity{}", erc20_contract))
.await
.unwrap()
.unwrap();
let token_name = coin.denom;
let amount = coin.amount;
let bridge_denom_fee = Coin {
denom: token_name.clone(),
amount: 1u64.into(),
};
let amount = amount - 5u64.into();
info!(
"Sending {}{} from {} on Cosmos back to Ethereum",
amount, token_name, dest_cosmos_address
);
let res = send_to_eth(
dest_cosmos_private_key,
dest_eth_address,
Coin {
denom: token_name.clone(),
amount: amount.clone(),
},
bridge_denom_fee.clone(),
bridge_denom_fee.clone(),
contact,
)
.await
.unwrap();
info!("Sent tokens to Ethereum with {:?}", res);
contact.wait_for_next_block(TOTAL_TIMEOUT).await.unwrap();
let requester_address = requester_cosmos_private_key
.to_address(&contact.get_prefix())
.unwrap();
get_oldest_unsigned_transaction_batches(
&mut grpc_client,
requester_address,
contact.get_prefix(),
)
.await
.expect("Failed to get batch to sign");
let mut current_eth_batch_nonce =
get_tx_batch_nonce(gravity_address, erc20_contract, *MINER_ADDRESS, web30)
.await
.expect("Failed to get current eth valset");
let starting_batch_nonce = current_eth_batch_nonce;
let start = Instant::now();
while starting_batch_nonce == current_eth_batch_nonce {
info!(
"Batch is not yet submitted {}>, waiting",
starting_batch_nonce
);
current_eth_batch_nonce =
get_tx_batch_nonce(gravity_address, erc20_contract, *MINER_ADDRESS, web30)
.await
.expect("Failed to get current eth tx batch nonce");
delay_for(Duration::from_secs(4)).await;
if Instant::now() - start > TOTAL_TIMEOUT {
panic!("Failed to submit transaction batch set");
}
}
if web30.eth_get_balance(dest_eth_address).await.unwrap() == 0u8.into() {
// we have to send this address one eth so that it can perform contract calls
send_one_eth(dest_eth_address, web30).await;
}
check_erc20_balance(erc20_contract, amount.clone(), dest_eth_address, web30).await;
info!(
"Successfully updated txbatch nonce to {} and sent {}{} tokens to Ethereum!",
current_eth_batch_nonce, amount, token_name
);
}
// this function submits a EthereumBridgeDepositClaim to the module with a given nonce. This can be set to be a nonce that has
// already been submitted to test the nonce functionality.
#[allow(clippy::too_many_arguments)]
async fn submit_duplicate_erc20_send(
nonce: u64,
contact: &Contact,
erc20_address: EthAddress,
amount: Uint256,
receiver: CosmosAddress,
keys: &[ValidatorKeys],
) {
let start_coin = contact
.get_balance(receiver, format!("gravity{}", erc20_address))
.await
.unwrap()
.unwrap();
let ethereum_sender = "0x912fd21d7a69678227fe6d08c64222db41477ba0"
.parse()
.unwrap();
let event = SendToCosmosEvent {
event_nonce: nonce,
block_height: 500u16.into(),
erc20: erc20_address,
sender: ethereum_sender,
destination: receiver.to_string(),
validated_destination: Some(receiver),
amount,
};
// iterate through all validators and try to send an event with duplicate nonce
for k in keys.iter() {
let c_key = k.orch_key;
let res = send_ethereum_claims(
contact,
c_key,
vec![event.clone()],
vec![],
vec![],
vec![],
vec![],
get_fee(None),
)
.await;
info!("Submitted duplicate sendToCosmos event: {:?}", res);
}
contact.wait_for_next_block(TOTAL_TIMEOUT).await.unwrap();
let end_coin = contact
.get_balance(receiver, format!("gravity{}", erc20_address))
.await
.unwrap()
.unwrap();
if start_coin.amount == end_coin.amount && start_coin.denom == end_coin.denom {
info!("Successfully failed to duplicate ERC20!");
} else {
panic!("Duplicated ERC20!")
}
}