Skip to content

Commit

Permalink
dbft: replace setters with extended constructor for dbft.ChangeView
Browse files Browse the repository at this point in the history
A part of #84.

Signed-off-by: Anna Shaleva <[email protected]>
  • Loading branch information
AnnaShaleva committed Mar 5, 2024
1 parent 164d0d1 commit 5086d09
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 35 deletions.
9 changes: 0 additions & 9 deletions change_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 0 additions & 14 deletions internal/payload/change_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
}
7 changes: 5 additions & 2 deletions internal/payload/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 1 addition & 4 deletions internal/payload/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})

Expand Down
5 changes: 1 addition & 4 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5086d09

Please sign in to comment.