Skip to content

Commit

Permalink
Iterable FeeInfo
Browse files Browse the repository at this point in the history
Closes #1659.
  • Loading branch information
tbro committed Jul 11, 2024
1 parent 3f38c76 commit f6e1930
Show file tree
Hide file tree
Showing 9 changed files with 232 additions and 109 deletions.
5 changes: 4 additions & 1 deletion sequencer/src/bin/nasty-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,10 @@ impl ResourceManager<Header> {
.await
})
.await?;
let builder_address = builder_header.fee_info().account();

// Since we have multiple fee accounts, we need to select one.
let accounts = builder_header.fee_info().accounts();
let builder_address = accounts.first().unwrap();

// Get the header of the state snapshot we're going to query so we can later verify our
// results.
Expand Down
2 changes: 1 addition & 1 deletion sequencer/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ mod test {
ProposalValidationError::InsufficientFee {
max_block_size: instance.chain_config.max_block_size,
base_fee: instance.chain_config.base_fee,
proposed_fee: header.fee_info().amount()
proposed_fee: header.fee_info().amount().unwrap()
},
err
);
Expand Down
4 changes: 2 additions & 2 deletions types/src/reference_tests.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ async fn reference_header(version: Version) -> Header {
ns_table,
state.fee_merkle_tree.commitment(),
state.block_merkle_tree.commitment(),
fee_info,
Some(builder_signature),
vec![fee_info],
vec![builder_signature],
version,
)
}
Expand Down
79 changes: 74 additions & 5 deletions types/src/v0/impls/fee_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use ethers::{
};
use hotshot_query_service::explorer::MonetaryValue;
use hotshot_types::traits::block_contents::BuilderFee;
use itertools::Itertools;
use jf_merkle_tree::{
ForgetableMerkleTreeScheme, ForgetableUniversalMerkleTreeScheme, LookupResult,
MerkleCommitment, MerkleTreeError, MerkleTreeScheme, ToTraversalPath,
Expand All @@ -26,8 +27,9 @@ use sequencer_utils::{
use thiserror::Error;

use crate::{
eth_signature_key::EthKeyPair, AccountQueryData, FeeAccount, FeeAccountProof, FeeAmount,
FeeInfo, FeeMerkleCommitment, FeeMerkleProof, FeeMerkleTree, SeqTypes,
eth_signature_key::EthKeyPair, v0_3::IterableFeeInfo, AccountQueryData, FeeAccount,
FeeAccountProof, FeeAmount, FeeInfo, FeeMerkleCommitment, FeeMerkleProof, FeeMerkleTree,
SeqTypes,
};

/// Possible charge fee failures
Expand Down Expand Up @@ -59,11 +61,12 @@ impl FeeInfo {
}
}

pub fn genesis() -> Self {
Self {
pub fn genesis() -> Vec<Self> {
let info = Self {
account: Default::default(),
amount: Default::default(),
}
};
vec![info]
}

pub fn account(&self) -> FeeAccount {
Expand All @@ -73,6 +76,48 @@ impl FeeInfo {
pub fn amount(&self) -> FeeAmount {
self.amount
}
/// Get a `Vec<FeeInfo>` from `Vec<BuilderFee>`
pub fn from_builder_fees(fees: Vec<BuilderFee<SeqTypes>>) -> Vec<FeeInfo> {
fees.into_iter().map(FeeInfo::from).collect()
}
}

impl IterableFeeInfo for Vec<FeeInfo> {
/// Get sum of fee amounts
fn amount(&self) -> Option<FeeAmount> {
self.iter()
// getting the u64 tests that the value fits
.map(|fee_info| fee_info.amount.as_u64())
.collect::<Option<Vec<u64>>>()
.and_then(|amounts| amounts.iter().try_fold(0u64, |acc, n| acc.checked_add(*n)))
.map(FeeAmount::from)
}

/// Get a `Vec` of all unique fee accounts
fn accounts(&self) -> Vec<FeeAccount> {
self.iter()
.unique_by(|entry| &entry.account)
.map(|entry| entry.account)
.collect()
}
}

impl IterableFeeInfo for Vec<BuilderFee<SeqTypes>> {
/// Get sum of amounts
fn amount(&self) -> Option<FeeAmount> {
self.iter()
.map(|fee_info| fee_info.fee_amount)
.try_fold(0u64, |acc, n| acc.checked_add(n))
.map(FeeAmount::from)
}

/// Get a `Vec` of all unique fee accounts
fn accounts(&self) -> Vec<FeeAccount> {
self.iter()
.unique_by(|entry| &entry.fee_account)
.map(|entry| entry.fee_account)
.collect()
}
}

impl From<BuilderFee<SeqTypes>> for FeeInfo {
Expand Down Expand Up @@ -375,3 +420,27 @@ impl From<(FeeAccountProof, U256)> for AccountQueryData {
Self { balance, proof }
}
}

#[cfg(test)]
mod test {
use ethers::abi::Address;

use crate::{FeeAccount, FeeAmount, FeeInfo};

use super::IterableFeeInfo;

// TODO find out where this test should go in versioned code
#[test]
fn test_iterable_fee_info() {
let addr = Address::zero();
let fee = FeeInfo::new(addr, FeeAmount::from(1));
let fees = vec![fee, fee, fee];
// check the sum of amounts
let sum = fees.amount().unwrap();
assert_eq!(FeeAmount::from(3), sum);

// check acounts collector
let accounts = fees.accounts();
assert_eq!(vec![FeeAccount::from(Address::zero())], accounts);
}
}
Loading

0 comments on commit f6e1930

Please sign in to comment.