Skip to content

Commit

Permalink
added max=32 validation to contract_addr_len
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed May 28, 2024
1 parent c646f34 commit a71be1f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
6 changes: 6 additions & 0 deletions contracts/main/voice/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub fn instantiate(
if contract_addr_len == 0 {
return Err(ContractError::ContractAddrLenCantBeZero);
}
if contract_addr_len > 32 {
return Err(ContractError::ContractAddrLenCantBeGreaterThan32);
}

PROXY_CODE_ID.save(deps.storage, &msg.proxy_code_id.u64())?;
BLOCK_MAX_GAS.save(deps.storage, &msg.block_max_gas.u64())?;
Expand Down Expand Up @@ -222,6 +225,9 @@ pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result<Response, Co
if contract_addr_len == 0 {
return Err(ContractError::ContractAddrLenCantBeZero);
}
if contract_addr_len > 32 {
return Err(ContractError::ContractAddrLenCantBeGreaterThan32);
}

// update the proxy code ID, block max gas, and contract addr len
PROXY_CODE_ID.save(deps.storage, &proxy_code_id.u64())?;
Expand Down
3 changes: 3 additions & 0 deletions contracts/main/voice/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ pub enum ContractError {

#[error("Contract address length can't be zero")]
ContractAddrLenCantBeZero,

#[error("Contract address length can't be greater than 32")]
ContractAddrLenCantBeGreaterThan32,
}
7 changes: 6 additions & 1 deletion contracts/main/voice/src/suite_tests/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ impl SuiteBuilder {
self.instantiate.proxy_code_id = code_id;
self
}

pub fn with_contract_addr_len(mut self, len: Option<u8>) -> Self {
self.instantiate.contract_addr_len = len;
self
}
}

impl Suite {
Expand Down Expand Up @@ -120,7 +125,7 @@ impl Suite {
&MigrateMsg::WithUpdate {
proxy_code_id: contract_code_id.into(),
block_max_gas: block_max_gas.into(),
contract_addr_len: contract_addr_len,
contract_addr_len,
},
self.voice_code,
)
Expand Down
32 changes: 32 additions & 0 deletions contracts/main/voice/src/suite_tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,22 @@ fn test_gas_validation() {
.build();
}

#[test]
#[should_panic]
fn test_contract_addr_len_min_validation() {
SuiteBuilder::default()
.with_contract_addr_len(Some(0))
.build();
}

#[test]
#[should_panic]
fn test_contract_addr_len_max_validation() {
SuiteBuilder::default()
.with_contract_addr_len(Some(33))
.build();
}

#[test]
fn test_migrate_validation() {
let mut suite = SuiteBuilder::default().build();
Expand All @@ -98,4 +114,20 @@ fn test_migrate_validation() {
.unwrap();

assert_eq!(err, ContractError::GasLimitsMismatch);

let err = suite
.update(Addr::unchecked(CREATOR_ADDR), 1, 110_000, 0)
.unwrap_err()
.downcast::<ContractError>()
.unwrap();

assert_eq!(err, ContractError::ContractAddrLenCantBeZero);

let err = suite
.update(Addr::unchecked(CREATOR_ADDR), 1, 110_000, 33)
.unwrap_err()
.downcast::<ContractError>()
.unwrap();

assert_eq!(err, ContractError::ContractAddrLenCantBeGreaterThan32);
}

0 comments on commit a71be1f

Please sign in to comment.