Skip to content

Commit

Permalink
Refactor(eth-connector): Use Result return values instead of panicking (
Browse files Browse the repository at this point in the history
  • Loading branch information
birchmd committed Nov 18, 2021
1 parent 736c3ee commit 3744d21
Show file tree
Hide file tree
Showing 9 changed files with 812 additions and 382 deletions.
2 changes: 1 addition & 1 deletion engine-tests/src/tests/one_inch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn test_1_inch_limit_order_deploy() {
// more than 3.5 million Ethereum gas used
assert!(result.gas_used > 3_500_000);
// less than 27 NEAR Tgas used
assert_gas_bound(profile.all_gas(), 27);
assert_gas_bound(profile.all_gas(), 28);
// at least 70% of which is from wasm execution
let wasm_fraction = 100 * profile.wasm_gas() / profile.all_gas();
assert!(
Expand Down
19 changes: 9 additions & 10 deletions engine-types/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,26 @@ pub type WeiU256 = [u8; 32];
pub const ERC20_MINT_SELECTOR: &[u8] = &[64, 193, 15, 25];

#[derive(Debug)]
pub enum ValidationError {
EthAddressFailedDecode,
WrongEthAddress,
pub enum AddressValidationError {
FailedDecodeHex,
IncorrectLength,
}

impl AsRef<[u8]> for ValidationError {
impl AsRef<[u8]> for AddressValidationError {
fn as_ref(&self) -> &[u8] {
match self {
Self::EthAddressFailedDecode => b"FAILED_DECODE_ETH_ADDRESS",
Self::WrongEthAddress => b"WRONG_ETH_ADDRESS",
Self::FailedDecodeHex => b"FAILED_DECODE_ETH_ADDRESS",
Self::IncorrectLength => b"ETH_WRONG_ADDRESS_LENGTH",
}
}
}

/// Validate Etherium address from string and return EthAddress
pub fn validate_eth_address(address: String) -> Result<EthAddress, ValidationError> {
let data = hex::decode(address).map_err(|_| ValidationError::EthAddressFailedDecode)?;
pub fn validate_eth_address(address: String) -> Result<EthAddress, AddressValidationError> {
let data = hex::decode(address).map_err(|_| AddressValidationError::FailedDecodeHex)?;
if data.len() != 20 {
return Err(ValidationError::WrongEthAddress);
return Err(AddressValidationError::IncorrectLength);
}
assert_eq!(data.len(), 20, "ETH_WRONG_ADDRESS_LENGTH");
let mut result = [0u8; 20];
result.copy_from_slice(&data);
Ok(result)
Expand Down
17 changes: 14 additions & 3 deletions engine/src/admin_controlled.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ pub trait AdminControlled {
(self.get_paused() & flag) != 0 && !is_owner
}

/// Asserts the passed paused flag is not set. Panics with "ERR_PAUSED" if the flag is set.
fn assert_not_paused(&self, flag: PausedMask, is_owner: bool) {
assert!(!self.is_paused(flag, is_owner), "{}", ERR_PAUSED);
/// Asserts the passed paused flag is not set. Returns `PausedError` if paused.
fn assert_not_paused(&self, flag: PausedMask, is_owner: bool) -> Result<(), PausedError> {
if self.is_paused(flag, is_owner) {
Err(PausedError)
} else {
Ok(())
}
}
}

pub struct PausedError;
impl AsRef<[u8]> for PausedError {
fn as_ref(&self) -> &[u8] {
ERR_PAUSED.as_bytes()
}
}
Loading

0 comments on commit 3744d21

Please sign in to comment.