Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P-54 Binary search for holding time assertion #2401

Merged
Merged
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
13 changes: 7 additions & 6 deletions tee-worker/litentry/core/assertion-build/src/holding_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn prepare_min_balance(
.map_err(|_| emit_error(htype, min_balance, ErrorDetail::ParseError))
}

#[derive(Clone)]
// Represents an individual account that may or not be holding the desired token amount
struct Account {
network: Web3Network,
Expand Down Expand Up @@ -239,9 +240,9 @@ fn search_holding_date(
) -> core::result::Result<Option<&'static str>, ErrorDetail> {
let mut client = AchainableClient::new(data_provider_config);

let mut pred = |date| {
let mut pred = |date: &&str| {
let (outcome, new_accounts) =
holding_time_search_step(&mut client, q_min_balance, accounts, date);
holding_time_search_step(&mut client, q_min_balance, accounts.clone(), *date);
accounts = new_accounts;
outcome.map(|is_holding| !is_holding) // negated to match the partition_point API
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Kailai-Wang for fixing this issue! Still gotta wrap my head around some of the subtleties here, apparently :D

In this case the extra clone shouldn't make a huge difference overall, since the cost is in any case dominated by the HTTP requests; in general though it does feel a bit wasteful to have to clone the array each time, only to replace the original anyway after the call to holding_time_search_step. What would (in theory) have been the better approach here? (Not for this PR to be sure, just as an overall learning)

Expand Down Expand Up @@ -311,10 +312,10 @@ fn match_token_address(htype: &AmountHoldingTimeType, network: &Web3Network) ->
}
}

fn partition_point<T, E>(
vector: &Vec<T>,
pred: &mut dyn FnMut(&T) -> core::result::Result<bool, E>,
) -> core::result::Result<usize, E> {
fn partition_point<T, E, P>(vector: &Vec<T>, pred: &mut P) -> core::result::Result<usize, E>
where
P: FnMut(&T) -> core::result::Result<bool, E>,
{
let mut trapped_error: Option<E> = None;
let wrapped_pred = |element: &T| -> bool {
if trapped_error.is_some() {
Expand Down
Loading