Skip to content

Commit 27762d5

Browse files
committed
feat(ethexe-rpc): add API to query announce info
Closes #5184
1 parent f0184ae commit 27762d5

File tree

4 files changed

+102
-5
lines changed

4 files changed

+102
-5
lines changed

ethexe/common/src/primitives.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub struct SimpleBlockData {
7878
pub header: BlockHeader,
7979
}
8080

81-
#[cfg_attr(feature = "serde", derive(Hash))]
81+
#[cfg_attr(feature = "serde", derive(Hash, serde::Serialize))]
8282
#[derive(Clone, Debug, Encode, Decode, TypeInfo, PartialEq, Eq, derive_more::Display)]
8383
#[display(
8484
"Announce(block: {block_hash}, parent: {parent}, gas: {gas_allowance:?}, txs: {injected_transactions:?})"

ethexe/rpc/src/apis/announce.rs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// This file is part of Gear.
2+
//
3+
// Copyright (C) 2026 Gear Technologies Inc.
4+
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5+
//
6+
// This program is free software: you can redistribute it and/or modify
7+
// it under the terms of the GNU General Public License as published by
8+
// the Free Software Foundation, either version 3 of the License, or
9+
// (at your option) any later version.
10+
//
11+
// This program is distributed in the hope that it will be useful,
12+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
// GNU General Public License for more details.
15+
//
16+
// You should have received a copy of the GNU General Public License
17+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
19+
use crate::{errors, utils};
20+
use ethexe_common::{Announce, ProgramStates, db::AnnounceStorageRO, gear::StateTransition};
21+
use ethexe_db::Database;
22+
use jsonrpsee::{
23+
core::{RpcResult, async_trait},
24+
proc_macros::rpc,
25+
};
26+
use sp_core::H256;
27+
28+
#[cfg_attr(not(feature = "client"), rpc(server))]
29+
#[cfg_attr(feature = "client", rpc(server, client))]
30+
pub trait Announce {
31+
#[method(name = "announce_by_hash")]
32+
async fn announce(&self, announce_hash: Option<H256>) -> RpcResult<(H256, Announce)>;
33+
34+
#[method(name = "announce_outcome")]
35+
async fn announce_outcome(
36+
&self,
37+
announce_hash: Option<H256>,
38+
) -> RpcResult<Vec<StateTransition>>;
39+
40+
#[method(name = "announce_program_states")]
41+
async fn announce_program_states(
42+
&self,
43+
announce_hash: Option<H256>,
44+
) -> RpcResult<ProgramStates>;
45+
}
46+
47+
#[derive(Debug, Clone)]
48+
pub struct AnnounceApi {
49+
db: Database,
50+
}
51+
52+
impl AnnounceApi {
53+
pub fn new(db: Database) -> Self {
54+
Self { db }
55+
}
56+
}
57+
58+
#[async_trait]
59+
impl AnnounceServer for AnnounceApi {
60+
async fn announce(&self, announce_hash: Option<H256>) -> RpcResult<(H256, Announce)> {
61+
let hash = utils::announce_at_or_latest_computed(&self.db, announce_hash)?;
62+
let announce = self
63+
.db
64+
.announce(hash)
65+
.ok_or_else(|| errors::db("Announce wasn't found"))?;
66+
67+
Ok((hash.inner(), announce))
68+
}
69+
70+
async fn announce_outcome(
71+
&self,
72+
announce_hash: Option<H256>,
73+
) -> RpcResult<Vec<StateTransition>> {
74+
let hash = utils::announce_at_or_latest_computed(&self.db, announce_hash)?;
75+
self.db
76+
.announce_outcome(hash)
77+
.ok_or_else(|| errors::db("Announce outcome wasn't found"))
78+
}
79+
80+
async fn announce_program_states(
81+
&self,
82+
announce_hash: Option<H256>,
83+
) -> RpcResult<ProgramStates> {
84+
let hash = utils::announce_at_or_latest_computed(&self.db, announce_hash)?;
85+
self.db
86+
.announce_program_states(hash)
87+
.ok_or_else(|| errors::db("Announce program states weren't found"))
88+
}
89+
}

ethexe/rpc/src/apis/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@
1616
// You should have received a copy of the GNU General Public License
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

19+
mod announce;
1920
mod block;
2021
mod code;
2122
mod injected;
2223
mod program;
2324

25+
pub use announce::{AnnounceApi, AnnounceServer};
2426
pub use block::{BlockApi, BlockServer};
2527
pub use code::{CodeApi, CodeServer};
2628
pub use injected::{InjectedApi, InjectedServer};
27-
pub use program::{FullProgramState, ProgramApi, ProgramServer};
29+
pub use program::{ProgramApi, ProgramServer};
2830

2931
#[cfg(feature = "client")]
3032
pub use crate::apis::{
31-
block::BlockClient, code::CodeClient, injected::InjectedClient, program::ProgramClient,
33+
announce::AnnounceClient, block::BlockClient, code::CodeClient, injected::InjectedClient,
34+
program::ProgramClient,
3235
};

ethexe/rpc/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub use crate::apis::{BlockClient, CodeClient, FullProgramState, InjectedClient,
2121

2222
use anyhow::Result;
2323
use apis::{
24-
BlockApi, BlockServer, CodeApi, CodeServer, InjectedApi, InjectedServer, ProgramApi,
25-
ProgramServer,
24+
AnnounceApi, AnnounceServer, BlockApi, BlockServer, CodeApi, CodeServer, InjectedApi,
25+
InjectedServer, ProgramApi, ProgramServer,
2626
};
2727
use ethexe_common::injected::{
2828
AddressedInjectedTransaction, InjectedTransactionAcceptance, SignedPromise,
@@ -113,6 +113,7 @@ impl RpcServer {
113113
let server_apis = RpcServerApis {
114114
code: CodeApi::new(self.db.clone()),
115115
block: BlockApi::new(self.db.clone()),
116+
announce: AnnounceApi::new(self.db.clone()),
116117
program: ProgramApi::new(self.db.clone(), processor, self.config.gas_allowance),
117118
injected: InjectedApi::new(rpc_sender),
118119
};
@@ -181,6 +182,7 @@ impl FusedStream for RpcService {
181182

182183
struct RpcServerApis {
183184
pub block: BlockApi,
185+
pub announce: AnnounceApi,
184186
pub code: CodeApi,
185187
pub injected: InjectedApi,
186188
pub program: ProgramApi,
@@ -193,6 +195,9 @@ impl RpcServerApis {
193195
module
194196
.merge(BlockServer::into_rpc(self.block))
195197
.expect("No conflicts");
198+
module
199+
.merge(AnnounceServer::into_rpc(self.announce))
200+
.expect("No conflicts");
196201
module
197202
.merge(CodeServer::into_rpc(self.code))
198203
.expect("No conflicts");

0 commit comments

Comments
 (0)