Skip to content

Commit

Permalink
dbft: replace setters with extended constructor for dbft.PrepareRequest
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 5086d09 commit 2b2e6cb
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 48 deletions.
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Config[H Hash, A Address] struct {
// NewConsensusPayload is a constructor for payload.ConsensusPayload.
NewConsensusPayload func(*Context[H, A], MessageType, any) ConsensusPayload[H, A]
// NewPrepareRequest is a constructor for payload.PrepareRequest.
NewPrepareRequest func() PrepareRequest[H, A]
NewPrepareRequest func(ts uint64, nonce uint64, nextConsensus A, transactionHashes []H) PrepareRequest[H, A]
// NewPrepareResponse is a constructor for payload.PrepareResponse.
NewPrepareResponse func() PrepareResponse[H]
// NewChangeView is a constructor for payload.ChangeView.
Expand Down Expand Up @@ -306,7 +306,7 @@ func WithNewConsensusPayload[H Hash, A Address](f func(*Context[H, A], MessageTy
}

// WithNewPrepareRequest sets NewPrepareRequest.
func WithNewPrepareRequest[H Hash, A Address](f func() PrepareRequest[H, A]) func(config *Config[H, A]) {
func WithNewPrepareRequest[H Hash, A Address](f func(ts uint64, nonce uint64, nextConsensus A, transactionsHashes []H) PrepareRequest[H, A]) func(config *Config[H, A]) {
return func(cfg *Config[H, A]) {
cfg.NewPrepareRequest = f
}
Expand Down
13 changes: 5 additions & 8 deletions dbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ func TestDBFT_Invalid(t *testing.T) {
require.Nil(t, dbft.New(opts...))
})

opts = append(opts, dbft.WithNewPrepareRequest[crypto.Uint256, crypto.Uint160](func() dbft.PrepareRequest[crypto.Uint256, crypto.Uint160] {
opts = append(opts, dbft.WithNewPrepareRequest[crypto.Uint256, crypto.Uint160](func(uint64, uint64, crypto.Uint160, []crypto.Uint256) dbft.PrepareRequest[crypto.Uint256, crypto.Uint160] {
return nil
}))
t.Run("without NewPrepareResponse", func(t *testing.T) {
Expand All @@ -475,7 +475,7 @@ func TestDBFT_Invalid(t *testing.T) {
require.Nil(t, dbft.New(opts...))
})

opts = append(opts, dbft.WithNewChangeView[crypto.Uint256, crypto.Uint160](func() dbft.ChangeView {
opts = append(opts, dbft.WithNewChangeView[crypto.Uint256, crypto.Uint160](func(byte, dbft.ChangeViewReason, uint64) dbft.ChangeView {
return nil
}))
t.Run("without NewCommit", func(t *testing.T) {
Expand Down Expand Up @@ -731,10 +731,9 @@ func TestDBFT_FourGoodNodesDeadlock(t *testing.T) {
}

func (s testState) getChangeView(from uint16, view byte) Payload {
cv := payload.NewChangeView()
cv.SetNewViewNumber(view)
cv := payload.NewChangeView(view, 0, 0)

p := payload.NewConsensusPayload(dbft.ChangeViewType, s.currHeight+1, from, view, cv)
p := payload.NewConsensusPayload(dbft.ChangeViewType, s.currHeight+1, from, 0, cv)
return p
}

Expand Down Expand Up @@ -762,9 +761,7 @@ func (s testState) getPrepareRequest(from uint16, hashes ...crypto.Uint256) Payl
}

func (s testState) getPrepareRequestWithHeight(from uint16, height uint32, hashes ...crypto.Uint256) Payload {
req := payload.NewPrepareRequest()
req.SetTransactionHashes(hashes)
req.SetNextConsensus(s.nextConsensus())
req := payload.NewPrepareRequest(0, 0, s.nextConsensus(), hashes)

p := payload.NewConsensusPayload(dbft.PrepareRequestType, height, from, 0, req)
return p
Expand Down
9 changes: 7 additions & 2 deletions internal/payload/constructors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ func NewConsensusPayload(t dbft.MessageType, height uint32, validatorIndex uint1
}

// NewPrepareRequest returns minimal prepareRequest implementation.
func NewPrepareRequest() dbft.PrepareRequest[crypto.Uint256, crypto.Uint160] {
return new(prepareRequest)
func NewPrepareRequest(ts uint64, nonce uint64, nextConsensus crypto.Uint160, transactionsHashes []crypto.Uint256) dbft.PrepareRequest[crypto.Uint256, crypto.Uint160] {
return &prepareRequest{
transactionHashes: transactionsHashes,
nonce: nonce,
timestamp: nanoSecToSec(ts),
nextConsensus: nextConsensus,
}
}

// NewPrepareResponse returns minimal PrepareResponse implementation.
Expand Down
20 changes: 0 additions & 20 deletions internal/payload/prepare_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,17 @@ func (p prepareRequest) Timestamp() uint64 {
return secToNanoSec(p.timestamp)
}

// SetTimestamp implements PrepareRequest interface.
func (p *prepareRequest) SetTimestamp(ts uint64) {
p.timestamp = nanoSecToSec(ts)
}

// Nonce implements PrepareRequest interface.
func (p prepareRequest) Nonce() uint64 {
return p.nonce
}

// SetNonce implements PrepareRequest interface.
func (p *prepareRequest) SetNonce(nonce uint64) {
p.nonce = nonce
}

// TransactionHashes implements PrepareRequest interface.
func (p prepareRequest) TransactionHashes() []crypto.Uint256 {
return p.transactionHashes
}

// SetTransactionHashes implements PrepareRequest interface.
func (p *prepareRequest) SetTransactionHashes(hs []crypto.Uint256) {
p.transactionHashes = hs
}

// NextConsensus implements PrepareRequest interface.
func (p prepareRequest) NextConsensus() crypto.Uint160 {
return p.nextConsensus
}

// SetNextConsensus implements PrepareRequest interface.
func (p *prepareRequest) SetNextConsensus(nc crypto.Uint160) {
p.nextConsensus = nc
}
11 changes: 0 additions & 11 deletions prepare_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,11 @@ package dbft
type PrepareRequest[H Hash, A Address] interface {
// Timestamp returns this message's timestamp.
Timestamp() uint64
// SetTimestamp sets timestamp of this message.
SetTimestamp(ts uint64)

// Nonce is a random nonce.
Nonce() uint64
// SetNonce sets Nonce.
SetNonce(nonce uint64)

// TransactionHashes returns hashes of all transaction in a proposed block.
TransactionHashes() []H
// SetTransactionHashes sets transaction's hashes.
SetTransactionHashes(hs []H)

// NextConsensus returns hash which is based on which validators will
// try to agree on a block in the current epoch.
NextConsensus() A
// SetNextConsensus sets next consensus field.
SetNextConsensus(nc A)
}
6 changes: 1 addition & 5 deletions send.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ func (d *DBFT[H, A]) broadcast(msg ConsensusPayload[H, A]) {
func (c *Context[H, A]) makePrepareRequest() ConsensusPayload[H, A] {
c.Fill()

req := c.Config.NewPrepareRequest()
req.SetTimestamp(c.Timestamp)
req.SetNonce(c.Nonce)
req.SetNextConsensus(c.NextConsensus)
req.SetTransactionHashes(c.TransactionHashes)
req := c.Config.NewPrepareRequest(c.Timestamp, c.Nonce, c.NextConsensus, c.TransactionHashes)

return c.Config.NewConsensusPayload(c, PrepareRequestType, req)
}
Expand Down

0 comments on commit 2b2e6cb

Please sign in to comment.