-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #51 from commerceblock/feature/responseModel
Improve Response model
- Loading branch information
Showing
9 changed files
with
212 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
//! # Response | ||
//! | ||
//! Response model for service challenge responses | ||
use std::collections::HashMap; | ||
|
||
use crate::challenger::ChallengeResponseIds; | ||
use bitcoin_hashes::sha256d; | ||
use serde::Serialize; | ||
|
||
/// Response struct that models responses to service challenges | ||
/// by keeping track of the total number of challengers and the | ||
/// number of challenges that each bid owner responded to | ||
#[derive(Debug, Serialize, PartialEq)] | ||
pub struct Response { | ||
/// Total number of challenges | ||
pub num_challenges: u32, | ||
/// Number of responses per bid txid | ||
pub bid_responses: HashMap<sha256d::Hash, u32>, | ||
} | ||
|
||
impl Response { | ||
/// Create new Response instance | ||
pub fn new() -> Response { | ||
Response { | ||
num_challenges: 0, | ||
bid_responses: HashMap::new(), | ||
} | ||
} | ||
|
||
/// Update Response struct from challenge response ids | ||
pub fn update(&mut self, responses: &ChallengeResponseIds) { | ||
self.num_challenges += 1; | ||
for txid in responses.iter() { | ||
let bid_entry = self.bid_responses.entry(*txid).or_insert(0); | ||
*bid_entry += 1; | ||
} | ||
} | ||
} |
Oops, something went wrong.