1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
    Copyright © 2023, ParallelChain Lab 
    Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
*/

//! The request-response structures for RPC.

use std::collections::{HashSet, HashMap};

use hotstuff_rs::types::{CryptoHash, BlockHeight};
use crate::{Transaction, Serializable, Block, BlockHeader, Receipt, PublicAddress, Stake, Deserializable, CommandReceipt};

/* Transaction RPCs */

/// Submit a transaction to the mempool.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct SubmitTransactionRequest {
    pub transaction: Transaction
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct SubmitTransactionResponse {
    pub error: Option<SubmitTransactionError>,
}

#[derive(Debug, borsh::BorshSerialize, borsh::BorshDeserialize)]
pub enum SubmitTransactionError {
    UnacceptableNonce,
    MempoolFull,
    Other,
}

/// Get a transaction and optionally its receipt.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct TransactionRequest {    
    pub transaction_hash: CryptoHash,
    pub include_receipt: bool,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct TransactionResponse {
    pub transaction: Option<Transaction>,
    pub receipt: Option<Receipt>,
    pub block_hash: Option<CryptoHash>,
    pub position: Option<u32>,
}

/// Find out where a transaction is in the blockchain.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct TransactionPositionRequest {
    pub transaction_hash: CryptoHash,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct TransactionPositionResponse {
    pub transaction_hash: Option<CryptoHash>,
    pub block_hash: Option<CryptoHash>,
    pub position: Option<u32>,
}

/// Get a transaction's receipt.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct ReceiptRequest {    
    pub transaction_hash: CryptoHash,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct ReceiptResponse {
    pub transaction_hash: CryptoHash,
    pub receipt: Option<Receipt>,
    pub block_hash: Option<CryptoHash>,
    pub position: Option<u32>,
}

/* Block RPCs */

/// Get a block by its block hash.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct BlockRequest {
    pub block_hash: CryptoHash
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct BlockResponse {
    pub block: Option<Block>,
}

/// Get a block header by its block hash.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct BlockHeaderRequest {
    pub block_hash: CryptoHash
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct BlockHeaderResponse {
    pub block_header: Option<BlockHeader>,
}

/// Get the height of the block with a given block hash.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct BlockHeightByHashRequest {
    pub block_hash: CryptoHash,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct BlockHeightByHashResponse {
    pub block_hash: CryptoHash,
    pub block_height: Option<BlockHeight>,
}

/// Get the hash of a block at a given height.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct BlockHashByHeightRequest {
    pub block_height: BlockHeight,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct BlockHashByHeightResponse {
    pub block_height: BlockHeight,
    pub block_hash: Option<CryptoHash>,
}

/// Return the hash of the highest committed block.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct HighestCommittedBlockResponse {
    pub block_hash: Option<CryptoHash>
}

/* State RPCs */

/// Get the state of a set of accounts (optionally including their contract code), and/or a set of storage tuples.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct StateRequest {
    pub accounts: HashSet<PublicAddress>,
    pub include_contract: bool,
    pub storage_keys: HashMap<PublicAddress, HashSet<Vec<u8>>>,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct StateResponse {
    pub accounts: HashMap<PublicAddress, Account>,
    pub storage_tuples: HashMap<PublicAddress, HashMap<Vec<u8>, Vec<u8>>>,
    pub block_hash: CryptoHash,
}

/// Get the previous, current, and next validator sets, optionally including the stakes delegated to them.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct ValidatorSetsRequest {
    pub include_prev: bool,
    pub include_prev_delegators: bool,
    pub include_curr: bool,
    pub include_curr_delegators: bool,
    pub include_next: bool,
    pub include_next_delegators: bool,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct ValidatorSetsResponse {
    // The inner Option is None if we are at Epoch 0.
    pub previous_validator_set: Option<Option<ValidatorSet>>,
    pub current_validator_set: Option<ValidatorSet>,
    pub next_validator_set: Option<ValidatorSet>,
    pub block_hash: CryptoHash,
}

/// Get a set of pools.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct PoolsRequest {
    pub operators: HashSet<Operator>,
    pub include_stakes: bool,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct PoolsResponse {
    pub pools: HashMap<Operator, Option<Pool>>,
    pub block_hash: CryptoHash,
}

/// Get a set of stakes.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct StakesRequest {
    pub stakes: HashSet<(Operator, Owner)>,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct StakesResponse {
    pub stakes: HashMap<(Operator, Owner), Option<Stake>>,
    pub block_hash: CryptoHash,
}

/// Get a set of deposits.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct DepositsRequest {
    pub stakes: HashSet<(Operator, Owner)>,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct DepositsResponse {
    pub deposits: HashMap<(Operator, Owner), Option<Deposit>>,
    pub block_hash: CryptoHash,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct Deposit {
    pub owner: PublicAddress,
    pub balance: u64,
    pub auto_stake_rewards: bool,
}

/* Account-related types */

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub enum Account {
    WithContract(AccountWithContract),
    WithoutContract(AccountWithoutContract)
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct AccountWithContract {
    pub nonce: u64,
    pub balance: u64,
    pub contract: Option<Vec<u8>>,
    pub cbi_version: Option<u32>,
    pub storage_hash: Option<CryptoHash>,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct AccountWithoutContract {
    pub nonce: u64,
    pub balance: u64,
    pub cbi_version: Option<u32>,
    pub storage_hash: Option<CryptoHash>,
}

/// Call a method in a contract in a read-only way.
#[derive(borsh::BorshSerialize, borsh::BorshDeserialize)]
pub struct ViewRequest {
    pub target: PublicAddress,
    pub method: Vec<u8>,
    pub arguments: Option<Vec<Vec<u8>>>
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct ViewResponse {
    pub receipt: CommandReceipt
}

/* Staking-related types */

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub enum ValidatorSet {
    WithDelegators(Vec<PoolWithDelegators>),
    WithoutDelegators(Vec<PoolWithoutDelegators>),
}

pub type Operator = PublicAddress;
pub type Owner = PublicAddress;

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct PoolWithDelegators {
    pub operator: PublicAddress,
    pub power: u64,
    pub commission_rate: u8, 
    pub operator_stake: Option<Stake>,
    pub delegated_stakes: Vec<Stake>,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub struct PoolWithoutDelegators {
    pub operator: PublicAddress,
    pub power: u64,
    pub commission_rate: u8, 
    pub operator_stake: Option<Stake>,
}

#[derive(borsh::BorshSerialize, borsh::BorshDeserialize, Debug)]
pub enum Pool {
    WithStakes(PoolWithDelegators),
    WithoutStakes(PoolWithoutDelegators),
}

macro_rules! define_serde {
    ($($t:ty),*) => {
        $(
            impl Serializable for $t {}
            impl Deserializable for $t {}
        )*
    }
}

define_serde!(
    SubmitTransactionRequest, SubmitTransactionResponse,
    TransactionRequest, TransactionResponse,
    TransactionPositionRequest, TransactionPositionResponse,
    ReceiptRequest, ReceiptResponse,
    BlockRequest, BlockResponse,
    BlockHeaderRequest, BlockHeaderResponse,
    BlockHeightByHashRequest, BlockHeightByHashResponse,
    BlockHashByHeightRequest, BlockHashByHeightResponse,
    HighestCommittedBlockResponse,
    StateRequest, StateResponse,
    ValidatorSetsRequest, ValidatorSetsResponse,
    PoolsRequest, PoolsResponse,
    StakesRequest, StakesResponse,
    DepositsRequest, DepositsResponse,
    ViewRequest, ViewResponse
);