Skip to content

Commit

Permalink
Merge pull request #102 from nspcc-dev/simplify-payload-setters
Browse files Browse the repository at this point in the history
dbft: remove useless setters of dBFT interfaces
  • Loading branch information
roman-khimov committed Mar 5, 2024
2 parents f3c9fd4 + a2fdfa4 commit 1db32ec
Show file tree
Hide file tree
Showing 22 changed files with 89 additions and 253 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)
}
3 changes: 0 additions & 3 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,4 @@ type Commit interface {
// Signature returns commit's signature field
// which is a block signature for the current epoch.
Signature() []byte

// SetSignature sets commit's signature.
SetSignature(signature []byte)
}
20 changes: 10 additions & 10 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ 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]
NewPrepareResponse func(preparationHash H) 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() 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 @@ -306,35 +306,35 @@ 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
}
}

// 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
}
}

// 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
}
}

// WithNewCommit sets NewCommit.
func WithNewCommit[H Hash, A Address](f func() Commit) func(config *Config[H, A]) {
func WithNewCommit[H Hash, A Address](f func([]byte) Commit) func(config *Config[H, A]) {
return func(cfg *Config[H, A]) {
cfg.NewCommit = f
}
}

// 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
8 changes: 0 additions & 8 deletions consensus_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,10 @@ package dbft
type ConsensusMessage[H Hash, A Address] interface {
// ViewNumber returns view number when this message was originated.
ViewNumber() byte
// SetViewNumber sets view number.
SetViewNumber(view byte)

// Type returns type of this message.
Type() MessageType
// SetType sets the type of this message.
SetType(t MessageType)

// Payload returns this message's actual payload.
Payload() any
// SetPayload sets this message's payload to p.
SetPayload(p any)

// GetChangeView returns payload as if it was ChangeView.
GetChangeView() ChangeView
Expand Down
1 change: 0 additions & 1 deletion consensus_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ type ConsensusPayload[H Hash, A Address] interface {
SetValidatorIndex(i uint16)

Height() uint32
SetHeight(h uint32)

// Hash returns 32-byte checksum of the payload.
Hash() H
Expand Down
72 changes: 21 additions & 51 deletions dbft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ func TestDBFT_OnReceiveRequestSendResponse(t *testing.T) {
})

t.Run("old height", func(t *testing.T) {
p := s.getPrepareRequest(5, txs[0].Hash())
p.SetHeight(3)
p := s.getPrepareRequestWithHeight(5, 3, txs[0].Hash())
service.OnReceive(p)
require.Nil(t, s.tryRecv())
})
Expand Down Expand Up @@ -462,35 +461,35 @@ 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) {
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) {
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) {
require.Nil(t, dbft.New(opts...))
})

opts = append(opts, dbft.WithNewCommit[crypto.Uint256, crypto.Uint160](func() dbft.Commit {
opts = append(opts, dbft.WithNewCommit[crypto.Uint256, crypto.Uint160](func([]byte) dbft.Commit {
return nil
}))
t.Run("without NewRecoveryRequest", func(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 @@ -732,63 +731,38 @@ func TestDBFT_FourGoodNodesDeadlock(t *testing.T) {
}

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

p := s.getPayload(from)
p.SetType(dbft.ChangeViewType)
p.SetPayload(cv)
cv := payload.NewChangeView(view, 0, 0)

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

func (s testState) getRecoveryRequest(from uint16) Payload {
p := s.getPayload(from)
p.SetType(dbft.RecoveryRequestType)
p.SetPayload(payload.NewRecoveryRequest())

p := payload.NewConsensusPayload(dbft.RecoveryRequestType, s.currHeight+1, from, 0, payload.NewRecoveryRequest(0))
return p
}

func (s testState) getCommit(from uint16, sign []byte) Payload {
c := payload.NewCommit()
c.SetSignature(sign)

p := s.getPayload(from)
p.SetType(dbft.CommitType)
p.SetPayload(c)

c := payload.NewCommit(sign)
p := payload.NewConsensusPayload(dbft.CommitType, s.currHeight+1, from, 0, c)
return p
}

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

p := s.getPayload(from)
p.SetType(dbft.PrepareResponseType)
p.SetPayload(resp)
resp := payload.NewPrepareResponse(phash)

p := payload.NewConsensusPayload(dbft.PrepareResponseType, s.currHeight+1, from, 0, resp)
return p
}

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

p := s.getPayload(from)
p.SetType(dbft.PrepareRequestType)
p.SetPayload(req)

return p
return s.getPrepareRequestWithHeight(from, s.currHeight+1, hashes...)
}

func (s testState) getPayload(from uint16) Payload {
p := payload.NewConsensusPayload()
p.SetHeight(s.currHeight + 1)
p.SetValidatorIndex(from)
func (s testState) getPrepareRequestWithHeight(from uint16, height uint32, hashes ...crypto.Uint256) Payload {
req := payload.NewPrepareRequest(0, 0, s.nextConsensus(), hashes)

p := payload.NewConsensusPayload(dbft.PrepareRequestType, height, from, 0, req)
return p
}

Expand Down Expand Up @@ -867,7 +841,9 @@ func (s *testState) getOptions() []func(*dbft.Config[crypto.Uint256, crypto.Uint
dbft.WithNewChangeView[crypto.Uint256, crypto.Uint160](payload.NewChangeView),
dbft.WithNewCommit[crypto.Uint256, crypto.Uint160](payload.NewCommit),
dbft.WithNewRecoveryRequest[crypto.Uint256, crypto.Uint160](payload.NewRecoveryRequest),
dbft.WithNewRecoveryMessage[crypto.Uint256, crypto.Uint160](payload.NewRecoveryMessage),
dbft.WithNewRecoveryMessage[crypto.Uint256, crypto.Uint160](func() dbft.RecoveryMessage[crypto.Uint256, crypto.Uint160] {
return payload.NewRecoveryMessage(nil)
}),
}

verify := s.verify
Expand Down Expand Up @@ -898,13 +874,7 @@ func newBlockFromContext(ctx *dbft.Context[crypto.Uint256, crypto.Uint160]) dbft
// newConsensusPayload is a function for creating consensus payload of specific
// type.
func newConsensusPayload(c *dbft.Context[crypto.Uint256, crypto.Uint160], t dbft.MessageType, msg any) dbft.ConsensusPayload[crypto.Uint256, crypto.Uint160] {
cp := payload.NewConsensusPayload()
cp.SetHeight(c.BlockIndex)
cp.SetValidatorIndex(uint16(c.MyIndex))
cp.SetViewNumber(c.ViewNumber)
cp.SetType(t)
cp.SetPayload(msg)

cp := payload.NewConsensusPayload(t, c.BlockIndex, uint16(c.MyIndex), c.ViewNumber, msg)
return cp
}

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) {
}
5 changes: 0 additions & 5 deletions internal/payload/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,3 @@ func (c *commit) DecodeBinary(r *gob.Decoder) error {
func (c commit) Signature() []byte {
return c.signature[:]
}

// SetSignature implements Commit interface.
func (c *commit) SetSignature(sig []byte) {
copy(c.signature[:], sig)
}
15 changes: 0 additions & 15 deletions internal/payload/consensus_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,12 @@ func (m message) ViewNumber() byte {
return m.viewNumber
}

// SetViewNumber implements ConsensusMessage interface.
func (m *message) SetViewNumber(view byte) {
m.viewNumber = view
}

// Type implements ConsensusMessage interface.
func (m message) Type() dbft.MessageType {
return m.cmType
}

// SetType implements ConsensusMessage interface.
func (m *message) SetType(t dbft.MessageType) {
m.cmType = t
}

// Payload implements ConsensusMessage interface.
func (m message) Payload() any {
return m.payload
}

// SetPayload implements ConsensusMessage interface.
func (m *message) SetPayload(p any) {
m.payload = p
}
Loading

0 comments on commit 1db32ec

Please sign in to comment.