Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:invalidation scope in ScheduleCork response #239

Merged
merged 4 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified crates/steward-proto/src/gen/descriptor.bin
Binary file not shown.
3 changes: 3 additions & 0 deletions crates/steward-proto/src/gen/steward.v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5515,6 +5515,9 @@ pub struct ScheduleResponse {
/// The ID of the chain on which the target Cellar resides
#[prost(uint64, tag = "2")]
pub chain_id: u64,
/// Invalidation scope for the gravity tx, keccak256 of the target contract address and encoded contract call
#[prost(string, tag = "3")]
pub invalidation_scope: ::prost::alloc::string::String,
}
#[derive(serde::Deserialize, serde::Serialize, Clone, PartialEq, ::prost::Message)]
pub struct EncodeRequest {
Expand Down
2 changes: 1 addition & 1 deletion hash_proto
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ff574cfd864f6ae653c2cfd2c1a0e022a8d6ea125630c72712eec9ccb1ec6772
4e0284025de03d59dfec8ca9913874737e4a310150543686ae35adbe406dbb42
2 changes: 2 additions & 0 deletions proto/steward/v4/steward.proto
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ message ScheduleResponse {
string id = 1;
// The ID of the chain on which the target Cellar resides
uint64 chain_id = 2;
// Invalidation scope for the gravity tx, keccak256 of the target contract address and encoded contract call
string invalidation_scope = 3;
}

message EncodeRequest {
Expand Down
7 changes: 4 additions & 3 deletions src/cellars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ pub async fn validate_cellar_id(chain_id: u64, cellar_id: &str) -> Result<(), Er
.into());
}

let cellar_id = to_checksum_address(cellar_id)?;
let (cellar_id, _) = to_checksum_address(cellar_id)?;

if !is_approved(chain_id, &cellar_id) {
if let Err(err) = cache::refresh_approved_cellars().await {
Expand All @@ -394,7 +394,7 @@ pub async fn validate_cellar_id(chain_id: u64, cellar_id: &str) -> Result<(), Er
Ok(())
}

pub(crate) fn to_checksum_address(address: &str) -> Result<String, Error> {
pub(crate) fn to_checksum_address(address: &str) -> Result<(String, Vec<u8>), Error> {
let address = match address.parse::<Address>() {
Ok(a) => a,
Err(e) => {
Expand All @@ -403,9 +403,10 @@ pub(crate) fn to_checksum_address(address: &str) -> Result<String, Error> {
.into())
}
};
let address_bytes = address.as_bytes().to_vec();
let address = ethers::utils::to_checksum(&address, None);

Ok(address)
Ok((address, address_bytes))
}

#[cfg(test)]
Expand Down
28 changes: 26 additions & 2 deletions src/cork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl proto::contract_call_service_server::ContractCallService for CorkHandler {
));
}

let cellar_id = match to_checksum_address(&request.cellar_id.clone()) {
let (cellar_id, cellar_id_bytes) = match to_checksum_address(&request.cellar_id.clone()) {
Ok(id) => id,
Err(err) => {
debug!(
Expand Down Expand Up @@ -92,12 +92,17 @@ impl proto::contract_call_service_server::ContractCallService for CorkHandler {
}

let id = id_hash(height, chain_id, &cellar_id, &encoded_call);
let invalidation_scope = invalidation_scope(cellar_id_bytes, &encoded_call);
info!(
"scheduled cork {} for cellar {} on chain {} at height {}",
id, cellar_id, chain_id, height
);

Ok(Response::new(ScheduleResponse { id, chain_id }))
Ok(Response::new(ScheduleResponse {
id,
chain_id,
invalidation_scope,
}))
}
}

Expand Down Expand Up @@ -267,6 +272,17 @@ pub fn id_hash(
format!("{:x}", hasher.finalize())
}

// returns the hex encoded invalidation scope of the contract call. this calculation mirrors
// what is done by the cork module when submitting the contract call to gravity.
pub fn invalidation_scope(target_contract_address_bytes: Vec<u8>, encoded_call: &[u8]) -> String {
let mut hasher = Keccak256::new();
let input = [&target_contract_address_bytes, encoded_call].concat();

hasher.update(input);

format!("{:x}", hasher.finalize())
}

#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -285,4 +301,12 @@ mod tests {

assert_eq!(expected, actual);
}

#[test]
fn test_invalidation_scope() {
let expected = "47c8800f7dd3fa4b8c7a08a30bdcf2c206574760663d30f1fbc78b5f632e2209";
let actual = invalidation_scope("bleep".as_bytes().to_vec(), "bloop".as_bytes());

assert_eq!(expected, actual);
}
}
4 changes: 2 additions & 2 deletions src/cork/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl CorkQueryClient {

let mut result: HashMap<u64, HashSet<String>> = HashMap::new();
for id in cork_result.into_inner().cellar_ids {
let normalized_id = match to_checksum_address(&id) {
let (normalized_id, _) = match to_checksum_address(&id) {
Ok(addr) => addr,
Err(err) => {
error!("failed to get checksum of cellar ID {}, it will not be cached as approved: {}", id, err);
Expand All @@ -61,7 +61,7 @@ impl CorkQueryClient {
let chain_id = set.chain_id;
let cellar_ids = set.ids.clone();
for id in cellar_ids.into_iter() {
let normalized_id = match to_checksum_address(&id) {
let (normalized_id, _) = match to_checksum_address(&id) {
Ok(addr) => addr,
Err(err) => {
error!("failed to get checksum of axelar cellar ID {}, it will not be cached as approved: {}", id, err);
Expand Down
Binary file added src/gen/proto/descriptor.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion src/pubsub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub(crate) async fn get_trust_state() -> Result<Vec<PublisherTrustData<'static>>
return None;
}
};
let Ok(cellar_id) = to_checksum_address(&cellar_id) else {
let Ok((cellar_id, _)) = to_checksum_address(&cellar_id) else {
error!("failed to convert cellarID part of subscription ID to checksum: {sid}, it will not be included in trust store");
return None;
};
Expand Down
Loading