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
use alloc::sync::Arc;
use ibc::core::ics24_host::identifier::ChainId;
use tokio::runtime::Runtime;
use crate::chain::driver::ChainDriver;
use crate::error::Error;
use crate::types::config::TestConfig;
use crate::util::random::{random_u32, random_unused_tcp_port};
#[derive(Debug)]
pub struct ChainBuilder {
pub command_path: String,
pub base_store_dir: String,
pub account_prefix: String,
pub runtime: Arc<Runtime>,
}
impl ChainBuilder {
pub fn new(
command_path: &str,
base_store_dir: &str,
account_prefix: &str,
runtime: Arc<Runtime>,
) -> Self {
Self {
command_path: command_path.to_string(),
base_store_dir: base_store_dir.to_string(),
account_prefix: account_prefix.to_string(),
runtime,
}
}
pub fn new_with_config(config: &TestConfig, runtime: Arc<Runtime>) -> Self {
Self::new(
&config.chain_command_path,
&format!("{}", config.chain_store_dir.display()),
&config.account_prefix,
runtime,
)
}
pub fn new_chain(&self, prefix: &str, use_random_id: bool) -> Result<ChainDriver, Error> {
let chain_id = if use_random_id {
ChainId::from_string(&format!("ibc-{}-{:x}", prefix, random_u32()))
} else {
ChainId::from_string(&format!("ibc-{}", prefix))
};
let rpc_port = random_unused_tcp_port();
let grpc_port = random_unused_tcp_port();
let grpc_web_port = random_unused_tcp_port();
let p2p_port = random_unused_tcp_port();
let home_path = format!("{}/{}", self.base_store_dir, chain_id);
let driver = ChainDriver::create(
self.command_path.clone(),
chain_id,
home_path,
self.account_prefix.clone(),
rpc_port,
grpc_port,
grpc_web_port,
p2p_port,
self.runtime.clone(),
)?;
Ok(driver)
}
}