Skip to main content

ab_farmer_rpc_primitives/
lib.rs

1//! Primitives for the farmer
2
3use ab_core_primitives::block::BlockRoot;
4use ab_core_primitives::block::header::OwnedBlockHeaderSeal;
5use ab_core_primitives::hashes::Blake3Hash;
6use ab_core_primitives::pot::SlotNumber;
7use ab_core_primitives::segments::HistorySize;
8use ab_core_primitives::shard::NumShards;
9use ab_core_primitives::solutions::{ShardMembershipEntropy, Solution, SolutionRange};
10use ab_farmer_components::FarmerProtocolInfo;
11use ab_networking::libp2p::Multiaddr;
12use parity_scale_codec::{Decode, Encode, EncodeLike, Input, Output};
13use serde::{Deserialize, Serialize};
14use std::time::Duration;
15
16/// Defines a limit for the number of super segments that can be requested over RPC
17pub const MAX_SUPER_SEGMENT_HEADERS_PER_REQUEST: usize = 1000;
18// TODO: This is a workaround for https://github.com/paritytech/jsonrpsee/issues/1617 and should be
19//  removed once that issue is resolved
20/// Shard membership expiration
21pub const SHARD_MEMBERSHIP_EXPIRATION: Duration = Duration::from_mins(1);
22
23/// Information necessary for farmer application
24#[derive(Debug, Clone, Serialize, Deserialize)]
25#[serde(rename_all = "camelCase")]
26pub struct FarmerAppInfo {
27    /// Genesis root of the beacon chain
28    pub genesis_root: BlockRoot,
29    /// Bootstrap nodes for DSN
30    pub dsn_bootstrap_nodes: Vec<Multiaddr>,
31    /// Whether node is syncing right now
32    pub syncing: bool,
33    /// How much time farmer has to audit sectors and generate a solution
34    pub farming_timeout: Duration,
35    /// Protocol info for farmer
36    pub protocol_info: FarmerProtocolInfo,
37}
38
39impl Encode for FarmerAppInfo {
40    fn size_hint(&self) -> usize {
41        0_usize
42            .saturating_add(Encode::size_hint(&self.genesis_root))
43            .saturating_add(Encode::size_hint(
44                &self
45                    .dsn_bootstrap_nodes
46                    .iter()
47                    .map(AsRef::as_ref)
48                    .collect::<Vec<_>>(),
49            ))
50            .saturating_add(Encode::size_hint(&self.syncing))
51            .saturating_add(Encode::size_hint(&self.farming_timeout))
52            .saturating_add(Encode::size_hint(&self.protocol_info))
53    }
54
55    fn encode_to<O>(&self, dest: &mut O)
56    where
57        O: Output + ?Sized,
58    {
59        Encode::encode_to(&self.genesis_root, dest);
60        Encode::encode_to(
61            &self
62                .dsn_bootstrap_nodes
63                .iter()
64                .map(AsRef::as_ref)
65                .collect::<Vec<_>>(),
66            dest,
67        );
68        Encode::encode_to(&self.syncing, dest);
69        Encode::encode_to(&self.farming_timeout, dest);
70        Encode::encode_to(&self.protocol_info, dest);
71    }
72}
73
74impl EncodeLike for FarmerAppInfo {}
75
76impl Decode for FarmerAppInfo {
77    fn decode<I>(input: &mut I) -> Result<Self, parity_scale_codec::Error>
78    where
79        I: Input,
80    {
81        Ok(FarmerAppInfo {
82            genesis_root: BlockRoot::decode(input)
83                .map_err(|error| error.chain("Could not decode `FarmerAppInfo::genesis_root`"))?,
84            dsn_bootstrap_nodes: Vec::<Vec<u8>>::decode(input)
85                .map_err(|error| {
86                    error.chain("Could not decode `FarmerAppInfo::dsn_bootstrap_nodes`")
87                })?
88                .into_iter()
89                .map(Multiaddr::try_from)
90                .collect::<Result<Vec<_>, _>>()
91                .map_err(|error| {
92                    parity_scale_codec::Error::from("Failed to decode bytes as Multiaddr")
93                        .chain(error.to_string())
94                        .chain("Could not decode `FarmerAppInfo::dsn_bootstrap_nodes`")
95                })?,
96            syncing: bool::decode(input)
97                .map_err(|error| error.chain("Could not decode `FarmerAppInfo::syncing`"))?,
98            farming_timeout: Duration::decode(input).map_err(|error| {
99                error.chain("Could not decode `FarmerAppInfo::farming_timeout`")
100            })?,
101            protocol_info: FarmerProtocolInfo::decode(input)
102                .map_err(|error| error.chain("Could not decode `FarmerAppInfo::protocol_info`"))?,
103        })
104    }
105}
106
107/// Information about new slot that just arrived
108#[derive(Debug, Copy, Clone, Eq, PartialEq, Encode, Decode, Serialize, Deserialize)]
109#[serde(rename_all = "camelCase")]
110pub struct SlotInfo {
111    /// Slot number
112    pub slot: SlotNumber,
113    /// Global slot challenge
114    pub global_challenge: Blake3Hash,
115    /// Acceptable solution range for farmer audits
116    pub solution_range: SolutionRange,
117    /// Shard membership entropy
118    pub shard_membership_entropy: ShardMembershipEntropy,
119    /// The number of shards in the network
120    pub num_shards: NumShards,
121}
122
123/// Response of a slot challenge consisting of an optional solution and
124/// the submitter(farmer)'s secret key for block signing.
125#[derive(Clone, Debug, Encode, Decode, Serialize, Deserialize)]
126#[serde(rename_all = "camelCase")]
127pub struct SolutionResponse {
128    /// Slot number.
129    pub slot_number: SlotNumber,
130    /// Solution farmer has for the challenge.
131    ///
132    /// Corresponds to `slot_number` above.
133    pub solution: Solution,
134}
135
136/// Block sealing info
137#[derive(Clone, Copy, Debug, Encode, Decode, Serialize, Deserialize)]
138#[serde(rename_all = "camelCase")]
139pub struct BlockSealInfo {
140    /// Block pre-seal hash to be signed
141    pub pre_seal_hash: Blake3Hash,
142    /// Public key hash of the plot identity that should create signature
143    pub public_key_hash: Blake3Hash,
144}
145
146/// Block sealing response
147#[derive(Clone, Copy, Debug, Encode, Decode, Serialize, Deserialize)]
148#[serde(rename_all = "camelCase")]
149pub struct BlockSealResponse {
150    /// Block pre-seal hash that was signed
151    pub pre_seal_hash: Blake3Hash,
152    /// The seal itself
153    pub seal: OwnedBlockHeaderSeal,
154}
155
156/// Farmer shard membership info
157#[derive(Debug, Clone, Eq, PartialEq, Encode, Decode, Serialize, Deserialize)]
158#[serde(rename_all = "camelCase")]
159pub struct FarmerShardMembershipInfo {
160    /// Public key hash of the plot identity
161    pub public_key_hash: Blake3Hash,
162    /// Seed used to derive the shard commitment (typically a hash of the private key)
163    pub shard_commitments_seed: Blake3Hash,
164    /// History sizes
165    pub history_sizes: Vec<HistorySize>,
166}