Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ use platform_version::version::PlatformVersion;

pub use v0::CompactedAddressBalanceChanges;

/// Proof envelope for compacted address balance changes.
///
/// The predecessor proof independently authenticates which range, if any,
/// contains the requested height. The forward proof can then be verified
/// against a query derived only from that authenticated result.
#[derive(Debug, bincode::Encode, bincode::Decode)]
pub(crate) struct CompactedAddressBalanceProof {
pub(crate) predecessor_proof: Vec<u8>,
pub(crate) forward_proof: Vec<u8>,
}

impl Drive {
/// Fetches compacted address balance changes starting from a given block height.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use grovedb::{Element, PathQuery, Query, SizedQuery, TransactionArg};
use platform_version::version::PlatformVersion;
use std::collections::BTreeMap;

use super::CompactedAddressBalanceProof;

/// Result type for fetched compacted address balance changes
/// Each entry is (start_block, end_block, address_balance_map)
pub type CompactedAddressBalanceChanges = Vec<(
Expand Down Expand Up @@ -189,9 +191,11 @@ impl Drive {

/// Version 0 implementation for proving compacted address balance changes.
///
/// Uses a two-step approach:
/// 1. First query (non-proving): descending to find any range containing start_block_height
/// 2. Second query (proving): ascending from the found start_block or start_block_height
/// Uses two independently verifiable proofs:
/// 1. A descending predecessor proof authenticates which range, if any,
/// contains `start_block_height`.
/// 2. A forward proof starts at that authenticated range (or at the
/// request-derived fallback key when no range contains the height).
///
/// This ensures the proof covers all relevant ranges efficiently.
pub(super) fn prove_compacted_address_balance_changes_v0(
Expand All @@ -203,7 +207,7 @@ impl Drive {
) -> Result<Vec<u8>, Error> {
let path = Self::saved_compacted_block_transactions_address_balances_path_vec();

// Step 1: Non-proving descending query to find any range containing start_block_height
// Step 1: Authenticate the predecessor used to select the forward query.
let mut desc_end_key = Vec::with_capacity(16);
desc_end_key.extend_from_slice(&start_block_height.to_be_bytes());
desc_end_key.extend_from_slice(&u64::MAX.to_be_bytes());
Expand All @@ -222,6 +226,13 @@ impl Drive {
&platform_version.drive,
)?;

let predecessor_proof = self.grove_get_proved_path_query(
&desc_path_query,
transaction,
&mut vec![],
&platform_version.drive,
)?;

// Determine the actual start key for the proved query
// If we found a containing range, use its exact key
// Otherwise use (start_block_height, start_block_height) since end_block >= start_block always
Expand Down Expand Up @@ -258,12 +269,27 @@ impl Drive {

let path_query = PathQuery::new(path, SizedQuery::new(query, limit, None));

self.grove_get_proved_path_query(
let forward_proof = self.grove_get_proved_path_query(
&path_query,
transaction,
&mut vec![],
&platform_version.drive,
)?;

bincode::encode_to_vec(
CompactedAddressBalanceProof {
predecessor_proof,
forward_proof,
},
bincode::config::standard()
.with_big_endian()
.with_no_limit(),
)
.map_err(|e| {
Error::Protocol(Box::new(ProtocolError::CorruptedSerialization(format!(
"cannot encode compacted address balance proof: {e}"
))))
})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ mod store_address_balances;

pub use fetch_address_balances::AddressBalanceChangesPerBlock;
pub use fetch_compacted_address_balances::CompactedAddressBalanceChanges;
pub(crate) use fetch_compacted_address_balances::CompactedAddressBalanceProof;
pub use queries::*;
Loading
Loading