-
Notifications
You must be signed in to change notification settings - Fork 532
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
PostBlock optimization #1972
Merged
Merged
PostBlock optimization #1972
Conversation
This file contains 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
goran-ethernal
force-pushed
the
post-block-optimization
branch
5 times, most recently
from
October 10, 2023 13:42
4b3c0dd
to
855900a
Compare
11 tasks
goran-ethernal
force-pushed
the
post-block-optimization
branch
from
October 13, 2023 12:06
ee6f73e
to
81e231f
Compare
goran-ethernal
force-pushed
the
post-block-optimization
branch
from
October 13, 2023 12:09
81e231f
to
30a860c
Compare
dusan-maksimovic
approved these changes
Oct 16, 2023
igorcrevar
approved these changes
Oct 16, 2023
rachit77
approved these changes
Oct 16, 2023
Stefan-Ethernal
approved these changes
Oct 16, 2023
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Description
This PR optimizes
PostBlock
andOnBlockInserted
handlers onconsensus_runtime
and its components (state_sync_manager
,checkpoint_manager
,stake_manager
andproposer_calculator
).Before, each of the given components was iterating through the receipts of given finalized block, in
OnBlockInserted
function, to get the required events (for example,checkpoint_manager
was gettingExit
events,stake_manager
was gettingTransfer
events, etc). To optimize this behavior, and remove iterating through the same receipts multiple times, this PR introduces these concepts:EventProvider
EventProvider
is a component which retrieves receipts of a range of blocks or a single block to get desired events from them.It requires components to
Subscribe
to it, using theSubscribe(subscriber EventSubscriber)
function.Each of components that subscribe to the
EventProvider
need to implement theEventSubscriber
interface:GetLogFilters
function returns the map, where key is the address of a contract on which we want to get some type of events, and value is the signature or ID of the event on that contract that the subscriber needs. This map is used to filter out events from receipts so that it can return correct results to the given subscriber.AddLog
function is the handler function on the subscriber. When theEventProvider
encounters an event that matches one of the filters in theGetLogFilters
function for that subscriber, it will call theAddLog
function on the subscriber, so that he can handle the given event (store it in memory, to a db, etc).This way, we provide only one iteration through the blocks and receipts to get the desired events of different components, plus, if
AddLog
returns an error on any component,EventProvider
immediately stops going through receipts, meaning that state of one component is tied to the state of other components, keeping the state more atomic, where either all components get updated, or no component does. This helps the consensus to have a more cleaner state. If one component is more advanced than the other, it may cause serious issues to running a more correct consensus.Global DB Transaction in
OnBlockInserted
In
state.go
we added afunc (s *State) beginTx(isWriteTx bool)
function so that it can open a manually maintained transaction. This is needed for keeping the correct state of each component as mentioned in the previous section. This means thatOnBlockInserted
function has more control over its components. If either of them fail to save their events, or to doPostBlock
functions, globaldb
transaction is rolledback, and state of all components for that block is cleaned and thrown away, keeping the consensus more accurate, and error prone.At the beginning of the
OnBlockInserted
function inconesnsus_runtime
, a globaldb
transaction is opened, and only after all components insert their events (gotten fromEventProvider
) and doPostBlock
succesfuly, only then we will commit the transaction and save the components state to db. If any of them fail in these actions, transaction is rolled back.Exceptions to previously mentioned concepts
PostBlock
function oncheckpoint_manager
is done after committing the globaldb
transaction, since it does not change any state inboltDB
, it only sends a checkpoint in a separate thread.validator_snapshot.go
-GetSnapshot
function will open adb
transaction if it is not passed to it. The reason behind this is that,GetSnapshot
function is called from multiple go routines:OnBlockInserted
, starting a new sequence inpolybft
, validating blocks infsm
, etc., and what can happen is that one routine can take its lock, but in the mean time,OnBlockInserted
opens a globaldb
transaction, which can result in a deadlock, since inOnBlockInserted
proposer calculator, updates the proposer snapshot, by calling thisGetSnapshot
function. So the first routine gets the lock onvalidatorSnapshot
, but it can not do anything with thedb
sinceOnBlockInserted
holds the globaldb
transaction. AndOnBlockInserted
can not update the proposer snapshot, because other routine holds thevalidatorSnapshot
lock.This is mitigated by opening
db
transaction onvalidatorSnapshot
if it is not already passed.Changes include
Checklist
Testing