-
Notifications
You must be signed in to change notification settings - Fork 578
Expand file tree
/
Copy pathtest_utils.rs
More file actions
109 lines (97 loc) · 4.31 KB
/
test_utils.rs
File metadata and controls
109 lines (97 loc) · 4.31 KB
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
use std::{collections::HashMap, sync::Arc};
use async_trait::async_trait;
use eyre::Result;
use tokio::sync::Mutex;
use hyperlane_base::db::{HyperlaneRocksDB, DB};
use hyperlane_core::identifiers::UniqueIdentifier;
use hyperlane_core::KnownHyperlaneDomain;
use crate::adapter::chains::ethereum::NonceDb;
use crate::adapter::{AdaptsChain, GasLimit, TxBuildingResult};
use crate::dispatcher::{DispatcherMetrics, PayloadDb, TransactionDb};
use crate::error::LanderError;
use crate::payload::{FullPayload, PayloadDetails, PayloadStatus};
use crate::transaction::{Transaction, TransactionStatus, TransactionUuid, VmSpecificTxData};
mockall::mock! {
pub Adapter {
}
#[async_trait]
impl AdaptsChain for Adapter {
async fn estimate_gas_limit(&self, payload: &FullPayload) -> Result<Option<GasLimit>, LanderError>;
async fn build_transactions(&self, payloads: &[FullPayload]) -> Vec<TxBuildingResult>;
async fn simulate_tx(&self, tx: &mut Transaction) -> Result<Vec<PayloadDetails>, LanderError>;
async fn estimate_tx(&self, tx: &mut Transaction) -> Result<(), LanderError>;
async fn submit(&self, tx: &mut Transaction) -> Result<(), LanderError>;
async fn get_tx_hash_status(&self, hash: hyperlane_core::H512) -> Result<TransactionStatus, LanderError>;
async fn tx_status(&self, tx: &Transaction) -> Result<TransactionStatus, LanderError>;
async fn tx_ready_for_resubmission(&self, _tx: &Transaction) -> bool;
async fn reverted_payloads(&self, tx: &Transaction) -> Result<Vec<PayloadDetails>, LanderError>;
fn estimated_block_time(&self) -> &std::time::Duration;
fn max_batch_size(&self) -> u32;
fn update_vm_specific_metrics(&self, _tx: &Transaction, _metrics: &DispatcherMetrics);
async fn nonce_gap_exists(&self) -> bool;
async fn replace_tx(&self, _tx: &Transaction) -> Result<(), LanderError>;
fn reprocess_txs_poll_rate(&self) -> Option<std::time::Duration>;
async fn get_reprocess_txs(&self) -> Result<Vec<Transaction>, LanderError>;
}
}
pub(crate) fn tmp_dbs() -> (Arc<dyn PayloadDb>, Arc<dyn TransactionDb>, Arc<dyn NonceDb>) {
let temp_dir = tempfile::tempdir().unwrap();
let db = DB::from_path(temp_dir.path()).unwrap();
let domain = KnownHyperlaneDomain::Arbitrum.into();
let rocksdb = Arc::new(HyperlaneRocksDB::new(&domain, db));
let payload_db = rocksdb.clone() as Arc<dyn PayloadDb>;
let tx_db = rocksdb.clone() as Arc<dyn TransactionDb>;
let nonce_db = rocksdb.clone() as Arc<dyn NonceDb>;
(payload_db, tx_db, nonce_db)
}
pub(crate) fn dummy_tx(payloads: Vec<FullPayload>, status: TransactionStatus) -> Transaction {
let details: Vec<PayloadDetails> = payloads
.into_iter()
.map(|payload| payload.details)
.collect();
Transaction {
uuid: UniqueIdentifier::random(),
tx_hashes: vec![],
vm_specific_data: VmSpecificTxData::CosmWasm,
payload_details: details.clone(),
status,
submission_attempts: 0,
creation_timestamp: chrono::Utc::now(),
last_submission_attempt: None,
last_status_check: None,
}
}
pub(crate) async fn create_random_txs_and_store_them(
num: usize,
payload_db: &Arc<dyn PayloadDb>,
tx_db: &Arc<dyn TransactionDb>,
status: TransactionStatus,
) -> Vec<Transaction> {
let mut txs = Vec::new();
for _ in 0..num {
let mut payload = FullPayload::random();
payload.status = PayloadStatus::InTransaction(status.clone());
payload_db.store_payload_by_uuid(&payload).await.unwrap();
let tx = dummy_tx(vec![payload], status.clone());
tx_db.store_transaction_by_uuid(&tx).await.unwrap();
txs.push(tx);
}
txs
}
pub(crate) async fn initialize_payload_db(payload_db: &Arc<dyn PayloadDb>, payload: &FullPayload) {
payload_db.store_payload_by_uuid(payload).await.unwrap();
}
pub async fn are_all_txs_in_pool(
txs: Vec<Transaction>,
pool: &Arc<Mutex<HashMap<TransactionUuid, Transaction>>>,
) -> bool {
let pool = pool.lock().await;
txs.iter().all(|tx| pool.contains_key(&tx.uuid))
}
pub async fn are_no_txs_in_pool(
txs: Vec<Transaction>,
pool: &Arc<Mutex<HashMap<TransactionUuid, Transaction>>>,
) -> bool {
let pool = pool.lock().await;
txs.iter().all(|tx| !pool.contains_key(&tx.uuid))
}