Skip to content

Commit 393a086

Browse files
authored
Fix witty handling of array20 (#4183)
## Motivation @Twey found a bug in the handling of `[u8; 20]` in Witty. ## Proposal Fix. Add a unit test. ## Test Plan CI ## Release Plan - Nothing to do / These changes follow the usual release cycle. ## Links Fixes #4182 - [reviewer checklist](https://github.com/linera-io/linera-protocol/blob/main/CONTRIBUTING.md#reviewer-checklist)
1 parent 7be4ef1 commit 393a086

File tree

6 files changed

+13
-12
lines changed

6 files changed

+13
-12
lines changed

linera-base/src/unit_tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use crate::{
3232
#[test_case(module_id_test_case(); "of_module_id")]
3333
#[test_case(timeout_config_test_case(); "of_timeout_config")]
3434
#[test_case(chain_ownership_test_case(); "of_chain_ownership")]
35+
#[test_case([5u8; 20]; "array20")]
3536
fn test_wit_roundtrip<T>(input: T)
3637
where
3738
T: Debug + Eq + WitLoad + WitStore,

linera-sdk/src/base/conversions_from_wit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ macro_rules! impl_from_wit {
3232
impl From<$wit_base_api::Array20> for [u8; 20] {
3333
fn from(ethereum_address: $wit_base_api::Array20) -> Self {
3434
let mut bytes = [0u8; 20];
35-
bytes[0..8].copy_from_slice(&ethereum_address.part1.to_le_bytes());
36-
bytes[8..16].copy_from_slice(&ethereum_address.part2.to_le_bytes());
37-
bytes[16..20].copy_from_slice(&ethereum_address.part3.to_le_bytes());
35+
bytes[0..8].copy_from_slice(&ethereum_address.part1.to_be_bytes());
36+
bytes[8..16].copy_from_slice(&ethereum_address.part2.to_be_bytes());
37+
bytes[16..20].copy_from_slice(&ethereum_address.part3.to_be_bytes()[0..4]);
3838
bytes
3939
}
4040
}

linera-sdk/src/base/conversions_to_wit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ macro_rules! impl_to_wit {
3535
$wit_base_api::Array20 {
3636
part1: u64::from_be_bytes(bytes[0..8].try_into().unwrap()),
3737
part2: u64::from_be_bytes(bytes[8..16].try_into().unwrap()),
38-
part3: u64::from_be_bytes(bytes[16..20].try_into().unwrap()),
38+
part3: (u32::from_be_bytes(bytes[16..20].try_into().unwrap()) as u64) << 32,
3939
}
4040
}
4141
}

linera-sdk/src/contract/conversions_from_wit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ impl From<wit_contract_api::CryptoHash> for CryptoHash {
3333
impl From<wit_contract_api::Array20> for [u8; 20] {
3434
fn from(ethereum_address: wit_contract_api::Array20) -> Self {
3535
let mut bytes = [0u8; 20];
36-
bytes[0..8].copy_from_slice(&ethereum_address.part1.to_le_bytes());
37-
bytes[8..16].copy_from_slice(&ethereum_address.part2.to_le_bytes());
38-
bytes[16..20].copy_from_slice(&ethereum_address.part3.to_le_bytes());
36+
bytes[0..8].copy_from_slice(&ethereum_address.part1.to_be_bytes());
37+
bytes[8..16].copy_from_slice(&ethereum_address.part2.to_be_bytes());
38+
bytes[16..20].copy_from_slice(&ethereum_address.part3.to_be_bytes()[0..4]);
3939
bytes
4040
}
4141
}

linera-sdk/src/contract/conversions_to_wit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl From<[u8; 20]> for wit_contract_api::Array20 {
4040
wit_contract_api::Array20 {
4141
part1: u64::from_be_bytes(bytes[0..8].try_into().unwrap()),
4242
part2: u64::from_be_bytes(bytes[8..16].try_into().unwrap()),
43-
part3: u64::from_be_bytes(bytes[16..20].try_into().unwrap()),
43+
part3: (u32::from_be_bytes(bytes[16..20].try_into().unwrap()) as u64) << 32,
4444
}
4545
}
4646
}

linera-witty/src/primitive_types/array.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl WitLoad for [u8; 20] {
4242
let mut dest = [0u8; 20];
4343
dest[0..8].copy_from_slice(&part1.to_be_bytes());
4444
dest[8..16].copy_from_slice(&part2.to_be_bytes());
45-
dest[16..20].copy_from_slice(&part3.to_be_bytes());
45+
dest[16..20].copy_from_slice(&part3.to_be_bytes()[0..4]);
4646
Ok(dest)
4747
}
4848

@@ -58,7 +58,7 @@ impl WitLoad for [u8; 20] {
5858
let mut dest = [0u8; 20];
5959
dest[0..8].copy_from_slice(&part1.to_be_bytes());
6060
dest[8..16].copy_from_slice(&part2.to_be_bytes());
61-
dest[16..20].copy_from_slice(&part3.to_be_bytes());
61+
dest[16..20].copy_from_slice(&part3.to_be_bytes()[0..4]);
6262
Ok(dest)
6363
}
6464
}
@@ -75,7 +75,7 @@ impl WitStore for [u8; 20] {
7575
{
7676
let part1 = u64::from_be_bytes(self[0..8].try_into().unwrap());
7777
let part2 = u64::from_be_bytes(self[8..16].try_into().unwrap());
78-
let part3 = u64::from_be_bytes(self[16..20].try_into().unwrap());
78+
let part3 = (u32::from_be_bytes(self[16..20].try_into().unwrap()) as u64) << 32;
7979
(part1, part2, part3).store(memory, location)
8080
}
8181

@@ -89,7 +89,7 @@ impl WitStore for [u8; 20] {
8989
{
9090
let part1 = u64::from_be_bytes(self[0..8].try_into().unwrap());
9191
let part2 = u64::from_be_bytes(self[8..16].try_into().unwrap());
92-
let part3 = u64::from_be_bytes(self[16..20].try_into().unwrap());
92+
let part3 = (u32::from_be_bytes(self[16..20].try_into().unwrap()) as u64) << 32;
9393
(part1, part2, part3).lower(memory)
9494
}
9595
}

0 commit comments

Comments
 (0)