Skip to content

Commit

Permalink
Improved filtering owned requests
Browse files Browse the repository at this point in the history
  • Loading branch information
miloszm committed Sep 28, 2023
1 parent 7dab433 commit 046f305
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
24 changes: 9 additions & 15 deletions license-provider/src/reference_lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl ReferenceLP {
RequestScanner::scan_block_range(height, height_end, cfg)
.await?;
total += requests.len();
let owned_requests = self.filter_owned_requests(&requests)?;
let owned_requests = self.retain_owned_requests(requests);
total_owned += owned_requests.len();
for owned_request in owned_requests {
self.insert_request(owned_request);
Expand All @@ -95,28 +95,22 @@ impl ReferenceLP {
let mut total_owned = 0usize;
let requests = RequestScanner::scan_last_blocks(n, cfg).await?;
total += requests.len();
let owned_requests = self.filter_owned_requests(&requests)?;
let owned_requests = self.retain_owned_requests(requests);
total_owned += owned_requests.len();
for owned_request in owned_requests {
self.insert_request(owned_request);
}
Ok((total, total_owned))
}

/// Given a collection of requests, returns a new collection
/// containing only requests relevant to `this` license provider
pub fn filter_owned_requests(
/// Given a collection of requests, retain only those requests
/// in the collection which are owned by 'this' license provider
pub fn retain_owned_requests(
&self,
requests: &[Request],
) -> Result<Vec<Request>, Error> {
let mut relevant_requests: Vec<Request> = Vec::new();
for request in requests.iter() {
if self.is_owned_request(request) {
let r = Request { ..*request };
relevant_requests.push(r);
}
}
Ok(relevant_requests)
mut requests: Vec<Request>,
) -> Vec<Request> {
requests.retain(|request| self.is_owned_request(request));
requests
}

fn is_owned_request(&self, request: &Request) -> bool {
Expand Down
7 changes: 3 additions & 4 deletions license-provider/tests/test_lp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ fn lp_filter_requests() -> Result<(), Error> {
.expect("transactions file should load correctly");

let requests = RequestScanner::scan_transactions(txs);

let relevant_requests = reference_lp.filter_owned_requests(&requests)?;

assert_eq!(requests.len(), 9);
assert_eq!(relevant_requests.len(), 2);

let owned_requests = reference_lp.retain_owned_requests(requests);
assert_eq!(owned_requests.len(), 2);

Ok(())
}
1 change: 0 additions & 1 deletion moat-core/src/blockchain_requests/request_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ impl RequestScanner {
height_end: u64,
cfg: &BlockchainAccessConfig,
) -> Result<(Vec<Request>, u64), Error> {
// let client = Client::new(cfg.graphql_address.clone());
let client = RuskHttpClient::new(cfg.rusk_address.clone());
let (txs, top) =
TxRetriever::txs_from_block_range(&client, height_beg, height_end)
Expand Down

0 comments on commit 046f305

Please sign in to comment.