Skip to content

Commit

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

Signed-off-by: Anna Shaleva <shaleva.ann@nspcc.ru>
  • Loading branch information
AnnaShaleva committed Mar 5, 2024
1 parent fca4ff6 commit a2fdfa4
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 17 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Config[H Hash, A Address] struct {
// NewCommit is a constructor for payload.Commit.
NewCommit func(signature []byte) Commit
// NewRecoveryRequest is a constructor for payload.RecoveryRequest.
NewRecoveryRequest func() RecoveryRequest
NewRecoveryRequest func(ts uint64) RecoveryRequest
// NewRecoveryMessage is a constructor for payload.RecoveryMessage.
NewRecoveryMessage func() RecoveryMessage[H, A]
// VerifyPrepareRequest can perform external payload verification and returns true iff it was successful.
Expand Down Expand Up @@ -334,7 +334,7 @@ func WithNewCommit[H Hash, A Address](f func([]byte) Commit) func(config *Config
}

// WithNewRecoveryRequest sets NewRecoveryRequest.
func WithNewRecoveryRequest[H Hash, A Address](f func() RecoveryRequest) func(config *Config[H, A]) {
func WithNewRecoveryRequest[H Hash, A Address](f func(ts uint64) RecoveryRequest) func(config *Config[H, A]) {
return func(cfg *Config[H, A]) {
cfg.NewRecoveryRequest = f
}
Expand Down
4 changes: 2 additions & 2 deletions dbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func TestDBFT_Invalid(t *testing.T) {
require.Nil(t, dbft.New(opts...))
})

opts = append(opts, dbft.WithNewRecoveryRequest[crypto.Uint256, crypto.Uint160](func() dbft.RecoveryRequest {
opts = append(opts, dbft.WithNewRecoveryRequest[crypto.Uint256, crypto.Uint160](func(uint64) dbft.RecoveryRequest {
return nil
}))
t.Run("without NewRecoveryMessage", func(t *testing.T) {
Expand Down Expand Up @@ -738,7 +738,7 @@ func (s testState) getChangeView(from uint16, view byte) Payload {
}

func (s testState) getRecoveryRequest(from uint16) Payload {
p := payload.NewConsensusPayload(dbft.RecoveryRequestType, s.currHeight+1, from, 0, payload.NewRecoveryRequest())
p := payload.NewConsensusPayload(dbft.RecoveryRequestType, s.currHeight+1, from, 0, payload.NewRecoveryRequest(0))
return p
}

Expand Down
6 changes: 4 additions & 2 deletions internal/payload/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ func NewCommit(signature []byte) dbft.Commit {
}

// NewRecoveryRequest returns minimal RecoveryRequest implementation.
func NewRecoveryRequest() dbft.RecoveryRequest {
return new(recoveryRequest)
func NewRecoveryRequest(ts uint64) dbft.RecoveryRequest {
return &recoveryRequest{
timestamp: nanoSecToSec(ts),
}
}

// NewRecoveryMessage returns minimal RecoveryMessage implementation.
Expand Down
3 changes: 1 addition & 2 deletions internal/payload/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ func TestPayload_Setters(t *testing.T) {
})

t.Run("RecoveryRequest", func(t *testing.T) {
r := NewRecoveryRequest()
r := NewRecoveryRequest(secToNanoSec(321))

r.SetTimestamp(secToNanoSec(321))
require.EqualValues(t, secToNanoSec(321), r.Timestamp())
})

Expand Down
5 changes: 0 additions & 5 deletions internal/payload/recovery_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,3 @@ func (m *recoveryRequest) DecodeBinary(r *gob.Decoder) error {
func (m *recoveryRequest) Timestamp() uint64 {
return secToNanoSec(m.timestamp)
}

// SetTimestamp implements RecoveryRequest interface.
func (m *recoveryRequest) SetTimestamp(ts uint64) {
m.timestamp = nanoSecToSec(ts)
}
2 changes: 0 additions & 2 deletions recovery_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ package dbft
type RecoveryRequest interface {
// Timestamp returns this message's timestamp.
Timestamp() uint64
// SetTimestamp sets this message's timestamp.
SetTimestamp(ts uint64)
}
3 changes: 1 addition & 2 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ func (d *DBFT[H, A]) sendRecoveryRequest() {
if d.RequestSentOrReceived() && !d.hasAllTransactions() {
d.processMissingTx()
}
req := d.NewRecoveryRequest()
req.SetTimestamp(uint64(d.Timer.Now().UnixNano()))
req := d.NewRecoveryRequest(uint64(d.Timer.Now().UnixNano()))
d.broadcast(d.Config.NewConsensusPayload(&d.Context, RecoveryRequestType, req))
}

Expand Down

0 comments on commit a2fdfa4

Please sign in to comment.