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

Fix BlockHeader hash #1868

Merged
merged 11 commits into from Apr 29, 2024
Merged

Fix BlockHeader hash #1868

merged 11 commits into from Apr 29, 2024

Conversation

MitchTurner
Copy link
Member

@MitchTurner MitchTurner commented Apr 26, 2024

  • Add event_inbox_root to hash calcuation
  • Use to_be_bytes instead of to_bytes to avoid unnecessary padding of u32 to u64.

Checklist

  • Breaking changes are clearly marked as such in the PR description and changelog
  • New behavior is reflected in tests
  • The specification matches the implemented behavior (link update PR if changes are needed)

Before requesting review

  • I have reviewed the code myself
  • I have created follow-up issues caused by this PR and linked them here

After merging, notify other teams

[Add or remove entries as needed]

@@ -441,6 +441,7 @@ impl ApplicationHeader<GeneratedApplicationFields> {
hasher.input(self.message_receipt_count.to_be_bytes());
hasher.input(self.transactions_root.as_ref());
hasher.input(self.message_outbox_root.as_ref());
hasher.input(self.event_inbox_root.as_ref());
Copy link
Member Author

Choose a reason for hiding this comment

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

Is there any logic to the order these things get added to the hash?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The order of fields in the header

@MitchTurner MitchTurner marked this pull request as ready for review April 26, 2024 21:47
@@ -436,13 +436,18 @@ impl ApplicationHeader<GeneratedApplicationFields> {
pub fn hash(&self) -> Bytes32 {
// Order matters and is the same as the spec.
let mut hasher = crate::fuel_crypto::Hasher::default();
hasher.input(self.da_height.to_bytes().as_slice());
hasher.input(self.da_height.to_be_bytes().as_slice());
hasher.input(self.transactions_count.to_be_bytes());
Copy link
Member Author

Choose a reason for hiding this comment

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

@xgreenx said on Slack:

The TxPointer.index_output is u16, so it means we can’t have more transactions than u16, but we use u64. The same for the messages receipts, we can’t have more than transactions_count * 255, so it can’t be more than u32=)
If we don’t need to pad them and it is a bug in the transactions_count and message_receipt_count, then we can remove it=)

But the docs still say:

| `txCount`                        | `uint64`   | Number of [transaction](../tx-format/transaction.md)s in this block.                                                                                                                                                                                |
| `message_receipt_count`          | `uint64`   | Number of [output message](../abi/receipts.md#messageout-receipt)s in this block.     

So I kept these to match the specs.

Copy link
Collaborator

Choose a reason for hiding this comment

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

xgreenx
xgreenx previously approved these changes Apr 29, 2024
CHANGELOG.md Outdated
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

- [#1868](https://github.com/FuelLabs/fuel-core/pull/1868): Include the `event_inbox_root` in header hash.
Copy link
Member Author

Choose a reason for hiding this comment

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

We also updated the types.

@@ -88,12 +91,16 @@ impl Block<Transaction> {
transactions: Vec<Transaction>,
outbox_message_ids: &[MessageId],
event_inbox_root: Bytes32,
) -> Self {
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think new should return Result here. Can we just pass it a valid BlockHeader instead of a PartialBlockHeader?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Passing the block header still requires verification that transactions match the header=)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah. That's right. Okay, I guess it's unavoidable.

I think new gives the wrong impression still. Maybe construct? I just think of new as being a dumb constructor generally, but it's not that important. I'm fine with this.

xgreenx
xgreenx previously approved these changes Apr 29, 2024
@MitchTurner MitchTurner enabled auto-merge (squash) April 29, 2024 18:08
@MitchTurner MitchTurner merged commit 5eca451 into master Apr 29, 2024
27 checks passed
@MitchTurner MitchTurner deleted the fix/correct-header-id-calculation branch April 29, 2024 18:41
@xgreenx xgreenx mentioned this pull request Apr 30, 2024
xgreenx added a commit that referenced this pull request Apr 30, 2024
## Version v0.26.0

### Fixed

#### Breaking

- [#1868](#1868): Include the
`event_inbox_root` in the header hash. Changed types of the
`transactions_count` to `u16` and `message_receipt_count` to `u32`
instead of `u64`. Updated the application hash root calculation to not
pad numbers.
- [#1866](#1866): Fixed a
runtime panic that occurred when restarting a node. The panic happens
when the relayer database is already populated, and the relayer attempts
an empty commit during start up. This invalid commit is removed in this
PR.
- [#1871](#1871): Fixed
`block` endpoint to return fetch the blocks from both databases after
regenesis.
- [#1856](#1856): Replaced
instances of `Union` with `Enum` for GraphQL definitions of
`ConsensusParametersVersion` and related types. This is needed because
`Union` does not support multiple `Version`s inside discriminants or
empty variants.
- [#1870](#1870): Fixed
benchmarks for the `0.25.3`.
- [#1870](#1870): Improves the
performance of getting the size of the contract from the
`InMemoryTransaction`.
- [#1851](#1851): Provided
migration capabilities (enabled addition of new column families) to
RocksDB instance.

### Added 

- [#1853](#1853): Added a test
case to verify the database's behavior when new columns are added to the
RocksDB database.
- [#1860](#1860): Regenesis
now preserves `FuelBlockIdsToHeights` off-chain table.

### Changed

- [#1847](#1847): Simplify the
validation interface to use `Block`. Remove `Validation` variant of
`ExecutionKind`.
- [#1832](#1832): Snapshot
generation can be cancelled. Progress is also reported.
- [#1837](#1837): Refactor the
executor and separate validation from the other use cases

## What's Changed
* Weekly `cargo update` by @github-actions in
#1850
* Refactor/separate validation from other executions by @MitchTurner in
#1837
* fix: Use `Enum` for `ConsensusParametersVersion` and related types by
@bvrooman in #1856
* feat: snapshot generation graceful shutdown by @segfault-magnet in
#1832
* regenesis: migrate FuelBlockIdsToHeights by @Dentosal in
#1860
* Weekly `cargo update` by @github-actions in
#1869
* Refactor/Simplify validation logic by @MitchTurner in
#1847
* Fixed `block` endpoint to return fetch the blocks from both databases
after regenesis by @xgreenx in
#1871
* Add Eq and Partial Eq to tx response and status by @MujkicA in
#1872
* test: restart with relayer data by @bvrooman in
#1866
* Fix `BlockHeader` hash by @MitchTurner in
#1868
* Added a test for the case of adding new columns into the existing
RocksDB database by @xgreenx in
#1853
* Fixed benchmarks for the `0.25.3` by @xgreenx in
#1870


**Full Changelog**:
v0.25.3...v0.26.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants