|
| 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 | +} |
0 commit comments