Skip to content

Commit

Permalink
feat(ethexe): refactor and optimize mirror; split initialization logi…
Browse files Browse the repository at this point in the history
…c; remove unnecessary token manipulations (#4372)
  • Loading branch information
breathx authored Dec 4, 2024
1 parent 9ce8b88 commit ea89849
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 276 deletions.
19 changes: 5 additions & 14 deletions ethexe/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ pub struct UploadCodeArgs {
#[derive(Clone, Debug, Deserialize, Parser)]
pub struct CreateProgramArgs {
code_id: String,
init_payload: String,
value: u128,
}

// TODO (breathx): support message sending here.

impl ExtraCommands {
pub async fn run(&self, config: &config::Config) -> anyhow::Result<()> {
let signer = ethexe_signer::Signer::new(config.key_path.clone())?;
Expand Down Expand Up @@ -288,15 +288,8 @@ impl ExtraCommands {
.code_id
.parse()
.map_err(|err| anyhow!("failed to parse code id: {err}"))?;

let salt = rand::random();
let init_payload = if let Some(init_payload) =
create_program_args.init_payload.strip_prefix("0x")
{
hex::decode(init_payload)?
} else {
create_program_args.init_payload.clone().into_bytes()
};
let value = create_program_args.value;

let Some((sender_address, ethexe_ethereum)) =
maybe_sender_address.zip(maybe_ethereum)
Expand All @@ -308,13 +301,11 @@ impl ExtraCommands {

let router = ethexe_ethereum.router();

let (tx, actor_id) = router
.create_program(code_id, salt, init_payload, value)
.await?;
let (tx, actor_id) = router.create_program(code_id, salt).await?;

println!("Completed in transaction {tx:?}");
println!(
"Waiting for state update of program {}...",
"Program address on Ethereum {}",
actor_id.to_address_lossy()
);

Expand Down
22 changes: 18 additions & 4 deletions ethexe/cli/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -986,25 +986,39 @@ mod utils {
Ok(WaitForUploadCode { listener, code_id })
}

// TODO (breathx): split it into different functions WITHIN THE PR.
pub async fn create_program(
&self,
code_id: CodeId,
payload: &[u8],
value: u128,
) -> Result<WaitForProgramCreation> {
const EXECUTABLE_BALANCE: u128 = 500_000_000_000_000;

log::info!(
"📗 Create program, code_id {code_id}, payload len {}",
payload.len()
);

let listener = self.events_publisher().subscribe().await;

let (_, program_id) = self
.ethereum
.router()
.create_program(code_id, H256::random(), payload, value)
let router = self.ethereum.router();

let (_, program_id) = router.create_program(code_id, H256::random()).await?;

let program_address = program_id.to_address_lossy().0.into();

router
.wvara()
.approve(program_address, value + EXECUTABLE_BALANCE)
.await?;

let mirror = self.ethereum.mirror(program_address.into_array().into());

mirror.executable_balance_top_up(EXECUTABLE_BALANCE).await?;

mirror.send_message(payload, value).await?;

Ok(WaitForProgramCreation {
listener,
program_id,
Expand Down
28 changes: 8 additions & 20 deletions ethexe/contracts/src/IMirror.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.26;

import {Gear} from "./libraries/Gear.sol";

// TODO (breathx): sort here everything.
interface IMirror {
/* Events section */
Expand Down Expand Up @@ -85,33 +87,19 @@ interface IMirror {

/* Primary Gear logic */

function sendMessage(bytes calldata payload, uint128 value) external payable returns (bytes32);
function sendMessage(bytes calldata payload, uint128 value) external returns (bytes32);

function sendReply(bytes32 repliedTo, bytes calldata payload, uint128 value) external payable;
function sendReply(bytes32 repliedTo, bytes calldata payload, uint128 value) external;

// payable?
function claimValue(bytes32 claimedId) external;

function executableBalanceTopUp(uint128 value) external payable;
function executableBalanceTopUp(uint128 value) external;

function sendValueToInheritor() external;
function transferLockedValueToInheritor() external;

/* Router-driven state and funds management */
// NOTE: all of these methods will have additional handler (with hooks) for decoder.

function updateState(bytes32 newStateHash) external;

function setInheritor(address inheritor) external;

function messageSent(bytes32 id, address destination, bytes calldata payload, uint128 value) external;

function replySent(address destination, bytes calldata payload, uint128 value, bytes32 replyTo, bytes4 replyCode)
external;

function valueClaimed(bytes32 claimedId, address destination, uint128 value) external;

function createDecoder(address implementation, bytes32 salt) external;
function initialize(address initializer, address decoder) external;

// TODO (breathx): consider removal of this in favor of separated creation and init.
function initMessage(address source, bytes calldata payload, uint128 value, uint128 executableBalance) external;
function performStateTransition(Gear.StateTransition calldata transition) external returns (bytes32);
}
12 changes: 2 additions & 10 deletions ethexe/contracts/src/IRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,9 @@ interface IRouter {
/// @dev CodeValidationRequested Emitted on success.
function requestCodeValidation(bytes32 codeId, bytes32 blobTxHash) external;
/// @dev ProgramCreated Emitted on success.
function createProgram(bytes32 codeId, bytes32 salt, bytes calldata payload, uint128 value)
external
returns (address);
function createProgram(bytes32 codeId, bytes32 salt) external returns (address);
/// @dev ProgramCreated Emitted on success.
function createProgramWithDecoder(
address decoderImpl,
bytes32 codeId,
bytes32 salt,
bytes calldata payload,
uint128 value
) external returns (address);
function createProgramWithDecoder(address decoderImpl, bytes32 codeId, bytes32 salt) external returns (address);

// # Validators calls.
/// @dev CodeGotValidated Emitted for each code in commitment.
Expand Down
2 changes: 1 addition & 1 deletion ethexe/contracts/src/Middleware.sol
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ contract Middleware {
require(cfg.minSlashExecutionDelay > 0, "Min slash execution delay cannot be zero");
require(
cfg.minVetoDuration + cfg.minSlashExecutionDelay <= cfg.minVaultEpochDuration,
"Veto duration and slash execution delay must be less than ot equal to min vaults epoch duration"
"Veto duration and slash execution delay must be less than or equal to min vaults epoch duration"
);

// In order to be able to change resolver, we need to limit max delay in epochs.
Expand Down
Loading

0 comments on commit ea89849

Please sign in to comment.