Skip to content

Commit

Permalink
feat(ethexe): zero program state; handle exec balances topups; handle…
Browse files Browse the repository at this point in the history
… unrecognised mirrors; refactor (#4173)
  • Loading branch information
breathx authored Aug 22, 2024
1 parent f7af27f commit f7eb906
Show file tree
Hide file tree
Showing 13 changed files with 371 additions and 293 deletions.
11 changes: 3 additions & 8 deletions ethexe/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,9 @@ impl Service {
outcomes
.into_iter()
.map(|outcome| match outcome {
LocalOutcome::CodeApproved(code_id) => Commitment::Code(CodeCommitment {
id: code_id,
valid: true,
}),
LocalOutcome::CodeRejected(code_id) => Commitment::Code(CodeCommitment {
id: code_id,
valid: false,
}),
LocalOutcome::CodeValidated { id, valid } => {
Commitment::Code(CodeCommitment { id, valid })
}
_ => unreachable!("Only code outcomes are expected here"),
})
.collect()
Expand Down
27 changes: 20 additions & 7 deletions ethexe/cli/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use ethexe_signer::Signer;
use ethexe_validator::Validator;
use futures::StreamExt;
use gear_core::ids::prelude::*;
use gprimitives::{ActorId, CodeId, MessageId, H256};
use gprimitives::{ActorId, CodeId, MessageId, H160, H256};
use std::{sync::Arc, time::Duration};
use tokio::{
sync::{
Expand Down Expand Up @@ -148,6 +148,7 @@ struct TestEnv {
validator_private_key: ethexe_signer::PrivateKey,
validator_public_key: ethexe_signer::PublicKey,
router_address: ethexe_signer::Address,
sender_address: ActorId,
block_time: Duration,
running_service_handle: Option<JoinHandle<Result<()>>>,
}
Expand Down Expand Up @@ -226,6 +227,7 @@ impl TestEnv {
validator_private_key,
validator_public_key,
router_address,
sender_address: ActorId::from(H160::from(sender_address.0)),
block_time,
running_service_handle: None,
};
Expand Down Expand Up @@ -327,6 +329,7 @@ impl Drop for TestEnv {
}
}
}

#[tokio::test(flavor = "multi_thread")]
#[ntest::timeout(60_000)]
async fn ping() {
Expand Down Expand Up @@ -414,9 +417,12 @@ async fn ping() {
if address == program_id {
match event {
MirrorEvent::MessageQueueingRequested {
id, payload, value, ..
id,
source,
payload,
value,
} => {
// TODO (breathx): assert source.
assert_eq!(source, env.sender_address);
assert_eq!(payload, b"PING");
assert_eq!(value, 0);
init_message_id = id;
Expand Down Expand Up @@ -583,9 +589,12 @@ async fn ping_reorg() {
if address == program_id {
match event {
MirrorEvent::MessageQueueingRequested {
id, payload, value, ..
id,
source,
payload,
value,
} => {
// TODO (breathx): assert source.
assert_eq!(source, env.sender_address);
assert_eq!(payload, b"PING");
assert_eq!(value, 0);
init_message_id = id;
Expand Down Expand Up @@ -829,9 +838,13 @@ async fn ping_deep_sync() {
if address == program_id {
match event {
MirrorEvent::MessageQueueingRequested {
id, payload, value, ..
id,
source,
payload,
value,
..
} => {
// TODO (breathx): assert source.
assert_eq!(source, env.sender_address);
assert_eq!(payload, b"PING");
assert_eq!(value, 0);
init_message_id = id;
Expand Down
1 change: 0 additions & 1 deletion ethexe/contracts/script/Deployment.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ contract RouterScript is Script {
mirror = new Mirror();
mirrorProxy = new MirrorProxy(address(router));

// TODO (breathx): remove this approve.
wrappedVara.approve(address(router), type(uint256).max);

vm.stopBroadcast();
Expand Down
1 change: 0 additions & 1 deletion ethexe/contracts/src/IMirror.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ interface IMirror {
event Reply(bytes payload, uint128 value, bytes32 replyTo, bytes4 indexed replyCode);

// TODO (breathx): should we deposit it? should we notify about successful reply sending?
// TODO (breathx): `value` could be removed from event.
/**
* @dev Emitted when a user succeed in claiming value request and receives balance.
*
Expand Down
17 changes: 13 additions & 4 deletions ethexe/db/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,23 @@ impl Database {
// TODO: consider to change decode panics to Results.
impl Storage for Database {
fn read_state(&self, hash: H256) -> Option<ProgramState> {
if hash.is_zero() {
return Some(ProgramState::zero());
}

let data = self.cas.read(&hash)?;
Some(
ProgramState::decode(&mut &data[..])
.expect("Failed to decode data into `ProgramState`"),
)

let state = ProgramState::decode(&mut &data[..])
.expect("Failed to decode data into `ProgramState`");

Some(state)
}

fn write_state(&self, state: ProgramState) -> H256 {
if state.is_zero() {
return H256::zero();
}

self.cas.write(&state.encode())
}

Expand Down
2 changes: 0 additions & 2 deletions ethexe/observer/src/observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ pub(crate) async fn read_block_events(
provider: &ObserverProvider,
router_address: AlloyAddress,
) -> Result<Vec<BlockEvent>> {
// TODO (breathx): discuss should we check validity of wvara address for router on some block.
let router_query = RouterQuery::from_provider(router_address, Arc::new(provider.clone()));
let wvara_address = router_query.wvara_address().await?;

Expand Down Expand Up @@ -265,7 +264,6 @@ pub(crate) async fn read_block_events_batch(
provider: &ObserverProvider,
router_address: AlloyAddress,
) -> Result<HashMap<H256, Vec<BlockEvent>>> {
// TODO (breathx): discuss should we check validity of wvara address for router on some block.
let router_query = RouterQuery::from_provider(router_address, Arc::new(provider.clone()));
let wvara_address = router_query.wvara_address().await?;

Expand Down
2 changes: 1 addition & 1 deletion ethexe/processor/src/host/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl ThreadParams {
pub fn pages(&mut self) -> &BTreeMap<GearPage, H256> {
self.pages.get_or_insert_with(|| {
let ProgramState {
state: Program::Active(ActiveProgram { pages_hash, .. }),
program: Program::Active(ActiveProgram { pages_hash, .. }),
..
} = self.db.read_state(self.state_hash).expect(UNKNOWN_STATE)
else {
Expand Down
Loading

0 comments on commit f7eb906

Please sign in to comment.