Skip to content

Commit

Permalink
Coordination window borderline blocks
Browse files Browse the repository at this point in the history
Here we add two methods to the `coordinationWindow` type:

The `activePhaseEndBlock` denotes the end block of the active communication
phase and will be used to complete communication in the right moment.

The `endBlock` denotes the end block of the whole window and will be used
to complete the coordination procedure.
  • Loading branch information
lukasz-zimnoch committed Nov 24, 2023
1 parent cba7d03 commit ef27aea
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
28 changes: 25 additions & 3 deletions pkg/tbtc/coordination.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,21 @@ const (
// coordinationFrequencyBlocks is the number of blocks between two
// consecutive coordination windows.
coordinationFrequencyBlocks = 900
// coordinationActivePhaseDurationBlocks is the number of blocks in the
// active phase of the coordination window. The active phase is the
// phase during which the communication between the coordination leader and
// their followers is allowed.
coordinationActivePhaseDurationBlocks = 80
// coordinationPassivePhaseDurationBlocks is the number of blocks in the
// passive phase of the coordination window. The passive phase is the
// phase during which communication is not allowed. Participants are
// expected to validate the result of the coordination and prepare for
// execution of the proposed wallet action.
coordinationPassivePhaseDurationBlocks = 20
// coordinationDurationBlocks is the number of blocks in a single
// coordination window.
coordinationDurationBlocks = 100
coordinationDurationBlocks = coordinationActivePhaseDurationBlocks +
coordinationPassivePhaseDurationBlocks
)

// errCoordinationExecutorBusy is an error returned when the coordination
Expand All @@ -25,9 +37,8 @@ var errCoordinationExecutorBusy = fmt.Errorf("coordination executor is busy")
// coordinationWindow represents a single coordination window. The coordination
// block is the first block of the window.
type coordinationWindow struct {
// coordinationBlock is the first block of the coordination window.
coordinationBlock uint64

// TODO: Add another coordination window fields.
}

// newCoordinationWindow creates a new coordination window for the given
Expand All @@ -38,6 +49,17 @@ func newCoordinationWindow(coordinationBlock uint64) *coordinationWindow {
}
}

// ActivePhaseEndBlock returns the block number at which the active phase
// of the coordination window ends.
func (cw *coordinationWindow) activePhaseEndBlock() uint64 {
return cw.coordinationBlock + coordinationActivePhaseDurationBlocks
}

// EndBlock returns the block number at which the coordination window ends.
func (cw *coordinationWindow) endBlock() uint64 {
return cw.coordinationBlock + coordinationDurationBlocks
}

// isAfter returns true if this coordination window is after the other
// window.
func (cw *coordinationWindow) isAfter(other *coordinationWindow) bool {
Expand Down
61 changes: 61 additions & 0 deletions pkg/tbtc/coordination_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package tbtc

import (
"github.com/keep-network/keep-core/internal/testutils"
"testing"
)

func TestCoordinationWindow_ActivePhaseEndBlock(t *testing.T) {
window := newCoordinationWindow(900)

testutils.AssertIntsEqual(
t,
"active phase end block",
980,
int(window.activePhaseEndBlock()),
)
}

func TestCoordinationWindow_EndBlock(t *testing.T) {
window := newCoordinationWindow(900)

testutils.AssertIntsEqual(
t,
"end block",
1000,
int(window.endBlock()),
)
}

func TestCoordinationWindow_IsAfterActivePhase(t *testing.T) {
window := newCoordinationWindow(1800)

previousWindow := newCoordinationWindow(900)
sameWindow := newCoordinationWindow(1800)
nextWindow := newCoordinationWindow(2700)

testutils.AssertBoolsEqual(
t,
"result for nil",
true,
window.isAfter(nil),
)
testutils.AssertBoolsEqual(
t,
"result for previous window",
true,
window.isAfter(previousWindow),
)
testutils.AssertBoolsEqual(
t,
"result for same window",
false,
window.isAfter(sameWindow),
)
testutils.AssertBoolsEqual(
t,
"result for next window",
false,
window.isAfter(nextWindow),
)
}

0 comments on commit ef27aea

Please sign in to comment.