Skip to content

Commit

Permalink
added limits per validator (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
janlegner committed Dec 5, 2023
1 parent 8fd325f commit be6bc45
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions insurance-engine/src/merkle_tree_collection.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::insurance_claims::InsuranceClaim;

use {
crate::insurance_claims::InsuranceClaimCollection,
crate::insurance_claims::{InsuranceClaim, InsuranceClaimCollection},
merkle_tree::MerkleTree,
serde::{Deserialize, Serialize},
solana_sdk::hash::{Hash, Hasher},
std::collections::HashMap,
};

#[derive(Default, Clone, Eq, Debug, Hash, PartialEq, Deserialize, Serialize)]
Expand All @@ -27,13 +26,21 @@ impl TreeNode {
}
}

#[derive(Default, Clone, Deserialize, Serialize)]
pub struct ClaimLimit {
pub vote_account: String,
pub max_total_claim_sum: u64,
pub max_total_claims: usize,
}

#[derive(Default, Clone, Deserialize, Serialize)]
pub struct MerkleTreeCollection {
pub epoch: u64,
pub slot: u64,
pub merkle_root: Option<Hash>,
pub max_total_claim_sum: u64,
pub max_total_claims: usize,
pub claim_limits: Vec<ClaimLimit>,
pub tree_nodes: Vec<TreeNode>,
}

Expand Down Expand Up @@ -61,6 +68,34 @@ pub fn generate_merkle_tree_collection(
)
.collect();

let claim_limits = insurance_claims_collection
.claims
.iter()
.fold(
HashMap::default(),
|mut claim_limits: HashMap<String, ClaimLimit>,
InsuranceClaim {
vote_account,
claim,
..
}| {
let claim_limit = claim_limits
.entry(vote_account.clone())
.or_insert(ClaimLimit {
vote_account: vote_account.clone(),
max_total_claim_sum: 0,
max_total_claims: 0,
});
claim_limit.max_total_claims += 1;
claim_limit.max_total_claim_sum += claim;

claim_limits
},
)
.values()
.cloned()
.collect();

let max_total_claim_sum: u64 = tree_nodes.iter().map(|node| node.claim).sum();

let hashed_nodes: Vec<[u8; 32]> = tree_nodes.iter().map(|n| n.hash().to_bytes()).collect();
Expand All @@ -76,6 +111,7 @@ pub fn generate_merkle_tree_collection(
merkle_root: merkle_tree.get_root().cloned(),
max_total_claim_sum,
max_total_claims: tree_nodes.len(),
claim_limits,
tree_nodes,
})
}
Expand Down

0 comments on commit be6bc45

Please sign in to comment.