Skip to content

Commit

Permalink
feat(ethexe): implement more rpc methods (#4470)
Browse files Browse the repository at this point in the history
  • Loading branch information
osipov-mit authored Jan 28, 2025
1 parent b1aa585 commit ae10eae
Show file tree
Hide file tree
Showing 16 changed files with 183 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,10 @@ ethexe-runtime-common = { path = "ethexe/runtime/common", default-features = fal
ethexe-prometheus = { path = "ethexe/prometheus", default-features = false }
ethexe-validator = { path = "ethexe/validator", default-features = false }
ethexe-rpc = { path = "ethexe/rpc", default-features = false }
ethexe-common = { path = "ethexe/common" }
ethexe-common = { path = "ethexe/common", default-features = false }

# Common executor between `sandbox-host` and `lazy-pages-fuzzer`
wasmi = { package = "wasmi", version = "0.38"}
wasmi = { package = "wasmi", version = "0.38" }

# Substrate deps
binary-merkle-tree = { version = "15.0.1", git = "https://github.com/gear-tech/polkadot-sdk.git", branch = "gear-polkadot-stable2409", default-features = false }
Expand Down
4 changes: 3 additions & 1 deletion ethexe/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ repository.workspace = true
[dependencies]
gear-core.workspace = true
gprimitives = { workspace = true, features = ["serde"] }

parity-scale-codec.workspace = true
derive_more.workspace = true
hex.workspace = true
anyhow.workspace = true
serde.workspace = true

[features]
std = ["gear-core/std", "gprimitives/serde"]
3 changes: 2 additions & 1 deletion ethexe/common/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ pub type Sum = ProgramId;
/// NOTE: generic keys differs to Vara and have been chosen dependent on storage organization of ethexe.
pub type ScheduledTask = gear_core::tasks::ScheduledTask<Rfm, Sd, Sum>;

#[derive(Debug, Clone, Default, Encode, Decode, serde::Serialize, PartialEq, Eq)]
#[derive(Debug, Clone, Default, Encode, Decode, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct BlockHeader {
pub height: u32,
pub timestamp: u64,
Expand Down
4 changes: 2 additions & 2 deletions ethexe/common/src/events/mirror.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use alloc::vec::Vec;
use gear_core::message::ReplyCode;
use gprimitives::{ActorId, MessageId, H256};
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, PartialOrd, Ord)]
pub enum Event {
Expand Down Expand Up @@ -103,7 +102,8 @@ impl Event {
}
}

#[derive(Clone, Debug, Encode, Decode, Serialize, Deserialize)]
#[derive(Clone, Debug, Encode, Decode)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum RequestEvent {
ExecutableBalanceTopUpRequested {
value: u128,
Expand Down
4 changes: 2 additions & 2 deletions ethexe/common/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

use gprimitives::ActorId;
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};

mod mirror;
mod router;
Expand Down Expand Up @@ -73,7 +72,8 @@ impl From<WVaraEvent> for BlockEvent {
}
}

#[derive(Clone, Debug, Encode, Decode, Serialize, Deserialize)]
#[derive(Clone, Debug, Encode, Decode)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum BlockRequestEvent {
Router(RouterRequestEvent),
Mirror {
Expand Down
4 changes: 2 additions & 2 deletions ethexe/common/src/events/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

use gprimitives::{ActorId, CodeId, H256};
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, PartialOrd, Ord)]
pub enum Event {
Expand Down Expand Up @@ -72,7 +71,8 @@ impl Event {
}
}

#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum RequestEvent {
CodeValidationRequested {
code_id: CodeId,
Expand Down
4 changes: 2 additions & 2 deletions ethexe/common/src/events/wvara.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

use gprimitives::{ActorId, U256};
use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq, PartialOrd, Ord)]
pub enum Event {
Expand All @@ -43,7 +42,8 @@ impl Event {
}
}

#[derive(Clone, Debug, Encode, Decode, Serialize, Deserialize)]
#[derive(Clone, Debug, Encode, Decode)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum RequestEvent {
Transfer {
/// Never router, wvara or zero address.
Expand Down
3 changes: 3 additions & 0 deletions ethexe/common/src/gear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub struct ComputationSettings {
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct Message {
pub id: MessageId,
pub destination: ActorId,
Expand Down Expand Up @@ -130,6 +131,7 @@ pub struct ProtocolData {
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct StateTransition {
pub actor_id: ActorId,
pub new_state_hash: H256,
Expand All @@ -147,6 +149,7 @@ pub struct ValidationSettings {
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct ValueClaim {
pub message_id: MessageId,
pub destination: ActorId,
Expand Down
2 changes: 1 addition & 1 deletion ethexe/db/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ impl CodesStorage for Database {

self.kv
.iter_prefix(&key_prefix)
.map(|#[allow(unused_variables)] (key, code_id)| {
.map(|(key, code_id)| {
let (split_key_prefix, program_id) = key.split_at(key_prefix.len());
debug_assert_eq!(split_key_prefix, key_prefix);
let program_id =
Expand Down
2 changes: 1 addition & 1 deletion ethexe/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ hyper = { workspace = true, features = ["server"] }
log.workspace = true
parity-scale-codec.workspace = true
hex.workspace = true
ethexe-common.workspace = true
ethexe-common = { workspace = true, features = ["std"] }
ethexe-runtime-common = { workspace = true, features = ["std"] }
sp-core = { workspace = true, features = ["serde"] }
gear-core = { workspace = true, features = ["std"] }
Expand Down
13 changes: 12 additions & 1 deletion ethexe/rpc/src/apis/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use crate::{common::block_header_at_or_latest, errors};
use ethexe_common::events::BlockRequestEvent;
use ethexe_common::{events::BlockRequestEvent, gear::StateTransition};
use ethexe_db::{BlockHeader, BlockMetaStorage, Database};
use gprimitives::H256;
use jsonrpsee::{
Expand All @@ -36,6 +36,9 @@ pub trait Block {

#[method(name = "block_events")]
async fn block_events(&self, block_hash: Option<H256>) -> RpcResult<Vec<BlockRequestEvent>>;

#[method(name = "block_outcome")]
async fn block_outcome(&self, block_hash: Option<H256>) -> RpcResult<Vec<StateTransition>>;
}

#[derive(Clone)]
Expand Down Expand Up @@ -70,4 +73,12 @@ impl BlockServer for BlockApi {
.block_events(block_hash)
.ok_or_else(|| errors::db("Block events weren't found"))
}

async fn block_outcome(&self, hash: Option<H256>) -> RpcResult<Vec<StateTransition>> {
let block_hash = block_header_at_or_latest(&self.db, hash)?.0;

self.db
.block_outcome(block_hash)
.ok_or_else(|| errors::db("Block outcome wasn't found"))
}
}
63 changes: 63 additions & 0 deletions ethexe/rpc/src/apis/code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// This file is part of Gear.
//
// Copyright (C) 2024-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/>.

use crate::errors;
use ethexe_db::{CodesStorage, Database};
use gprimitives::H256;
use jsonrpsee::{
core::{async_trait, RpcResult},
proc_macros::rpc,
};
use parity_scale_codec::Encode;
use sp_core::Bytes;

#[rpc(server)]
pub trait Code {
#[method(name = "code_getOriginal")]
async fn get_original_code(&self, id: H256) -> RpcResult<Bytes>;

#[method(name = "code_getInstrumented")]
async fn get_instrumented_code(&self, runtime_id: u32, code_id: H256) -> RpcResult<Bytes>;
}

pub struct CodeApi {
db: Database,
}

impl CodeApi {
pub fn new(db: Database) -> Self {
Self { db }
}
}

#[async_trait]
impl CodeServer for CodeApi {
async fn get_original_code(&self, id: H256) -> RpcResult<Bytes> {
self.db
.original_code(id.into())
.map(|bytes| bytes.encode().into())
.ok_or_else(|| errors::db("Failed to get code by supplied id"))
}

async fn get_instrumented_code(&self, runtime_id: u32, code_id: H256) -> RpcResult<Bytes> {
self.db
.instrumented_code(runtime_id, code_id.into())
.map(|bytes| bytes.encode().into())
.ok_or_else(|| errors::db("Failed to get code by supplied id"))
}
}
2 changes: 2 additions & 0 deletions ethexe/rpc/src/apis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

mod block;
mod code;
mod dev;
mod program;

pub use block::{BlockApi, BlockServer};
pub use code::{CodeApi, CodeServer};
pub use dev::{DevApi, DevServer};
pub use program::{ProgramApi, ProgramServer};
Loading

0 comments on commit ae10eae

Please sign in to comment.