Skip to content

Commit 6c724e7

Browse files
committed
pull upstream
2 parents 02aaf01 + c054024 commit 6c724e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2248
-1698
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ packx = { version = "0.2.5", features = ["solana"] }
2525
bytemuck = "1.14.3"
2626
bytemuck_derive = "1.7.0"
2727
num_enum = "0.7.2"
28-
brine-tree = { version = "0.5.4", features = ["solana"] }
28+
brine-tree = { version = "0.6.2", features = ["solana"] }
2929
array-const-fn-init = "0.1.1"
3030
const-crypto = "0.3.0"
3131

@@ -48,7 +48,7 @@ spl-associated-token-account = { version = "^6", features = [ "no-entrypoint" ]
4848
# client-specific
4949
anyhow = "1.0"
5050
flate2 = "1.0"
51-
sha3 = "0.10.8"
51+
blake3 = "1.8.2"
5252
bincode = "1.3"
5353
base64 = "0.13"
5454
serde = { version = "1.0", features = ["derive"] }

api/src/consts.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ pub const TAPE_PROOF_LEN: usize = TAPE_TREE_HEIGHT;
4545
// ====================================================================
4646
/// Segment size in bytes
4747
pub const SEGMENT_SIZE: usize = 128;
48+
/// Packed Segment size in bytes
49+
pub const PACKED_SEGMENT_SIZE: usize = 152; // packx::SOLUTION_SIZE
50+
4851
/// Maximum number of segments in a tape
4952
pub const MAX_SEGMENTS_PER_TAPE: usize = 1 << SEGMENT_TREE_HEIGHT - 1;
5053
/// Maximum number of tapes in a spool
5154
pub const MAX_TAPES_PER_SPOOL: usize = 1 << TAPE_TREE_HEIGHT - 1;
52-
/// Packed Segment size in bytes
53-
pub const PACKED_SEGMENT_SIZE: usize = packx::SOLUTION_SIZE;
5455

5556
// ====================================================================
5657
// Token Economics
@@ -154,3 +155,13 @@ pub const TREASURY_ATA: Pubkey = Pubkey::new_from_array(
154155
.0,
155156
);
156157

158+
pub const TREASURY_ATA_BUMP: u8 =
159+
ed25519::derive_program_address(
160+
&[
161+
unsafe { &*(&TREASURY_ADDRESS as *const Pubkey as *const [u8; 32]) },
162+
unsafe { &*(&spl_token::id() as *const Pubkey as *const [u8; 32]) },
163+
unsafe { &*(&MINT_ADDRESS as *const Pubkey as *const [u8; 32]) },
164+
],
165+
unsafe { &*(&spl_associated_token_account::id() as *const Pubkey as *const [u8; 32]) },
166+
)
167+
.1;

api/src/instruction/spool.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use steel::*;
22
use crate::{
33
consts::*,
4-
pda::*
4+
pda::*,
5+
types::*,
56
};
67

78
#[repr(u8)]
@@ -48,7 +49,7 @@ pub struct Unpack {
4849
#[derive(Clone, Copy, Debug, Pod, Zeroable)]
4950
pub struct Commit {
5051
pub index: [u8; 8],
51-
pub proof: [[u8; 32]; SEGMENT_PROOF_LEN],
52+
pub proof: ProofPath,
5253
pub value: [u8; 32],
5354
}
5455

@@ -139,7 +140,7 @@ pub fn build_commit_ix(
139140
miner_address: Pubkey,
140141
spool_address: Pubkey,
141142
index: u64, // index of the value to commit
142-
proof: [[u8; 32]; SEGMENT_PROOF_LEN], // proof of the value
143+
proof: ProofPath, // proof of the value
143144
value: [u8; 32], // value to commit
144145
) -> Instruction {
145146
Instruction {

api/src/instruction/tape.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use steel::*;
22
use crate::{
33
consts::*,
44
pda::*,
5+
types::*,
56
utils,
67
};
78

@@ -41,7 +42,7 @@ pub struct Update {
4142
pub segment_number: [u8; 8],
4243
pub old_data: [u8; SEGMENT_SIZE],
4344
pub new_data: [u8; SEGMENT_SIZE],
44-
pub proof: [[u8; 32]; SEGMENT_PROOF_LEN],
45+
pub proof: ProofPath,
4546
}
4647

4748
#[repr(C)]
@@ -130,7 +131,7 @@ pub fn build_update_ix(
130131
segment_number: u64,
131132
old_data: [u8; SEGMENT_SIZE],
132133
new_data: [u8; SEGMENT_SIZE],
133-
proof: [[u8;32]; SEGMENT_PROOF_LEN],
134+
proof: ProofPath,
134135
) -> Instruction {
135136

136137
let segment_number = segment_number.to_le_bytes();

api/src/pda.rs

Lines changed: 82 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::mem::MaybeUninit;
55
use steel::*;
66
use crate::consts::*;
77

8-
98
pub fn pda_derive_address<const N: usize>(
109
seeds: &[&[u8]; N],
1110
bump: Option<u8>,
@@ -71,43 +70,41 @@ pub fn pda_derive_address<const N: usize>(
7170
unreachable!("deriving a pda is only available on target `solana`");
7271
}
7372

73+
74+
#[inline(always)]
7475
pub fn archive_pda() -> (Pubkey, u8) {
7576
(ARCHIVE_ADDRESS, ARCHIVE_BUMP)
7677
}
7778

79+
#[inline(always)]
7880
pub fn epoch_pda() -> (Pubkey, u8) {
7981
(EPOCH_ADDRESS, EPOCH_BUMP)
8082
}
8183

84+
#[inline(always)]
8285
pub fn block_pda() -> (Pubkey, u8) {
8386
(BLOCK_ADDRESS, BLOCK_BUMP)
8487
}
8588

89+
#[inline(always)]
8690
pub fn treasury_pda() -> (Pubkey, u8) {
8791
(TREASURY_ADDRESS, TREASURY_BUMP)
8892
}
8993

94+
#[inline(always)]
9095
pub fn treasury_ata() -> (Pubkey, u8) {
91-
let (treasury_pda,_bump) = treasury_pda();
92-
let (mint_pda, _bump) = mint_pda();
93-
94-
Pubkey::find_program_address(
95-
&[
96-
treasury_pda.as_ref(),
97-
spl_token::ID.as_ref(),
98-
mint_pda.as_ref()
99-
],
100-
&spl_associated_token_account::ID,
101-
)
96+
(TREASURY_ATA, TREASURY_ATA_BUMP)
10297
}
10398

99+
100+
#[inline(always)]
104101
pub fn mint_pda() -> (Pubkey, u8) {
105102
(MINT_ADDRESS, MINT_BUMP)
106103
}
107104

108105
pub fn metadata_find_pda(mint: Pubkey) -> (Pubkey, u8) {
109106
Pubkey::find_program_address(
110-
&[METADATA, mpl_token_metadata::ID.as_ref(), mint.as_ref() ],
107+
&[METADATA, mpl_token_metadata::ID.as_ref(), mint.as_ref()],
111108
&mpl_token_metadata::ID,
112109
)
113110
}
@@ -147,7 +144,8 @@ pub fn miner_derive_pda(authority: Pubkey, name: &[u8; NAME_LEN], bump: u8) -> P
147144

148145
pub fn spool_find_pda(miner: Pubkey, number: u64) -> (Pubkey, u8) {
149146
Pubkey::find_program_address(
150-
&[SPOOL, miner.as_ref(), number.to_le_bytes().as_ref()], &crate::id()
147+
&[SPOOL, miner.as_ref(), number.to_le_bytes().as_ref()],
148+
&crate::id(),
151149
)
152150
}
153151

@@ -157,4 +155,73 @@ pub fn spool_derive_pda(miner: Pubkey, number: u64, bump: u8) -> Pubkey {
157155
Some(bump),
158156
&crate::id(),
159157
)
160-
}
158+
}
159+
160+
161+
#[cfg(test)]
162+
mod tests {
163+
use super::*;
164+
165+
pub fn archive_pda() -> (Pubkey, u8) {
166+
Pubkey::find_program_address(&[ARCHIVE], &crate::id())
167+
}
168+
pub fn epoch_pda() -> (Pubkey, u8) {
169+
Pubkey::find_program_address(&[EPOCH], &crate::id())
170+
}
171+
172+
pub fn block_pda() -> (Pubkey, u8) {
173+
Pubkey::find_program_address(&[BLOCK], &crate::id())
174+
}
175+
176+
pub fn treasury_pda() -> (Pubkey, u8) {
177+
Pubkey::find_program_address(&[TREASURY], &crate::id())
178+
}
179+
180+
pub fn treasury_ata() -> (Pubkey, u8) {
181+
let (treasury_pda, _bump) = treasury_pda();
182+
let (mint_pda, _bump) = mint_pda();
183+
Pubkey::find_program_address(
184+
&[
185+
treasury_pda.as_ref(),
186+
spl_token::ID.as_ref(),
187+
mint_pda.as_ref(),
188+
],
189+
&spl_associated_token_account::ID,
190+
)
191+
}
192+
193+
pub fn mint_pda() -> (Pubkey, u8) {
194+
Pubkey::find_program_address(&[MINT, MINT_SEED], &crate::id())
195+
}
196+
197+
#[test]
198+
fn test_pda_against_consts() {
199+
// These tests, as nonsensical as they seem, are to ensure that the PDAs generated by the
200+
// consts match the ones generated by the official functions. The consts are generated by
201+
// external deps, so if we straight up use the consts, we are trusting that the external
202+
// deps are working as expected, which is not a good idea. Always be testing.
203+
204+
let (pda, bump) = archive_pda();
205+
assert_eq!(bump, ARCHIVE_BUMP);
206+
assert_eq!(pda, ARCHIVE_ADDRESS);
207+
208+
let (pda, bump) = epoch_pda();
209+
assert_eq!(bump, EPOCH_BUMP);
210+
assert_eq!(pda, EPOCH_ADDRESS);
211+
212+
let (pda, bump) = block_pda();
213+
assert_eq!(bump, BLOCK_BUMP);
214+
assert_eq!(pda, BLOCK_ADDRESS);
215+
216+
let (pda, bump) = mint_pda();
217+
assert_eq!(bump, MINT_BUMP);
218+
assert_eq!(pda, MINT_ADDRESS);
219+
220+
let (pda, bump) = treasury_pda();
221+
assert_eq!(bump, TREASURY_BUMP);
222+
assert_eq!(pda, TREASURY_ADDRESS);
223+
224+
let (pda, _bump) = treasury_ata();
225+
assert_eq!(pda, TREASURY_ATA);
226+
}
227+
}

api/src/state/tape.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ pub struct Tape {
1616
pub merkle_root: [u8; 32],
1717
pub header: [u8; HEADER_SIZE],
1818

19-
pub first_slot: u64,
20-
pub tail_slot: u64,
19+
pub first_slot: u64,
20+
pub tail_slot: u64,
2121

22-
pub balance: u64,
22+
pub balance: u64,
2323
pub last_rent_block: u64,
24-
25-
pub total_segments: u64,
26-
24+
pub total_segments: u64,
2725
pub pda_bump: u64,
2826
}
2927

@@ -37,3 +35,24 @@ pub enum TapeState {
3735
}
3836

3937
state!(AccountType, Tape);
38+
39+
//pub fn encode_tape(tape: &Tape, nodes: &[[u8; 32]]) -> Vec<u8> {
40+
// let mut out = Vec::with_capacity(core::mem::size_of::<Tape>() + nodes.len() * 32);
41+
// out.extend_from_slice(bytemuck::bytes_of(tape));
42+
// out.extend_from_slice(bytemuck::cast_slice(nodes));
43+
// out
44+
//}
45+
//
46+
//pub fn decode_tape(data: &[u8]) -> Result<(&Tape, &[[u8; 32]]), ProgramError> {
47+
// let need = core::mem::size_of::<Tape>();
48+
// if data.len() < need {
49+
// return Err(ProgramError::AccountDataTooSmall);
50+
// }
51+
// let (head_bytes, tail) = data.split_at(need);
52+
// if tail.len() % 32 != 0 {
53+
// return Err(ProgramError::AccountDataTooSmall);
54+
// }
55+
// let header: &Tape = bytemuck::from_bytes(head_bytes);
56+
// let nodes: &[[u8; 32]] = bytemuck::try_cast_slice(tail).expect("len checked");
57+
// Ok((header, nodes))
58+
//}

0 commit comments

Comments
 (0)