espresso_types/v0/v0_4/
state.rs

1use std::collections::HashSet;
2
3use alloy::primitives::{Address, U256};
4use derive_more::{Display, From, Into};
5use jf_merkle_tree::{
6    universal_merkle_tree::UniversalMerkleTree, MerkleTreeScheme, UniversalMerkleTreeScheme,
7};
8use serde::{Deserialize, Serialize};
9
10use super::FeeAccount;
11use crate::{
12    v0::sparse_mt::{Keccak256Hasher, KeccakNode},
13    v0_3::{RewardAccountV1, RewardAmount},
14};
15
16pub const REWARD_MERKLE_TREE_V2_HEIGHT: usize = 160;
17pub const REWARD_MERKLE_TREE_V2_ARITY: usize = 2;
18
19pub type RewardMerkleCommitmentV2 = <RewardMerkleTreeV2 as MerkleTreeScheme>::Commitment;
20
21pub type RewardMerkleTreeV2 = UniversalMerkleTree<
22    RewardAmount,
23    Keccak256Hasher,
24    RewardAccountV2,
25    REWARD_MERKLE_TREE_V2_ARITY,
26    KeccakNode,
27>;
28// New Type for `Address` in order to implement `CanonicalSerialize` and
29// `CanonicalDeserialize`
30// This is the same as `RewardAccountV1` but the `ToTraversal` trait implementation
31// for this type is different
32#[derive(
33    Default,
34    Hash,
35    Copy,
36    Clone,
37    Debug,
38    Display,
39    Deserialize,
40    Serialize,
41    PartialEq,
42    Eq,
43    PartialOrd,
44    Ord,
45    From,
46    Into,
47)]
48#[display("{_0}")]
49pub struct RewardAccountV2(pub Address);
50
51impl From<RewardAccountV2> for RewardAccountV1 {
52    fn from(account: RewardAccountV2) -> Self {
53        RewardAccountV1(account.0)
54    }
55}
56
57impl From<RewardAccountV1> for RewardAccountV2 {
58    fn from(account: RewardAccountV1) -> Self {
59        RewardAccountV2(account.0)
60    }
61}
62
63/// A proof of the balance of an account in the fee ledger.
64///
65/// If the account of interest does not exist in the fee state, this is a Merkle non-membership
66/// proof, and the balance is implicitly zero. Otherwise, this is a normal Merkle membership proof.
67#[derive(Clone, Debug, Deserialize, Serialize)]
68pub struct RewardAccountProofV2 {
69    pub account: Address,
70    pub proof: RewardMerkleProofV2,
71}
72
73#[derive(Clone, Debug, Deserialize, Serialize)]
74pub enum RewardMerkleProofV2 {
75    Presence(<RewardMerkleTreeV2 as MerkleTreeScheme>::MembershipProof),
76    Absence(<RewardMerkleTreeV2 as UniversalMerkleTreeScheme>::NonMembershipProof),
77}
78
79#[derive(Clone, Debug, Serialize, Deserialize)]
80pub struct RewardAccountQueryDataV2 {
81    pub balance: U256,
82    pub proof: RewardAccountProofV2,
83}
84
85#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
86pub struct Delta {
87    pub fees_delta: HashSet<FeeAccount>,
88    pub rewards_delta: HashSet<RewardAccountV2>,
89}