Skip to content

Commit

Permalink
add more commenting to relayer and secret gateway & CI improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
SecretSaturn committed Oct 1, 2024
1 parent 3662b8f commit bf86a3a
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 107 deletions.
37 changes: 0 additions & 37 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,46 +45,9 @@ jobs:
working-directory: TNLS-Gateways/public-gateway
run: forge snapshot --check --tolerance 1

secret_gateway_tests:
name: Secret Gateway Tests
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
target:
- x86_64-unknown-linux-gnu
- wasm32-unknown-unknown
env:
CARGO_TERM_COLOR: always
SCCACHE_GHA_ENABLED: "true"
RUSTC_WRAPPER: "sccache"
steps:
- uses: actions/checkout@v4

- uses: actions/cache@v4
with:
path: |
~/.cargo
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: ${{ runner.os }}-cargo-

- uses: mozilla-actions/[email protected]

- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}

- name: Build
working-directory: TNLS-Gateways/secret
run: cargo build --target ${{ matrix.target }} --no-default-features --release

secret_unit_tests:
name: Secret Gateway Unit Tests
runs-on: ubuntu-latest
needs: secret_gateway_tests
strategy:
matrix:
rust:
Expand Down
39 changes: 27 additions & 12 deletions TNLS-Gateways/secret/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
return Err(StdError::generic_err("input hash does not match task"));
}

// Decode the base64-encoded result
let result = match STANDARD.decode(msg.result) {
Ok(bytes) => bytes,
Err(_) => {
Expand All @@ -363,7 +364,7 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
// rename for clarity (original source network is now the routing destination)
let routing_info = task_info.source_network;

// "hasher" is used to perform multiple Keccak256 hashes
// Initialize Keccak256 hasher
let mut hasher = Keccak256::new();

// Calculate the total length of the concatenated data
Expand All @@ -379,21 +380,23 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
let mut data = Vec::with_capacity(total_length);

// Extend the vector with slices from the individual data components
data.extend_from_slice(env.block.chain_id.as_bytes()); // source network
data.extend_from_slice(routing_info.as_bytes()); // task_destination_network
data.extend_from_slice(msg.task.task_id.as_bytes()); // task ID
data.extend_from_slice(&task_info.payload_hash); // original payload message
data.extend_from_slice(&result); // result
data.extend_from_slice(&task_info.callback_address); // callback address
data.extend_from_slice(&task_info.callback_selector); // callback selector
data.extend_from_slice(env.block.chain_id.as_bytes()); // Source network
data.extend_from_slice(routing_info.as_bytes()); // Task destination network
data.extend_from_slice(msg.task.task_id.as_bytes()); // Task ID
data.extend_from_slice(&task_info.payload_hash); // Original payload hash
data.extend_from_slice(&result); // Execution result
data.extend_from_slice(&task_info.callback_address); // Callback address
data.extend_from_slice(&task_info.callback_selector); // Callback selector

hasher.update(&data);
let packet_hash = hasher.finalize();

// load this gateway's signing key
// Load the gateway's signing key
let private_key = CONFIG.load(deps.storage)?.signing_keys.sk;

// used in production to create signature
// Generate the packet signature using the signing key

// Used in production to create signature
#[cfg(target_arch = "wasm32")]
// NOTE: api.secp256k1_sign() will perform an additional sha_256 hash operation on the given data
let packet_signature = {
Expand All @@ -402,7 +405,7 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
.map_err(|err| StdError::generic_err(err.to_string()))?
};

// used only in unit testing to create signature
// Handle signature generation for unit testing (non-WASM environment)
#[cfg(not(target_arch = "wasm32"))]
let packet_signature = {
let secp = secp256k1::Secp256k1::signing_only();
Expand Down Expand Up @@ -472,6 +475,7 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
.serialize() // Serialize the public key to its compressed form
};

// Determine the correct recovery ID based on matching public keys
let packet_recovery_id = if packet_public_key_27 == compressed_public_key {
27
} else if packet_public_key_28 == compressed_public_key {
Expand All @@ -482,6 +486,7 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
));
};

// Format data for response
let payload_hash = format!(
"0x{}",
task_info.payload_hash.as_slice().encode_hex::<String>()
Expand Down Expand Up @@ -512,7 +517,7 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
.encode_hex::<String>()
);

// task info
// Create a ResultInfo object to store the result
let result_info = ResultInfo {
source_network: env.block.chain_id,
task_destination_network: routing_info,
Expand All @@ -526,8 +531,10 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
callback_gas_limit: callback_gas_limit,
};

// Store the result info in the result map
RESULT_MAP.insert(deps.storage, &msg.task, &result_info)?;

// Return a response with the packet information
Ok(Response::new()
.add_attribute_plaintext("source_network", result_info.source_network)
.add_attribute_plaintext(
Expand All @@ -554,13 +561,21 @@ fn post_execution(deps: DepsMut, env: Env, msg: PostExecutionMsg) -> StdResult<R
/// * `msg` - QueryMsg passed in with the query call
#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
// Match the query message and call the appropriate handler function
let response = match msg {
QueryMsg::GetPublicKeys {} => query_public_keys(deps),
QueryMsg::GetExecutionResult { task } => query_execution_result(deps, task),
};
// Pad the query result to prevent information leakage based on response size
pad_query_result(response, BLOCK_SIZE)
}

/// Queries the execution result for a given task.
///
/// # Arguments
///
/// * `deps` - Dependencies.
/// * `task` - The task to query.
fn query_execution_result(deps: Deps, task: Task) -> StdResult<Binary> {
let task_info = RESULT_MAP
.get(deps.storage, &task)
Expand Down
Loading

0 comments on commit bf86a3a

Please sign in to comment.