Skip to content

Commit

Permalink
Merge pull request #2675 from Conflux-Chain/v2.0-stable-merge
Browse files Browse the repository at this point in the history
Merge for release v2.2.4.
  • Loading branch information
peilun-conflux authored May 5, 2023
2 parents 644caaf + 854043e commit 179a5bc
Show file tree
Hide file tree
Showing 33 changed files with 814 additions and 176 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "conflux"
version = "2.2.3"
version = "2.2.4"
edition = "2018"
build = "build.rs"

Expand Down
2 changes: 1 addition & 1 deletion client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "client"
version = "2.2.3"
version = "2.2.4"
edition = "2018"

[dependencies]
Expand Down
6 changes: 6 additions & 0 deletions client/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ build_config! {
(ln_max_parallel_epochs_to_request, (Option<usize>), None)
(ln_num_epochs_to_request, (Option<usize>), None)
(ln_num_waiting_headers_threshold, (Option<usize>), None)
(keep_snapshot_before_stable_checkpoint, (bool), true)
(force_recompute_height_during_construct_pivot, (Option<u64>), None)
}
{
// Development related section.
Expand Down Expand Up @@ -600,6 +602,7 @@ impl Configuration {
}
None => None,
},
force_recompute_height_during_construct_pivot: self.raw_conf.force_recompute_height_during_construct_pivot,
},
bench_mode: false,
transaction_epoch_bound: self.raw_conf.transaction_epoch_bound,
Expand Down Expand Up @@ -741,6 +744,9 @@ impl Configuration {
.raw_conf
.cip90_transition_height
.unwrap_or(self.raw_conf.hydra_transition_height.unwrap_or(0)),
keep_snapshot_before_stable_checkpoint: self
.raw_conf
.keep_snapshot_before_stable_checkpoint,
}
}

Expand Down
6 changes: 3 additions & 3 deletions client/src/rpc/impls/cfx_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::rpc::{
MAX_BLOCK_HISTORY_SIZE,
},
traits::cfx::CfxFilter,
types::{CfxFilterChanges, CfxFilterLog, CfxRpcLogFilter, Log},
types::{CfxFilterChanges, CfxFilterLog, CfxRpcLogFilter, Log, RevertTo},
};
use cfx_addr::Network;
use cfx_types::{Space, H128, H256};
Expand Down Expand Up @@ -514,9 +514,9 @@ impl<T: Filterable + Send + Sync + 'static> CfxFilter for T {
}

if reorg_len > 0 {
logs.push(CfxFilterLog::ChainReorg {
logs.push(CfxFilterLog::ChainReorg(RevertTo {
revert_to: epochs.first().unwrap().0.into(),
});
}));
}
let data_man =
self.consensus_graph().get_data_manager().clone();
Expand Down
139 changes: 97 additions & 42 deletions client/src/rpc/impls/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use crate::{
BlockNumber, LocalizedTrace as EthLocalizedTrace,
Res as EthRes, TraceFilter as EthTraceFilter,
},
Action as RpcAction, LocalizedTrace as RpcLocalizedTrace,
LocalizedTrace, TraceFilter as RpcTraceFilter,
Action as RpcAction, EpochNumber as RpcEpochNumber, EpochTrace,
LocalizedTrace as RpcLocalizedTrace, LocalizedTrace,
TraceFilter as RpcTraceFilter,
},
RpcResult,
},
Expand All @@ -23,7 +24,8 @@ use cfx_types::{Space, H256};
use cfxcore::{
block_data_manager::DataVersionTuple,
observer::trace_filter::TraceFilter as PrimitiveTraceFilter,
BlockDataManager, ConsensusGraph, SharedConsensusGraph,
BlockDataManager, ConsensusGraph, ConsensusGraphTrait,
SharedConsensusGraph,
};
use jsonrpc_core::{Error as JsonRpcError, Result as JsonRpcResult};
use primitives::EpochNumber;
Expand All @@ -38,6 +40,7 @@ macro_rules! unwrap_or_return {
};
}

#[derive(Clone)]
pub struct TraceHandler {
data_man: Arc<BlockDataManager>,
consensus: SharedConsensusGraph,
Expand Down Expand Up @@ -193,6 +196,49 @@ impl TraceHandler {
})
}))
}

fn epoch_trace_impl(
&self, epoch_number: EpochNumber,
) -> RpcResult<EpochTrace> {
// Make sure we use the same epoch_hash in two spaces. Using
// epoch_number cannot guarantee the atomicity.
let epoch_hash = self
.consensus
.get_hash_from_epoch_number(epoch_number.clone())?;

Ok(EpochTrace::new(
self.space_epoch_traces(Space::Native, epoch_hash)?,
to_eth_traces(
self.space_epoch_traces(Space::Ethereum, epoch_hash)?,
)?,
))
}

fn space_epoch_traces(
&self, space: Space, epoch_hash: H256,
) -> RpcResult<Vec<LocalizedTrace>> {
let consensus = self.consensus_graph();
let epoch = consensus
.get_block_epoch_number(&epoch_hash)
.ok_or(JsonRpcError::internal_error())?;
let mut trace_filter = PrimitiveTraceFilter::space_filter(space);
trace_filter.from_epoch = EpochNumber::Number(epoch);
trace_filter.to_epoch = EpochNumber::Number(epoch);
let block_traces = consensus.collect_traces_single_epoch(
&trace_filter,
epoch,
epoch_hash,
)?;
let traces = consensus
.filter_block_traces(&trace_filter, block_traces)?
.into_iter()
.map(|trace| {
RpcLocalizedTrace::from(trace, self.network)
.expect("Local address conversion should succeed")
})
.collect();
Ok(traces)
}
}

impl Trace for TraceHandler {
Expand All @@ -214,6 +260,10 @@ impl Trace for TraceHandler {
) -> JsonRpcResult<Option<Vec<LocalizedTrace>>> {
into_jsonrpc_result(self.transaction_trace_impl(&tx_hash))
}

fn epoch_traces(&self, epoch: RpcEpochNumber) -> JsonRpcResult<EpochTrace> {
into_jsonrpc_result(self.epoch_trace_impl(epoch.into_primitive()))
}
}

pub struct EthTraceHandler {
Expand Down Expand Up @@ -305,45 +355,7 @@ impl EthTrace for EthTraceHandler {
Some(traces) => traces,
};

let mut eth_traces: Vec<EthLocalizedTrace> = Vec::new();
let mut stack_index = Vec::new();
let mut sublen_stack = Vec::new();

for trace in traces {
match &trace.action {
RpcAction::Call(_) | RpcAction::Create(_) => {
if let Some(parent_subtraces) = sublen_stack.last_mut() {
*parent_subtraces += 1;
}

sublen_stack.push(0);
stack_index.push(eth_traces.len());

eth_traces.push(trace.try_into().map_err(|e| {
error!("eth trace conversion error: {:?}", e);
JsonRpcError::internal_error()
})?);
}
RpcAction::CallResult(_) | RpcAction::CreateResult(_) => {
let index = stack_index
.pop()
.ok_or(JsonRpcError::internal_error())?;

eth_traces[index].set_result(trace.action)?;

eth_traces[index].subtraces =
sublen_stack.pop().expect("stack_index matches");
}
RpcAction::InternalTransferAction(_) => {}
}
}

if !stack_index.is_empty() {
error!("eth::filter_traces: actions left unmatched");
bail!(JsonRpcError::internal_error());
}

Ok(Some(eth_traces))
Ok(Some(to_eth_traces(traces)?))
}

fn transaction_traces(
Expand Down Expand Up @@ -426,3 +438,46 @@ impl EthTrace for EthTraceHandler {
Ok(Some(eth_traces))
}
}

fn to_eth_traces(
traces: Vec<LocalizedTrace>,
) -> JsonRpcResult<Vec<EthLocalizedTrace>> {
let mut eth_traces: Vec<EthLocalizedTrace> = Vec::new();
let mut stack_index = Vec::new();
let mut sublen_stack = Vec::new();

for trace in traces {
match &trace.action {
RpcAction::Call(_) | RpcAction::Create(_) => {
if let Some(parent_subtraces) = sublen_stack.last_mut() {
*parent_subtraces += 1;
}

sublen_stack.push(0);
stack_index.push(eth_traces.len());

eth_traces.push(trace.try_into().map_err(|e| {
error!("eth trace conversion error: {:?}", e);
JsonRpcError::internal_error()
})?);
}
RpcAction::CallResult(_) | RpcAction::CreateResult(_) => {
let index =
stack_index.pop().ok_or(JsonRpcError::internal_error())?;

eth_traces[index].set_result(trace.action)?;

eth_traces[index].subtraces =
sublen_stack.pop().expect("stack_index matches");
}
RpcAction::InternalTransferAction(_) => {}
}
}

if !stack_index.is_empty() {
error!("eth::filter_traces: actions left unmatched");
bail!(JsonRpcError::internal_error());
}

Ok(eth_traces)
}
8 changes: 7 additions & 1 deletion client/src/rpc/traits/cfx_space/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// Conflux is free software and distributed under GNU General Public License.
// See http://www.gnu.org/licenses/

use crate::rpc::types::{LocalizedBlockTrace, LocalizedTrace, TraceFilter};
use crate::rpc::types::{
EpochNumber, EpochTrace, LocalizedBlockTrace, LocalizedTrace, TraceFilter,
};
use cfx_types::H256;
use jsonrpc_core::Result as JsonRpcResult;
use jsonrpc_derive::rpc;
Expand All @@ -27,4 +29,8 @@ pub trait Trace {
fn transaction_traces(
&self, tx_hash: H256,
) -> JsonRpcResult<Option<Vec<LocalizedTrace>>>;

/// Return all traces of both spaces in an epoch.
#[rpc(name = "trace_epoch")]
fn epoch_traces(&self, epoch: EpochNumber) -> JsonRpcResult<EpochTrace>;
}
5 changes: 3 additions & 2 deletions client/src/rpc/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use self::{
},
consensus_graph_states::ConsensusGraphStates,
epoch_number::{BlockHashOrEpochNumber, EpochNumber},
filter::{CfxFilterChanges, CfxFilterLog, CfxRpcLogFilter},
filter::{CfxFilterChanges, CfxFilterLog, CfxRpcLogFilter, RevertTo},
index::Index,
log::Log,
pos_economics::PoSEconomics,
Expand All @@ -55,7 +55,8 @@ pub use self::{
sync_graph_states::SyncGraphStates,
token_supply_info::TokenSupplyInfo,
trace::{
Action, LocalizedBlockTrace, LocalizedTrace, LocalizedTransactionTrace,
Action, EpochTrace, LocalizedBlockTrace, LocalizedTrace,
LocalizedTransactionTrace,
},
trace_filter::TraceFilter,
transaction::{PackedOrExecuted, Transaction, WrapTransaction},
Expand Down
48 changes: 46 additions & 2 deletions client/src/rpc/types/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,25 @@ impl CfxRpcLogFilter {
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RevertTo {
pub revert_to: U256,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CfxFilterLog {
Log(Log),
ChainReorg { revert_to: U256 },
ChainReorg(RevertTo),
}

impl Serialize for CfxFilterLog {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where S: Serializer {
match *self {
CfxFilterLog::Log(ref log) => log.serialize(s),
CfxFilterLog::ChainReorg(ref revert_to) => revert_to.serialize(s),
}
}
}

/// Results of the filter_changes RPC.
Expand All @@ -206,11 +222,13 @@ impl Serialize for CfxFilterChanges {

#[cfg(test)]
mod tests {
use crate::rpc::types::{filter::RevertTo, CfxFilterLog, Log};

use super::{
super::RpcAddress, CfxRpcLogFilter, EpochNumber, VariadicValue,
};
use cfx_addr::Network;
use cfx_types::{Space, H160, H256, U64};
use cfx_types::{Space, H160, H256, U256, U64};
use primitives::{
epoch::EpochNumber as PrimitiveEpochNumber,
filter::{LogFilter as PrimitiveFilter, LogFilterParams},
Expand Down Expand Up @@ -490,4 +508,30 @@ mod tests {
Ok(primitive_block_hash_filter)
);
}

#[test]
fn test_serialize_cfx_filter_log() {
let mut logs = vec![];
let log = Log {
address: RpcAddress::try_from_h160(H160::from_str("13990122638b9132ca29c723bdf037f1a891a70c").unwrap(), Network::Test).unwrap(),
topics: vec![
H256::from_str("a6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc").unwrap(),
H256::from_str("4861736852656700000000000000000000000000000000000000000000000000").unwrap(),
],
data: vec![].into(),
block_hash: Some(H256::from_str("ed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5").unwrap()),
epoch_number: Some(U256::from(0x4510c)),
transaction_hash: Some(H256::default()),
transaction_index: Some(U256::default()),
transaction_log_index: Some(1.into()),
log_index: Some(U256::from(1)),
};

logs.push(CfxFilterLog::Log(log));
logs.push(CfxFilterLog::ChainReorg(RevertTo {
revert_to: U256::from(1),
}));
let serialized = serde_json::to_string(&logs).unwrap();
assert_eq!(serialized, r#"[{"address":"CFXTEST:TYPE.USER:AAK3WAKCPSF3CP0MFHDWHTTUG924VERHBUV9NMM3YC","topics":["0xa6697e974e6a320f454390be03f74955e8978f1a6971ea6730542e37b66179bc","0x4861736852656700000000000000000000000000000000000000000000000000"],"data":"0x","blockHash":"0xed76641c68a1c641aee09a94b3b471f4dc0316efe5ac19cf488e2674cf8d05b5","epochNumber":"0x4510c","transactionHash":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionIndex":"0x0","logIndex":"0x1","transactionLogIndex":"0x1"},{"revertTo":"0x1"}]"#);
}
}
Loading

0 comments on commit 179a5bc

Please sign in to comment.