Skip to content

Commit

Permalink
fix: store validators as list of byte array
Browse files Browse the repository at this point in the history
  • Loading branch information
sherpalden committed Nov 8, 2024
1 parent 727ec1f commit 788df3e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<'a> ClusterConnection<'a> {
&mut self,
deps: DepsMut,
info: MessageInfo,
validators: Vec<String>,
validators: Vec<Vec<u8>>,
threshold: u8,
) -> Result<Response, ContractError> {
self.ensure_admin(deps.storage, info.sender)?;
Expand Down
15 changes: 8 additions & 7 deletions contracts/cosmwasm-vm/cw-cluster-connection/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ pub fn to_truncated_le_bytes(n: u128) -> Vec<u8> {
let trimmed_bytes = bytes
.iter()
.rev()
.skip_while(|&&b| b == 0).copied()
.skip_while(|&&b| b == 0)
.copied()
.collect::<Vec<u8>>();
trimmed_bytes.into_iter().rev().collect()
}
Expand Down Expand Up @@ -115,7 +116,7 @@ impl<'a> ClusterConnection<'a> {

let message_hash = keccak256(&signed_msg);

let mut signers: HashMap<String, bool> = HashMap::new();
let mut signers: HashMap<Vec<u8>, bool> = HashMap::new();

for signature in signatures {
if signature.len() != 65 {
Expand All @@ -132,18 +133,18 @@ impl<'a> ClusterConnection<'a> {
Ok(pubkey) => {
let pk = VerifyingKey::from_sec1_bytes(&pubkey)
.map_err(|_| ContractError::InvalidSignature)?;
let pk_hex = hex::encode(pk.to_bytes());
if self.is_validator(deps.storage, pk_hex.clone())
&& !signers.contains_key(&pk_hex)
let pk_vec = pk.to_bytes().to_vec();

if self.is_validator(deps.storage, pk_vec.clone())
&& !signers.contains_key(&pk_vec)
{
signers.insert(pk_hex, true);
signers.insert(pk_vec, true);
if signers.len() >= threshold.into() {
return Ok(());
}
}
}
Err(e) => {
println!("am i here{:?}", e);
continue;
}
}
Expand Down
4 changes: 1 addition & 3 deletions contracts/cosmwasm-vm/cw-cluster-connection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {

QueryMsg::GetValidators {} => {
let validators = conn.get_validators(deps.storage)?;
let validators_str: Vec<String> =
validators.iter().map(|addr| addr.to_string()).collect();
to_json_binary(&validators_str)
to_json_binary(&validators)
}

QueryMsg::GetSignatureThreshold {} => {
Expand Down
2 changes: 1 addition & 1 deletion contracts/cosmwasm-vm/cw-cluster-connection/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum ExecuteMsg {
},

SetValidators {
validators: Vec<String>,
validators: Vec<Vec<u8>>,
threshold: u8,
},

Expand Down
14 changes: 9 additions & 5 deletions contracts/cosmwasm-vm/cw-cluster-connection/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct ClusterConnection<'a> {
xcall: Item<'a, Addr>,
admin: Item<'a, Addr>,
relayer: Item<'a, Addr>,
validators: Map<'a, String, bool>,
validators: Map<'a, Vec<u8>, bool>,
signature_threshold: Item<'a, u8>,

message_fee: Map<'a, NetId, u128>,
Expand Down Expand Up @@ -130,15 +130,19 @@ impl<'a> ClusterConnection<'a> {
self.denom.load(store).unwrap()
}

pub fn store_validator(&mut self, store: &mut dyn Storage, validator: String) -> StdResult<()> {
pub fn store_validator(
&mut self,
store: &mut dyn Storage,
validator: Vec<u8>,
) -> StdResult<()> {
self.validators.save(store, validator, &true)?;
Ok(())
}

pub fn remove_validator(
&mut self,
store: &mut dyn Storage,
validator: String,
validator: Vec<u8>,
) -> StdResult<()> {
self.validators.remove(store, validator);
Ok(())
Expand All @@ -158,14 +162,14 @@ impl<'a> ClusterConnection<'a> {
for item in validators_iter {
let (validator_addr, is_active) = item?;
if is_active {
validators_list.push(validator_addr);
validators_list.push(hex::encode(validator_addr));
}
}

Ok(validators_list)
}

pub fn is_validator(&self, store: &dyn Storage, pub_key: String) -> bool {
pub fn is_validator(&self, store: &dyn Storage, pub_key: Vec<u8>) -> bool {
self.validators.has(store, pub_key)
}

Expand Down
64 changes: 43 additions & 21 deletions contracts/cosmwasm-vm/cw-cluster-connection/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,30 @@ fn test_set_relayer_unauthorized() {
pub fn test_set_validators() {
let (mut deps, env, ctx) = instantiate(ADMIN);

let val1 = "02e27e3817bf0b6d451004609c2a5d29fe315dc1d1017500399fab540785958b7a";
let val2 = "03ea8d2913ce5bb5637fe732f920ccee7a454a8f1c32a531e7abc1a58a23cc8db0";
let val3 = "03cc5598f8f40103592b6ed9e04adcf9bd67fe06d677bf5b392af0ad9b553a5b16";
let validators = vec![
"validator1".to_string(),
"validator2".to_string(),
"validator3".to_string(),
hex::decode(val1).unwrap(),
hex::decode(val2).unwrap(),
hex::decode(val3).unwrap(),
];

let threshold = 2;

let msg = ExecuteMsg::SetValidators {
validators: validators.clone(),
validators: validators,
threshold: threshold,
};

let info = mock_info(ADMIN, &[]);
let res = execute(deps.as_mut(), env.clone(), info, msg.clone());
assert!(res.is_ok());

let stored_validators = ctx.get_validators(deps.as_ref().storage).unwrap();
let mut stored_validators = ctx.get_validators(deps.as_ref().storage).unwrap();
let mut set_validators = vec![val1.to_string(), val2.to_string(), val3.to_string()];

assert_eq!(stored_validators, validators);
assert_eq!(stored_validators.sort(), set_validators.sort());

let stored_threshold = ctx.get_signature_threshold(deps.as_ref().storage);
assert_eq!(stored_threshold, threshold);
Expand All @@ -140,10 +144,13 @@ pub fn test_set_validators() {
pub fn test_set_validators_unauthorized() {
let (mut deps, env, ctx) = instantiate(ADMIN);

let val1 = "02e27e3817bf0b6d451004609c2a5d29fe315dc1d1017500399fab540785958b7a";
let val2 = "03ea8d2913ce5bb5637fe732f920ccee7a454a8f1c32a531e7abc1a58a23cc8db0";
let val3 = "03cc5598f8f40103592b6ed9e04adcf9bd67fe06d677bf5b392af0ad9b553a5b16";
let validators = vec![
"validator1".to_string(),
"validator2".to_string(),
"validator3".to_string(),
hex::decode(val1).unwrap(),
hex::decode(val2).unwrap(),
hex::decode(val3).unwrap(),
];

let threshold = 2;
Expand All @@ -163,7 +170,14 @@ pub fn test_set_validators_unauthorized() {
pub fn test_set_validators_empty() {
let (mut deps, env, ctx) = instantiate(ADMIN);

let validators = vec!["val1".to_string(), "val2".to_string(), "val3".to_string()];
let val1 = "02e27e3817bf0b6d451004609c2a5d29fe315dc1d1017500399fab540785958b7a";
let val2 = "03ea8d2913ce5bb5637fe732f920ccee7a454a8f1c32a531e7abc1a58a23cc8db0";
let val3 = "03cc5598f8f40103592b6ed9e04adcf9bd67fe06d677bf5b392af0ad9b553a5b16";
let validators = vec![
hex::decode(val1).unwrap(),
hex::decode(val2).unwrap(),
hex::decode(val3).unwrap(),
];

let info = mock_info(ADMIN, &[]);
let res = execute(
Expand All @@ -188,18 +202,27 @@ pub fn test_set_validators_empty() {
);
assert!(res.is_ok());

let stored_validators = ctx.get_validators(deps.as_ref().storage).unwrap();
let mut stored_validators = ctx.get_validators(deps.as_ref().storage).unwrap();
let stored_threshold = ctx.get_signature_threshold(deps.as_ref().storage);

assert_eq!(stored_validators, validators);
let mut set_validators = vec![val1.to_string(), val2.to_string(), val3.to_string()];
assert_eq!(set_validators.sort(), stored_validators.sort());

assert_eq!(stored_threshold, 2);
}

#[test]
pub fn test_set_validators_invalid_threshold() {
let (mut deps, env, ctx) = instantiate(ADMIN);

let validators = vec!["val1".to_string(), "val2".to_string(), "val3".to_string()];
let val1 = "02e27e3817bf0b6d451004609c2a5d29fe315dc1d1017500399fab540785958b7a";
let val2 = "03ea8d2913ce5bb5637fe732f920ccee7a454a8f1c32a531e7abc1a58a23cc8db0";
let val3 = "03cc5598f8f40103592b6ed9e04adcf9bd67fe06d677bf5b392af0ad9b553a5b16";
let validators = vec![
hex::decode(val1).unwrap(),
hex::decode(val2).unwrap(),
hex::decode(val3).unwrap(),
];

let info = mock_info(ADMIN, &[]);
let res = execute(
Expand Down Expand Up @@ -372,8 +395,9 @@ pub fn test_send_message_unauthorized() {
pub fn test_recv_message() {
let (mut deps, env, ctx) = instantiate(ADMIN);

let validators =
vec!["03ea8d2913ce5bb5637fe732f920ccee7a454a8f1c32a531e7abc1a58a23cc8db0".to_string()];
let val2 = "03ea8d2913ce5bb5637fe732f920ccee7a454a8f1c32a531e7abc1a58a23cc8db0";
let validators = vec![hex::decode(val2).unwrap()];

let set_validators_msg = ExecuteMsg::SetValidators {
validators: validators.clone(),
threshold: 1,
Expand Down Expand Up @@ -426,10 +450,9 @@ pub fn test_recv_message() {
pub fn test_recv_message_signatures_insufficient() {
let (mut deps, env, ctx) = instantiate(ADMIN);

let validators = vec![
"val1".to_string(),
"03ea8d2913ce5bb5637fe732f920ccee7a454a8f1c32a531e7abc1a58a23cc8db0".to_string(),
];
let val2 = "03ea8d2913ce5bb5637fe732f920ccee7a454a8f1c32a531e7abc1a58a23cc8db0";
let validators = vec![hex::decode(val2).unwrap()];

let set_validators_msg = ExecuteMsg::SetValidators {
validators: validators.clone(),
threshold: 2,
Expand All @@ -445,8 +468,7 @@ pub fn test_recv_message_signatures_insufficient() {
let src_network = NetId::from_str("0x2.icon").unwrap();
let conn_sn: u128 = 1;
let msg = string_to_hex("hello");
let mut sign_1 = hex::decode("1c829514989de5c10e61b6571374180641c2c886997f7b6248a90df0e1e51d3de41eead0350d099f4460164e6509dc430cd6f212493013228fb51a122c4fb6b9eb").unwrap();
sign_1.push(1);
let sign_1 = hex::decode("1c829514989de5c10e61b6571374180641c2c886997f7b6248a90df0e1e51d3de41eead0350d099f4460164e6509dc430cd6f212493013228fb51a122c4fb6b9eb").unwrap();
let signatures = vec![sign_1];

let msg_with_signatures = ExecuteMsg::RecvMessage {
Expand Down

0 comments on commit 788df3e

Please sign in to comment.