Skip to content

Commit 39aee7d

Browse files
committed
refactor(gamma): big decimal
1 parent 1216305 commit 39aee7d

File tree

5 files changed

+30
-19
lines changed

5 files changed

+30
-19
lines changed

Cargo.lock

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/audit/src/offline/bin/main.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ fn main() -> Result<(), Box<dyn Error>> {
6868
info!("Audit Tool.");
6969
info!("Starting Offline Tally");
7070

71+
if let Some(gamma) = args.gamma {
72+
const GAMMA: &str = "QUADRATIC_VOTING_GAMMA";
73+
std::env::set_var(GAMMA, gamma);
74+
}
75+
76+
if let Some(precision) = args.precision {
77+
const PRECISION: &str = "QUADRATIC_VOTING_PRECISION";
78+
std::env::set_var(PRECISION, precision);
79+
}
80+
7181
// Load and replay fund fragments from storage
7282
let storage_path = PathBuf::from(args.fragments);
7383

@@ -102,16 +112,6 @@ fn main() -> Result<(), Box<dyn Error>> {
102112
// use tally tool to validate decrypted results
103113
let shares_and_results = extract_decryption_shares_and_results(all_fragments);
104114

105-
if let Some(gamma) = args.gamma {
106-
const GAMMA: &str = "QUADRATIC_VOTING_GAMMA";
107-
std::env::set_var(GAMMA, gamma);
108-
}
109-
110-
if let Some(precision) = args.precision {
111-
const PRECISION: &str = "QUADRATIC_VOTING_PRECISION";
112-
std::env::set_var(PRECISION, precision);
113-
}
114-
115115
// Compare decrypted tallies with official results if provided
116116
if let Some(official_results) = args.official_results {
117117
// official catalyst results in json format

src/chain-libs/chain-impl-mockchain/src/vote/tally.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,15 @@ impl TallyResult {
178178

179179
// Apply quadratic scaling if gamma value specified in env var. Else gamma is 1 and has no effect.
180180
let gamma = env::var(GAMMA).unwrap_or(1.to_string());
181+
181182
let precision =
182183
i64::from_str(&env::var(PRECISION).unwrap_or(1.to_string())).unwrap_or(1);
183184

184-
let gamma = BigDecimal::from_str(&gamma).unwrap_or(BigDecimal::from(1));
185+
let mut gamma = BigDecimal::from_str(&gamma).unwrap_or(BigDecimal::from(1));
186+
// Gamma must be between 0 and 1, anything else is treated as bad input; defaulting gamma to 1.
187+
if gamma < BigDecimal::from(0) || gamma > BigDecimal::from(1) {
188+
gamma = BigDecimal::from(1);
189+
}
185190
let stake = BigDecimal::from(weight.0);
186191

187192
let weight = (gamma * stake)

src/chain-libs/chain-vote/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ cryptoxide = "^0.4.2"
1515
const_format = "0.2"
1616
base64 = "0.21.0"
1717
bigdecimal = "0.4.7"
18-
18+
tracing = "0.1.41"
1919

2020

2121
[dev-dependencies]

src/chain-libs/chain-vote/src/tally.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::num::NonZeroI64;
22
use std::num::NonZeroU64;
33

44
use std::str::FromStr;
5+
use tracing::info;
56

67
use crate::GroupElement;
78
use crate::{
@@ -172,7 +173,11 @@ impl EncryptedTally {
172173
let gamma = env::var(GAMMA).unwrap_or(1.to_string());
173174
let precision = i64::from_str(&env::var(PRECISION).unwrap_or(1.to_string())).unwrap_or(1);
174175

175-
let gamma = BigDecimal::from_str(&gamma).unwrap_or(BigDecimal::from(1));
176+
let mut gamma = BigDecimal::from_str(&gamma).unwrap_or(BigDecimal::from(1));
177+
// Gamma must be between 0 and 1, anything else is treated as bad input; defaulting gamma to 1.
178+
if gamma < BigDecimal::from(0) || gamma > BigDecimal::from(1) {
179+
gamma = BigDecimal::from(1);
180+
}
176181
let stake = BigDecimal::from(weight);
177182

178183
let weight = (gamma * stake).round(precision).to_u64().unwrap_or(weight);

0 commit comments

Comments
 (0)