feat: Support incremental mode:

- Support incremental mode in heromodels
- Updated the example to refelct the changes
- Updated the tests to reflect the changes
This commit is contained in:
Mahmoud Emad
2025-05-17 11:12:09 +03:00
parent bd4770b99b
commit bde5db0e52
16 changed files with 1074 additions and 136 deletions

View File

@@ -6,9 +6,9 @@ use heromodels::models::governance::{Proposal, ProposalStatus, VoteEventStatus};
fn main() {
println!("Governance Proposal Model Example\n");
// Create a new proposal
// Create a new proposal with auto-generated ID
let mut proposal = Proposal::new(
1, // id
None, // id (auto-generated)
"user_creator_123", // creator_id
"Community Fund Allocation for Q3", // title
"Proposal to allocate funds for community projects in the third quarter.", // description
@@ -33,18 +33,18 @@ fn main() {
// Simulate casting votes
println!("Simulating Votes...");
// User 1 votes for 'Approve Allocation' with 100 shares
proposal = proposal.cast_vote(101, 1, 1, 100);
// User 2 votes for 'Reject Allocation' with 50 shares
proposal = proposal.cast_vote(102, 2, 2, 50);
// User 3 votes for 'Approve Allocation' with 75 shares
proposal = proposal.cast_vote(103, 3, 1, 75);
// User 4 abstains with 20 shares
proposal = proposal.cast_vote(104, 4, 3, 20);
// User 1 votes for 'Approve Allocation' with 100 shares (with explicit ballot ID)
proposal = proposal.cast_vote(Some(101), 1, 1, 100);
// User 2 votes for 'Reject Allocation' with 50 shares (with explicit ballot ID)
proposal = proposal.cast_vote(Some(102), 2, 2, 50);
// User 3 votes for 'Approve Allocation' with 75 shares (with auto-generated ballot ID)
proposal = proposal.cast_vote(None, 3, 1, 75);
// User 4 abstains with 20 shares (with auto-generated ballot ID)
proposal = proposal.cast_vote(None, 4, 3, 20);
// User 5 attempts to vote for a non-existent option (should be handled gracefully)
proposal = proposal.cast_vote(105, 5, 99, 10);
proposal = proposal.cast_vote(Some(105), 5, 99, 10);
// User 1 tries to vote again (not explicitly prevented by current model, but could be a future enhancement)
// proposal = proposal.cast_vote(106, 1, 1, 10);
// proposal = proposal.cast_vote(Some(106), 1, 1, 10);
println!("\nVote Counts After Simulation:");
for option in &proposal.options {
@@ -53,7 +53,7 @@ fn main() {
println!("\nBallots Cast:");
for ballot in &proposal.ballots {
println!("- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
println!("- Ballot ID: {}, User ID: {}, Option ID: {}, Shares: {}",
ballot.base_data.id, ballot.user_id, ballot.vote_option_id, ballot.shares_count);
}
println!("");
@@ -68,7 +68,7 @@ fn main() {
// Attempt to cast a vote after closing (should be handled)
println!("\nAttempting to cast vote after voting is closed...");
proposal = proposal.cast_vote(107, 6, 1, 25);
proposal = proposal.cast_vote(None, 6, 1, 25);
// Final proposal state
println!("\nFinal Proposal State:");
@@ -83,24 +83,24 @@ fn main() {
// Example of a private proposal (not fully implemented in cast_vote eligibility yet)
let mut private_proposal = Proposal::new(
2,
"user_admin_001",
"Internal Team Restructure Vote",
"Vote on proposed internal team changes.",
Utc::now(),
Some(2), // explicit ID
"user_admin_001",
"Internal Team Restructure Vote",
"Vote on proposed internal team changes.",
Utc::now(),
Utc::now() + Duration::days(7)
);
private_proposal.private_group = Some(vec![10, 20, 30]); // Only users 10, 20, 30 can vote
private_proposal = private_proposal.add_option(1, "Accept Restructure");
private_proposal = private_proposal.add_option(2, "Reject Restructure");
println!("\nCreated Private Proposal: '{}'", private_proposal.title);
println!("Eligible Voters (Group): {:?}", private_proposal.private_group);
// User 10 (eligible) votes
private_proposal = private_proposal.cast_vote(201, 10, 1, 100);
// User 40 (ineligible) tries to vote
private_proposal = private_proposal.cast_vote(202, 40, 1, 50);
// User 10 (eligible) votes with explicit ballot ID
private_proposal = private_proposal.cast_vote(Some(201), 10, 1, 100);
// User 40 (ineligible) tries to vote with auto-generated ballot ID
private_proposal = private_proposal.cast_vote(None, 40, 1, 50);
println!("Private Proposal Vote Counts:");
for option in &private_proposal.options {
println!(" - {}: {} (Votes: {})", option.id, option.text, option.count);