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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use crate::error::RPCError;
use ckb_chain::chain::ChainController;
use ckb_jsonrpc_types::{Block, BlockTemplate, Uint64, Version};
use ckb_logger::{debug, error, info};
use ckb_network::{NetworkController, SupportProtocols};
use ckb_shared::{shared::Shared, Snapshot};
use ckb_types::{core, packed, prelude::*, H256};
use ckb_verification::HeaderVerifier;
use ckb_verification_traits::Verifier;
use faketime::unix_time_as_millis;
use jsonrpc_core::{Error, Result};
use jsonrpc_derive::rpc;
use std::collections::HashSet;
use std::fmt::Debug;
use std::sync::Arc;
/// RPC Module Miner for miners.
///
/// A miner gets a template from CKB, optionally selects transactions, resolves the PoW puzzle, and
/// submits the found new block.
#[rpc(server)]
pub trait MinerRpc {
/// Returns block template for miners.
///
/// Miners can assemble the new block from the template. The RPC is designed to allow miners
/// to remove transactions and adding new transactions to the block.
///
/// ## Params
///
/// * `bytes_limit` - the max serialization size in bytes of the block.
/// (**Optional:** the default is the consensus limit.)
/// * `proposals_limit` - the max count of proposals.
/// (**Optional:** the default is the consensus limit.)
/// * `max_version` - the max block version.
/// (**Optional:** the default is one configured in the current client version.)
///
/// ## Examples
///
/// Request
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "method": "get_block_template",
/// "params": [
/// null,
/// null,
/// null
/// ]
/// }
/// ```
///
/// Response
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "result": {
/// "bytes_limit": "0x91c08",
/// "cellbase": {
/// "cycles": null,
/// "data": {
/// "cell_deps": [],
/// "header_deps": [],
/// "inputs": [
/// {
/// "previous_output": {
/// "index": "0xffffffff",
/// "tx_hash": "0x0000000000000000000000000000000000000000000000000000000000000000"
/// },
/// "since": "0x401"
/// }
/// ],
/// "outputs": [
/// {
/// "capacity": "0x18e64efc04",
/// "lock": {
/// "code_hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5",
/// "hash_type": "data",
/// "args": "0x"
/// },
/// "type": null
/// }
/// ],
/// "outputs_data": [
/// "0x"
/// ],
/// "version": "0x0",
/// "witnesses": [
/// "0x650000000c00000055000000490000001000000030000000310000001892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df20114000000b2e61ff569acf041b3c2c17724e2379c581eeac30c00000054455354206d657373616765"
/// ]
/// },
/// "hash": "0xbaf7e4db2fd002f19a597ca1a31dfe8cfe26ed8cebc91f52b75b16a7a5ec8bab"
/// },
/// "compact_target": "0x1e083126",
/// "current_time": "0x174c45e17a3",
/// "cycles_limit": "0xd09dc300",
/// "dao": "0xd495a106684401001e47c0ae1d5930009449d26e32380000000721efd0030000",
/// "epoch": "0x7080019000001",
/// "extension": null,
/// "number": "0x401",
/// "parent_hash": "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40",
/// "proposals": ["0xa0ef4eb5f4ceeb08a4c8"],
/// "transactions": [],
/// "uncles": [
/// {
/// "hash": "0xdca341a42890536551f99357612cef7148ed471e3b6419d0844a4e400be6ee94",
/// "header": {
/// "compact_target": "0x1e083126",
/// "dao": "0xb5a3e047474401001bc476b9ee573000c0c387962a38000000febffacf030000",
/// "epoch": "0x7080018000001",
/// "extra_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
/// "nonce": "0x0",
/// "number": "0x400",
/// "parent_hash": "0xae003585fa15309b30b31aed3dcf385e9472c3c3e93746a6c4540629a6a1ed2d",
/// "proposals_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
/// "timestamp": "0x5cd2b118",
/// "transactions_root": "0xc47d5b78b3c4c4c853e2a32810818940d0ee403423bea9ec7b8e566d9595206c",
/// "version":"0x0"
/// },
/// "proposals": [],
/// "required": false
/// }
/// ],
/// "uncles_count_limit": "0x2",
/// "version": "0x0",
/// "work_id": "0x0"
/// }
/// }
/// ```
#[rpc(name = "get_block_template")]
fn get_block_template(
&self,
bytes_limit: Option<Uint64>,
proposals_limit: Option<Uint64>,
max_version: Option<Version>,
) -> Result<BlockTemplate>;
/// Submit new block to the network.
///
/// ## Params
///
/// * `work_id` - The same work ID returned from [`get_block_template`](#tymethod.get_block_template).
/// * `block` - The assembled block from the block template and which PoW puzzle has been resolved.
///
/// ## Examples
///
/// Request
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "method": "submit_block",
/// "params": [
/// "example",
/// {
/// "header": {
/// "compact_target": "0x1e083126",
/// "dao": "0xb5a3e047474401001bc476b9ee573000c0c387962a38000000febffacf030000",
/// "epoch": "0x7080018000001",
/// "extra_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
/// "nonce": "0x0",
/// "number": "0x400",
/// "parent_hash": "0xae003585fa15309b30b31aed3dcf385e9472c3c3e93746a6c4540629a6a1ed2d",
/// "proposals_hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
/// "timestamp": "0x5cd2b117",
/// "transactions_root": "0xc47d5b78b3c4c4c853e2a32810818940d0ee403423bea9ec7b8e566d9595206c",
/// "version": "0x0"
/// },
/// "proposals": [],
/// "transactions": [
/// {
/// "cell_deps": [],
/// "header_deps": [],
/// "inputs": [
/// {
/// "previous_output": {
/// "index": "0xffffffff",
/// "tx_hash": "0x0000000000000000000000000000000000000000000000000000000000000000"
/// },
/// "since": "0x400"
/// }
/// ],
/// "outputs": [
/// {
/// "capacity": "0x18e64b61cf",
/// "lock": {
/// "code_hash": "0x28e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5",
/// "hash_type": "data",
/// "args": "0x"
/// },
/// "type": null
/// }
/// ],
/// "outputs_data": [
/// "0x"
/// ],
/// "version": "0x0",
/// "witnesses": [
/// "0x450000000c000000410000003500000010000000300000003100000028e83a1277d48add8e72fadaa9248559e1b632bab2bd60b27955ebc4c03800a5000000000000000000"
/// ]
/// }
/// ],
/// "uncles": []
/// }
/// ]
/// }
/// ```
///
/// Response
///
/// ```json
/// {
/// "id": 42,
/// "jsonrpc": "2.0",
/// "result": "0xa5f5c85987a15de25661e5a214f2c1449cd803f071acc7999820f25246471f40"
/// }
/// ```
#[rpc(name = "submit_block")]
fn submit_block(&self, work_id: String, block: Block) -> Result<H256>;
}
pub(crate) struct MinerRpcImpl {
pub network_controller: NetworkController,
pub shared: Shared,
pub chain: ChainController,
}
impl MinerRpc for MinerRpcImpl {
fn get_block_template(
&self,
bytes_limit: Option<Uint64>,
proposals_limit: Option<Uint64>,
max_version: Option<Version>,
) -> Result<BlockTemplate> {
let bytes_limit = match bytes_limit {
Some(b) => Some(b.into()),
None => None,
};
let proposals_limit = match proposals_limit {
Some(b) => Some(b.into()),
None => None,
};
self.shared
.get_block_template(bytes_limit, proposals_limit, max_version.map(Into::into))
.map_err(|err| {
error!("send get_block_template request error {}", err);
RPCError::ckb_internal_error(err)
})?
.map_err(|err| {
error!("get_block_template result error {}", err);
RPCError::from_any_error(err)
})
}
fn submit_block(&self, work_id: String, block: Block) -> Result<H256> {
let block: packed::Block = block.into();
let block: Arc<core::BlockView> = Arc::new(block.into_view());
let header = block.header();
debug!(
"start to submit block, work_id = {}, block = #{}({})",
work_id,
block.number(),
block.hash()
);
let snapshot: &Snapshot = &self.shared.snapshot();
let consensus = snapshot.consensus();
// Reject block extension for public chain: mainnet and testnet.
if block.extension().is_some() && consensus.is_public_chain() {
let err = "the block extension should be null";
return Err(RPCError::custom_with_error(RPCError::Invalid, err));
}
// Verify header
HeaderVerifier::new(snapshot, consensus)
.verify(&header)
.map_err(|err| handle_submit_error(&work_id, &err))?;
// Verify and insert block
let is_new = self
.chain
.process_block(Arc::clone(&block))
.map_err(|err| handle_submit_error(&work_id, &err))?;
info!(
"end to submit block, work_id = {}, is_new = {}, block = #{}({})",
work_id,
is_new,
block.number(),
block.hash()
);
// Announce only new block
if is_new {
debug!(
"[block_relay] announce new block {} {} {}",
header.number(),
header.hash(),
unix_time_as_millis()
);
let content = packed::CompactBlock::build_from_block(&block, &HashSet::new());
let message = packed::RelayMessage::new_builder().set(content).build();
let pid = if self.network_controller.load_ckb2021() {
SupportProtocols::RelayV2.protocol_id()
} else {
SupportProtocols::Relay.protocol_id()
};
if let Err(err) = self
.network_controller
.quick_broadcast(pid, message.as_bytes())
{
error!("Broadcast new block failed: {:?}", err);
}
}
Ok(header.hash().unpack())
}
}
fn handle_submit_error<E: std::fmt::Display + Debug>(work_id: &str, err: &E) -> Error {
error!("[{}] submit_block error: {:?}", work_id, err);
RPCError::custom_with_error(RPCError::Invalid, err)
}