Skip to content

Commit 7e7196b

Browse files
authored
feat(rpc): integrate ec finality calulator into Eth TipsetResolver (#6897)
1 parent e839f42 commit 7e7196b

6 files changed

Lines changed: 27 additions & 41 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
### Changed
3939

40+
- [#6897](https://github.com/ChainSafe/forest/pull/6897): Integrated EC finality into Eth RPC methods.
41+
4042
- [#6821](https://github.com/ChainSafe/forest/pull/6821): Added message receipt size and event size to `forest-tool archive info` output.
4143

4244
- [#6830](https://github.com/ChainSafe/forest/pull/6830): Make base fee FIP-0115 activation configurable via `FOREST_FEES_FIP0115HEIGHT` environment variable. The FIP will NOT be automatically activated on the next network upgrade with this change, for now.

src/rpc/methods/chain.rs

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,26 +1070,9 @@ impl ChainGetTipSetV2 {
10701070
pub async fn get_latest_finalized_tipset(
10711071
ctx: &Ctx<impl Blockstore + Send + Sync + 'static>,
10721072
) -> anyhow::Result<Tipset> {
1073-
let Some(f3_finalized_head) = ctx.chain_store().f3_finalized_tipset() else {
1074-
return Self::get_ec_finalized_tipset(ctx);
1075-
};
1076-
let head = ctx.chain_store().heaviest_tipset();
1077-
// Latest F3 finalized tipset is older than EC finality, falling back to EC finality
1078-
if head.epoch() > f3_finalized_head.epoch() + ctx.chain_config().policy.chain_finality {
1079-
Self::get_ec_finalized_tipset(ctx)
1080-
} else {
1081-
Ok(f3_finalized_head)
1082-
}
1083-
}
1084-
1085-
pub fn get_ec_finalized_tipset(ctx: &Ctx<impl Blockstore>) -> anyhow::Result<Tipset> {
1086-
let head = ctx.chain_store().heaviest_tipset();
1087-
let ec_finality_epoch = (head.epoch() - ctx.chain_config().policy.chain_finality).max(0);
1088-
Ok(ctx.chain_index().tipset_by_height(
1089-
ec_finality_epoch,
1090-
head,
1091-
ResolveNullTipset::TakeOlder,
1092-
)?)
1073+
ChainGetTipSetFinalityStatus::get_finality_status(ctx)
1074+
.finalized_tip_set
1075+
.context("failed to resolve finalized tipset")
10931076
}
10941077

10951078
pub async fn get_tipset(
@@ -1146,7 +1129,7 @@ impl ChainGetTipSetFinalityStatus {
11461129
pub fn get_finality_status(ctx: &Ctx<impl Blockstore>) -> ChainFinalityStatus {
11471130
let head = ctx.chain_store().heaviest_tipset();
11481131
let (ec_finality_threshold_depth, ec_finalized_tip_set) =
1149-
Self::get_ec_finality_threshold_depth_and_tipset_with_cache(ctx, head.clone());
1132+
Self::get_ec_finality_threshold_depth_and_tipset_with_cache(ctx, head.shallow_clone());
11501133
let f3_finalized_tip_set = ctx.chain_store().f3_finalized_tipset();
11511134
let finalized_tip_set = match (&ec_finalized_tip_set, &f3_finalized_tip_set) {
11521135
(Some(ec), Some(f3)) => {
@@ -1169,7 +1152,7 @@ impl ChainGetTipSetFinalityStatus {
11691152
}
11701153
}
11711154

1172-
fn get_ec_finality_threshold_depth_and_tipset_with_cache(
1155+
pub fn get_ec_finality_threshold_depth_and_tipset_with_cache(
11731156
ctx: &Ctx<impl Blockstore>,
11741157
head: Tipset,
11751158
) -> (i64, Option<Tipset>) {
@@ -1208,7 +1191,7 @@ impl ChainGetTipSetFinalityStatus {
12081191
let finality = ctx.chain_config().policy.chain_finality;
12091192
let chain_len = finality as usize + FINALITY_CHAIN_EXTRA_EPOCHS;
12101193
let mut chain = Vec::with_capacity(chain_len);
1211-
let mut ts = head.clone();
1194+
let mut ts = head.shallow_clone();
12121195
while chain.len() < chain_len {
12131196
chain.push(ts.len() as i64);
12141197
if let Ok(parent) = ctx.chain_index().load_required_tipset(ts.parents()) {
@@ -1245,12 +1228,16 @@ impl ChainGetTipSetFinalityStatus {
12451228
let finalized = if depth >= 0
12461229
&& let Ok(ts) = ctx.chain_index().tipset_by_height(
12471230
(head.epoch() - depth).max(0),
1248-
head,
1231+
head.shallow_clone(),
12491232
ResolveNullTipset::TakeOlder,
12501233
) {
1251-
Some(ts.shallow_clone())
1234+
Some(ts)
12521235
} else {
1253-
None
1236+
let ec_finality_epoch =
1237+
(head.epoch() - ctx.chain_config().policy.chain_finality).max(0);
1238+
ctx.chain_index()
1239+
.tipset_by_height(ec_finality_epoch, head, ResolveNullTipset::TakeOlder)
1240+
.ok()
12541241
};
12551242
(depth, finalized)
12561243
}

src/rpc/methods/eth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod eth_tx;
66
pub mod filter;
77
pub mod pubsub;
88
pub(crate) mod pubsub_trait;
9-
mod tipset_resolver;
9+
pub mod tipset_resolver;
1010
pub(crate) mod trace;
1111
pub mod types;
1212
mod utils;

src/rpc/methods/eth/tipset_resolver.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

44
use super::*;
5-
use crate::rpc::chain::SAFE_HEIGHT_DISTANCE;
5+
use crate::rpc::chain::{ChainGetTipSetFinalityStatus, SAFE_HEIGHT_DISTANCE};
66

77
pub struct TipsetResolver<'a, DB>
88
where
@@ -167,17 +167,14 @@ where
167167
)?)
168168
}
169169

170-
/// Returns the tipset considered finalized by expected-consensus finality.
171-
///
172-
/// The finalized epoch is computed as head.epoch() minus the chain's `policy.chain_finality`, clamped to zero. The tipset at that epoch is returned; when the exact height is unavailable, an older tipset is selected.
170+
/// Returns the tipset considered finalized by the expected-consensus finality calculator(`FRC-0089`).
173171
pub fn get_ec_finalized_tipset(&self) -> anyhow::Result<Tipset> {
174172
let head = self.ctx.chain_store().heaviest_tipset();
175-
let ec_finality_epoch =
176-
(head.epoch() - self.ctx.chain_config().policy.chain_finality).max(0);
177-
Ok(self.ctx.chain_index().tipset_by_height(
178-
ec_finality_epoch,
179-
head,
180-
ResolveNullTipset::TakeOlder,
181-
)?)
173+
let (_, ec_finalized_tipset) =
174+
ChainGetTipSetFinalityStatus::get_ec_finality_threshold_depth_and_tipset_with_cache(
175+
self.ctx,
176+
head.clone(),
177+
);
178+
ec_finalized_tipset.context("failed to resolve EC finalized tipset")
182179
}
183180
}

src/tool/subcommands/api_cmd/api_compare_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ fn chain_tests(offline: bool) -> Vec<RpcTest> {
490490
} else {
491491
RpcTest::identity(ChainGetTipSetFinalityStatus::request(()).unwrap())
492492
},
493+
RpcTest::basic(ChainGetFinalizedTipset::request(()).unwrap()),
493494
]
494495
}
495496

@@ -567,7 +568,6 @@ fn chain_tests_with_tipset<DB: Blockstore>(
567568
.clone()
568569
.into(),))?),
569570
RpcTest::identity(ChainTipSetWeight::request((tipset.key().into(),))?),
570-
RpcTest::basic(ChainGetFinalizedTipset::request(())?),
571571
];
572572

573573
if !offline {

src/tool/subcommands/api_cmd/test_snapshots.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ filecoin_chaingetevents_1764864316078100.rpcsnap.json.zst
55
filecoin_chaingetevents_1765289237680041.rpcsnap.json.zst
66
filecoin_chaingetevents_1765289237680294.rpcsnap.json.zst
77
filecoin_chaingetevents_1765289237681455.rpcsnap.json.zst
8-
filecoin_chaingetfinalizedtipset_1759828121342574.rpcsnap.json.zst
8+
filecoin_chaingetfinalizedtipset_1776082510830037.rpcsnap.json.zst
99
filecoin_chaingetgenesis_1736937286915866.rpcsnap.json.zst
1010
filecoin_chaingetmessage_1758734340836824.rpcsnap.json.zst
1111
filecoin_chaingetmessagesintipset_1758734696116136.rpcsnap.json.zst
@@ -108,7 +108,7 @@ filecoin_ethgettransactionbyhash_1741272955520821.rpcsnap.json.zst
108108
filecoin_ethgettransactionbyhashlimited_1741272955509708.rpcsnap.json.zst
109109
filecoin_ethgettransactioncount_1740132538183426.rpcsnap.json.zst
110110
filecoin_ethgettransactioncount_v1_unknown_addr_1770288294132251.rpcsnap.json.zst
111-
filecoin_ethgettransactioncount_v2_1767847407595348.rpcsnap.json.zst
111+
filecoin_ethgettransactioncount_v2_1776080971678636.rpcsnap.json.zst
112112
filecoin_ethgettransactioncount_v2_unknown_addr_1770288294132366.rpcsnap.json.zst
113113
filecoin_ethgettransactionhashbycid_1737446676698540.rpcsnap.json.zst
114114
filecoin_ethgettransactionreceipt_1741272955712904.rpcsnap.json.zst

0 commit comments

Comments
 (0)