data-anchor-proofs 0.4.6

Proofs regarding the presence or absence of a blob uploaded using the Blober program.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Proof of the state of one or many accounts in a specific Solana slot, without needing
//! to know the public key of the accounts in advance. When combined with a
//! [bank hash proof][`crate::bank_hash::BankHashProof`] (to ensure no updates were left out) it can
//! additionally prove that no account states were censored.

use std::{collections::BTreeMap, fmt::Debug, sync::Arc};

use anchor_lang::{
    AccountDeserialize, AnchorDeserialize, Discriminator,
    error::{Error, ErrorCode},
    prelude::Pubkey,
    solana_program::{clock::Slot, hash::HASH_BYTES},
};
use data_anchor_blober::{U32_SIZE_BYTES, hash_blob, merge_hashes, state::blober::Blober};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::{compound::ProofBlob, debug::NoPrettyPrint};

/// Failures that can occur when verifying a [`BloberAccountStateProof`].
#[derive(Debug, Clone, Error)]
pub enum BloberAccountStateError {
    #[error("Discriminator mismatch, wrong account type")]
    DiscriminatorMismatch,
    #[error(transparent)]
    BorshDeserialize(#[from] Arc<std::io::Error>),
    #[error("Proof is not for the correct slot")]
    SlotMismatch { expected: Slot, found: Slot },
    #[error("Digest does not match the expected value")]
    DigestMismatch { expected: String, found: String },
    #[error("Invalid state data")]
    InvalidStateData,
    #[error("Invalid blob account data: {0:?}")]
    InvalidBlobAccountData(Vec<u8>),
    #[error("Blob size mismatch at index: expected {expected}, found {found}")]
    BlobSizeMismatch { expected: usize, found: usize },
}

pub type BloberAccountStateResult<T = ()> = Result<T, BloberAccountStateError>;

/// An account whose state was hashed using the blober program.
///
/// The bytes should already be sliced to the exact offset and length that the
/// [`data_anchor_blober::instructions::FinalizeBlob`] instruction slices them to.
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct BlobAccount {
    pub address: Pubkey,
    pub raw_data: Vec<u8>,
}

impl BlobAccount {
    pub fn new(address: Pubkey, raw_data: Vec<u8>) -> Self {
        Self { address, raw_data }
    }

    pub fn hash_blob(&self) -> [u8; HASH_BYTES] {
        hash_blob(&self.address, &self.raw_data)
    }

    pub fn verify(
        &self,
        blob: &ProofBlob<impl AsRef<[u8]>>,
    ) -> BloberAccountStateResult<[u8; HASH_BYTES]> {
        let Some((blob_account_digest_bytes, blob_account_blob_size_bytes)) =
            self.raw_data.split_at_checked(HASH_BYTES)
        else {
            return Err(BloberAccountStateError::InvalidBlobAccountData(
                self.raw_data.clone(),
            ));
        };

        let blob_account_digest: [u8; HASH_BYTES] = blob_account_digest_bytes
            .try_into()
            .map_err(|_| BloberAccountStateError::InvalidBlobAccountData(self.raw_data.clone()))?;
        let blob_account_blob_size_bytes: [u8; U32_SIZE_BYTES as usize] =
            blob_account_blob_size_bytes.try_into().map_err(|_| {
                BloberAccountStateError::InvalidBlobAccountData(self.raw_data.clone())
            })?;

        let blob_account_blob_size = u32::from_le_bytes(blob_account_blob_size_bytes) as usize;

        if let Some(blob_size) = blob.blob_size()
            && blob_account_blob_size != blob_size
        {
            return Err(BloberAccountStateError::BlobSizeMismatch {
                expected: blob_account_blob_size,
                found: blob_size,
            });
        }

        Ok(blob_account_digest)
    }
}

impl Debug for BlobAccount {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("SourceAccount")
            .field(&self.address.to_string())
            .field(&hex::encode(&self.raw_data))
            .finish()
    }
}

/// A proof for the state of one or many accounts in a specific Solana slot.
///
/// To create this proof, the Blober account's [`data_anchor_blober::blober::finalize_blob`] instruction must
/// be invoked for each blob whose state should be proven. The starting offset and length of the
/// "interesting" part of the account data that is to be hashed must also be provided.
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct BloberAccountStateProof {
    pub initial_hash: [u8; HASH_BYTES],
    pub initial_slot: Slot,
    pub uploads: BTreeMap<Slot, Vec<BlobAccount>>,
}

impl Debug for BloberAccountStateProof {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Proof")
            .field("initial_slot", &self.initial_slot)
            .field("initial_hash", &hex::encode(self.initial_hash))
            .field("uploads", &NoPrettyPrint(&self.uploads))
            .finish()
    }
}

impl BloberAccountStateProof {
    pub fn new(
        initial_hash: [u8; HASH_BYTES],
        initial_slot: Slot,
        uploads: BTreeMap<Slot, Vec<BlobAccount>>,
    ) -> Self {
        assert!(
            uploads
                .first_key_value()
                .map(|(slot, _)| *slot > initial_slot)
                .unwrap_or(true),
            "All uploads must be in a slot after the initial slot"
        );
        Self {
            initial_hash,
            initial_slot,
            uploads,
        }
    }

    pub fn blobs(&self) -> impl Iterator<Item = &BlobAccount> {
        self.uploads.values().flat_map(|blobs| blobs.iter())
    }

    pub fn target_slot(&self) -> Slot {
        self.uploads
            .last_key_value()
            .map(|(slot, _)| *slot)
            .unwrap_or(self.initial_slot)
    }

    pub fn calculate_hash(&self) -> [u8; HASH_BYTES] {
        merge_all_hashes(
            std::iter::once(self.initial_hash).chain(self.blobs().map(|blob| blob.hash_blob())),
        )
    }

    pub fn hash_blobs(&self) -> [u8; HASH_BYTES] {
        merge_all_hashes(self.blobs().map(|blob| blob.hash_blob()))
    }

    pub fn verify(&self, blober_account_data: &[u8]) -> BloberAccountStateResult {
        let mut data = blober_account_data;

        let state = Blober::try_deserialize(&mut data).map_err(|e| match e {
            Error::AnchorError(anchor_error)
                if anchor_error.error_code_number
                    == ErrorCode::AccountDiscriminatorMismatch as u32 =>
            {
                BloberAccountStateError::DiscriminatorMismatch
            }
            _ => BloberAccountStateError::InvalidStateData,
        })?;

        if let Some((&slot, _)) = self.uploads.last_key_value() {
            if slot != state.slot {
                return Err(BloberAccountStateError::SlotMismatch {
                    expected: slot,
                    found: state.slot,
                });
            }
        } else if state.slot != self.initial_slot {
            return Err(BloberAccountStateError::SlotMismatch {
                expected: self.initial_slot,
                found: state.slot,
            });
        }

        let hash = self.calculate_hash();

        if hash != state.hash {
            return Err(BloberAccountStateError::DigestMismatch {
                expected: hex::encode(hash),
                found: hex::encode(state.hash),
            });
        }

        Ok(())
    }
}

pub fn get_blober_hash(blober_account_data: &[u8]) -> BloberAccountStateResult<[u8; HASH_BYTES]> {
    if &blober_account_data[..8] != Blober::DISCRIMINATOR {
        return Err(BloberAccountStateError::DiscriminatorMismatch);
    }

    let state = Blober::try_from_slice(&blober_account_data[8..]).map_err(Arc::new)?;

    Ok(state.hash)
}

pub fn merge_all_hashes(hashes: impl Iterator<Item = [u8; HASH_BYTES]>) -> [u8; HASH_BYTES] {
    hashes
        .reduce(|acc, hash| merge_hashes(&acc, &hash))
        .expect("account list to not be empty")
}

#[cfg(test)]
mod tests {
    use anchor_lang::AnchorSerialize;
    use arbtest::arbtest;
    use data_anchor_blober::initial_hash;
    use solana_signer::Signer;

    use super::*;
    use crate::testing::ArbKeypair;

    #[test]
    fn test_merge_all_hashes() {
        arbtest(|u| {
            let hashes = [u.arbitrary()?, u.arbitrary()?, u.arbitrary()?];

            let expected = merge_hashes(&merge_hashes(&hashes[0], &hashes[1]), &hashes[2]);

            assert_eq!(merge_all_hashes(hashes.iter().cloned()), expected);

            Ok(())
        });
    }

    #[test]
    #[should_panic]
    fn blobs_before_initial_slot_panics() {
        BloberAccountStateProof::new(
            initial_hash(),
            2,
            BTreeMap::from([(1, vec![BlobAccount::new(Pubkey::default(), vec![0; 10])])]),
        );
    }

    #[test]
    fn single_account() {
        arbtest(|u| {
            let slot = u.arbitrary()?;
            let source_account: (ArbKeypair, Vec<u8>) = u.arbitrary()?;
            let source_accounts = vec![BlobAccount::new(
                source_account.0.pubkey(),
                source_account.1,
            )];

            let proof = BloberAccountStateProof::new(
                initial_hash(),
                slot,
                [(slot + 1, source_accounts.clone())].into_iter().collect(),
            );
            let blober_account_data: Vec<u8> = [
                Blober::DISCRIMINATOR.to_vec(),
                Blober {
                    slot: slot + 1,
                    hash: merge_all_hashes(
                        [initial_hash(), source_accounts[0].hash_blob()].into_iter(),
                    ),
                    caller: u.arbitrary::<ArbKeypair>()?.pubkey().to_bytes().into(),
                    namespace: u.arbitrary()?,
                }
                .try_to_vec()
                .unwrap(),
            ]
            .concat();

            proof.verify(&blober_account_data).unwrap();

            Ok(())
        })
        .size_max(100_000_000);
    }

    #[test]
    fn single_account_wrong_data() {
        arbtest(|u| {
            let slot = u.arbitrary()?;
            let source_account: (ArbKeypair, Vec<u8>) = u.arbitrary()?;
            let source_accounts = vec![BlobAccount::new(
                source_account.0.pubkey(),
                source_account.1,
            )];

            let proof = BloberAccountStateProof::new(
                initial_hash(),
                slot,
                [(slot + 1, source_accounts.clone())].into_iter().collect(),
            );
            let wrong_data = BlobAccount::new(source_account.0.pubkey(), u.arbitrary::<Vec<u8>>()?);
            let blober_account_data: Vec<u8> = [
                Blober::DISCRIMINATOR.to_vec(),
                Blober {
                    slot: slot + 1,
                    hash: merge_all_hashes([initial_hash(), wrong_data.hash_blob()].into_iter()),
                    caller: u.arbitrary::<ArbKeypair>()?.pubkey().to_bytes().into(),
                    namespace: u.arbitrary()?,
                }
                .try_to_vec()
                .unwrap(),
            ]
            .into_iter()
            .flatten()
            .collect();

            if wrong_data.raw_data != source_accounts[0].raw_data {
                proof.verify(&blober_account_data).unwrap_err();
            } else {
                proof.verify(&blober_account_data).unwrap();
            }

            Ok(())
        })
        .size_max(100_000_000);
    }

    #[test]
    fn multiple_accounts() {
        arbtest(|u| {
            let slot = u.arbitrary()?;
            // At least two accounts are needed for this test to make sense.
            let count = u.int_in_range(2..=1000)?;
            let blob_accounts: Vec<_> = (0..count)
                .map(|_| {
                    let keypair = u.arbitrary::<ArbKeypair>()?;
                    let bytes = u.arbitrary::<Vec<u8>>()?;
                    Ok(BlobAccount::new(keypair.pubkey(), bytes))
                })
                .collect::<Result<_, _>>()?;

            let proof = BloberAccountStateProof::new(
                initial_hash(),
                slot,
                [(slot + 1, blob_accounts.clone())].into_iter().collect(),
            );

            let hash = merge_all_hashes(
                std::iter::once(initial_hash())
                    .chain(blob_accounts.iter().map(|blob| blob.hash_blob())),
            );

            let blober_account_data: Vec<u8> = [
                Blober::DISCRIMINATOR.to_vec(),
                Blober {
                    slot: slot + 1,
                    hash,
                    caller: u.arbitrary::<ArbKeypair>()?.pubkey().to_bytes().into(),
                    namespace: u.arbitrary()?,
                }
                .try_to_vec()
                .unwrap(),
            ]
            .into_iter()
            .flatten()
            .collect();

            proof.verify(&blober_account_data).unwrap();

            Ok(())
        })
        .size_max(100_000_000);
    }

    #[test]
    fn multiple_accounts_wrong_data() {
        arbtest(|u| {
            let slot = u.arbitrary()?;
            // At least two accounts are needed for this test to make sense.
            let count = u.int_in_range(2..=1000)?;
            let mut blob_accounts: Vec<_> = (0..count)
                .map(|_| {
                    let keypair = u.arbitrary::<ArbKeypair>()?;
                    let bytes = u.arbitrary::<Vec<u8>>()?;
                    Ok(BlobAccount::new(keypair.pubkey(), bytes))
                })
                .collect::<Result<_, _>>()?;

            let proof = BloberAccountStateProof::new(
                initial_hash(),
                slot,
                [(slot + 1, blob_accounts.clone())].into_iter().collect(),
            );

            let wrong_data = u.arbitrary::<Vec<u8>>()?;
            let wrong_data_index = u.choose_index(blob_accounts.len())?;
            if blob_accounts[wrong_data_index].raw_data == wrong_data {
                // Data wasn't changed, so the test is invalid.
                return Ok(());
            }
            blob_accounts[wrong_data_index].raw_data = wrong_data;

            let hash = merge_all_hashes(
                std::iter::once(initial_hash())
                    .chain(blob_accounts.iter().map(|blob| blob.hash_blob())),
            );

            let blober_account_data: Vec<u8> = [
                Blober::DISCRIMINATOR.to_vec(),
                Blober {
                    slot: slot + 1,
                    hash,
                    caller: u.arbitrary::<ArbKeypair>()?.pubkey().to_bytes().into(),
                    namespace: u.arbitrary()?,
                }
                .try_to_vec()
                .unwrap(),
            ]
            .into_iter()
            .flatten()
            .collect();

            proof.verify(&blober_account_data).unwrap_err();

            Ok(())
        })
        .size_max(100_000_000);
    }
}