Skip to content

Commit 17cf4cc

Browse files
committed
program: extract the rest of the offchain API
1 parent ea3bef2 commit 17cf4cc

File tree

9 files changed

+38
-27
lines changed

9 files changed

+38
-27
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

program/src/instruction.rs renamed to clients/rust/src/instruction_helpers.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
//! Program instruction helpers.
22
33
use {
4-
crate::{
5-
id,
6-
state::{ConfigKeys, ConfigState},
7-
},
4+
crate::{ConfigKeys, ShortVec, ID},
85
bincode::serialized_size,
96
solana_program::{
107
instruction::{AccountMeta, Instruction},
@@ -13,10 +10,21 @@ use {
1310
},
1411
};
1512

13+
/// Trait defining config state to be stored at the end of the account data.
14+
pub trait ConfigState: serde::Serialize + Default {
15+
/// Maximum space that the serialized representation will require
16+
fn max_space() -> u64;
17+
}
18+
1619
fn initialize_account<T: ConfigState>(config_pubkey: &Pubkey) -> Instruction {
1720
let account_metas = vec![AccountMeta::new(*config_pubkey, true)];
18-
let account_data = (ConfigKeys { keys: vec![] }, T::default());
19-
Instruction::new_with_bincode(id(), &account_data, account_metas)
21+
let account_data = (
22+
ConfigKeys {
23+
keys: ShortVec(vec![]),
24+
},
25+
T::default(),
26+
);
27+
Instruction::new_with_bincode(ID, &account_data, account_metas)
2028
}
2129

2230
/// Create a new, empty configuration account
@@ -26,14 +34,19 @@ pub fn create_account<T: ConfigState>(
2634
lamports: u64,
2735
keys: Vec<(Pubkey, bool)>,
2836
) -> Vec<Instruction> {
29-
let space = T::max_space().saturating_add(serialized_size(&ConfigKeys { keys }).unwrap());
37+
let space = T::max_space().saturating_add(
38+
serialized_size(&ConfigKeys {
39+
keys: ShortVec(keys),
40+
})
41+
.unwrap(),
42+
);
3043
vec![
3144
system_instruction::create_account(
3245
from_account_pubkey,
3346
config_account_pubkey,
3447
lamports,
3548
space,
36-
&id(),
49+
&ID,
3750
),
3851
initialize_account::<T>(config_account_pubkey),
3952
]
@@ -52,6 +65,11 @@ pub fn store<T: ConfigState>(
5265
account_metas.push(AccountMeta::new(*signer_pubkey, true));
5366
}
5467
}
55-
let account_data = (ConfigKeys { keys }, data);
56-
Instruction::new_with_bincode(id(), &account_data, account_metas)
68+
let account_data = (
69+
ConfigKeys {
70+
keys: ShortVec(keys),
71+
},
72+
data,
73+
);
74+
Instruction::new_with_bincode(ID, &account_data, account_metas)
5775
}

clients/rust/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
mod generated;
22
mod hooked;
33

4+
#[cfg(feature = "serde")]
5+
pub mod instruction_helpers;
6+
47
pub use {
58
generated::{programs::SOLANA_CONFIG_ID as ID, *},
69
hooked::*,

program/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ thiserror = { workspace = true }
2626
[dev-dependencies]
2727
mollusk-svm = { workspace = true, features = ["fuzz-fd"] }
2828
mollusk-svm-bencher = { workspace = true }
29+
solana-config-program-client = { path = "../clients/rust", features = ["serde"] }
2930
solana-sdk = { workspace = true }
3031

3132
[lib]

program/benches/setup.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use {
22
mollusk_svm_bencher::Bench,
33
serde::Serialize,
4-
solana_config_program::{
5-
instruction::store,
6-
state::{ConfigKeys, ConfigState},
7-
},
4+
solana_config_program::state::ConfigKeys,
5+
solana_config_program_client::instruction_helpers::{store, ConfigState},
86
solana_sdk::{
97
account::AccountSharedData,
108
hash::Hash,

program/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#[cfg(all(target_os = "solana", feature = "bpf-entrypoint"))]
44
mod entrypoint;
55
pub mod error;
6-
pub mod instruction;
76
pub mod processor;
87
pub mod state;
98

program/src/state.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ use {
55
solana_program::{pubkey::Pubkey, short_vec},
66
};
77

8-
/// Trait defining config state to be stored at the end of the account data.
9-
pub trait ConfigState: serde::Serialize + Default {
10-
/// Maximum space that the serialized representation will require
11-
fn max_space() -> u64;
12-
}
13-
148
/// A collection of keys to be stored in Config account data.
159
#[derive(Debug, Default, Deserialize, Serialize)]
1610
pub struct ConfigKeys {

program/tests/functional.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
#![cfg(feature = "test-sbf")]
2+
#![allow(clippy::arithmetic_side_effects)]
23

34
use {
45
bincode::serialized_size,
56
mollusk_svm::{result::Check, Mollusk},
67
serde::{Deserialize, Serialize},
7-
solana_config_program::{
8-
error::ConfigError,
9-
instruction as config_instruction,
10-
state::{ConfigKeys, ConfigState},
11-
},
8+
solana_config_program::{error::ConfigError, state::ConfigKeys},
9+
solana_config_program_client::instruction_helpers::{self as config_instruction, ConfigState},
1210
solana_sdk::{
1311
account::AccountSharedData,
1412
instruction::{AccountMeta, Instruction},

scripts/program/lint.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ const lintArgs = [
1616
'--all-features',
1717
'--',
1818
'--deny=warnings',
19-
'--deny=clippy::arithmetic_side_effects',
2019
...cliArguments()
2120
];
2221

0 commit comments

Comments
 (0)