Skip to content

Commit

Permalink
Merge branch 'tiago/rm-from-u64-on-ethbridge-stake' (#1692)
Browse files Browse the repository at this point in the history
* origin/tiago/rm-from-u64-on-ethbridge-stake:
  changelog: add #1692
  Call try_into() instead of into() on EthBridgeVotingPower
  Replace From<u64> with TryFrom<u64> impl on EthBridgeVotingPower
  Use new const in From impl
  Add MAX value for EthBridgeVotingPower
  • Loading branch information
Fraccaman committed Jul 21, 2023
2 parents af8d6c6 + c0cbacb commit 57bd106
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Removed `impl From<u64> for EthBridgeVotingPower` and replaced it with a
`TryFrom`. ([\#1692](https://github.com/anoma/namada/pull/1692))
2 changes: 1 addition & 1 deletion core/src/types/eth_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ mod tests {
)
.expect("Test failed"),
],
voting_powers: vec![8828299.into()],
voting_powers: vec![8828299.try_into().unwrap()],
epoch: 0.into(),
};
let encoded = valset_update.encode().into_inner();
Expand Down
22 changes: 18 additions & 4 deletions core/src/types/voting_power.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,31 @@ use crate::types::uint::Uint;
)]
pub struct EthBridgeVotingPower(u64);

impl From<u64> for EthBridgeVotingPower {
fn from(val: u64) -> Self {
Self(val)
impl EthBridgeVotingPower {
/// Maximum value that can be represented for the voting power
/// stored in an Ethereum bridge smart contract.
pub const MAX: Self = Self(1 << 32);
}

impl TryFrom<u64> for EthBridgeVotingPower {
type Error = ();

#[inline]
fn try_from(val: u64) -> Result<Self, ()> {
if val <= Self::MAX.0 {
Ok(Self(val))
} else {
Err(())
}
}
}

impl From<&FractionalVotingPower> for EthBridgeVotingPower {
fn from(ratio: &FractionalVotingPower) -> Self {
// normalize the voting power
// https://github.com/anoma/ethereum-bridge/blob/fe93d2e95ddb193a759811a79c8464ad4d709c12/test/utils/utilities.js#L29
const NORMALIZED_VOTING_POWER: Uint = Uint::from_u64(1 << 32);
const NORMALIZED_VOTING_POWER: Uint =
Uint::from_u64(EthBridgeVotingPower::MAX.0);

let voting_power = ratio.0 * NORMALIZED_VOTING_POWER;
let voting_power = voting_power.round().to_integer().low_u64();
Expand Down

0 comments on commit 57bd106

Please sign in to comment.