From 67c9b44460a74bcea2c7be87eebd87602d40a12a Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Sun, 9 Jul 2023 22:09:19 +0100 Subject: [PATCH 1/5] Add MAX value for EthBridgeVotingPower --- core/src/types/voting_power.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/src/types/voting_power.rs b/core/src/types/voting_power.rs index 96a9579279..9fdc5f47c0 100644 --- a/core/src/types/voting_power.rs +++ b/core/src/types/voting_power.rs @@ -31,6 +31,12 @@ use crate::types::uint::Uint; )] pub struct EthBridgeVotingPower(u64); +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 From for EthBridgeVotingPower { fn from(val: u64) -> Self { Self(val) From 2425cce7097075f46aa37652ac35d73031b3a86a Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Sun, 9 Jul 2023 22:10:25 +0100 Subject: [PATCH 2/5] Use new const in From impl --- core/src/types/voting_power.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/types/voting_power.rs b/core/src/types/voting_power.rs index 9fdc5f47c0..8fa594d693 100644 --- a/core/src/types/voting_power.rs +++ b/core/src/types/voting_power.rs @@ -47,7 +47,8 @@ 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(); From 8df0a04663bfa6faf1f7ec7bf5325e7f52aeeb19 Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Sun, 9 Jul 2023 22:12:35 +0100 Subject: [PATCH 3/5] Replace From with TryFrom impl on EthBridgeVotingPower --- core/src/types/voting_power.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/core/src/types/voting_power.rs b/core/src/types/voting_power.rs index 8fa594d693..946e08b834 100644 --- a/core/src/types/voting_power.rs +++ b/core/src/types/voting_power.rs @@ -37,9 +37,16 @@ impl EthBridgeVotingPower { pub const MAX: Self = Self(1 << 32); } -impl From for EthBridgeVotingPower { - fn from(val: u64) -> Self { - Self(val) +impl TryFrom for EthBridgeVotingPower { + type Error = (); + + #[inline] + fn try_from(val: u64) -> Result { + if val <= Self::MAX.0 { + Ok(Self(val)) + } else { + Err(()) + } } } From 9187d2e847440c5c099f44f92d4676c22d60c92d Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Sun, 9 Jul 2023 22:16:03 +0100 Subject: [PATCH 4/5] Call try_into() instead of into() on EthBridgeVotingPower --- core/src/types/eth_abi.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/types/eth_abi.rs b/core/src/types/eth_abi.rs index 5beaa6fe8f..adda87e6c9 100644 --- a/core/src/types/eth_abi.rs +++ b/core/src/types/eth_abi.rs @@ -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(); From c0cbacbcfeb927a4baa4027961db0be200709d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Zemanovi=C4=8D?= Date: Wed, 12 Jul 2023 08:30:06 +0100 Subject: [PATCH 5/5] changelog: add #1692 --- .../improvements/1692-rm-from-u64-on-ethbridge-stake.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changelog/unreleased/improvements/1692-rm-from-u64-on-ethbridge-stake.md diff --git a/.changelog/unreleased/improvements/1692-rm-from-u64-on-ethbridge-stake.md b/.changelog/unreleased/improvements/1692-rm-from-u64-on-ethbridge-stake.md new file mode 100644 index 0000000000..4c6813670c --- /dev/null +++ b/.changelog/unreleased/improvements/1692-rm-from-u64-on-ethbridge-stake.md @@ -0,0 +1,2 @@ +- Removed `impl From for EthBridgeVotingPower` and replaced it with a + `TryFrom`. ([\#1692](https://github.com/anoma/namada/pull/1692)) \ No newline at end of file