Skip to content
Open
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
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ demo-proxy = { path = "examples/proxy", default-features = false }
demo-proxy-relay = { path = "examples/proxy-relay" }
demo-proxy-reservation-with-gas = { path = "examples/proxy-reservation-with-gas" }
demo-read-big-state = { path = "examples/read-big-state", default-features = false }
demo-reply-callback = { path = "examples/reply-callback" }
demo-reservation-manager = { path = "examples/reservation-manager" }
demo-reserve-gas = { path = "examples/reserve-gas", default-features = false }
demo-rwlock = { path = "examples/rwlock" }
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ethexe-contracts-pre-commit:
@ echo " > Copying ERC1967Proxy artifact" && cp ./ethexe/contracts/out/ERC1967Proxy.sol/ERC1967Proxy.json ./ethexe/ethereum/abi
@ echo " > Copying WrappedVara artifact" && cp ./ethexe/contracts/out/WrappedVara.sol/WrappedVara.json ./ethexe/ethereum/abi
@ echo " > Copying BatchMulticall" && cp ./ethexe/contracts/out/BatchMulticall.sol/BatchMulticall.json ./ethexe/ethereum/abi
@ echo " > Copying DemoCaller" && cp ./ethexe/contracts/out/DemoCaller.sol/DemoCaller.json ./ethexe/ethereum/abi

# Common section
.PHONY: show
Expand Down
54 changes: 54 additions & 0 deletions ethexe/contracts/test/DemoCaller.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.33;

import {IDemoCallbacks} from "./IDemoCallbacks.sol";
import {IMirror} from "src/IMirror.sol";

contract DemoCaller is IDemoCallbacks {
IMirror public immutable VARA_ETH_PROGRAM;

bool public replyOnMethodNameCalled;
bool public onErrorReplyCalled;

event MethodNameReplied(bytes32 messageId);

event ErrorReplied(bytes32 messageId, bytes payload, bytes4 replyCode);

error UnauthorizedCaller();

constructor(IMirror _varaEthProgram) {
VARA_ETH_PROGRAM = _varaEthProgram;
}

modifier onlyVaraEthProgram() {
_onlyVaraEthProgram();
_;
}

function _onlyVaraEthProgram() internal view {
if (msg.sender != address(VARA_ETH_PROGRAM)) {
revert UnauthorizedCaller();
}
}

function methodName(bool isPanic) external returns (bytes32) {
return VARA_ETH_PROGRAM.sendMessage(abi.encodePacked(isPanic), true);
}

/// forge-lint: disable-next-line(mixed-case-function)
function replyOn_methodName(bytes32 messageId) external onlyVaraEthProgram {
replyOnMethodNameCalled = true;

emit MethodNameReplied(messageId);
}

function onErrorReply(bytes32 messageId, bytes calldata payload, bytes4 replyCode)
external
payable
onlyVaraEthProgram
{
onErrorReplyCalled = true;

emit ErrorReplied(messageId, payload, replyCode);
}
}
9 changes: 9 additions & 0 deletions ethexe/contracts/test/IDemoCallbacks.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.33;

import {ICallbacks} from "src/ICallbacks.sol";

interface IDemoCallbacks is ICallbacks {
/// forge-lint: disable-next-line(mixed-case-function)
function replyOn_methodName(bytes32 messageId) external;
}
1 change: 1 addition & 0 deletions ethexe/ethereum/abi/DemoCaller.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions ethexe/ethereum/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ sol!(
"abi/WrappedVara.json"
);

sol!(
#[sol(rpc)]
IDemoCaller,
"abi/DemoCaller.json"
);

/// Bindings for Symbiotic contracts.
/// Only uses for local deployments and tests.
pub mod symbiotic_abi {
Expand Down
1 change: 1 addition & 0 deletions ethexe/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ demo-async = { workspace = true, features = ["debug", "ethexe"] }
demo-async-init = { workspace = true, features = ["debug", "ethexe"] }
demo-mul-by-const = { workspace = true, features = ["debug"] }
demo-piggy-bank = { workspace = true, features = ["debug", "ethexe"] }
demo-reply-callback = { workspace = true, features = ["debug", "ethexe"] }
demo-fungible-token = { workspace = true }
demo-delayed-sender-ethexe = { workspace = true, features = [
"debug",
Expand Down
100 changes: 99 additions & 1 deletion ethexe/service/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ use ethexe_common::{
use ethexe_compute::{ComputeConfig, ComputeEvent};
use ethexe_consensus::{BatchCommitter, ConsensusEvent};
use ethexe_db::verifier::IntegrityVerifier;
use ethexe_ethereum::{TryGetReceipt, deploy::ContractsDeploymentParams, router::Router};
use ethexe_ethereum::{
TryGetReceipt, abi::IDemoCaller, deploy::ContractsDeploymentParams, router::Router,
};
use ethexe_observer::{EthereumConfig, ObserverEvent};
use ethexe_prometheus::PrometheusConfig;
use ethexe_rpc::{DEFAULT_BLOCK_GAS_LIMIT_MULTIPLIER, InjectedClient, RpcConfig};
Expand Down Expand Up @@ -3366,3 +3368,99 @@ async fn catch_up_test_case(commitment_delay_limit: u32) {
unreachable!();
}
}

#[tokio::test(flavor = "multi_thread")]
#[ntest::timeout(60_000)]
async fn reply_callback() {
init_logger();

let mut env = TestEnv::new(Default::default()).await.unwrap();

let mut node = env.new_node(NodeConfig::default().validator(env.validators[0]));
node.start_service().await;

let res = env
.upload_code(demo_reply_callback::WASM_BINARY)
.await
.unwrap()
.wait_for()
.await
.unwrap();
assert!(res.valid);

let code_id = res.code_id;

let code = node
.db
.original_code(code_id)
.expect("After approval, the code is guaranteed to be in the database");
assert_eq!(code, demo_reply_callback::WASM_BINARY);

let _ = node
.db
.instrumented_code(1, code_id)
.expect("After approval, instrumented code is guaranteed to be in the database");
let res = env
.create_program(code_id, 500_000_000_000_000)
.await
.unwrap()
.wait_for()
.await
.unwrap();
assert_eq!(res.code_id, code_id);

let res = env
.send_message(res.program_id, b"")
.await
.unwrap()
.wait_for()
.await
.unwrap();

assert_eq!(res.code, ReplyCode::Success(SuccessReplyReason::Auto));
assert_eq!(res.payload, b"");
assert_eq!(res.value, 0);

let program_id = res.program_id;

let provider = env.ethereum.provider();
let demo_caller = IDemoCaller::deploy(provider.clone(), program_id.into())
.await
.expect("deploying DemoCaller failed");

assert!(!demo_caller.replyOnMethodNameCalled().call().await.unwrap());

demo_caller
.methodName(false)
.send()
.await
.unwrap()
.try_get_receipt()
.await
.unwrap();

env.new_observer_events()
.filter_map_block_synced()
.find(|e| matches!(e, BlockEvent::Router(RouterEvent::BatchCommitted { .. })))
.await;

assert!(demo_caller.replyOnMethodNameCalled().call().await.unwrap());

assert!(!demo_caller.onErrorReplyCalled().call().await.unwrap());

demo_caller
.methodName(true)
.send()
.await
.unwrap()
.try_get_receipt()
.await
.unwrap();

env.new_observer_events()
.filter_map_block_synced()
.find(|e| matches!(e, BlockEvent::Router(RouterEvent::BatchCommitted { .. })))
.await;

assert!(demo_caller.onErrorReplyCalled().call().await.unwrap());
}
21 changes: 21 additions & 0 deletions examples/reply-callback/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "demo-reply-callback"
version = "0.1.0"
authors.workspace = true
edition.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true

[dependencies]
gstd.workspace = true
gear-workspace-hack.workspace = true

[build-dependencies]
gear-wasm-builder.workspace = true

[features]
debug = ["gstd/debug"]
default = ["std"]
std = []
ethexe = ["gstd/ethexe"]
21 changes: 21 additions & 0 deletions examples/reply-callback/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// This file is part of Gear.

// Copyright (C) 2021-2025 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

fn main() {
gear_wasm_builder::build();
}
30 changes: 30 additions & 0 deletions examples/reply-callback/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This file is part of Gear.

// Copyright (C) 2026 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

#![no_std]

#[cfg(feature = "std")]
mod code {
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
}

#[cfg(feature = "std")]
pub use code::WASM_BINARY_OPT as WASM_BINARY;

#[cfg(not(feature = "std"))]
mod wasm;
41 changes: 41 additions & 0 deletions examples/reply-callback/src/wasm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// This file is part of Gear.

// Copyright (C) 2026 Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use gstd::{msg, prelude::*};

#[unsafe(no_mangle)]
extern "C" fn init() {}

#[unsafe(no_mangle)]
extern "C" fn handle() {
let payload = msg::load_bytes().expect("Failed to load payload");
let is_panic = payload[0] == 0x01;

if is_panic {
panic!();
} else {
let message_id = msg::id().into_bytes();

// cast calldata "function replyOn_methodName(bytes32 messageId) external" "0x..."
let mut payload = [0u8; 36];
payload[..4].copy_from_slice(&[0xb5, 0x2a, 0xb5, 0x55]); // DemoCaller.replyOn_methodName.selector
payload[4..].copy_from_slice(&message_id);

msg::reply_bytes(payload, 0).expect("Failed to send reply");
}
}
Loading