pchain_types/
rpc.rs

1/*
2    Copyright © 2023, ParallelChain Lab 
3    Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
4*/
5
6//! RPC requests and responses, and the additional types included in them.
7
8use std::collections::{HashSet, HashMap};
9use borsh::{BorshSerialize, BorshDeserialize};
10use hotstuff_rs::types::{CryptoHash, BlockHeight};
11use crate::serialization::{Serializable, Deserializable};
12use crate::cryptography::PublicAddress;
13use crate::blockchain::{Block, BlockHeader, Transaction, Receipt, CommandReceipt};
14
15/* Transaction RPCs */
16
17/// Submit a transaction to the mempool.
18#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
19pub struct SubmitTransactionRequest {
20    pub transaction: Transaction
21}
22
23#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
24pub struct SubmitTransactionResponse {
25    pub error: Option<SubmitTransactionError>,
26}
27
28#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
29pub enum SubmitTransactionError {
30    UnacceptableNonce,
31    MempoolFull,
32    Other,
33}
34
35/// Get a transaction and optionally its receipt.
36#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
37pub struct TransactionRequest {    
38    pub transaction_hash: CryptoHash,
39    pub include_receipt: bool,
40}
41
42#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
43pub struct TransactionResponse {
44    pub transaction: Option<Transaction>,
45    pub receipt: Option<Receipt>,
46    pub block_hash: Option<CryptoHash>,
47    pub position: Option<u32>,
48}
49
50/// Find out where a transaction is in the blockchain.
51#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
52pub struct TransactionPositionRequest {
53    pub transaction_hash: CryptoHash,
54}
55
56#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
57pub struct TransactionPositionResponse {
58    pub transaction_hash: Option<CryptoHash>,
59    pub block_hash: Option<CryptoHash>,
60    pub position: Option<u32>,
61}
62
63/// Get a transaction's receipt.
64#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
65pub struct ReceiptRequest {    
66    pub transaction_hash: CryptoHash,
67}
68
69#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
70pub struct ReceiptResponse {
71    pub transaction_hash: CryptoHash,
72    pub receipt: Option<Receipt>,
73    pub block_hash: Option<CryptoHash>,
74    pub position: Option<u32>,
75}
76
77/* Block RPCs */
78
79/// Get a block by its block hash.
80#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
81pub struct BlockRequest {
82    pub block_hash: CryptoHash
83}
84
85#[derive(Clone, BorshSerialize, BorshDeserialize)]
86pub struct BlockResponse {
87    pub block: Option<Block>,
88}
89
90/// Get a block header by its block hash.
91#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
92pub struct BlockHeaderRequest {
93    pub block_hash: CryptoHash
94}
95
96#[derive(Clone, BorshSerialize, BorshDeserialize)]
97pub struct BlockHeaderResponse {
98    pub block_header: Option<BlockHeader>,
99}
100
101/// Get the height of the block with a given block hash.
102#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
103pub struct BlockHeightByHashRequest {
104    pub block_hash: CryptoHash,
105}
106
107#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
108pub struct BlockHeightByHashResponse {
109    pub block_hash: CryptoHash,
110    pub block_height: Option<BlockHeight>,
111}
112
113/// Get the hash of a block at a given height.
114#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
115pub struct BlockHashByHeightRequest {
116    pub block_height: BlockHeight,
117}
118
119#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
120pub struct BlockHashByHeightResponse {
121    pub block_height: BlockHeight,
122    pub block_hash: Option<CryptoHash>,
123}
124
125/// Return the hash of the highest committed block.
126#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
127pub struct HighestCommittedBlockResponse {
128    pub block_hash: Option<CryptoHash>
129}
130
131/* State RPCs */
132
133/// Get the state of a set of accounts (optionally including their contract code), and/or a set of storage tuples.
134#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
135pub struct StateRequest {
136    pub accounts: HashSet<PublicAddress>,
137    pub include_contract: bool,
138    pub storage_keys: HashMap<PublicAddress, HashSet<Vec<u8>>>,
139}
140
141#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
142pub struct StateResponse {
143    pub accounts: HashMap<PublicAddress, Account>,
144    pub storage_tuples: HashMap<PublicAddress, HashMap<Vec<u8>, Vec<u8>>>,
145    pub block_hash: CryptoHash,
146}
147
148/// Get the previous, current, and next validator sets, optionally including the stakes delegated to them.
149#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
150pub struct ValidatorSetsRequest {
151    pub include_prev: bool,
152    pub include_prev_delegators: bool,
153    pub include_curr: bool,
154    pub include_curr_delegators: bool,
155    pub include_next: bool,
156    pub include_next_delegators: bool,
157}
158
159#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
160pub struct ValidatorSetsResponse {
161    // The inner Option is None if we are at Epoch 0.
162    pub previous_validator_set: Option<Option<ValidatorSet>>,
163    pub current_validator_set: Option<ValidatorSet>,
164    pub next_validator_set: Option<ValidatorSet>,
165    pub block_hash: CryptoHash,
166}
167
168/// Get a set of pools.
169#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
170pub struct PoolsRequest {
171    pub operators: HashSet<Operator>,
172    pub include_stakes: bool,
173}
174
175#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
176pub struct PoolsResponse {
177    pub pools: HashMap<Operator, Option<Pool>>,
178    pub block_hash: CryptoHash,
179}
180
181/// Get a set of stakes.
182#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
183pub struct StakesRequest {
184    pub stakes: HashSet<(Operator, Owner)>,
185}
186
187#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
188pub struct StakesResponse {
189    pub stakes: HashMap<(Operator, Owner), Option<Stake>>,
190    pub block_hash: CryptoHash,
191}
192
193/// Get a set of deposits.
194
195#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
196pub struct DepositsRequest {
197    pub stakes: HashSet<(Operator, Owner)>,
198}
199
200#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
201pub struct DepositsResponse {
202    pub deposits: HashMap<(Operator, Owner), Option<Deposit>>,
203    pub block_hash: CryptoHash,
204}
205
206/// Call a method in a contract in a read-only way.
207#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
208pub struct ViewRequest {
209    pub target: PublicAddress,
210    pub method: Vec<u8>,
211    pub arguments: Option<Vec<Vec<u8>>>
212}
213
214#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
215pub struct ViewResponse {
216    pub receipt: CommandReceipt
217}
218
219/* Account-related types */
220
221#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
222pub enum Account {
223    WithContract(AccountWithContract),
224    WithoutContract(AccountWithoutContract)
225}
226
227#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
228pub struct AccountWithContract {
229    pub nonce: u64,
230    pub balance: u64,
231    pub contract: Option<Vec<u8>>,
232    pub cbi_version: Option<u32>,
233    pub storage_hash: Option<CryptoHash>,
234}
235
236#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
237pub struct AccountWithoutContract {
238    pub nonce: u64,
239    pub balance: u64,
240    pub cbi_version: Option<u32>,
241    pub storage_hash: Option<CryptoHash>,
242}
243
244/* DPoS-related types */
245
246#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
247pub enum ValidatorSet {
248    WithDelegators(Vec<PoolWithDelegators>),
249    WithoutDelegators(Vec<PoolWithoutDelegators>),
250}
251
252pub type Operator = PublicAddress;
253pub type Owner = PublicAddress;
254
255#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
256
257pub struct PoolWithDelegators {
258    pub operator: PublicAddress,
259    pub power: u64,
260    pub commission_rate: u8, 
261    pub operator_stake: Option<Stake>,
262    pub delegated_stakes: Vec<Stake>,
263}
264
265#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
266pub struct PoolWithoutDelegators {
267    pub operator: PublicAddress,
268    pub power: u64,
269    pub commission_rate: u8, 
270    pub operator_stake: Option<Stake>,
271}
272
273#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
274pub struct Deposit {
275    pub owner: PublicAddress,
276    pub balance: u64,
277    pub auto_stake_rewards: bool,
278}
279
280#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
281pub struct Stake {
282    pub owner: PublicAddress,
283    pub power: u64, 
284}
285
286#[derive(Debug, Clone, BorshSerialize, BorshDeserialize)]
287pub enum Pool {
288    WithStakes(PoolWithDelegators),
289    WithoutStakes(PoolWithoutDelegators),
290}
291
292macro_rules! define_serde {
293    ($($t:ty),*) => {
294        $(
295            impl Serializable for $t {}
296            impl Deserializable for $t {}
297        )*
298    }
299}
300
301define_serde!(
302    SubmitTransactionRequest, SubmitTransactionResponse,
303    TransactionRequest, TransactionResponse,
304    TransactionPositionRequest, TransactionPositionResponse,
305    ReceiptRequest, ReceiptResponse,
306    BlockRequest, BlockResponse,
307    BlockHeaderRequest, BlockHeaderResponse,
308    BlockHeightByHashRequest, BlockHeightByHashResponse,
309    BlockHashByHeightRequest, BlockHashByHeightResponse,
310    HighestCommittedBlockResponse,
311    StateRequest, StateResponse,
312    ValidatorSetsRequest, ValidatorSetsResponse,
313    PoolsRequest, PoolsResponse,
314    StakesRequest, StakesResponse,
315    DepositsRequest, DepositsResponse,
316    ViewRequest, ViewResponse
317);