-
Notifications
You must be signed in to change notification settings - Fork 182
feat(rpc): ChainGetTipset v2 #6231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
hanabi1224
wants to merge
13
commits into
main
Choose a base branch
from
hm/ChainGetTipSetV2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+458
−49
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2417f12
feat(rpc): Filecoin.ChainGetTipSet for v2 endpoint
hanabi1224 990f180
Merge remote-tracking branch 'origin/main' into hm/ChainGetTipSetV2
hanabi1224 4f58610
Merge remote-tracking branch 'origin/main' into hm/ChainGetTipSetV2
hanabi1224 5307602
complete impl
hanabi1224 43ad5ef
parity tests
hanabi1224 51bd6c5
fix tests
hanabi1224 3f4c11f
Merge branch 'main' into hm/ChainGetTipSetV2
hanabi1224 10ccd6a
fix offline RPC parity test
hanabi1224 710d0f4
Merge remote-tracking branch 'origin/main' into hm/ChainGetTipSetV2
hanabi1224 6c84c1a
changelog
hanabi1224 cd31ce2
Merge branch 'main' into hm/ChainGetTipSetV2
hanabi1224 4a6cc02
Merge branch 'main' into hm/ChainGetTipSetV2
hanabi1224 b2a0375
Merge branch 'main' into hm/ChainGetTipSetV2
hanabi1224 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| // Copyright 2019-2025 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| mod types; | ||
| pub mod types; | ||
| use enumflags2::{BitFlags, make_bitflags}; | ||
| use types::*; | ||
|
|
||
|
|
@@ -54,6 +54,21 @@ use tokio_util::sync::CancellationToken; | |
|
|
||
| const HEAD_CHANNEL_CAPACITY: usize = 10; | ||
|
|
||
| // SafeHeightDistance is the distance from the latest tipset, i.e. heaviest, that | ||
| // is considered to be safe from re-orgs at an increasingly diminishing | ||
| // probability. | ||
| // | ||
| // This is used to determine the safe tipset when using the "safe" tag in | ||
| // TipSetSelector or via Eth JSON-RPC APIs. Note that "safe" doesn't guarantee | ||
| // finality, but rather a high probability of not being reverted. For guaranteed | ||
| // finality, use the "finalized" tag. | ||
| // | ||
| // This constant is experimental and may change in the future. | ||
| // Discussion on this current value and a tracking item to document the | ||
| // probabilistic impact of various values is in | ||
| // https://github.com/filecoin-project/go-f3/issues/944 | ||
| const SAFE_HEIGHT_DISTANCE: ChainEpoch = 200; | ||
|
|
||
| static CHAIN_EXPORT_LOCK: LazyLock<Mutex<Option<CancellationToken>>> = | ||
| LazyLock::new(|| Mutex::new(None)); | ||
|
|
||
|
|
@@ -978,28 +993,155 @@ impl RpcMethod<1> for ChainGetTipSet { | |
|
|
||
| async fn handle( | ||
| ctx: Ctx<impl Blockstore>, | ||
| (ApiTipsetKey(tipset_key),): Self::Params, | ||
| (ApiTipsetKey(tsk),): Self::Params, | ||
| ) -> Result<Self::Ok, ServerError> { | ||
| let ts = ctx | ||
| .chain_store() | ||
| .load_required_tipset_or_heaviest(&tipset_key)?; | ||
| Ok(ts) | ||
| if let Some(tsk) = &tsk { | ||
| let ts = ctx.chain_index().load_required_tipset(tsk)?; | ||
| Ok(ts) | ||
| } else { | ||
| // Error message here matches lotus | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to match the Lotus error? This would actually be a very confusing message for me. |
||
| Err(anyhow::anyhow!("NewTipSet called with zero length array of blocks").into()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub enum ChainGetTipSetV2 {} | ||
|
|
||
| impl ChainGetTipSetV2 { | ||
| pub async fn get_tipset_by_anchor( | ||
| ctx: &Ctx<impl Blockstore + Send + Sync + 'static>, | ||
| anchor: &Option<TipsetAnchor>, | ||
| ) -> anyhow::Result<Option<Arc<Tipset>>> { | ||
| if let Some(anchor) = anchor { | ||
| match (&anchor.key.0, &anchor.tag) { | ||
| // Anchor is zero-valued. Fall back to heaviest tipset. | ||
| (None, None) => Ok(Some(ctx.state_manager.heaviest_tipset())), | ||
| // Get tipset at the specified key. | ||
| (Some(tsk), None) => Ok(Some(ctx.chain_index().load_required_tipset(tsk)?)), | ||
| (None, Some(tag)) => Self::get_tipset_by_tag(ctx, *tag).await, | ||
| _ => { | ||
| anyhow::bail!("invalid anchor") | ||
| } | ||
| } | ||
| } else { | ||
| // No anchor specified. Fall back to finalized tipset. | ||
| Self::get_tipset_by_tag(ctx, TipsetTag::Finalized).await | ||
| } | ||
| } | ||
|
|
||
| pub async fn get_tipset_by_tag( | ||
| ctx: &Ctx<impl Blockstore + Send + Sync + 'static>, | ||
| tag: TipsetTag, | ||
| ) -> anyhow::Result<Option<Arc<Tipset>>> { | ||
| match tag { | ||
| TipsetTag::Latest => Ok(Some(ctx.state_manager.heaviest_tipset())), | ||
| TipsetTag::Finalized => Self::get_latest_finalized_tipset(ctx).await, | ||
| TipsetTag::Safe => Some(Self::get_latest_safe_tipset(ctx).await).transpose(), | ||
| } | ||
| } | ||
|
|
||
| pub async fn get_latest_safe_tipset( | ||
| ctx: &Ctx<impl Blockstore + Send + Sync + 'static>, | ||
| ) -> anyhow::Result<Arc<Tipset>> { | ||
| let finalized = Self::get_latest_finalized_tipset(ctx).await?; | ||
| let head = ctx.chain_store().heaviest_tipset(); | ||
| let safe_height = (head.epoch() - SAFE_HEIGHT_DISTANCE).max(0); | ||
| if let Some(finalized) = finalized | ||
| && finalized.epoch() >= safe_height | ||
| { | ||
| Ok(finalized) | ||
| } else { | ||
| Ok(ctx.chain_index().tipset_by_height( | ||
| safe_height, | ||
| head, | ||
| ResolveNullTipset::TakeOlder, | ||
| )?) | ||
| } | ||
| } | ||
|
|
||
| pub async fn get_latest_finalized_tipset( | ||
| ctx: &Ctx<impl Blockstore + Send + Sync + 'static>, | ||
| ) -> anyhow::Result<Option<Arc<Tipset>>> { | ||
| let Ok(f3_finalized_cert) = | ||
| crate::rpc::f3::F3GetLatestCertificate::handle(ctx.clone(), ()).await | ||
| else { | ||
| return Self::get_ec_finalized_tipset(ctx); | ||
| }; | ||
|
|
||
| let f3_finalized_head = f3_finalized_cert.chain_head(); | ||
| let head = ctx.chain_store().heaviest_tipset(); | ||
| // Latest F3 finalized tipset is older than EC finality, falling back to EC finality | ||
| if head.epoch() > f3_finalized_head.epoch + ctx.chain_config().policy.chain_finality { | ||
| return Self::get_ec_finalized_tipset(ctx); | ||
| } | ||
|
|
||
| let ts = ctx | ||
| .chain_index() | ||
| .load_required_tipset(&f3_finalized_head.key) | ||
| .map_err(|e| { | ||
| anyhow::anyhow!( | ||
| "Failed to load F3 finalized tipset at epoch {} with key {}: {e}", | ||
| f3_finalized_head.epoch, | ||
| f3_finalized_head.key, | ||
| ) | ||
| })?; | ||
| Ok(Some(ts)) | ||
| } | ||
|
|
||
| pub fn get_ec_finalized_tipset( | ||
| ctx: &Ctx<impl Blockstore>, | ||
| ) -> anyhow::Result<Option<Arc<Tipset>>> { | ||
| let head = ctx.chain_store().heaviest_tipset(); | ||
| let ec_finality_epoch = head.epoch() - ctx.chain_config().policy.chain_finality; | ||
| if ec_finality_epoch >= 0 { | ||
| let ts = ctx.chain_index().tipset_by_height( | ||
| ec_finality_epoch, | ||
| head, | ||
| ResolveNullTipset::TakeOlder, | ||
| )?; | ||
| Ok(Some(ts)) | ||
| } else { | ||
| Ok(None) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl RpcMethod<1> for ChainGetTipSetV2 { | ||
| const NAME: &'static str = "Filecoin.ChainGetTipSet"; | ||
| const PARAM_NAMES: [&'static str; 1] = ["tipsetSelector"]; | ||
| const API_PATHS: BitFlags<ApiPaths> = make_bitflags!(ApiPaths::{ V2 }); | ||
| const PERMISSION: Permission = Permission::Read; | ||
| const DESCRIPTION: Option<&'static str> = Some("Returns the tipset with the specified CID."); | ||
|
|
||
| type Params = (ApiTipsetKey,); | ||
| type Ok = Arc<Tipset>; | ||
| type Params = (TipsetSelector,); | ||
| type Ok = Option<Arc<Tipset>>; | ||
|
|
||
| async fn handle(_: Ctx<impl Blockstore>, _: Self::Params) -> Result<Self::Ok, ServerError> { | ||
| Err(ServerError::unsupported_method()) | ||
| async fn handle( | ||
| ctx: Ctx<impl Blockstore + Send + Sync + 'static>, | ||
| (selector,): Self::Params, | ||
| ) -> Result<Self::Ok, ServerError> { | ||
| selector.validate()?; | ||
| // Get tipset by key. | ||
| if let ApiTipsetKey(Some(tsk)) = &selector.key { | ||
| let ts = ctx.chain_index().load_required_tipset(tsk)?; | ||
| return Ok(Some(ts)); | ||
| } | ||
| // Get tipset by height. | ||
| if let Some(height) = &selector.height { | ||
| let anchor = Self::get_tipset_by_anchor(&ctx, &height.anchor).await?; | ||
| let ts = ctx.chain_index().tipset_by_height( | ||
| height.at, | ||
| anchor.unwrap_or_else(|| ctx.chain_store().heaviest_tipset()), | ||
| height.resolve_null_tipset_policy(), | ||
| )?; | ||
| return Ok(Some(ts)); | ||
| } | ||
| // Get tipset by tag, either latest or finalized. | ||
| if let Some(tag) = &selector.tag { | ||
| let ts = Self::get_tipset_by_tag(&ctx, *tag).await?; | ||
| return Ok(ts); | ||
| } | ||
| Err(anyhow::anyhow!("no tipset found for selector").into()) | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, let's not just copy over Lotus docs.