espresso_types/v0/v0_1/
state.rs

1use std::collections::HashSet;
2
3use alloy::primitives::{Address, U256};
4use committable::Commitment;
5use derive_more::{derive::AddAssign, Add, Display, From, Into, Mul, Sub};
6use jf_merkle_tree::{
7    prelude::{LightWeightSHA3MerkleTree, Sha3Digest, Sha3Node},
8    universal_merkle_tree::UniversalMerkleTree,
9    MerkleTreeScheme, UniversalMerkleTreeScheme,
10};
11use serde::{Deserialize, Serialize};
12
13use super::{FeeAccount, FeeAmount};
14use crate::Header;
15
16#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
17pub struct Delta {
18    pub fees_delta: HashSet<FeeAccount>,
19    pub rewards_delta: HashSet<RewardAccount>,
20}
21
22pub const BLOCK_MERKLE_TREE_HEIGHT: usize = 32;
23pub const FEE_MERKLE_TREE_HEIGHT: usize = 20;
24pub const REWARD_MERKLE_TREE_HEIGHT: usize = 20;
25const FEE_MERKLE_TREE_ARITY: usize = 256;
26const REWARD_MERKLE_TREE_ARITY: usize = 256;
27
28// The block merkle tree accumulates header commitments. However, since the underlying
29// representation of the commitment type remains the same even while the header itself changes,
30// using the underlying type `[u8; 32]` allows us to use the same state type across minor versions.
31pub type BlockMerkleTree = LightWeightSHA3MerkleTree<Commitment<Header>>;
32pub type BlockMerkleCommitment = <BlockMerkleTree as MerkleTreeScheme>::Commitment;
33
34pub type FeeMerkleTree =
35    UniversalMerkleTree<FeeAmount, Sha3Digest, FeeAccount, FEE_MERKLE_TREE_ARITY, Sha3Node>;
36pub type FeeMerkleCommitment = <FeeMerkleTree as MerkleTreeScheme>::Commitment;
37
38// TODO: Update JELLYFISH crate to use KECCACK256
39pub type RewardMerkleTree = UniversalMerkleTree<
40    RewardAmount,
41    Sha3Digest,
42    RewardAccount,
43    REWARD_MERKLE_TREE_ARITY,
44    Sha3Node,
45>;
46pub type RewardMerkleCommitment = <RewardMerkleTree as MerkleTreeScheme>::Commitment;
47
48// New Type for `Address` in order to implement `CanonicalSerialize` and
49// `CanonicalDeserialize`
50#[derive(
51    Default,
52    Hash,
53    Copy,
54    Clone,
55    Debug,
56    Display,
57    Deserialize,
58    Serialize,
59    PartialEq,
60    Eq,
61    PartialOrd,
62    Ord,
63    From,
64    Into,
65)]
66#[display("{_0:x}")]
67pub struct RewardAccount(pub Address);
68
69// New Type for `U256` in order to implement `CanonicalSerialize` and
70// `CanonicalDeserialize`
71#[derive(
72    Default,
73    Hash,
74    Copy,
75    Clone,
76    Debug,
77    Display,
78    PartialEq,
79    Eq,
80    PartialOrd,
81    Ord,
82    Add,
83    Sub,
84    Mul,
85    From,
86    Into,
87    AddAssign,
88)]
89#[display("{_0}")]
90pub struct RewardAmount(pub U256);
91
92// This function is used to calculate the reward for a block
93// It does not currently take block height into account
94// The reward is currently fixed at 1.902 tokens at 3% inflation per block
95pub fn block_reward() -> RewardAmount {
96    U256::from(REWARD_PER_BLOCK).into()
97}
98
99// 10 billion tokens with 18 decimals
100const TOTAL_SUPPLY: u128 = 10_000_000_000 * 10_u128.pow(18);
101const INFLATION_RATE: u128 = 300; // 3% in basis points
102const BLOCK_TIME_SECONDS: u128 = 2;
103const SECONDS_PER_YEAR: u128 = 60 * 60 * 24 * 365;
104const BLOCKS_PER_YEAR: u128 = SECONDS_PER_YEAR / BLOCK_TIME_SECONDS;
105const REWARD_PER_BLOCK: u128 =
106    ((TOTAL_SUPPLY * INFLATION_RATE) / BLOCKS_PER_YEAR) / COMMISSION_BASIS_POINTS as u128;
107pub const COMMISSION_BASIS_POINTS: u16 = 10_000;
108
109#[derive(Clone, Debug, Default)]
110pub struct RewardInfo {
111    pub account: RewardAccount,
112    pub amount: RewardAmount,
113}
114
115/// A proof of the balance of an account in the fee ledger.
116///
117/// If the account of interest does not exist in the fee state, this is a Merkle non-membership
118/// proof, and the balance is implicitly zero. Otherwise, this is a normal Merkle membership proof.
119#[derive(Clone, Debug, Deserialize, Serialize)]
120pub struct RewardAccountProof {
121    pub account: Address,
122    pub proof: RewardMerkleProof,
123}
124
125#[derive(Clone, Debug, Deserialize, Serialize)]
126pub enum RewardMerkleProof {
127    Presence(<RewardMerkleTree as MerkleTreeScheme>::MembershipProof),
128    Absence(<RewardMerkleTree as UniversalMerkleTreeScheme>::NonMembershipProof),
129}
130
131#[derive(Clone, Debug, Serialize, Deserialize)]
132pub struct RewardAccountQueryData {
133    pub balance: U256,
134    pub proof: RewardAccountProof,
135}