Skip to content

Commit acbdf95

Browse files
committed
Add sudo address, and test for important address only alerts
1 parent c1656ce commit acbdf95

File tree

2 files changed

+89
-11
lines changed

2 files changed

+89
-11
lines changed

chain-alerter/src/alerts/tests.rs

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use subxt::utils::H256;
2121
/// <https://github.com/autonomys/subspace/releases/tag/runtime-mainnet-2025-jul-31>
2222
///
2323
/// TODO: turn this into a struct
24-
const SUDO_BLOCK: (BlockNumber, RawBlockHash, ExtrinsicIndex, EventIndex, Slot) = (
24+
const SUDO_EXTRINSIC_BLOCK: (BlockNumber, RawBlockHash, ExtrinsicIndex, EventIndex, Slot) = (
2525
3_795_487,
2626
hex_literal::hex!("18c2f211b752cbc2f06943788ed011ab1fe64fb2e28ffcd1aeb4490c2e8b1baa"),
2727
5,
@@ -68,7 +68,7 @@ const LARGE_TRANSFER_BLOCKS: [(
6868
clippy::type_complexity,
6969
reason = "this is a test, TODO: turn this into a struct"
7070
)]
71-
const IMPORTANT_ADDRESS_BLOCKS: [(
71+
const IMPORTANT_ADDRESS_TRANSFER_BLOCKS: [(
7272
BlockNumber,
7373
RawBlockHash,
7474
ExtrinsicIndex,
@@ -134,6 +134,21 @@ const IMPORTANT_ADDRESS_BLOCKS: [(
134134
),
135135
];
136136

137+
/// Some extrinsics for important address alerts (which aren't any other higher
138+
/// priority alert).
139+
/// TODO: find an event sent by an important address, that isn't a higher priority alert.
140+
const IMPORTANT_ADDRESS_ONLY_BLOCKS: [(BlockNumber, RawBlockHash, ExtrinsicIndex, &str, Slot); 1] = [
141+
// <https://autonomys.subscan.io/account/subKQqsYRyVkugvKQqLXEuhsefa9728PBAqtwxpeM5N4VD6mv>
142+
(
143+
3_497_809,
144+
hex_literal::hex!("bfa548573d1ff035e2009fdaa68499fe74c4ab30a775f5eb35624fdb9f95dc91"),
145+
// <https://autonomys.subscan.io/extrinsic/3497809-9>
146+
9,
147+
"Sudo",
148+
Slot(21_070_789),
149+
),
150+
];
151+
137152
// TODO: force transfer blocks:
138153
// Failed force_transfer: <https://autonomys.subscan.io/extrinsic/2173351-7>
139154
// Failed force_set_balance: <https://autonomys.subscan.io/extrinsic/1154587-11>
@@ -177,11 +192,15 @@ async fn test_sudo_alerts() -> anyhow::Result<()> {
177192
let (subspace_client, _, alert_tx, mut alert_rx, update_task) =
178193
test_setup(node_rpc_url()).await?;
179194

180-
let (block_info, extrinsics, events) =
181-
fetch_block_info(&subspace_client, H256::from(SUDO_BLOCK.1), SUDO_BLOCK.0).await?;
195+
let (block_info, extrinsics, events) = fetch_block_info(
196+
&subspace_client,
197+
H256::from(SUDO_EXTRINSIC_BLOCK.1),
198+
SUDO_EXTRINSIC_BLOCK.0,
199+
)
200+
.await?;
182201

183202
let (extrinsic, extrinsic_info) =
184-
decode_extrinsic(&block_info, &extrinsics, SUDO_BLOCK.2).await?;
203+
decode_extrinsic(&block_info, &extrinsics, SUDO_EXTRINSIC_BLOCK.2).await?;
185204

186205
alerts::check_extrinsic(BlockCheckMode::Replay, &alert_tx, &extrinsic, &block_info).await?;
187206
let alert = alert_rx.try_recv().expect("no alert received");
@@ -201,7 +220,7 @@ async fn test_sudo_alerts() -> anyhow::Result<()> {
201220
Some(("Sudo", "sudo"))
202221
);
203222

204-
let (event, event_info) = decode_event(&block_info, &events, SUDO_BLOCK.3).await?;
223+
let (event, event_info) = decode_event(&block_info, &events, SUDO_EXTRINSIC_BLOCK.3).await?;
205224

206225
alerts::check_event(BlockCheckMode::Replay, &alert_tx, &event, &block_info).await?;
207226
let alert = alert_rx.try_recv().expect("no alert received");
@@ -226,7 +245,7 @@ async fn test_sudo_alerts() -> anyhow::Result<()> {
226245
.expect_err("alert received when none expected");
227246

228247
// Check block slot parsing works on a known slot value.
229-
assert_eq!(alert.block_info.slot, Some(SUDO_BLOCK.4));
248+
assert_eq!(alert.block_info.slot, Some(SUDO_EXTRINSIC_BLOCK.4));
230249

231250
let result = update_task.now_or_never();
232251
assert!(
@@ -303,9 +322,9 @@ async fn test_large_balance_transfer_alerts() -> anyhow::Result<()> {
303322
Ok(())
304323
}
305324

306-
/// Check that the important address alert works on known important address blocks.
325+
/// Check that important address transfer alerts work on known important address transfer blocks.
307326
#[tokio::test(flavor = "multi_thread")]
308-
async fn test_important_address_alerts() -> anyhow::Result<()> {
327+
async fn test_important_address_transfer_alerts() -> anyhow::Result<()> {
309328
let (subspace_client, _, alert_tx, mut alert_rx, update_task) =
310329
test_setup(node_rpc_url()).await?;
311330

@@ -318,7 +337,7 @@ async fn test_important_address_alerts() -> anyhow::Result<()> {
318337
extrinsic_transfer_value,
319338
event_transfer_value,
320339
slot,
321-
) in IMPORTANT_ADDRESS_BLOCKS
340+
) in IMPORTANT_ADDRESS_TRANSFER_BLOCKS
322341
{
323342
let (block_info, extrinsics, events) =
324343
fetch_block_info(&subspace_client, H256::from(block_hash), block_number).await?;
@@ -410,6 +429,53 @@ async fn test_important_address_alerts() -> anyhow::Result<()> {
410429
Ok(())
411430
}
412431

432+
/// Check that the important address alert works on known important address blocks.
433+
#[tokio::test(flavor = "multi_thread")]
434+
async fn test_important_address_only_alerts() -> anyhow::Result<()> {
435+
let (subspace_client, _, alert_tx, mut alert_rx, update_task) =
436+
test_setup(node_rpc_url()).await?;
437+
438+
for (block_number, block_hash, extrinsic_index, address_kind, slot) in
439+
IMPORTANT_ADDRESS_ONLY_BLOCKS
440+
{
441+
let (block_info, extrinsics, _events) =
442+
fetch_block_info(&subspace_client, H256::from(block_hash), block_number).await?;
443+
444+
let (extrinsic, extrinsic_info) =
445+
decode_extrinsic(&block_info, &extrinsics, extrinsic_index).await?;
446+
447+
alerts::check_extrinsic(BlockCheckMode::Replay, &alert_tx, &extrinsic, &block_info).await?;
448+
let alert = alert_rx.try_recv().expect("no extrinsic alert received");
449+
450+
assert_eq!(
451+
alert,
452+
Alert::new(
453+
AlertKind::ImportantAddressExtrinsic {
454+
address_kind: address_kind.to_string(),
455+
extrinsic_info: extrinsic_info.clone(),
456+
},
457+
block_info,
458+
BlockCheckMode::Replay,
459+
)
460+
);
461+
462+
alert_rx
463+
.try_recv()
464+
.expect_err("alert received when none expected");
465+
466+
// Check block slot parsing works on a known slot value.
467+
assert_eq!(alert.block_info.slot, Some(slot));
468+
}
469+
470+
let result = update_task.now_or_never();
471+
assert!(
472+
result.is_none(),
473+
"metadata update task exited unexpectedly with: {result:?}"
474+
);
475+
476+
Ok(())
477+
}
478+
413479
/// Check that the slot time alert is not triggered when the time per slot is below the threshold.
414480
#[tokio::test(flavor = "multi_thread")]
415481
async fn no_expected_test_slot_time_alert() -> anyhow::Result<()> {

chain-alerter/src/alerts/transfer.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ pub const IMPORTANT_ADDRESSES: &[(&str, &str)] = &[
6262
"Guardians of Growth",
6363
"sugQzjjyAfhzktFDdAkZrcTq5qzMaRoSV2qs1gTcjjuBeybWT",
6464
),
65+
// <https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc-0.mainnet.autonomys.xyz%2Fws#/chainstate>
66+
// sudo.key()
67+
// <https://autonomys.subscan.io/account/subKQqsYRyVkugvKQqLXEuhsefa9728PBAqtwxpeM5N4VD6mv>
68+
// TODO: dynamically look this up from storage instead of hardcoding it
69+
("Sudo", "subKQqsYRyVkugvKQqLXEuhsefa9728PBAqtwxpeM5N4VD6mv"),
6570
];
6671

6772
/// If the address is an important address, returns the kind of important address, otherwise returns
@@ -72,7 +77,14 @@ pub fn important_address_kind(address: &Account) -> Option<&'static str> {
7277
IMPORTANT_ADDRESSES
7378
.iter()
7479
.find(|(_, addr)| {
75-
let addr_id = AccountId32::from_str(addr).expect("constants are valid addresses");
80+
let addr_id = if let Ok(account) = AccountId32::from_str(addr) {
81+
account
82+
} else {
83+
let bytes = hex::decode(addr).expect("constants are valid ss58check or hex");
84+
let array = <[u8; 32]>::try_from(bytes).expect("hex constants are 32 bytes");
85+
AccountId32::from(array)
86+
};
87+
7688
trace!(?addr_id, ?account_id, "important address kind check");
7789
&addr_id == account_id
7890
})

0 commit comments

Comments
 (0)