Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Introduce notion of finality to substrate#760

Merged
gavofyork merged 16 commits intomasterfrom
rh-finality
Sep 21, 2018
Merged

Introduce notion of finality to substrate#760
gavofyork merged 16 commits intomasterfrom
rh-finality

Conversation

@rphmeier
Copy link
Copy Markdown
Contributor

@rphmeier rphmeier commented Sep 17, 2018

Partially addresses #746

This rewrites the blockchain DB to handle non-instant finality and chain reorganizations.
Pruning and CHT generation is done after finality now.

@rphmeier rphmeier added the A3-in_progress Pull request is in progress. No review needed at this stage. label Sep 17, 2018
@rphmeier rphmeier added this to the 1.0 beta milestone Sep 17, 2018
@rphmeier
Copy link
Copy Markdown
Contributor Author

I am not quite sure how to handle the finality case where we finalize a block that the "best" chain doesn't contain. One solution I had was to prevent indexing by number for non-finalized blocks, but that would probably hamstring the UX a little too much.

@arkpar
Copy link
Copy Markdown
Member

arkpar commented Sep 18, 2018

I've noticed this changes DB keys for block data to use hashes instead of numbers. Using numbers is quite beneficial for DB performance. Blocks tend to be requested in groups by number when syncing and it helps a lot when they are located close in the DB. Geth did that optimization a while a go and the improvement was significant. I'd prefer to keep the current design. Additional blocks sharing the same number could be suffixed with an extra index and the suffix would be removed on finalization.

@rphmeier
Copy link
Copy Markdown
Contributor Author

rphmeier commented Sep 18, 2018

I'd rather have correctness than optimization for the moment, so let's re-optimize the DB once this is merged and the new consensus is integrated. Could you make a proposal for a new database schema that is efficient and handles reorgs/non-instant finality and file it?

@rphmeier rphmeier added A0-please_review Pull request needs code review. and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Sep 18, 2018
@gavofyork gavofyork removed this from the 1.0 beta milestone Sep 19, 2018
Copy link
Copy Markdown
Contributor

@svyatonik svyatonik left a comment

Choose a reason for hiding this comment

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

Looked mostly at light + db-related changes (haven't touched state-db changes at all) to resolve #765 later - seems good. The only important thing I haven't understood - why not to fail when trying to revert finalized block (left a comment) - could you please elaborate?

Comment thread core/client/db/src/lib.rs Outdated
// TODO: ensure this doesn't conflict with old finalized block.
let meta = self.blockchain.meta.read();
let f_num = f_header.number().clone();
let number_u64: u64 = f_num.as_().into();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like .into() here is redundant.

Comment thread core/client/db/src/lib.rs Outdated
}

let mut canonicalize_state = |canonical_hash| {
trace!(target: "db", "Canonicalize block #{} ({:?})", number_u64 - self.pruning_window, canonical_hash);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't the number_u64 - self.pruning_window expression is constant? I mean this will print the same number, but different hashes

Comment thread core/client/db/src/lib.rs
for retracted in tree_route.retracted() {
if retracted.hash == meta.finalized_hash {
// TODO: can we recover here?
warn!("Safety failure: reverting finalized block {:?}",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why don't we return an error here? I thought that retracting finalized blocks should be impossible.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

in general yes, but after having one experience with authorities signing a bad block I thought this would leave things easier to recover from in the future.

Comment thread core/client/db/src/utils.rs Outdated
/// ```
///
/// ```ignore
/// Tree route from C to E. Retracted empty. Common is C, enacted [E1, E2]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

"Tree route from C to E" -> "Tree route from C to E2"?

@arkpar
Copy link
Copy Markdown
Member

arkpar commented Sep 20, 2018

I am not quite sure how to handle the finality case where we finalize a block that the "best" chain doesn't contain. One solution I had was to prevent indexing by number for non-finalized blocks, but that would probably hamstring the UX a little too much.

The way I see it only the best chain can be finalized. If there's a need to finalize some other chain the best chain should be reorganized to that chain first. I.e. it should become the new best

@gavofyork
Copy link
Copy Markdown
Member

"best" here just means longest?

@arkpar
Copy link
Copy Markdown
Member

arkpar commented Sep 20, 2018

"best" here just means longest?

Currently yes, although the blockchain backend does not enforce this. The "is_new_best" flag is provided by the client.

@gavofyork
Copy link
Copy Markdown
Member

If the finalised leaf is on the non-best-chain, then we'll need to reorg first.

@gavofyork gavofyork added A3-in_progress Pull request is in progress. No review needed at this stage. and removed A0-please_review Pull request needs code review. labels Sep 21, 2018
@rphmeier
Copy link
Copy Markdown
Contributor Author

rphmeier commented Sep 21, 2018

Doing this correctly depends on #740 -- the logic for handling finality on a block which is not an ancestor of the current best block requires finding the best_chain_containing(newly_finalized). Noted this in #746

@rphmeier rphmeier added A8-looksgood and removed A3-in_progress Pull request is in progress. No review needed at this stage. labels Sep 21, 2018
@arkpar
Copy link
Copy Markdown
Member

arkpar commented Sep 21, 2018

tree_route could use some tests. Looks good otherwise.

@rphmeier
Copy link
Copy Markdown
Contributor Author

I'll add tree-route tests in the follow-up PR

@gavofyork gavofyork merged commit 3cb5d88 into master Sep 21, 2018
@gavofyork gavofyork deleted the rh-finality branch September 21, 2018 13:56
liuchengxu added a commit to chainx-org/substrate that referenced this pull request Aug 23, 2021
* Update referral_or_council_of accordingly

* Nit

* Fix rls warnings in tokens/src/lib.rs

* Fix tokens/src/mock.rs

* Keep original asset_power and add internal_asset_power

* Fix staking/src/mock.rs

* Build wasm

* Nits

* Finish cross_chain_assets threshold test

* Build wasm
helin6 pushed a commit to boolnetwork/substrate that referenced this pull request Jul 25, 2023
…paritytech#760)

* begin porting over traits; remove Config use of Hash

* port over the Header bits that we need

* sp_core_hashing where possible, move Verify to PairSigner, remove unused errors

* tidy up Config things and move related bits into one place

* fix codegen

* copy Era over

* move AccountId, Address, Signer to Signer trait and a pass over fixing examples

* impl MultiAddress, MultiSignature, AccountId32 and add back to Config (for decoding later)

* Copy over StorageKey, StorageData, StorageChangeSet

* subxt core compiling with no sp_core or sp_runtime

* Get examples compiling

* pass over fixing tests

* cargo fmt

* clippy tweaks and update polkadot.rs

* fix codegen docs

* port over special DigestItem encoding/decoding

* clippy and doc fixes

* cargo fmt and example fix

* more cargo fmt-ing...

* substrate-extra to substrate-compat

* cargo.toml comments

* simplify PairSigner trait bounds

* move RPC types to a separate file

* fix docs

* Add some tests for things and other PR feedback

* bump to latest sp deps

* avoid needing substrate-compat feature in a test
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants