|
| 1 | +use alloy_primitives::{Address, B256, Bytes}; |
| 2 | +use jsonrpsee::proc_macros::rpc; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use ssz_types::{VariableList, typenum}; |
| 5 | +use tree_hash_derive::TreeHash; |
| 6 | + |
| 7 | +use crate::communication::messages::RpcResult; |
| 8 | + |
| 9 | +pub type MaxCommitmentRequestPayloadSize = typenum::U1048576; // 1MB. |
| 10 | +pub type CommitmentRequestPayload = VariableList<u8, MaxCommitmentRequestPayloadSize>; |
| 11 | + |
| 12 | +pub const FRAG_COMMITMENT_TYPE: u64 = 7; |
| 13 | + |
| 14 | +/// A CommitmentRequest message created by a user |
| 15 | +#[derive(Clone, Serialize, Deserialize, Debug, TreeHash)] |
| 16 | +#[serde(rename_all = "camelCase")] |
| 17 | +pub struct CommitmentRequest { |
| 18 | + pub commitment_type: u64, |
| 19 | + pub payload: CommitmentRequestPayload, |
| 20 | + pub slasher: Address, |
| 21 | +} |
| 22 | + |
| 23 | +/// A Commitment message responding to a CommitmentRequest |
| 24 | +#[derive(Clone, Serialize, Deserialize, Debug)] |
| 25 | +#[serde(rename_all = "camelCase")] |
| 26 | +pub struct Commitment { |
| 27 | + pub commitment_type: u64, |
| 28 | + pub payload: Bytes, |
| 29 | + pub request_hash: B256, |
| 30 | + pub slasher: Address, |
| 31 | +} |
| 32 | + |
| 33 | +/// A signed Commitment binding to a CommitmentRequest |
| 34 | +#[derive(Clone, Serialize, Deserialize, Debug)] |
| 35 | +#[serde(rename_all = "camelCase")] |
| 36 | +pub struct SignedCommitment { |
| 37 | + pub commitment: Commitment, |
| 38 | + pub signature: Bytes, |
| 39 | +} |
| 40 | + |
| 41 | +/// Specifies which commitments can be made for a specific chain |
| 42 | +#[derive(Clone, Serialize, Deserialize, Debug)] |
| 43 | +#[serde(rename_all = "camelCase")] |
| 44 | +pub struct Offering { |
| 45 | + pub chain_id: u64, |
| 46 | + pub commitment_types: Vec<u64>, |
| 47 | +} |
| 48 | + |
| 49 | +/// Information about a Gateway's offerings at a specific slot |
| 50 | +#[derive(Clone, Serialize, Deserialize, Debug)] |
| 51 | +#[serde(rename_all = "camelCase")] |
| 52 | +pub struct SlotInfo { |
| 53 | + pub slot: u64, |
| 54 | + pub offerings: Vec<Offering>, |
| 55 | +} |
| 56 | + |
| 57 | +/// Response containing multiple SlotInfo |
| 58 | +#[derive(Clone, Serialize, Deserialize, Debug, Default)] |
| 59 | +#[serde(rename_all = "camelCase")] |
| 60 | +pub struct SlotInfoResponse { |
| 61 | + pub slots: Vec<SlotInfo>, |
| 62 | +} |
| 63 | + |
| 64 | +/// Fee information for a specific commitment request |
| 65 | +#[derive(Clone, Serialize, Deserialize, Debug)] |
| 66 | +#[serde(rename_all = "camelCase")] |
| 67 | +pub struct FeeInfo { |
| 68 | + pub payload: Bytes, |
| 69 | + pub commitment_type: u64, |
| 70 | +} |
| 71 | + |
| 72 | +impl Default for FeeInfo { |
| 73 | + fn default() -> Self { |
| 74 | + Self { payload: Bytes::new(), commitment_type: FRAG_COMMITMENT_TYPE } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +#[rpc(client, server, namespace = "gateway")] |
| 79 | +pub trait FabricGatewayApi { |
| 80 | + /// Request a new SignedCommitment |
| 81 | + #[method(name = "commitment")] |
| 82 | + async fn post_commitment(&self, commitment_request: CommitmentRequest) -> RpcResult<SignedCommitment>; |
| 83 | + |
| 84 | + /// Request an existing SignedCommitment by request hash |
| 85 | + #[method(name = "getCommitment")] |
| 86 | + async fn get_commitment(&self, request_hash: B256) -> RpcResult<SignedCommitment>; |
| 87 | + |
| 88 | + /// Get Gateway information for upcoming slots |
| 89 | + #[method(name = "slots")] |
| 90 | + async fn get_slots(&self) -> RpcResult<SlotInfoResponse>; |
| 91 | + |
| 92 | + /// Get commitment fee information |
| 93 | + #[method(name = "fee")] |
| 94 | + async fn get_fee_info(&self, commitment_request: CommitmentRequest) -> RpcResult<FeeInfo>; |
| 95 | +} |
0 commit comments