From 3c6e33d65a8559bd89919e53c53f4ed831bbf62b Mon Sep 17 00:00:00 2001 From: Mike Rostecki Date: Tue, 6 Jun 2023 02:40:09 +0200 Subject: [PATCH] Update Solana SDK to 1.16 and Anchor to the latest change including it The newest solana-program crate was pulled unintentionally anyway which resulted in unexpected errors due to incompatibility of dependencies. More context: coral-xyz/anchor#2512 --- light-merkle-tree/Cargo.toml | 3 ++- light-merkle-tree/src/lib.rs | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/light-merkle-tree/Cargo.toml b/light-merkle-tree/Cargo.toml index f489b2d..3bb2ee7 100644 --- a/light-merkle-tree/Cargo.toml +++ b/light-merkle-tree/Cargo.toml @@ -6,7 +6,8 @@ description = "Sparse Merkle tree implementation" license = "Apache-2.0" [dependencies] -anchor-lang = "0.26" +anchor-lang = { git = "https://github.com/coral-xyz/anchor", rev = "1c6f86e5f7793ce6adefe9cbfa11939647c509ce" } +bytemuck = "1.13.1" [dev-dependencies] sha2 = "0.10" diff --git a/light-merkle-tree/src/lib.rs b/light-merkle-tree/src/lib.rs index 60310bc..2df5eff 100644 --- a/light-merkle-tree/src/lib.rs +++ b/light-merkle-tree/src/lib.rs @@ -3,6 +3,7 @@ use std::marker::PhantomData; #[cfg(feature = "solana")] use anchor_lang::prelude::*; +use bytemuck::{Pod, Zeroable}; use config::MerkleTreeConfig; use hasher::{Hash, Hasher}; @@ -26,6 +27,7 @@ pub enum HashFunction { // generics when generating IDL. #[cfg_attr(feature = "solana", derive(AnchorSerialize, AnchorDeserialize))] #[derive(PartialEq, Eq, Debug, Clone, Copy)] +#[repr(C)] pub struct MerkleTree where H: Hasher, @@ -153,6 +155,41 @@ where } } +/// The [`Pod`](bytemuck::Pod) trait is used under the hood by the +/// [`zero_copy`](anchor_lang::zero_copy) attribute macro and is required for +/// usage in zero-copy Solana accounts. +/// +/// SAFETY: Generic parameters are used only as `PhantomData` and they don't +/// affect the layout of the struct nor its size or padding. The only reason +/// why we can't `#[derive(Pod)]` is because bytemuck is not aware of that and +/// it doesn't allow to derive `Pod` for structs with generic parameters. +/// Would be nice to fix that upstream: +/// https://github.com/Lokathor/bytemuck/issues/191 +unsafe impl Pod for MerkleTree +where + H: Hasher + Copy + 'static, + C: MerkleTreeConfig + Copy + 'static, +{ +} + +/// The [`Zeroable`](bytemuck::Zeroable) trait is used under the hood by the +/// [`zero_copy`](anchor_lang::zero_copy) attribute macro and is required for +/// usage in zero-copy Solana accounts. +/// +/// SAFETY: Generic parameters are used only as `PhantomData` and they don't +/// affect the layout of the struct nor its size or padding. The only reason +/// why we can't `#[derive(Zeroable)]` is because bytemuck is not aware of that +/// and it doesn't allow to derive `Zeroable` for structs with generic +/// parameters. +/// Would be nice to fix that upstream: +/// https://github.com/Lokathor/bytemuck/issues/191 +unsafe impl Zeroable for MerkleTree +where + H: Hasher, + C: MerkleTreeConfig, +{ +} + #[cfg(feature = "solana")] impl Owner for MerkleTree where