Skip to content

Commit

Permalink
dbft: rename InitializeConsensus to Reset
Browse files Browse the repository at this point in the history
InitializeConsensus was once an internal function, for external use only view
zero is relevant, so API should be somewhat more strict. Some shorter name is
nice to have as well.

Signed-off-by: Roman Khimov <[email protected]>
  • Loading branch information
roman-khimov committed Feb 13, 2024
1 parent c486807 commit 902e6b5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ A client of the library must implement its own event loop.
The library provides 5 callbacks that change the state of the consensus
process:
- `Start()` which initializes internal dBFT structures
- `InitializeConsensus()` which resets the consensus process
- `Reset()` which reinitializes the consensus process
- `OnTransaction()` which must be called everytime new transaction appears
- `OnReceive()` which must be called everytime new payload is received
- `OnTimer()` which must be called everytime timer fires
Expand All @@ -61,7 +61,7 @@ callback which is called at the start of every epoch. In the simple case where v
it can return the same value everytime it is called.
3. `ProcessBlock` is a callback which is called synchronously every time new block is accepted.
It can or can not persist block; it also may keep the blockchain state unchanged. dBFT will NOT
be initialized at the next height by itself to collect the next block until the `InitializeConsensus`
be initialized at the next height by itself to collect the next block until `Reset`
is called. In other words, it's the caller's responsibility to initialize dBFT at the next height even
after block collection at the current height. It's also the caller's responsibility to update the
blockchain state before the next height initialization so that other callbacks including
Expand Down
4 changes: 2 additions & 2 deletions check.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (d *DBFT[H, A]) checkCommit() {
d.ProcessBlock(d.block)

// Do not initialize consensus process immediately. It's the caller's duty to
// start the new block acceptance process and call InitializeConsensus at the
// start the new block acceptance process and call Reset at the
// new height.
}

Expand Down Expand Up @@ -102,5 +102,5 @@ func (d *DBFT[H, A]) checkChangeView(view byte) {
}
}

d.InitializeConsensus(view, d.lastBlockTimestamp)
d.initializeConsensus(view, d.lastBlockTimestamp)
}
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Context[H crypto.Hash, A crypto.Address] struct {
// blockProcessed denotes whether Config.ProcessBlock callback was called for the current
// height. If so, then no second call must happen. After new block is received by the user,
// dBFT stops any new transaction or messages processing as far as timeouts handling till
// the next call to InitializeConsensus.
// the next call to Reset.
blockProcessed bool

// BlockIndex is current block index.
Expand Down
17 changes: 11 additions & 6 deletions dbft.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,20 @@ func (d *DBFT[H, A]) addTransaction(tx block.Transaction[H]) {
// per DBFT lifetime.
func (d *DBFT[H, A]) Start(ts uint64) {
d.cache = newCache[H, A]()
d.InitializeConsensus(0, ts)
d.initializeConsensus(0, ts)
d.start()
}

// InitializeConsensus reinitializes dBFT instance and the given view with the
// given timestamp of the previous block. It's used if the current consensus
// state is outdated (which can happen when a new block is received by node
// before completing its assembly via dBFT). View 0 is expected to be used.
func (d *DBFT[H, A]) InitializeConsensus(view byte, ts uint64) {
// Reset reinitializes dBFT instance with the given timestamp of the previous
// block. It's used if the current consensus state is outdated which happens
// after new block is processed by ledger (the block can come from dBFT or be
// received by other means). The height is to be derived from the configured
// CurrentHeight callback and view will be set to 0.
func (d *DBFT[H, A]) Reset(ts uint64) {
d.initializeConsensus(0, ts)

Check warning on line 87 in dbft.go

View check run for this annotation

Codecov / codecov/patch

dbft.go#L86-L87

Added lines #L86 - L87 were not covered by tests
}

func (d *DBFT[H, A]) initializeConsensus(view byte, ts uint64) {
d.reset(view, ts)

var role string
Expand Down

0 comments on commit 902e6b5

Please sign in to comment.