Skip to content

Commit

Permalink
Support u8 slice digester in frozen abi struct. (#23726)
Browse files Browse the repository at this point in the history
* support u8 slice in frozen abi digester

* use slice in account struct

* add bpf cargo lock file

* no need to pass account.data to serializer

* fix comments
  • Loading branch information
HaoranYi authored Mar 18, 2022
1 parent c694703 commit f54e746
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frozen-abi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ lazy_static = "1.4.0"
log = "0.4.14"
serde = "1.0.136"
serde_derive = "1.0.103"
serde_bytes = "0.11"
sha2 = "0.10.2"
solana-frozen-abi-macro = { path = "macro", version = "=1.10.4" }
thiserror = "1.0"
Expand Down
11 changes: 11 additions & 0 deletions frozen-abi/src/abi_digester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,17 @@ mod tests {
#[derive(Serialize, AbiExample)]
struct TestNewtypeStruct(i8);

#[frozen_abi(digest = "Hbs1X2X7TF2gFEfsspwfZ1JKr8ZGbLY3uidQBebqcMYt")]
#[derive(Serialize, AbiExample)]
struct Foo<'a> {
#[serde(with = "serde_bytes")]
data1: Vec<u8>,
#[serde(with = "serde_bytes")]
data2: &'a [u8],
#[serde(with = "serde_bytes")]
data3: &'a Vec<u8>,
}

#[frozen_abi(digest = "5qio5qYurHDv6fq5kcwP2ue2RBEazSZF8CPk2kUuwC2j")]
#[derive(Serialize, AbiExample)]
struct TestStructReversed {
Expand Down
7 changes: 7 additions & 0 deletions frozen-abi/src/abi_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,13 @@ impl AbiExample for &Vec<u8> {
}
}

impl AbiExample for &[u8] {
fn example() -> Self {
info!("AbiExample for (&[u8]): {}", type_name::<Self>());
&VEC_U8[..]
}
}

impl<T: AbiExample> AbiExample for VecDeque<T> {
fn example() -> Self {
info!("AbiExample for (Vec<T>): {}", type_name::<Self>());
Expand Down
1 change: 1 addition & 0 deletions programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions sdk/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ mod account_serialize {
struct Account<'a> {
lamports: u64,
#[serde(with = "serde_bytes")]
// a ref so we don't have to make a copy just to serialize this
data: &'a Vec<u8>,
// a slice so we don't have to make a copy just to serialize this
data: &'a [u8],
// can't be &pubkey because abi example doesn't support it
owner: Pubkey,
executable: bool,
Expand All @@ -57,16 +57,14 @@ mod account_serialize {
/// allows us to implement serialize on AccountSharedData that is equivalent to Account::serialize without making a copy of the Vec<u8>
pub fn serialize_account<S>(
account: &(impl ReadableAccount + Serialize),
data: &Vec<u8>,
serializer: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let temp = Account {
lamports: account.lamports(),
// note this is a ref, which is the whole point of 'account_serialize'
data,
data: account.data(),
owner: *account.owner(),
executable: account.executable(),
rent_epoch: account.rent_epoch(),
Expand All @@ -80,7 +78,7 @@ impl Serialize for Account {
where
S: Serializer,
{
crate::account::account_serialize::serialize_account(self, &self.data, serializer)
crate::account::account_serialize::serialize_account(self, serializer)
}
}

Expand All @@ -89,7 +87,7 @@ impl Serialize for AccountSharedData {
where
S: Serializer,
{
crate::account::account_serialize::serialize_account(self, &self.data, serializer)
crate::account::account_serialize::serialize_account(self, serializer)
}
}

Expand Down

0 comments on commit f54e746

Please sign in to comment.