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

Added claim limits per validator #7

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Changes from all commits
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
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