Skip to content

Commit 764446c

Browse files
authored
Merge pull request #3242 from Pana/op/decoupleRpc
Decouple core space rpc type (Block) with cfxcore Types
2 parents 0ea96a3 + 68eeff4 commit 764446c

File tree

7 files changed

+298
-278
lines changed

7 files changed

+298
-278
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ pow-types = { path = "./crates/pos/types/pow-types" }
244244
diem-types = { path = "./crates/pos/types/types" }
245245
diem-network-address-encryption = { path = "./crates/pos/config/management/network-address-encryption" }
246246
cfx-tasks = { path = "./crates/tasks" }
247-
eest_types = { path = "./crates/eest_types" }
247+
# eest_types = { path = "./crates/eest_types" }
248248
cfx-config = { path = "./crates/config" }
249249

250250
# basics
@@ -335,7 +335,6 @@ blst = "0.3"
335335
hashbrown = "0.7.1"
336336

337337
clap = "4"
338-
clap-verbosity-flag = "3"
339338

340339
# rand & rng
341340
rand = "0.9"
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
use crate::rpc::types::{
2+
cfx::{
3+
block::{Block, BlockTransactions, Header},
4+
transaction::PackedOrExecuted,
5+
RpcAddress,
6+
},
7+
Receipt, Transaction,
8+
};
9+
use cfx_addr::Network;
10+
use cfx_types::{Space, H256, U256, U64};
11+
use cfxcore::{
12+
block_data_manager::{BlockDataManager, DataVersionTuple},
13+
consensus::ConsensusGraphInner,
14+
pow, ConsensusGraph, SharedConsensusGraph,
15+
};
16+
use primitives::{
17+
Block as PrimitiveBlock, BlockHeader as PrimitiveBlockHeader,
18+
TransactionIndex, TransactionStatus,
19+
};
20+
use std::sync::Arc;
21+
22+
pub fn build_block(
23+
b: &PrimitiveBlock, network: Network, consensus: &ConsensusGraph,
24+
consensus_inner: &ConsensusGraphInner, data_man: &Arc<BlockDataManager>,
25+
include_txs: bool, tx_space_filter: Option<Space>,
26+
) -> Result<Block, String> {
27+
let block_hash = b.block_header.hash();
28+
29+
let epoch_number = consensus_inner
30+
.get_block_epoch_number(&block_hash)
31+
.or_else(|| data_man.block_epoch_number(&block_hash))
32+
.map(Into::into);
33+
34+
let block_number = consensus.get_block_number(&block_hash)?.map(Into::into);
35+
36+
// get the block.gas_used
37+
let tx_len = b.transactions.len();
38+
39+
let (gas_used, transactions) = {
40+
let maybe_results = consensus_inner.block_execution_results_by_hash(
41+
&b.hash(),
42+
false, /* update_cache */
43+
);
44+
45+
// calculate block gasUsed according block.execution_result and
46+
// tx_space_filter
47+
let gas_used_sum = match maybe_results {
48+
Some(DataVersionTuple(_, ref execution_result)) => {
49+
match tx_space_filter {
50+
Some(space_filter) => {
51+
let mut total_gas_used = U256::zero();
52+
let mut prev_acc_gas_used = U256::zero();
53+
for (idx, tx) in b.transactions.iter().enumerate() {
54+
let ref receipt =
55+
execution_result.block_receipts.receipts[idx];
56+
if tx.space() == space_filter {
57+
total_gas_used += receipt.accumulated_gas_used
58+
- prev_acc_gas_used;
59+
}
60+
prev_acc_gas_used = receipt.accumulated_gas_used;
61+
}
62+
Some(total_gas_used)
63+
}
64+
None => Some(
65+
execution_result.block_receipts.receipts[tx_len - 1]
66+
.accumulated_gas_used,
67+
),
68+
}
69+
}
70+
None => None,
71+
};
72+
73+
// prepare the transaction array according include_txs,
74+
// execution_result, tx_space_filter
75+
let transactions = match include_txs {
76+
false => BlockTransactions::Hashes(
77+
b.transaction_hashes(Some(Space::Native)),
78+
),
79+
true => {
80+
let tx_vec = match maybe_results {
81+
Some(DataVersionTuple(_, ref execution_result)) => {
82+
let maybe_state_root =
83+
data_man.get_executed_state_root(&b.hash());
84+
85+
b.transactions
86+
.iter()
87+
.enumerate()
88+
.filter(|(_idx, tx)| {
89+
tx_space_filter.is_none()
90+
|| tx.space() == tx_space_filter.unwrap()
91+
})
92+
.enumerate()
93+
.map(|(new_index, (original_index, tx))| {
94+
let receipt = execution_result
95+
.block_receipts
96+
.receipts
97+
.get(original_index)
98+
.unwrap();
99+
let prior_gas_used = if original_index == 0 {
100+
U256::zero()
101+
} else {
102+
execution_result.block_receipts.receipts
103+
[original_index - 1]
104+
.accumulated_gas_used
105+
};
106+
match receipt.outcome_status {
107+
TransactionStatus::Success
108+
| TransactionStatus::Failure => {
109+
let tx_index = TransactionIndex {
110+
block_hash: b.hash(),
111+
real_index: original_index,
112+
is_phantom: false,
113+
rpc_index: Some(new_index),
114+
};
115+
let tx_exec_error_msg =
116+
&execution_result
117+
.block_receipts
118+
.tx_execution_error_messages
119+
[original_index];
120+
Transaction::from_signed(
121+
tx,
122+
Some(PackedOrExecuted::Executed(
123+
Receipt::new(
124+
(**tx).clone(),
125+
receipt.clone(),
126+
tx_index,
127+
prior_gas_used,
128+
epoch_number,
129+
execution_result
130+
.block_receipts
131+
.block_number,
132+
b.block_header.base_price(),
133+
maybe_state_root,
134+
if tx_exec_error_msg
135+
.is_empty()
136+
{
137+
None
138+
} else {
139+
Some(
140+
tx_exec_error_msg
141+
.clone(),
142+
)
143+
},
144+
network,
145+
false,
146+
false,
147+
)?,
148+
)),
149+
network,
150+
)
151+
}
152+
TransactionStatus::Skipped => {
153+
Transaction::from_signed(
154+
tx, None, network,
155+
)
156+
}
157+
}
158+
})
159+
.collect::<Result<_, _>>()?
160+
}
161+
None => b
162+
.transactions
163+
.iter()
164+
.filter(|tx| {
165+
tx_space_filter.is_none()
166+
|| tx.space() == tx_space_filter.unwrap()
167+
})
168+
.map(|x| Transaction::from_signed(x, None, network))
169+
.collect::<Result<_, _>>()?,
170+
};
171+
BlockTransactions::Full(tx_vec)
172+
}
173+
};
174+
175+
(gas_used_sum, transactions)
176+
};
177+
178+
let base_fee_per_gas: Option<U256> =
179+
b.block_header.base_price().map(|x| x[Space::Native]).into();
180+
181+
// if a block is 1559 block(has base_fee_per_gas) then it's
182+
// block.gas_limit is 90% of the actual block.gas_limit
183+
let gas_limit: U256 = b.block_header.core_space_gas_limit();
184+
185+
Ok(Block {
186+
hash: H256::from(block_hash),
187+
parent_hash: H256::from(b.block_header.parent_hash().clone()),
188+
height: b.block_header.height().into(),
189+
miner: RpcAddress::try_from_h160(*b.block_header.author(), network)?,
190+
deferred_state_root: H256::from(
191+
b.block_header.deferred_state_root().clone(),
192+
),
193+
deferred_receipts_root: H256::from(
194+
b.block_header.deferred_receipts_root().clone(),
195+
),
196+
deferred_logs_bloom_hash: H256::from(
197+
b.block_header.deferred_logs_bloom_hash().clone(),
198+
),
199+
blame: U64::from(b.block_header.blame()),
200+
transactions_root: H256::from(
201+
b.block_header.transactions_root().clone(),
202+
),
203+
// PrimitiveBlock does not contain this information
204+
epoch_number: epoch_number.map(|e| U256::from(e)),
205+
block_number,
206+
// fee system
207+
gas_used,
208+
gas_limit,
209+
base_fee_per_gas,
210+
timestamp: b.block_header.timestamp().into(),
211+
difficulty: b.block_header.difficulty().clone().into(),
212+
pow_quality: b
213+
.block_header
214+
.pow_hash
215+
.map(|h| pow::pow_hash_to_quality(&h, &b.block_header.nonce())),
216+
adaptive: b.block_header.adaptive(),
217+
referee_hashes: b
218+
.block_header
219+
.referee_hashes()
220+
.iter()
221+
.map(|x| H256::from(*x))
222+
.collect(),
223+
nonce: b.block_header.nonce().into(),
224+
transactions,
225+
custom: b
226+
.block_header
227+
.custom()
228+
.clone()
229+
.into_iter()
230+
.map(Into::into)
231+
.collect(),
232+
size: Some(b.size().into()),
233+
pos_reference: b.block_header.pos_reference().clone(),
234+
})
235+
}
236+
237+
pub fn build_header(
238+
h: &PrimitiveBlockHeader, network: Network, consensus: SharedConsensusGraph,
239+
) -> Result<Header, String> {
240+
let hash = h.hash();
241+
242+
let epoch_number = consensus
243+
.get_block_epoch_number(&hash)
244+
.or_else(|| consensus.data_manager().block_epoch_number(&hash))
245+
.map(Into::into);
246+
247+
let block_number = consensus.get_block_number(&hash)?.map(Into::into);
248+
249+
let base_fee_per_gas: Option<U256> =
250+
h.base_price().map(|x| x[Space::Native]).into();
251+
252+
let referee_hashes =
253+
h.referee_hashes().iter().map(|x| H256::from(*x)).collect();
254+
255+
Ok(Header {
256+
hash: H256::from(hash),
257+
parent_hash: H256::from(*h.parent_hash()),
258+
height: h.height().into(),
259+
miner: RpcAddress::try_from_h160(*h.author(), network)?,
260+
deferred_state_root: H256::from(*h.deferred_state_root()),
261+
deferred_receipts_root: H256::from(*h.deferred_receipts_root()),
262+
deferred_logs_bloom_hash: H256::from(*h.deferred_logs_bloom_hash()),
263+
blame: U64::from(h.blame()),
264+
transactions_root: H256::from(*h.transactions_root()),
265+
epoch_number,
266+
block_number,
267+
gas_limit: h.gas_limit().into(),
268+
base_fee_per_gas,
269+
timestamp: h.timestamp().into(),
270+
difficulty: h.difficulty().into(),
271+
adaptive: h.adaptive(),
272+
referee_hashes,
273+
nonce: h.nonce().into(),
274+
pow_quality: h
275+
.pow_hash
276+
.map(|pow_hash| pow::pow_hash_to_quality(&pow_hash, &h.nonce())),
277+
pos_reference: *h.pos_reference(),
278+
custom: h.custom().clone().into_iter().map(Into::into).collect(),
279+
})
280+
}

crates/client/src/rpc/helpers/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Conflux is free software and distributed under GNU General Public License.
33
// See http://www.gnu.org/licenses/
44

5+
mod block_provider;
56
mod subscribers;
67

8+
pub use block_provider::{build_block, build_header};
79
pub use cfx_rpc::helpers::{
810
poll_filter::{
911
limit_logs, PollFilter, SyncPollFilter, MAX_BLOCK_HISTORY_SIZE,

crates/client/src/rpc/impls/cfx/common.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use std::{
1212

1313
use crate::rpc::{
1414
errors::invalid_params_check,
15-
helpers::MAX_FEE_HISTORY_CACHE_BLOCK_COUNT,
15+
helpers::{build_block, MAX_FEE_HISTORY_CACHE_BLOCK_COUNT},
1616
impls::pos::{convert_to_pos_epoch_reward, hash_value_to_h256},
1717
types::{
1818
cfx::check_rpc_address_network, pos::PoSEpochReward,
@@ -249,7 +249,7 @@ impl RpcImpl {
249249
.block_by_hash(&pivot_hash, false /* update_cache */);
250250
match maybe_block {
251251
None => Ok(None),
252-
Some(b) => Ok(Some(RpcBlock::new(
252+
Some(b) => Ok(Some(build_block(
253253
&*b,
254254
*self.network.get_network_type(),
255255
consensus_graph,
@@ -376,7 +376,7 @@ impl RpcImpl {
376376

377377
match maybe_block {
378378
None => Ok(None),
379-
Some(b) => Ok(Some(RpcBlock::new(
379+
Some(b) => Ok(Some(build_block(
380380
&*b,
381381
*self.network.get_network_type(),
382382
consensus_graph,
@@ -429,7 +429,7 @@ impl RpcImpl {
429429
.ok_or_else(|| RpcError::invalid_params("Block not found"))?;
430430

431431
debug!("Build RpcBlock {}", block.hash());
432-
Ok(RpcBlock::new(
432+
Ok(build_block(
433433
&*block,
434434
*self.network.get_network_type(),
435435
consensus_graph,
@@ -467,7 +467,7 @@ impl RpcImpl {
467467

468468
match maybe_block {
469469
None => Ok(None),
470-
Some(b) => Ok(Some(RpcBlock::new(
470+
Some(b) => Ok(Some(build_block(
471471
&*b,
472472
*self.network.get_network_type(),
473473
consensus_graph,
@@ -684,7 +684,7 @@ impl RpcImpl {
684684
.block_by_hash(hash, false /* update_cache */)
685685
.expect("Error to get block by hash");
686686

687-
RpcBlock::new(
687+
build_block(
688688
&*block,
689689
*self.network.get_network_type(),
690690
consensus_graph,

crates/client/src/rpc/impls/cfx/light.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use crate::{
3333
common::delegate_convert,
3434
rpc::{
3535
errors::{self, invalid_params_check},
36-
helpers::MAX_FEE_HISTORY_CACHE_BLOCK_COUNT,
36+
helpers::{build_block, MAX_FEE_HISTORY_CACHE_BLOCK_COUNT},
3737
impls::common::{self, RpcImpl as CommonImpl},
3838
traits::{cfx::Cfx, debug::LocalRpc, test::TestRpc},
3939
types::{
@@ -796,7 +796,7 @@ impl RpcImpl {
796796

797797
let inner = consensus_graph.inner.read();
798798

799-
Ok(Some(RpcBlock::new(
799+
Ok(Some(build_block(
800800
&block,
801801
*light.get_network_type(),
802802
&*consensus_graph,
@@ -844,7 +844,7 @@ impl RpcImpl {
844844

845845
let inner = consensus_graph.inner.read();
846846

847-
Ok(RpcBlock::new(
847+
Ok(build_block(
848848
&block,
849849
*light.get_network_type(),
850850
&*consensus_graph,
@@ -892,7 +892,7 @@ impl RpcImpl {
892892

893893
let inner = consensus_graph.inner.read();
894894

895-
Ok(Some(RpcBlock::new(
895+
Ok(Some(build_block(
896896
&block,
897897
*light.get_network_type(),
898898
&*consensus_graph,

0 commit comments

Comments
 (0)