Skip to content

Commit f94db17

Browse files
committed
fix(RPC): backport F3 fanality resolution to eth V1 methods
1 parent deb4bd8 commit f94db17

7 files changed

Lines changed: 404 additions & 669 deletions

File tree

src/eth/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,3 @@ pub const LEGACY_V_VALUE_27: u64 = 27;
3232
pub const LEGACY_V_VALUE_28: u64 = 28;
3333

3434
pub const ETH_LEGACY_HOMESTEAD_TX_CHAIN_ID: u64 = 0;
35-
36-
/// From Lotus:
37-
/// > Research into Filecoin chain behavior suggests that probabilistic finality
38-
/// > generally approaches the intended stability guarantee at, or near, 30 epochs.
39-
/// > Although a strictly "finalized" safe recommendation remains 900 epochs.
40-
/// > See <https://github.com/filecoin-project/FIPs/blob/98e33b9fa306959aa0131519eb4cc155522b2081/FRCs/frc-0089.md>
41-
pub const SAFE_EPOCH_DELAY: i64 = 30;

src/rpc/methods/chain.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const HEAD_CHANNEL_CAPACITY: usize = 10;
6868
/// Discussion on this current value and a tracking item to document the
6969
/// probabilistic impact of various values is in
7070
/// https://github.com/filecoin-project/go-f3/issues/944
71-
const SAFE_HEIGHT_DISTANCE: ChainEpoch = 200;
71+
pub const SAFE_HEIGHT_DISTANCE: ChainEpoch = 200;
7272

7373
static CHAIN_EXPORT_LOCK: LazyLock<Mutex<Option<CancellationToken>>> =
7474
LazyLock::new(|| Mutex::new(None));
@@ -988,6 +988,7 @@ impl RpcMethod<1> for ChainGetBlock {
988988
}
989989

990990
pub enum ChainGetTipSet {}
991+
991992
impl RpcMethod<1> for ChainGetTipSet {
992993
const NAME: &'static str = "Filecoin.ChainGetTipSet";
993994
const PARAM_NAMES: [&'static str; 1] = ["tipsetKey"];
@@ -1045,7 +1046,7 @@ impl ChainGetTipSetV2 {
10451046
) -> anyhow::Result<Option<Tipset>> {
10461047
match tag {
10471048
TipsetTag::Latest => Ok(Some(ctx.state_manager.heaviest_tipset())),
1048-
TipsetTag::Finalized => Self::get_latest_finalized_tipset(ctx).await,
1049+
TipsetTag::Finalized => Some(Self::get_latest_finalized_tipset(ctx).await).transpose(),
10491050
TipsetTag::Safe => Some(Self::get_latest_safe_tipset(ctx).await).transpose(),
10501051
}
10511052
}
@@ -1056,9 +1057,7 @@ impl ChainGetTipSetV2 {
10561057
let finalized = Self::get_latest_finalized_tipset(ctx).await?;
10571058
let head = ctx.chain_store().heaviest_tipset();
10581059
let safe_height = (head.epoch() - SAFE_HEIGHT_DISTANCE).max(0);
1059-
if let Some(finalized) = finalized
1060-
&& finalized.epoch() >= safe_height
1061-
{
1060+
if finalized.epoch() >= safe_height {
10621061
Ok(finalized)
10631062
} else {
10641063
Ok(ctx.chain_index().tipset_by_height(
@@ -1069,9 +1068,17 @@ impl ChainGetTipSetV2 {
10691068
}
10701069
}
10711070

1071+
pub fn get_ec_safe_tipset(ctx: &Ctx<impl Blockstore>) -> anyhow::Result<Tipset> {
1072+
let head = ctx.chain_store().heaviest_tipset();
1073+
let safe_height = (head.epoch() - SAFE_HEIGHT_DISTANCE).max(0);
1074+
Ok(ctx
1075+
.chain_index()
1076+
.tipset_by_height(safe_height, head, ResolveNullTipset::TakeOlder)?)
1077+
}
1078+
10721079
pub async fn get_latest_finalized_tipset(
10731080
ctx: &Ctx<impl Blockstore + Send + Sync + 'static>,
1074-
) -> anyhow::Result<Option<Tipset>> {
1081+
) -> anyhow::Result<Tipset> {
10751082
let Ok(f3_finalized_cert) =
10761083
crate::rpc::f3::F3GetLatestCertificate::handle(ctx.clone(), ()).await
10771084
else {
@@ -1095,22 +1102,17 @@ impl ChainGetTipSetV2 {
10951102
f3_finalized_head.key,
10961103
)
10971104
})?;
1098-
Ok(Some(ts))
1105+
Ok(ts)
10991106
}
11001107

1101-
pub fn get_ec_finalized_tipset(ctx: &Ctx<impl Blockstore>) -> anyhow::Result<Option<Tipset>> {
1108+
pub fn get_ec_finalized_tipset(ctx: &Ctx<impl Blockstore>) -> anyhow::Result<Tipset> {
11021109
let head = ctx.chain_store().heaviest_tipset();
1103-
let ec_finality_epoch = head.epoch() - ctx.chain_config().policy.chain_finality;
1104-
if ec_finality_epoch >= 0 {
1105-
let ts = ctx.chain_index().tipset_by_height(
1106-
ec_finality_epoch,
1107-
head,
1108-
ResolveNullTipset::TakeOlder,
1109-
)?;
1110-
Ok(Some(ts))
1111-
} else {
1112-
Ok(None)
1113-
}
1110+
let ec_finality_epoch = (head.epoch() - ctx.chain_config().policy.chain_finality).max(0);
1111+
Ok(ctx.chain_index().tipset_by_height(
1112+
ec_finality_epoch,
1113+
head,
1114+
ResolveNullTipset::TakeOlder,
1115+
)?)
11141116
}
11151117

11161118
pub async fn get_tipset(

0 commit comments

Comments
 (0)