-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmod.rs
More file actions
251 lines (223 loc) · 9.34 KB
/
mod.rs
File metadata and controls
251 lines (223 loc) · 9.34 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
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
mod unit;
#[cfg(test)]
mod tests {
use alloy::signers::local::PrivateKeySigner;
use alloy::signers::Signer;
use alloy_primitives::{address, FixedBytes, U256};
use alloy_provider::ProviderBuilder;
use alloy_sol_types::SolEvent;
use basilica_miner::config::MinerConfig;
use collateral_contract::CollateralUpgradeable::{self, CollateralUpgradeableInstance};
use rand::Rng;
const TESTNET_URL: &str = "https://test.finney.opentensor.ai";
/// 0xa8b2b82247e3f2b49ee8858b088405e35755c096 deployed in testnet
/// minCollateralIncrease is 1, decisionTimeout is 1, trustee is 0xABCaD56aa87f3718C8892B48cB443c017Cd632BB
///
/// 0x119ecacb1322cd9d581d550b52e199ec97a33e2e deployed in testnet
/// decisionTimeout is 20 for the reclaim deny testing
/// ~/.basilca/private_key is default for private key file path
async fn get_contract(
) -> anyhow::Result<CollateralUpgradeableInstance<impl alloy_provider::Provider>> {
let config = MinerConfig::load()?;
let rpc_url = TESTNET_URL;
let private_key = config.security.get_private_key()?;
let proxy_contract_address = address!("0xa8b2b82247e3f2b49ee8858b088405e35755c096");
let mut signer: PrivateKeySigner = private_key.parse().unwrap();
signer.set_chain_id(Some(945));
let provider = ProviderBuilder::new()
.wallet(signer)
.connect(rpc_url)
.await?;
let contract = CollateralUpgradeable::new(proxy_contract_address, provider);
Ok(contract)
}
#[tokio::test]
#[ignore]
// cargo test --package basilica-miner --test mod -- tests::test_deposit --exact --nocapture
async fn test_deposit() -> anyhow::Result<()> {
let contract = get_contract().await?;
println!("trustee: {:?}", contract.trustee().call().await.unwrap());
println!("netuid: {:?}", contract.netuid().call().await.unwrap());
println!(
"decision_timeout: {:?}",
contract.decisionTimeout().call().await.unwrap()
);
println!(
"min_collateral_increase: {:?}",
contract.minCollateralIncrease().call().await.unwrap()
);
let node_id: u128 = rand::thread_rng().gen_range(0..10000000000);
let hotkey: [u8; 32] = [1u8; 32];
let amount = U256::from(10);
let alpha_hotkey: [u8; 32] = [2u8; 32];
let deposit_tx = contract
.deposit(
FixedBytes::from_slice(&hotkey),
FixedBytes::from_slice(&node_id.to_be_bytes()),
FixedBytes::from_slice(&alpha_hotkey),
amount,
)
.value(U256::ZERO);
let deposit_tx_receipt = deposit_tx.send().await?.get_receipt().await?;
let mut deposit_found = false;
deposit_tx_receipt.logs().iter().for_each(|log| {
if let Ok(event) = CollateralUpgradeable::Deposit::decode_log(&log.inner) {
assert!(FixedBytes::from(node_id) == event.nodeId);
deposit_found = true;
}
});
assert!(deposit_found);
let collaterals = contract
.alphaCollaterals(
FixedBytes::from_slice(&hotkey),
FixedBytes::from_slice(&node_id.to_be_bytes()),
)
.call()
.await
.unwrap();
assert_eq!(collaterals, amount);
Ok(())
}
#[tokio::test]
#[ignore]
// cargo test --package basilica-miner --test mod -- tests::test_reclaim_finalize --exact --nocapture
async fn test_reclaim_finalize() -> anyhow::Result<()> {
let contract = get_contract().await?;
// Deposit first
let node_id: u128 = rand::thread_rng().gen_range(0..10000000000);
let hotkey: [u8; 32] = [1u8; 32];
let amount = U256::from(10);
let alpha_hotkey: [u8; 32] = [2u8; 32];
let deposit_tx = contract
.deposit(
FixedBytes::from_slice(&hotkey),
FixedBytes::from_slice(&node_id.to_be_bytes()),
FixedBytes::from_slice(&alpha_hotkey),
amount,
)
.value(U256::ZERO);
let _deposit_tx_receipt = deposit_tx.send().await?.get_receipt().await?;
// Start reclaim process
let url = "example.com";
let url_checksum = 123_u128;
let reclaim_tx = contract.reclaimCollateral(
FixedBytes::from_slice(&hotkey),
FixedBytes::from_slice(&node_id.to_be_bytes()),
url.to_owned(),
FixedBytes::from_slice(&url_checksum.to_be_bytes()),
);
let reclaim_receipt = reclaim_tx.send().await?.get_receipt().await?;
let mut reclaim_id = U256::from(0);
reclaim_receipt.logs().iter().for_each(|log| {
if let Ok(event) = CollateralUpgradeable::ReclaimProcessStarted::decode_log(&log.inner)
{
reclaim_id = event.reclaimRequestId;
}
});
tokio::time::sleep(std::time::Duration::from_secs(30)).await;
// Test denyReclaimRequest (trustee only)
let finalize_tx = contract.finalizeReclaim(reclaim_id);
let finalize_receipt = finalize_tx.send().await?.get_receipt().await?;
let mut finalize_found = false;
// Check for Denied event
finalize_receipt.logs().iter().for_each(|log| {
if let Ok(event) = CollateralUpgradeable::Reclaimed::decode_log(&log.inner) {
assert_eq!(event.reclaimRequestId, reclaim_id);
finalize_found = true;
}
});
assert!(finalize_found);
Ok(())
}
#[tokio::test]
#[ignore]
// cargo test --package basilica-miner --test mod -- tests::test_reclaim_deny --exact --nocapture
// to test deny, need set the decision timeout to a bigger number
async fn test_reclaim_deny() -> anyhow::Result<()> {
let contract = get_contract().await?;
let node_id: u128 = rand::thread_rng().gen_range(0..10000000000);
let hotkey: [u8; 32] = [1u8; 32];
let amount = U256::from(10);
let alpha_hotkey: [u8; 32] = [2u8; 32];
let deposit_tx = contract
.deposit(
FixedBytes::from_slice(&hotkey),
FixedBytes::from_slice(&node_id.to_be_bytes()),
FixedBytes::from_slice(&alpha_hotkey),
amount,
)
.value(U256::ZERO);
let _deposit_tx_receipt = deposit_tx.send().await?.get_receipt().await?;
// Start reclaim process
let url = "example.com";
let url_checksum = 123_u128;
let reclaim_tx = contract.reclaimCollateral(
FixedBytes::from_slice(&hotkey),
FixedBytes::from_slice(&node_id.to_be_bytes()),
url.to_owned(),
FixedBytes::from_slice(&url_checksum.to_be_bytes()),
);
let reclaim_receipt = reclaim_tx.send().await?.get_receipt().await?;
let mut reclaim_id = U256::from(0);
reclaim_receipt.logs().iter().for_each(|log| {
if let Ok(event) = CollateralUpgradeable::ReclaimProcessStarted::decode_log(&log.inner)
{
reclaim_id = event.reclaimRequestId;
}
});
// Test denyReclaimRequest (trustee only)
let deny_tx = contract.denyReclaimRequest(
reclaim_id,
url.to_owned(),
FixedBytes::from_slice(&url_checksum.to_be_bytes()),
);
let deny_receipt = deny_tx.send().await?.get_receipt().await?;
// Check for Denied event
let mut denied_found = false;
deny_receipt.logs().iter().for_each(|log| {
if let Ok(event) = CollateralUpgradeable::Denied::decode_log(&log.inner) {
assert_eq!(event.reclaimRequestId, reclaim_id);
denied_found = true;
}
});
assert!(denied_found);
Ok(())
}
#[tokio::test]
#[ignore]
// cargo test --package basilica-miner --test mod -- tests::test_slash --exact --nocapture
async fn test_slash() -> anyhow::Result<()> {
let contract = get_contract().await?;
let node_id: u128 = rand::thread_rng().gen_range(0..10000000000);
let hotkey: [u8; 32] = [1u8; 32];
let amount = U256::from(10);
let alpha_hotkey: [u8; 32] = [2u8; 32];
let deposit_tx = contract
.deposit(
FixedBytes::from_slice(&hotkey),
FixedBytes::from_slice(&node_id.to_be_bytes()),
FixedBytes::from_slice(&alpha_hotkey),
amount,
)
.value(U256::ZERO);
let _deposit_tx_receipt = deposit_tx.send().await?.get_receipt().await?;
// Start reclaim process
let url = "example.com";
let url_checksum = 123_u128;
let slash_tx = contract.slashCollateral(
FixedBytes::from_slice(&hotkey),
FixedBytes::from_slice(&node_id.to_be_bytes()),
U256::ZERO,
amount,
url.to_owned(),
FixedBytes::from_slice(&url_checksum.to_be_bytes()),
);
let slash_receipt = slash_tx.send().await?.get_receipt().await?;
slash_receipt.logs().iter().for_each(|log| {
if let Ok(event) = CollateralUpgradeable::Slashed::decode_log(&log.inner) {
assert_eq!(event.nodeId, FixedBytes::from(node_id));
}
});
Ok(())
}
}