Skip to content

Commit

Permalink
dbft: replace setters with extended constructor for dbft.PrepareResponse
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 2b2e6cb commit fca4ff6
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 16 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Config[H Hash, A Address] struct {
// NewPrepareRequest is a constructor for payload.PrepareRequest.
NewPrepareRequest func(ts uint64, nonce uint64, nextConsensus A, transactionHashes []H) PrepareRequest[H, A]
// NewPrepareResponse is a constructor for payload.PrepareResponse.
NewPrepareResponse func() PrepareResponse[H]
NewPrepareResponse func(preparationHash H) PrepareResponse[H]
// NewChangeView is a constructor for payload.ChangeView.
NewChangeView func(newViewNumber byte, reason ChangeViewReason, timestamp uint64) ChangeView
// NewCommit is a constructor for payload.Commit.
Expand Down Expand Up @@ -313,7 +313,7 @@ func WithNewPrepareRequest[H Hash, A Address](f func(ts uint64, nonce uint64, ne
}

// WithNewPrepareResponse sets NewPrepareResponse.
func WithNewPrepareResponse[H Hash, A Address](f func() PrepareResponse[H]) func(config *Config[H, A]) {
func WithNewPrepareResponse[H Hash, A Address](f func(preparationHash H) PrepareResponse[H]) func(config *Config[H, A]) {
return func(cfg *Config[H, A]) {
cfg.NewPrepareResponse = f
}
Expand Down
5 changes: 2 additions & 3 deletions dbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ func TestDBFT_Invalid(t *testing.T) {
require.Nil(t, dbft.New(opts...))
})

opts = append(opts, dbft.WithNewPrepareResponse[crypto.Uint256, crypto.Uint160](func() dbft.PrepareResponse[crypto.Uint256] {
opts = append(opts, dbft.WithNewPrepareResponse[crypto.Uint256, crypto.Uint160](func(crypto.Uint256) dbft.PrepareResponse[crypto.Uint256] {
return nil
}))
t.Run("without NewChangeView", func(t *testing.T) {
Expand Down Expand Up @@ -749,8 +749,7 @@ func (s testState) getCommit(from uint16, sign []byte) Payload {
}

func (s testState) getPrepareResponse(from uint16, phash crypto.Uint256) Payload {
resp := payload.NewPrepareResponse()
resp.SetPreparationHash(phash)
resp := payload.NewPrepareResponse(phash)

p := payload.NewConsensusPayload(dbft.PrepareResponseType, s.currHeight+1, from, 0, resp)
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 @@ -29,8 +29,10 @@ func NewPrepareRequest(ts uint64, nonce uint64, nextConsensus crypto.Uint160, tr
}

// NewPrepareResponse returns minimal PrepareResponse implementation.
func NewPrepareResponse() dbft.PrepareResponse[crypto.Uint256] {
return new(prepareResponse)
func NewPrepareResponse(preparationHash crypto.Uint256) dbft.PrepareResponse[crypto.Uint256] {
return &prepareResponse{
preparationHash: preparationHash,
}
}

// NewChangeView returns minimal ChangeView implementation.
Expand Down
5 changes: 0 additions & 5 deletions internal/payload/prepare_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,3 @@ func (p *prepareResponse) DecodeBinary(r *gob.Decoder) error {
func (p *prepareResponse) PreparationHash() crypto.Uint256 {
return p.preparationHash
}

// SetPreparationHash implements PrepareResponse interface.
func (p *prepareResponse) SetPreparationHash(h crypto.Uint256) {
p.preparationHash = h
}
2 changes: 0 additions & 2 deletions prepare_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ type PrepareResponse[H Hash] interface {
// PreparationHash returns the hash of PrepareRequest payload
// for this epoch.
PreparationHash() H
// SetPreparationHash sets preparations hash.
SetPreparationHash(h H)
}
3 changes: 1 addition & 2 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ func (d *DBFT[H, A]) sendChangeView(reason ChangeViewReason) {
}

func (c *Context[H, A]) makePrepareResponse() ConsensusPayload[H, A] {
resp := c.Config.NewPrepareResponse()
resp.SetPreparationHash(c.PreparationPayloads[c.PrimaryIndex].Hash())
resp := c.Config.NewPrepareResponse(c.PreparationPayloads[c.PrimaryIndex].Hash())

msg := c.Config.NewConsensusPayload(c, PrepareResponseType, resp)
c.PreparationPayloads[c.MyIndex] = msg
Expand Down

0 comments on commit fca4ff6

Please sign in to comment.