From f434e5608b089343cfcbe6ff7fd9753b9cb60e60 Mon Sep 17 00:00:00 2001 From: Lukasz Zimnoch Date: Fri, 24 Nov 2023 11:08:16 +0100 Subject: [PATCH] Coordination window borderline blocks 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. --- pkg/tbtc/coordination.go | 28 ++++++++++++++++-- pkg/tbtc/coordination_test.go | 55 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 pkg/tbtc/coordination_test.go diff --git a/pkg/tbtc/coordination.go b/pkg/tbtc/coordination.go index 1203ce6187..d0233f3b22 100644 --- a/pkg/tbtc/coordination.go +++ b/pkg/tbtc/coordination.go @@ -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 @@ -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 @@ -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 { diff --git a/pkg/tbtc/coordination_test.go b/pkg/tbtc/coordination_test.go new file mode 100644 index 0000000000..e92bddf295 --- /dev/null +++ b/pkg/tbtc/coordination_test.go @@ -0,0 +1,55 @@ +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 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), + ) +} \ No newline at end of file