-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ethexe/rpc): implement rpc methods (#4299)
Co-authored-by: Dmitry Novikov <novikov.dm.al@gmail.com>
1 parent
386b3b0
commit 08e0da7
Showing
24 changed files
with
403 additions
and
105 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// This file is part of Gear. | ||
// | ||
// Copyright (C) 2024 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::{common::block_header_at_or_latest, errors}; | ||
use ethexe_common::BlockRequestEvent; | ||
use ethexe_db::{BlockHeader, BlockMetaStorage, Database}; | ||
use gprimitives::H256; | ||
use jsonrpsee::{ | ||
core::{async_trait, RpcResult}, | ||
proc_macros::rpc, | ||
}; | ||
use std::collections::VecDeque; | ||
|
||
#[rpc(server)] | ||
pub trait Block { | ||
#[method(name = "block_header")] | ||
async fn block_header(&self, hash: Option<H256>) -> RpcResult<(H256, BlockHeader)>; | ||
|
||
#[method(name = "block_commitmentQueue")] | ||
async fn block_commitment_queue(&self, hash: Option<H256>) -> RpcResult<VecDeque<H256>>; | ||
|
||
#[method(name = "block_events")] | ||
async fn block_events(&self, block_hash: Option<H256>) -> RpcResult<Vec<BlockRequestEvent>>; | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct BlockApi { | ||
db: Database, | ||
} | ||
|
||
impl BlockApi { | ||
pub fn new(db: Database) -> Self { | ||
Self { db } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl BlockServer for BlockApi { | ||
async fn block_header(&self, hash: Option<H256>) -> RpcResult<(H256, BlockHeader)> { | ||
block_header_at_or_latest(&self.db, hash) | ||
} | ||
|
||
async fn block_commitment_queue(&self, hash: Option<H256>) -> RpcResult<VecDeque<H256>> { | ||
let block_hash = block_header_at_or_latest(&self.db, hash)?.0; | ||
|
||
self.db | ||
.block_commitment_queue(block_hash) | ||
.ok_or_else(|| errors::db("Block commitment queue wasn't found")) | ||
} | ||
|
||
async fn block_events(&self, hash: Option<H256>) -> RpcResult<Vec<BlockRequestEvent>> { | ||
let block_hash = block_header_at_or_latest(&self.db, hash)?.0; | ||
|
||
self.db | ||
.block_events(block_hash) | ||
.ok_or_else(|| errors::db("Block events weren't found")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// This file is part of Gear. | ||
// | ||
// Copyright (C) 2024 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/>. | ||
|
||
mod block; | ||
mod program; | ||
|
||
pub use block::{BlockApi, BlockServer}; | ||
pub use program::{ProgramApi, ProgramServer}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// This file is part of Gear. | ||
// | ||
// Copyright (C) 2024 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::{common::block_header_at_or_latest, errors}; | ||
use ethexe_db::{CodesStorage, Database}; | ||
use ethexe_processor::Processor; | ||
use ethexe_runtime_common::state::{ | ||
Mailbox, MemoryPages, MessageQueue, ProgramState, Storage, Waitlist, | ||
}; | ||
use gear_core::message::ReplyInfo; | ||
use gprimitives::{H160, H256}; | ||
use jsonrpsee::{ | ||
core::{async_trait, RpcResult}, | ||
proc_macros::rpc, | ||
}; | ||
use parity_scale_codec::Encode; | ||
use sp_core::Bytes; | ||
|
||
#[rpc(server)] | ||
pub trait Program { | ||
#[method(name = "program_calculateReplyForHandle")] | ||
async fn calculate_reply_for_handle( | ||
&self, | ||
at: Option<H256>, | ||
source: H160, | ||
program_id: H160, | ||
payload: Bytes, | ||
value: u128, | ||
) -> RpcResult<ReplyInfo>; | ||
|
||
#[method(name = "program_ids")] | ||
async fn ids(&self) -> RpcResult<Vec<H160>>; | ||
|
||
#[method(name = "program_codeId")] | ||
async fn code_id(&self, program_id: H160) -> RpcResult<H256>; | ||
|
||
#[method(name = "program_readState")] | ||
async fn read_state(&self, hash: H256) -> RpcResult<ProgramState>; | ||
|
||
#[method(name = "program_readQueue")] | ||
async fn read_queue(&self, hash: H256) -> RpcResult<MessageQueue>; | ||
|
||
#[method(name = "program_readMailbox")] | ||
async fn read_mailbox(&self, hash: H256) -> RpcResult<Mailbox>; | ||
|
||
#[method(name = "program_readPages")] | ||
async fn read_pages(&self, hash: H256) -> RpcResult<MemoryPages>; | ||
|
||
#[method(name = "program_readWaitlist")] | ||
async fn read_waitlist(&self, hash: H256) -> RpcResult<Waitlist>; | ||
|
||
#[method(name = "program_readPageData")] | ||
async fn read_page_data(&self, hash: H256) -> RpcResult<Bytes>; | ||
} | ||
|
||
pub struct ProgramApi { | ||
db: Database, | ||
} | ||
|
||
impl ProgramApi { | ||
pub fn new(db: Database) -> Self { | ||
Self { db } | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ProgramServer for ProgramApi { | ||
async fn calculate_reply_for_handle( | ||
&self, | ||
at: Option<H256>, | ||
source: H160, | ||
program_id: H160, | ||
payload: Bytes, | ||
value: u128, | ||
) -> RpcResult<ReplyInfo> { | ||
let block_hash = block_header_at_or_latest(&self.db, at)?.0; | ||
|
||
// TODO (breathx): spawn in a new thread and catch panics. (?) Generally catch runtime panics (?). | ||
// TODO (breathx): optimize here instantiation if matches actual runtime. | ||
let processor = Processor::new(self.db.clone()).map_err(|_| errors::internal())?; | ||
|
||
let mut overlaid_processor = processor.overlaid(); | ||
|
||
overlaid_processor | ||
.execute_for_reply( | ||
block_hash, | ||
source.into(), | ||
program_id.into(), | ||
payload.0, | ||
value, | ||
) | ||
.map_err(errors::runtime) | ||
} | ||
|
||
async fn ids(&self) -> RpcResult<Vec<H160>> { | ||
Ok(self | ||
.db | ||
.program_ids() | ||
.into_iter() | ||
.map(|id| id.try_into().unwrap()) | ||
.collect()) | ||
} | ||
|
||
async fn code_id(&self, program_id: H160) -> RpcResult<H256> { | ||
self.db | ||
.program_code_id(program_id.into()) | ||
.ok_or_else(|| errors::db("Failed to get code id")) | ||
.map(|code_id| code_id.into()) | ||
} | ||
|
||
async fn read_state(&self, hash: H256) -> RpcResult<ProgramState> { | ||
self.db | ||
.read_state(hash) | ||
.ok_or_else(|| errors::db("Failed to read state by hash")) | ||
} | ||
|
||
async fn read_queue(&self, hash: H256) -> RpcResult<MessageQueue> { | ||
self.db | ||
.read_queue(hash) | ||
.ok_or_else(|| errors::db("Failed to read queue by hash")) | ||
} | ||
|
||
async fn read_mailbox(&self, hash: H256) -> RpcResult<Mailbox> { | ||
self.db | ||
.read_mailbox(hash) | ||
.ok_or_else(|| errors::db("Failed to read mailbox by hash")) | ||
} | ||
|
||
async fn read_pages(&self, hash: H256) -> RpcResult<MemoryPages> { | ||
self.db | ||
.read_pages(hash) | ||
.ok_or_else(|| errors::db("Failed to read pages by hash")) | ||
} | ||
|
||
// TODO: read the whole program state in a single query | ||
async fn read_waitlist(&self, hash: H256) -> RpcResult<Waitlist> { | ||
self.db | ||
.read_waitlist(hash) | ||
.ok_or_else(|| errors::db("Failed to read waitlist by hash")) | ||
} | ||
|
||
async fn read_page_data(&self, hash: H256) -> RpcResult<Bytes> { | ||
self.db | ||
.read_page_data(hash) | ||
.map(|buf| buf.encode().into()) | ||
.ok_or_else(|| errors::db("Failed to read page data by hash")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// This file is part of Gear. | ||
// | ||
// Copyright (C) 2024 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::{BlockHeader, BlockMetaStorage, Database}; | ||
use gprimitives::H256; | ||
use jsonrpsee::core::RpcResult; | ||
|
||
pub fn block_header_at_or_latest( | ||
db: &Database, | ||
at: impl Into<Option<H256>>, | ||
) -> RpcResult<(H256, BlockHeader)> { | ||
if let Some(hash) = at.into() { | ||
db.block_header(hash) | ||
.map(|header| (hash, header)) | ||
.ok_or_else(|| errors::db("Block header for requested hash wasn't found")) | ||
} else { | ||
db.latest_valid_block() | ||
.ok_or_else(|| errors::db("Latest block header wasn't found")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// This file is part of Gear. | ||
// | ||
// Copyright (C) 2024 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 jsonrpsee::types::ErrorObject; | ||
|
||
pub fn db(err: &'static str) -> ErrorObject<'static> { | ||
ErrorObject::owned(8000, "Database error", Some(err)) | ||
} | ||
|
||
pub fn runtime(err: anyhow::Error) -> ErrorObject<'static> { | ||
ErrorObject::owned(8000, "Runtime error", Some(format!("{err}"))) | ||
} | ||
|
||
pub fn internal() -> ErrorObject<'static> { | ||
ErrorObject::owned(8000, "Internal error", None::<&str>) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters