Skip to content

Commit cf2ecfb

Browse files
Change the way that UserApplicationId is computed for ApplicationDescription of EVM smart contracts. (#3657)
## Motivation EVM applications should have a hash that is of length 20. We can achieve that by setting the last 12 bytes to 0. ## Proposal The changes are done to the computation in the hash in the `data_types.rs`. It requires matching the `ApplicationDescription` for the blob type. In that case, we need to deserialize the description in order to obtain the module_id and whether they are EVM or not. It has to be done in two places. ## Test Plan The CI should be doing the adequate test. ## Release Plan Normal testing release. ## Links None.
1 parent 2e538d4 commit cf2ecfb

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

linera-base/src/crypto/hash.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ impl CryptoHash {
4646
&self.0
4747
}
4848

49+
/// Force the last 12 bytes of the hash to be zeroes. This is currently used for EVM compatibility
50+
pub fn make_evm_compatible(&mut self) {
51+
self.0[20..32].fill(0);
52+
}
53+
4954
/// Returns the hash of `TestString(s)`, for testing purposes.
5055
#[cfg(with_testing)]
5156
pub fn test_hash(s: impl Into<String>) -> Self {

linera-base/src/data_types.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use crate::{
3939
},
4040
limited_writer::{LimitedWriter, LimitedWriterError},
4141
time::{Duration, SystemTime},
42+
vm::VmRuntime,
4243
};
4344

4445
/// A non-negative amount of tokens.
@@ -845,9 +846,11 @@ pub struct ApplicationDescription {
845846

846847
impl From<&ApplicationDescription> for ApplicationId {
847848
fn from(description: &ApplicationDescription) -> Self {
848-
ApplicationId::new(CryptoHash::new(&BlobContent::new_application_description(
849-
description,
850-
)))
849+
let mut hash = CryptoHash::new(&BlobContent::new_application_description(description));
850+
if matches!(description.module_id.vm_runtime, VmRuntime::Evm) {
851+
hash.make_evm_compatible();
852+
}
853+
ApplicationId::new(hash)
851854
}
852855
}
853856

@@ -1076,7 +1079,14 @@ pub struct Blob {
10761079
impl Blob {
10771080
/// Computes the hash and returns the hashed blob for the given content.
10781081
pub fn new(content: BlobContent) -> Self {
1079-
let hash = CryptoHash::new(&content);
1082+
let mut hash = CryptoHash::new(&content);
1083+
if matches!(content.blob_type, BlobType::ApplicationDescription) {
1084+
let application_description = bcs::from_bytes::<ApplicationDescription>(&content.bytes)
1085+
.expect("to obtain an application description");
1086+
if matches!(application_description.module_id.vm_runtime, VmRuntime::Evm) {
1087+
hash.make_evm_compatible();
1088+
}
1089+
}
10801090
Blob { hash, content }
10811091
}
10821092

0 commit comments

Comments
 (0)