From 5086d091095408c0077d499cca6671a5980e9538 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 5 Mar 2024 20:03:37 +0300 Subject: [PATCH] dbft: replace setters with extended constructor for dbft.ChangeView A part of #84. Signed-off-by: Anna Shaleva --- change_view.go | 9 --------- config.go | 4 ++-- internal/payload/change_view.go | 14 -------------- internal/payload/constructors.go | 7 +++++-- internal/payload/message_test.go | 5 +---- send.go | 5 +---- 6 files changed, 9 insertions(+), 35 deletions(-) diff --git a/change_view.go b/change_view.go index 93067f7c..ec0ac7a9 100644 --- a/change_view.go +++ b/change_view.go @@ -5,18 +5,9 @@ type ChangeView interface { // NewViewNumber returns proposed view number. NewViewNumber() byte - // SetNewViewNumber sets the proposed view number. - SetNewViewNumber(view byte) - // Timestamp returns message's timestamp. Timestamp() uint64 - // SetTimestamp sets message's timestamp. - SetTimestamp(ts uint64) - // Reason returns change view reason. Reason() ChangeViewReason - - // SetReason sets change view reason. - SetReason(reason ChangeViewReason) } diff --git a/config.go b/config.go index d2e4a01d..6239868f 100644 --- a/config.go +++ b/config.go @@ -66,7 +66,7 @@ type Config[H Hash, A Address] struct { // NewPrepareResponse is a constructor for payload.PrepareResponse. NewPrepareResponse func() PrepareResponse[H] // NewChangeView is a constructor for payload.ChangeView. - NewChangeView func() ChangeView + NewChangeView func(newViewNumber byte, reason ChangeViewReason, timestamp uint64) ChangeView // NewCommit is a constructor for payload.Commit. NewCommit func(signature []byte) Commit // NewRecoveryRequest is a constructor for payload.RecoveryRequest. @@ -320,7 +320,7 @@ func WithNewPrepareResponse[H Hash, A Address](f func() PrepareResponse[H]) func } // WithNewChangeView sets NewChangeView. -func WithNewChangeView[H Hash, A Address](f func() ChangeView) func(config *Config[H, A]) { +func WithNewChangeView[H Hash, A Address](f func(byte, ChangeViewReason, uint64) ChangeView) func(config *Config[H, A]) { return func(cfg *Config[H, A]) { cfg.NewChangeView = f } diff --git a/internal/payload/change_view.go b/internal/payload/change_view.go index 1e5d0693..de3d03a9 100644 --- a/internal/payload/change_view.go +++ b/internal/payload/change_view.go @@ -41,26 +41,12 @@ func (c changeView) NewViewNumber() byte { return c.newViewNumber } -// SetNewViewNumber implements ChangeView interface. -func (c *changeView) SetNewViewNumber(view byte) { - c.newViewNumber = view -} - // Timestamp implements ChangeView interface. func (c changeView) Timestamp() uint64 { return secToNanoSec(c.timestamp) } -// SetTimestamp implements ChangeView interface. -func (c *changeView) SetTimestamp(ts uint64) { - c.timestamp = nanoSecToSec(ts) -} - // Reason implements ChangeView interface. func (c changeView) Reason() dbft.ChangeViewReason { return dbft.CVUnknown } - -// SetReason implements ChangeView interface. -func (c *changeView) SetReason(_ dbft.ChangeViewReason) { -} diff --git a/internal/payload/constructors.go b/internal/payload/constructors.go index 5517ee77..a6db5aee 100644 --- a/internal/payload/constructors.go +++ b/internal/payload/constructors.go @@ -29,8 +29,11 @@ func NewPrepareResponse() dbft.PrepareResponse[crypto.Uint256] { } // NewChangeView returns minimal ChangeView implementation. -func NewChangeView() dbft.ChangeView { - return new(changeView) +func NewChangeView(newViewNumber byte, _ dbft.ChangeViewReason, ts uint64) dbft.ChangeView { + return &changeView{ + newViewNumber: newViewNumber, + timestamp: nanoSecToSec(ts), + } } // NewCommit returns minimal Commit implementation. diff --git a/internal/payload/message_test.go b/internal/payload/message_test.go index d9de377e..f0a51bad 100644 --- a/internal/payload/message_test.go +++ b/internal/payload/message_test.go @@ -154,12 +154,9 @@ func TestCompact_EncodeDecode(t *testing.T) { func TestPayload_Setters(t *testing.T) { t.Run("ChangeView", func(t *testing.T) { - cv := NewChangeView() + cv := NewChangeView(4, 0, secToNanoSec(1234)) - cv.SetTimestamp(secToNanoSec(1234)) assert.EqualValues(t, secToNanoSec(1234), cv.Timestamp()) - - cv.SetNewViewNumber(4) assert.EqualValues(t, 4, cv.NewViewNumber()) }) diff --git a/send.go b/send.go index 384968eb..02431ced 100644 --- a/send.go +++ b/send.go @@ -42,10 +42,7 @@ func (d *DBFT[H, A]) sendPrepareRequest() { } func (c *Context[H, A]) makeChangeView(ts uint64, reason ChangeViewReason) ConsensusPayload[H, A] { - cv := c.Config.NewChangeView() - cv.SetNewViewNumber(c.ViewNumber + 1) - cv.SetTimestamp(ts) - cv.SetReason(reason) + cv := c.Config.NewChangeView(c.ViewNumber+1, reason, ts) msg := c.Config.NewConsensusPayload(c, ChangeViewType, cv) c.ChangeViewPayloads[c.MyIndex] = msg