Skip to content

Commit eee8619

Browse files
smartcontract: add Index PDA derivation to Rust SDK commands
Update Rust SDK multicast group commands to derive and pass Index PDAs for create, update, get, delete, and deactivate operations. Add Index command wrappers for standalone create/delete. Update allowlist commands with Index account support.
1 parent f95f302 commit eee8619

13 files changed

Lines changed: 412 additions & 63 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use crate::{commands::globalstate::get::GetGlobalStateCommand, DoubleZeroClient};
2+
use doublezero_program_common::validate_account_code;
3+
use doublezero_serviceability::{
4+
instructions::DoubleZeroInstruction, pda::get_index_pda,
5+
processors::index::create::IndexCreateArgs,
6+
};
7+
use solana_sdk::{instruction::AccountMeta, pubkey::Pubkey, signature::Signature};
8+
9+
#[derive(Debug, PartialEq, Clone)]
10+
pub struct CreateIndexCommand {
11+
pub entity_seed: String,
12+
pub code: String,
13+
pub entity_pubkey: Pubkey,
14+
}
15+
16+
impl CreateIndexCommand {
17+
pub fn execute(&self, client: &dyn DoubleZeroClient) -> eyre::Result<(Signature, Pubkey)> {
18+
let code =
19+
validate_account_code(&self.code).map_err(|err| eyre::eyre!("invalid code: {err}"))?;
20+
21+
let (globalstate_pubkey, _) = GetGlobalStateCommand
22+
.execute(client)
23+
.map_err(|_err| eyre::eyre!("Globalstate not initialized"))?;
24+
25+
let (index_pda, _) =
26+
get_index_pda(&client.get_program_id(), self.entity_seed.as_bytes(), &code);
27+
28+
let accounts = vec![
29+
AccountMeta::new(index_pda, false),
30+
AccountMeta::new_readonly(self.entity_pubkey, false),
31+
AccountMeta::new_readonly(globalstate_pubkey, false),
32+
];
33+
34+
client
35+
.execute_transaction(
36+
DoubleZeroInstruction::CreateIndex(IndexCreateArgs {
37+
entity_seed: self.entity_seed.clone(),
38+
code,
39+
}),
40+
accounts,
41+
)
42+
.map(|sig| (sig, index_pda))
43+
}
44+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::{commands::globalstate::get::GetGlobalStateCommand, DoubleZeroClient};
2+
use doublezero_serviceability::{
3+
instructions::DoubleZeroInstruction, processors::index::delete::IndexDeleteArgs,
4+
};
5+
use solana_sdk::{instruction::AccountMeta, pubkey::Pubkey, signature::Signature};
6+
7+
#[derive(Debug, PartialEq, Clone)]
8+
pub struct DeleteIndexCommand {
9+
pub index_pubkey: Pubkey,
10+
}
11+
12+
impl DeleteIndexCommand {
13+
pub fn execute(&self, client: &dyn DoubleZeroClient) -> eyre::Result<Signature> {
14+
let (globalstate_pubkey, _) = GetGlobalStateCommand
15+
.execute(client)
16+
.map_err(|_err| eyre::eyre!("Globalstate not initialized"))?;
17+
18+
let accounts = vec![
19+
AccountMeta::new(self.index_pubkey, false),
20+
AccountMeta::new_readonly(globalstate_pubkey, false),
21+
];
22+
23+
client.execute_transaction(
24+
DoubleZeroInstruction::DeleteIndex(IndexDeleteArgs {}),
25+
accounts,
26+
)
27+
}
28+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod create;
2+
pub mod delete;

smartcontract/sdk/rs/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub mod device;
55
pub mod exchange;
66
pub mod globalconfig;
77
pub mod globalstate;
8+
pub mod index;
89
pub mod link;
910
pub mod location;
1011
pub mod migrate;

smartcontract/sdk/rs/src/commands/multicastgroup/allowlist/publisher/add.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ mod tests {
9393
map.insert(pubkey, AccountData::MulticastGroup(cloned_mgroup.clone()));
9494
Ok(map)
9595
});
96+
// Catch-all for Index PDA lookups
97+
client
98+
.expect_get()
99+
.with(predicate::always())
100+
.returning(|_| Err(eyre::eyre!("Account not found")));
96101
client
97102
.expect_execute_transaction()
98103
.with(

smartcontract/sdk/rs/src/commands/multicastgroup/allowlist/publisher/remove.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ mod tests {
9595
map.insert(pubkey, AccountData::MulticastGroup(cloned_mgroup.clone()));
9696
Ok(map)
9797
});
98+
// Catch-all for Index PDA lookups
99+
client
100+
.expect_get()
101+
.with(predicate::always())
102+
.returning(|_| Err(eyre::eyre!("Account not found")));
98103
client
99104
.expect_execute_transaction()
100105
.with(

smartcontract/sdk/rs/src/commands/multicastgroup/allowlist/subscriber/add.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ mod tests {
9595
map.insert(pubkey, AccountData::MulticastGroup(cloned_mgroup.clone()));
9696
Ok(map)
9797
});
98+
// Catch-all for Index PDA lookups
99+
client
100+
.expect_get()
101+
.with(predicate::always())
102+
.returning(|_| Err(eyre::eyre!("Account not found")));
98103
client
99104
.expect_execute_transaction()
100105
.with(

smartcontract/sdk/rs/src/commands/multicastgroup/allowlist/subscriber/remove.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ mod tests {
9595
map.insert(pubkey, AccountData::MulticastGroup(cloned_mgroup.clone()));
9696
Ok(map)
9797
});
98+
// Catch-all for Index PDA lookups
99+
client
100+
.expect_get()
101+
.with(predicate::always())
102+
.returning(|_| Err(eyre::eyre!("Account not found")));
98103
client
99104
.expect_execute_transaction()
100105
.with(

smartcontract/sdk/rs/src/commands/multicastgroup/create.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use doublezero_program_common::validate_account_code;
22
use doublezero_serviceability::{
33
instructions::DoubleZeroInstruction,
4-
pda::{get_multicastgroup_pda, get_resource_extension_pda},
4+
pda::{get_index_pda, get_multicastgroup_pda, get_resource_extension_pda},
55
processors::multicastgroup::create::MulticastGroupCreateArgs,
66
resource::ResourceType,
7+
seeds::SEED_MULTICAST_GROUP,
78
state::feature_flags::{is_feature_enabled, FeatureFlag},
89
};
910
use solana_sdk::{instruction::AccountMeta, pubkey::Pubkey, signature::Signature};
@@ -45,6 +46,10 @@ impl CreateMulticastGroupCommand {
4546
accounts.push(AccountMeta::new(multicast_group_block_ext, false));
4647
}
4748

49+
// Index account (payer and system_program are appended by the framework)
50+
let (index_pda, _) = get_index_pda(&client.get_program_id(), SEED_MULTICAST_GROUP, &code);
51+
accounts.push(AccountMeta::new(index_pda, false));
52+
4853
client
4954
.execute_transaction(
5055
DoubleZeroInstruction::CreateMulticastGroup(MulticastGroupCreateArgs {
@@ -67,9 +72,12 @@ mod tests {
6772
};
6873
use doublezero_serviceability::{
6974
instructions::DoubleZeroInstruction,
70-
pda::{get_globalstate_pda, get_multicastgroup_pda, get_resource_extension_pda},
75+
pda::{
76+
get_globalstate_pda, get_index_pda, get_multicastgroup_pda, get_resource_extension_pda,
77+
},
7178
processors::multicastgroup::create::MulticastGroupCreateArgs,
7279
resource::ResourceType,
80+
seeds::SEED_MULTICAST_GROUP,
7381
state::{
7482
accountdata::AccountData, accounttype::AccountType, feature_flags::FeatureFlag,
7583
globalstate::GlobalState,
@@ -84,6 +92,8 @@ mod tests {
8492

8593
let (globalstate_pubkey, _globalstate) = get_globalstate_pda(&client.get_program_id());
8694
let (pda_pubkey, _) = get_multicastgroup_pda(&client.get_program_id(), 1);
95+
let (index_pda, _) =
96+
get_index_pda(&client.get_program_id(), SEED_MULTICAST_GROUP, "test_group");
8797

8898
client
8999
.expect_execute_transaction()
@@ -99,6 +109,7 @@ mod tests {
99109
predicate::eq(vec![
100110
AccountMeta::new(pda_pubkey, false),
101111
AccountMeta::new(globalstate_pubkey, false),
112+
AccountMeta::new(index_pda, false),
102113
]),
103114
)
104115
.returning(|_, _| Ok(Signature::new_unique()));
@@ -155,6 +166,7 @@ mod tests {
155166
let (pda_pubkey, _) = get_multicastgroup_pda(&program_id, 1);
156167
let (multicast_group_block_ext, _, _) =
157168
get_resource_extension_pda(&program_id, ResourceType::MulticastGroupBlock);
169+
let (index_pda, _) = get_index_pda(&program_id, SEED_MULTICAST_GROUP, "test_group");
158170

159171
let owner = Pubkey::new_unique();
160172
client
@@ -172,6 +184,7 @@ mod tests {
172184
AccountMeta::new(pda_pubkey, false),
173185
AccountMeta::new(globalstate_pubkey, false),
174186
AccountMeta::new(multicast_group_block_ext, false),
187+
AccountMeta::new(index_pda, false),
175188
]),
176189
)
177190
.returning(|_, _| Ok(Signature::new_unique()));

0 commit comments

Comments
 (0)