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

Remove From<u64> on Ethereum bridge voting power type #1692

Merged
merged 5 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
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
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
Loading