Skip to content
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

Expand chain api #538

Merged
merged 8 commits into from
Apr 19, 2023
Merged

Expand chain api #538

merged 8 commits into from
Apr 19, 2023

Conversation

Niederb
Copy link
Contributor

@Niederb Niederb commented Apr 17, 2023

  • In order to use get_finalized_head() inside of get_finalized_block() I had to add a trait bound for Runtime::Header but I think it is not too limiting.
  • There is already a genesis_hash() in API. I implemented get_genesis_block() instead.
  • Merged the GetBlock and GetHeader traits into a single GetChainInfo trait

@Niederb Niederb self-assigned this Apr 17, 2023
@Niederb Niederb linked an issue Apr 17, 2023 that may be closed by this pull request
@Niederb Niederb marked this pull request as ready for review April 17, 2023 08:29
@Niederb Niederb added F8-newfeature Introduces a new feature and removed F8-newfeature Introduces a new feature labels Apr 17, 2023

let header_hash = api.get_finalized_head().unwrap().unwrap();
println!("Latest Finalized Header Hash:\n {} \n", header_hash);

let h = api.get_header(Some(header_hash)).unwrap().unwrap();
println!("Finalized header:\n {:?} \n", h);

let b = api.get_signed_block(Some(header_hash)).unwrap().unwrap();
println!("Finalized signed block:\n {:?} \n", b);
let b = api.get_finalized_block().unwrap().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let b = api.get_finalized_block().unwrap().unwrap();
let signed_block = api.get_finalized_block().unwrap().unwrap();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

src/api/rpc_api/author.rs Show resolved Hide resolved
let b = api.get_finalized_block().unwrap().unwrap();
println!("Finalized block:\n {:?} \n", b);
let last_block_number = b.block.header.number;
let block_numbers = std::cmp::max(0, last_block_number - 3)..last_block_number;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this part is a little too complex for an example. It needs quite some thinking on what exactly is done here.

How about adding comments or use different variable names? Such as number_of_last_three_blocks = ...

Would it be possible to add the .collect to the first line? E.g.

number_of_last_three_blocks: Vec<BlockNumber> = (std::cmp::max(0, last_block_number - 3)..last_block_number).collect()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it a bit clearer and added some comments

@@ -76,6 +80,13 @@ pub trait GetBlock {
&self,
number: Option<Self::BlockNumber>,
) -> Result<Option<SignedBlock<Self::Block>>>;

fn get_finalized_block(&self) -> Result<Option<SignedBlock<Self::Block>>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some comments? E.g. /// Get the last finalized signed block.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -116,6 +132,25 @@ where
) -> Result<Option<SignedBlock<Self::Block>>> {
self.get_block_hash(number).map(|h| self.get_signed_block(h))?
}

fn get_finalized_block(&self) -> Result<Option<SignedBlock<Self::Block>>> {
self.get_finalized_head()?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this, two prior independent traits are now not independent anymore. Since GetHeader is so small anyway, it might be sensible to merge these two traits to a GetChain Trait.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think merging the traits makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged the two traits now into the new GetChainInfo trait

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks !

src/api/rpc_api/events.rs Show resolved Hide resolved
src/api/rpc_api/events.rs Show resolved Hide resolved
@Niederb Niederb requested a review from haerdib April 18, 2023 07:37
Copy link
Contributor

@haerdib haerdib left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

One last request: Could you change the label to E2 (breaks api) and include in the PR description that you merged the two chain traits?

@@ -116,6 +132,25 @@ where
) -> Result<Option<SignedBlock<Self::Block>>> {
self.get_block_hash(number).map(|h| self.get_signed_block(h))?
}

fn get_finalized_block(&self) -> Result<Option<SignedBlock<Self::Block>>> {
self.get_finalized_head()?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks !

Copy link
Contributor

@echevrier echevrier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Niederb Niederb merged commit 1b6cc11 into master Apr 19, 2023
@Niederb Niederb deleted the tn/expand-chain-api branch April 19, 2023 06:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce chain api helpers
3 participants