clean up error messages, duplicated files, fix bug where asset can be attested multiple times

Change-Id: I95655968d3582e837a28eddc34d15d4ca55a488b
This commit is contained in:
Alwin 2021-05-31 18:04:12 -04:00
parent 16cc520187
commit 08ca466a40
6 changed files with 19 additions and 193 deletions

View File

@ -1,67 +0,0 @@
use cosmwasm_std::{CanonicalAddr, StdError, StdResult};
pub trait ByteUtils {
fn get_u8(&self, index: usize) -> u8;
fn get_u16(&self, index: usize) -> u16;
fn get_u32(&self, index: usize) -> u32;
fn get_u64(&self, index: usize) -> u64;
fn get_u128_be(&self, index: usize) -> u128;
/// High 128 then low 128
fn get_u256(&self, index: usize) -> (u128, u128);
fn get_address(&self, index: usize) -> CanonicalAddr;
fn get_bytes32(&self, index: usize) -> &[u8];
}
impl ByteUtils for &[u8] {
fn get_u8(&self, index: usize) -> u8 {
self[index]
}
fn get_u16(&self, index: usize) -> u16 {
let mut bytes: [u8; 16 / 8] = [0; 16 / 8];
bytes.copy_from_slice(&self[index..index + 2]);
u16::from_be_bytes(bytes)
}
fn get_u32(&self, index: usize) -> u32 {
let mut bytes: [u8; 32 / 8] = [0; 32 / 8];
bytes.copy_from_slice(&self[index..index + 4]);
u32::from_be_bytes(bytes)
}
fn get_u64(&self, index: usize) -> u64 {
let mut bytes: [u8; 64 / 8] = [0; 64 / 8];
bytes.copy_from_slice(&self[index..index + 8]);
u64::from_be_bytes(bytes)
}
fn get_u128_be(&self, index: usize) -> u128 {
let mut bytes: [u8; 128 / 8] = [0; 128 / 8];
bytes.copy_from_slice(&self[index..index + 128 / 8]);
u128::from_be_bytes(bytes)
}
fn get_u256(&self, index: usize) -> (u128, u128) {
(self.get_u128_be(index), self.get_u128_be(index + 128 / 8))
}
fn get_address(&self, index: usize) -> CanonicalAddr {
// 32 bytes are reserved for addresses, but only the last 20 bytes are taken by the actual address
CanonicalAddr::from(&self[index + 32 - 20..index + 32])
}
fn get_bytes32(&self, index: usize) -> &[u8] {
&self[index..index + 32]
}
}
pub fn extend_address_to_32(addr: &CanonicalAddr) -> Vec<u8> {
let mut result: Vec<u8> = vec![0; 12];
result.extend(addr.as_slice());
result
}
pub fn extend_string_to_32(s: &String) -> StdResult<Vec<u8>> {
let bytes = s.as_bytes();
if bytes.len() > 32 {
return Err(StdError::generic_err("string more than 32 "))
}
let mut result = vec![0; 32 - bytes.len()];
result.extend(bytes);
Ok(result)
}

View File

@ -4,15 +4,15 @@ use cosmwasm_std::{
InitResponse, Querier, QueryRequest, StdError, StdResult, Storage, Uint128, WasmMsg, WasmQuery,
};
use crate::byte_utils::ByteUtils;
use crate::byte_utils::{extend_address_to_32, extend_string_to_32};
use crate::error::ContractError;
use crate::msg::{HandleMsg, InitMsg, QueryMsg};
use crate::state::{
bridge_contracts, bridge_contracts_read, config, config_read, wrapped_asset,
wrapped_asset_address, wrapped_asset_address_read, wrapped_asset_read, Action, AssetMeta,
ConfigInfo, TokenBridgeMessage, TransferInfo,
};
use wormhole::byte_utils::ByteUtils;
use wormhole::byte_utils::{extend_address_to_32, extend_string_to_32};
use wormhole::error::ContractError;
use cw20_base::msg::HandleMsg as TokenMsg;
use cw20_base::msg::QueryMsg as TokenQuery;
@ -173,12 +173,23 @@ fn handle_attest_meta<S: Storage, A: Api, Q: Querier>(
) -> StdResult<HandleResponse> {
let meta = AssetMeta::deserialize(data)?;
if CHAIN_ID == meta.token_chain {
return Err(StdError::generic_err("matching chain id, kinda cringe"));
return Err(StdError::generic_err(
"this asset is native to this chain and should not be attested",
));
}
let cfg = config_read(&deps.storage).load()?;
let asset_id = build_asset_id(meta.token_chain, &meta.token_address.as_slice());
if wrapped_asset_read(&mut deps.storage)
.load(&asset_id)
.is_ok()
{
return Err(StdError::generic_err(
"this asset has already been attested",
));
}
wrapped_asset(&mut deps.storage).save(&asset_id, &HumanAddr::from(WRAPPED_ASSET_UPDATING))?;
Ok(HandleResponse {
@ -296,7 +307,7 @@ fn handle_complete_transfer<S: Storage, A: Api, Q: Querier>(
if transfer_info.recipient_chain != CHAIN_ID {
return Err(StdError::generic_err(
"you sent the message to the wrong chain, idiot",
"this transfer is not directed at this chain",
));
}

View File

@ -1,114 +0,0 @@
use cosmwasm_std::StdError;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ContractError {
/// Invalid VAA version
#[error("InvalidVersion")]
InvalidVersion,
/// Guardian set with this index does not exist
#[error("InvalidGuardianSetIndex")]
InvalidGuardianSetIndex,
/// Guardian set expiration date is zero or in the past
#[error("GuardianSetExpired")]
GuardianSetExpired,
/// Not enough signers on the VAA
#[error("NoQuorum")]
NoQuorum,
/// Wrong guardian index order, order must be ascending
#[error("WrongGuardianIndexOrder")]
WrongGuardianIndexOrder,
/// Some problem with signature decoding from bytes
#[error("CannotDecodeSignature")]
CannotDecodeSignature,
/// Some problem with public key recovery from the signature
#[error("CannotRecoverKey")]
CannotRecoverKey,
/// Recovered pubkey from signature does not match guardian address
#[error("GuardianSignatureError")]
GuardianSignatureError,
/// VAA action code not recognized
#[error("InvalidVAAAction")]
InvalidVAAAction,
/// VAA guardian set is not current
#[error("NotCurrentGuardianSet")]
NotCurrentGuardianSet,
/// Only 128-bit amounts are supported
#[error("AmountTooHigh")]
AmountTooHigh,
/// Amount should be higher than zero
#[error("AmountTooLow")]
AmountTooLow,
/// Source and target chain ids must be different
#[error("SameSourceAndTarget")]
SameSourceAndTarget,
/// Target chain id must be the same as the current CHAIN_ID
#[error("WrongTargetChain")]
WrongTargetChain,
/// Wrapped asset init hook sent twice for the same asset id
#[error("AssetAlreadyRegistered")]
AssetAlreadyRegistered,
/// Guardian set must increase in steps of 1
#[error("GuardianSetIndexIncreaseError")]
GuardianSetIndexIncreaseError,
/// VAA was already executed
#[error("VaaAlreadyExecuted")]
VaaAlreadyExecuted,
/// Message sender not permitted to execute this operation
#[error("PermissionDenied")]
PermissionDenied,
/// Could not decode target address from canonical to human-readable form
#[error("WrongTargetAddressFormat")]
WrongTargetAddressFormat,
/// More signatures than active guardians found
#[error("TooManySignatures")]
TooManySignatures,
/// Wrapped asset not found in the registry
#[error("AssetNotFound")]
AssetNotFound,
/// Generic error when there is a problem with VAA structure
#[error("InvalidVAA")]
InvalidVAA,
/// Thrown when fee is enabled for the action, but was not sent with the transaction
#[error("FeeTooLow")]
FeeTooLow,
/// Registering asset outside of the wormhole
#[error("RegistrationForbidden")]
RegistrationForbidden,
}
impl ContractError {
pub fn std(&self) -> StdError {
StdError::GenericErr {
msg: format!("{}", self),
backtrace: None,
}
}
pub fn std_err<T>(&self) -> Result<T, StdError> {
Err(self.std())
}
}

View File

@ -2,13 +2,9 @@
#[macro_use]
extern crate lazy_static;
mod byte_utils;
pub mod contract;
mod error;
pub mod msg;
pub mod state;
pub use crate::error::ContractError;
#[cfg(all(target_arch = "wasm32", not(feature = "library")))]
cosmwasm_std::create_entry_points!(contract);

View File

@ -7,7 +7,7 @@ use cosmwasm_storage::{
Singleton,
};
use crate::byte_utils::ByteUtils;
use wormhole::byte_utils::ByteUtils;
pub static CONFIG_KEY: &[u8] = b"config";

View File

@ -2,9 +2,9 @@
#[macro_use]
extern crate lazy_static;
mod byte_utils;
pub mod byte_utils;
pub mod contract;
mod error;
pub mod error;
pub mod msg;
pub mod state;