Skip to content

Commit 1db32ec

Browse files
authored
Merge pull request #102 from nspcc-dev/simplify-payload-setters
dbft: remove useless setters of dBFT interfaces
2 parents f3c9fd4 + a2fdfa4 commit 1db32ec

22 files changed

+89
-253
lines changed

change_view.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,9 @@ type ChangeView interface {
55
// NewViewNumber returns proposed view number.
66
NewViewNumber() byte
77

8-
// SetNewViewNumber sets the proposed view number.
9-
SetNewViewNumber(view byte)
10-
118
// Timestamp returns message's timestamp.
129
Timestamp() uint64
1310

14-
// SetTimestamp sets message's timestamp.
15-
SetTimestamp(ts uint64)
16-
1711
// Reason returns change view reason.
1812
Reason() ChangeViewReason
19-
20-
// SetReason sets change view reason.
21-
SetReason(reason ChangeViewReason)
2213
}

commit.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,4 @@ type Commit interface {
55
// Signature returns commit's signature field
66
// which is a block signature for the current epoch.
77
Signature() []byte
8-
9-
// SetSignature sets commit's signature.
10-
SetSignature(signature []byte)
118
}

config.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ type Config[H Hash, A Address] struct {
6262
// NewConsensusPayload is a constructor for payload.ConsensusPayload.
6363
NewConsensusPayload func(*Context[H, A], MessageType, any) ConsensusPayload[H, A]
6464
// NewPrepareRequest is a constructor for payload.PrepareRequest.
65-
NewPrepareRequest func() PrepareRequest[H, A]
65+
NewPrepareRequest func(ts uint64, nonce uint64, nextConsensus A, transactionHashes []H) PrepareRequest[H, A]
6666
// NewPrepareResponse is a constructor for payload.PrepareResponse.
67-
NewPrepareResponse func() PrepareResponse[H]
67+
NewPrepareResponse func(preparationHash H) PrepareResponse[H]
6868
// NewChangeView is a constructor for payload.ChangeView.
69-
NewChangeView func() ChangeView
69+
NewChangeView func(newViewNumber byte, reason ChangeViewReason, timestamp uint64) ChangeView
7070
// NewCommit is a constructor for payload.Commit.
71-
NewCommit func() Commit
71+
NewCommit func(signature []byte) Commit
7272
// NewRecoveryRequest is a constructor for payload.RecoveryRequest.
73-
NewRecoveryRequest func() RecoveryRequest
73+
NewRecoveryRequest func(ts uint64) RecoveryRequest
7474
// NewRecoveryMessage is a constructor for payload.RecoveryMessage.
7575
NewRecoveryMessage func() RecoveryMessage[H, A]
7676
// VerifyPrepareRequest can perform external payload verification and returns true iff it was successful.
@@ -306,35 +306,35 @@ func WithNewConsensusPayload[H Hash, A Address](f func(*Context[H, A], MessageTy
306306
}
307307

308308
// WithNewPrepareRequest sets NewPrepareRequest.
309-
func WithNewPrepareRequest[H Hash, A Address](f func() PrepareRequest[H, A]) func(config *Config[H, A]) {
309+
func WithNewPrepareRequest[H Hash, A Address](f func(ts uint64, nonce uint64, nextConsensus A, transactionsHashes []H) PrepareRequest[H, A]) func(config *Config[H, A]) {
310310
return func(cfg *Config[H, A]) {
311311
cfg.NewPrepareRequest = f
312312
}
313313
}
314314

315315
// WithNewPrepareResponse sets NewPrepareResponse.
316-
func WithNewPrepareResponse[H Hash, A Address](f func() PrepareResponse[H]) func(config *Config[H, A]) {
316+
func WithNewPrepareResponse[H Hash, A Address](f func(preparationHash H) PrepareResponse[H]) func(config *Config[H, A]) {
317317
return func(cfg *Config[H, A]) {
318318
cfg.NewPrepareResponse = f
319319
}
320320
}
321321

322322
// WithNewChangeView sets NewChangeView.
323-
func WithNewChangeView[H Hash, A Address](f func() ChangeView) func(config *Config[H, A]) {
323+
func WithNewChangeView[H Hash, A Address](f func(byte, ChangeViewReason, uint64) ChangeView) func(config *Config[H, A]) {
324324
return func(cfg *Config[H, A]) {
325325
cfg.NewChangeView = f
326326
}
327327
}
328328

329329
// WithNewCommit sets NewCommit.
330-
func WithNewCommit[H Hash, A Address](f func() Commit) func(config *Config[H, A]) {
330+
func WithNewCommit[H Hash, A Address](f func([]byte) Commit) func(config *Config[H, A]) {
331331
return func(cfg *Config[H, A]) {
332332
cfg.NewCommit = f
333333
}
334334
}
335335

336336
// WithNewRecoveryRequest sets NewRecoveryRequest.
337-
func WithNewRecoveryRequest[H Hash, A Address](f func() RecoveryRequest) func(config *Config[H, A]) {
337+
func WithNewRecoveryRequest[H Hash, A Address](f func(ts uint64) RecoveryRequest) func(config *Config[H, A]) {
338338
return func(cfg *Config[H, A]) {
339339
cfg.NewRecoveryRequest = f
340340
}

consensus_message.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,10 @@ package dbft
44
type ConsensusMessage[H Hash, A Address] interface {
55
// ViewNumber returns view number when this message was originated.
66
ViewNumber() byte
7-
// SetViewNumber sets view number.
8-
SetViewNumber(view byte)
9-
107
// Type returns type of this message.
118
Type() MessageType
12-
// SetType sets the type of this message.
13-
SetType(t MessageType)
14-
159
// Payload returns this message's actual payload.
1610
Payload() any
17-
// SetPayload sets this message's payload to p.
18-
SetPayload(p any)
1911

2012
// GetChangeView returns payload as if it was ChangeView.
2113
GetChangeView() ChangeView

consensus_payload.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ type ConsensusPayload[H Hash, A Address] interface {
1313
SetValidatorIndex(i uint16)
1414

1515
Height() uint32
16-
SetHeight(h uint32)
1716

1817
// Hash returns 32-byte checksum of the payload.
1918
Hash() H

dbft_test.go

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ func TestDBFT_OnReceiveRequestSendResponse(t *testing.T) {
202202
})
203203

204204
t.Run("old height", func(t *testing.T) {
205-
p := s.getPrepareRequest(5, txs[0].Hash())
206-
p.SetHeight(3)
205+
p := s.getPrepareRequestWithHeight(5, 3, txs[0].Hash())
207206
service.OnReceive(p)
208207
require.Nil(t, s.tryRecv())
209208
})
@@ -462,35 +461,35 @@ func TestDBFT_Invalid(t *testing.T) {
462461
require.Nil(t, dbft.New(opts...))
463462
})
464463

465-
opts = append(opts, dbft.WithNewPrepareRequest[crypto.Uint256, crypto.Uint160](func() dbft.PrepareRequest[crypto.Uint256, crypto.Uint160] {
464+
opts = append(opts, dbft.WithNewPrepareRequest[crypto.Uint256, crypto.Uint160](func(uint64, uint64, crypto.Uint160, []crypto.Uint256) dbft.PrepareRequest[crypto.Uint256, crypto.Uint160] {
466465
return nil
467466
}))
468467
t.Run("without NewPrepareResponse", func(t *testing.T) {
469468
require.Nil(t, dbft.New(opts...))
470469
})
471470

472-
opts = append(opts, dbft.WithNewPrepareResponse[crypto.Uint256, crypto.Uint160](func() dbft.PrepareResponse[crypto.Uint256] {
471+
opts = append(opts, dbft.WithNewPrepareResponse[crypto.Uint256, crypto.Uint160](func(crypto.Uint256) dbft.PrepareResponse[crypto.Uint256] {
473472
return nil
474473
}))
475474
t.Run("without NewChangeView", func(t *testing.T) {
476475
require.Nil(t, dbft.New(opts...))
477476
})
478477

479-
opts = append(opts, dbft.WithNewChangeView[crypto.Uint256, crypto.Uint160](func() dbft.ChangeView {
478+
opts = append(opts, dbft.WithNewChangeView[crypto.Uint256, crypto.Uint160](func(byte, dbft.ChangeViewReason, uint64) dbft.ChangeView {
480479
return nil
481480
}))
482481
t.Run("without NewCommit", func(t *testing.T) {
483482
require.Nil(t, dbft.New(opts...))
484483
})
485484

486-
opts = append(opts, dbft.WithNewCommit[crypto.Uint256, crypto.Uint160](func() dbft.Commit {
485+
opts = append(opts, dbft.WithNewCommit[crypto.Uint256, crypto.Uint160](func([]byte) dbft.Commit {
487486
return nil
488487
}))
489488
t.Run("without NewRecoveryRequest", func(t *testing.T) {
490489
require.Nil(t, dbft.New(opts...))
491490
})
492491

493-
opts = append(opts, dbft.WithNewRecoveryRequest[crypto.Uint256, crypto.Uint160](func() dbft.RecoveryRequest {
492+
opts = append(opts, dbft.WithNewRecoveryRequest[crypto.Uint256, crypto.Uint160](func(uint64) dbft.RecoveryRequest {
494493
return nil
495494
}))
496495
t.Run("without NewRecoveryMessage", func(t *testing.T) {
@@ -732,63 +731,38 @@ func TestDBFT_FourGoodNodesDeadlock(t *testing.T) {
732731
}
733732

734733
func (s testState) getChangeView(from uint16, view byte) Payload {
735-
cv := payload.NewChangeView()
736-
cv.SetNewViewNumber(view)
737-
738-
p := s.getPayload(from)
739-
p.SetType(dbft.ChangeViewType)
740-
p.SetPayload(cv)
734+
cv := payload.NewChangeView(view, 0, 0)
741735

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

745740
func (s testState) getRecoveryRequest(from uint16) Payload {
746-
p := s.getPayload(from)
747-
p.SetType(dbft.RecoveryRequestType)
748-
p.SetPayload(payload.NewRecoveryRequest())
749-
741+
p := payload.NewConsensusPayload(dbft.RecoveryRequestType, s.currHeight+1, from, 0, payload.NewRecoveryRequest(0))
750742
return p
751743
}
752744

753745
func (s testState) getCommit(from uint16, sign []byte) Payload {
754-
c := payload.NewCommit()
755-
c.SetSignature(sign)
756-
757-
p := s.getPayload(from)
758-
p.SetType(dbft.CommitType)
759-
p.SetPayload(c)
760-
746+
c := payload.NewCommit(sign)
747+
p := payload.NewConsensusPayload(dbft.CommitType, s.currHeight+1, from, 0, c)
761748
return p
762749
}
763750

764751
func (s testState) getPrepareResponse(from uint16, phash crypto.Uint256) Payload {
765-
resp := payload.NewPrepareResponse()
766-
resp.SetPreparationHash(phash)
767-
768-
p := s.getPayload(from)
769-
p.SetType(dbft.PrepareResponseType)
770-
p.SetPayload(resp)
752+
resp := payload.NewPrepareResponse(phash)
771753

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

775758
func (s testState) getPrepareRequest(from uint16, hashes ...crypto.Uint256) Payload {
776-
req := payload.NewPrepareRequest()
777-
req.SetTransactionHashes(hashes)
778-
req.SetNextConsensus(s.nextConsensus())
779-
780-
p := s.getPayload(from)
781-
p.SetType(dbft.PrepareRequestType)
782-
p.SetPayload(req)
783-
784-
return p
759+
return s.getPrepareRequestWithHeight(from, s.currHeight+1, hashes...)
785760
}
786761

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

765+
p := payload.NewConsensusPayload(dbft.PrepareRequestType, height, from, 0, req)
792766
return p
793767
}
794768

@@ -867,7 +841,9 @@ func (s *testState) getOptions() []func(*dbft.Config[crypto.Uint256, crypto.Uint
867841
dbft.WithNewChangeView[crypto.Uint256, crypto.Uint160](payload.NewChangeView),
868842
dbft.WithNewCommit[crypto.Uint256, crypto.Uint160](payload.NewCommit),
869843
dbft.WithNewRecoveryRequest[crypto.Uint256, crypto.Uint160](payload.NewRecoveryRequest),
870-
dbft.WithNewRecoveryMessage[crypto.Uint256, crypto.Uint160](payload.NewRecoveryMessage),
844+
dbft.WithNewRecoveryMessage[crypto.Uint256, crypto.Uint160](func() dbft.RecoveryMessage[crypto.Uint256, crypto.Uint160] {
845+
return payload.NewRecoveryMessage(nil)
846+
}),
871847
}
872848

873849
verify := s.verify
@@ -898,13 +874,7 @@ func newBlockFromContext(ctx *dbft.Context[crypto.Uint256, crypto.Uint160]) dbft
898874
// newConsensusPayload is a function for creating consensus payload of specific
899875
// type.
900876
func newConsensusPayload(c *dbft.Context[crypto.Uint256, crypto.Uint160], t dbft.MessageType, msg any) dbft.ConsensusPayload[crypto.Uint256, crypto.Uint160] {
901-
cp := payload.NewConsensusPayload()
902-
cp.SetHeight(c.BlockIndex)
903-
cp.SetValidatorIndex(uint16(c.MyIndex))
904-
cp.SetViewNumber(c.ViewNumber)
905-
cp.SetType(t)
906-
cp.SetPayload(msg)
907-
877+
cp := payload.NewConsensusPayload(t, c.BlockIndex, uint16(c.MyIndex), c.ViewNumber, msg)
908878
return cp
909879
}
910880

internal/payload/change_view.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,12 @@ func (c changeView) NewViewNumber() byte {
4141
return c.newViewNumber
4242
}
4343

44-
// SetNewViewNumber implements ChangeView interface.
45-
func (c *changeView) SetNewViewNumber(view byte) {
46-
c.newViewNumber = view
47-
}
48-
4944
// Timestamp implements ChangeView interface.
5045
func (c changeView) Timestamp() uint64 {
5146
return secToNanoSec(c.timestamp)
5247
}
5348

54-
// SetTimestamp implements ChangeView interface.
55-
func (c *changeView) SetTimestamp(ts uint64) {
56-
c.timestamp = nanoSecToSec(ts)
57-
}
58-
5949
// Reason implements ChangeView interface.
6050
func (c changeView) Reason() dbft.ChangeViewReason {
6151
return dbft.CVUnknown
6252
}
63-
64-
// SetReason implements ChangeView interface.
65-
func (c *changeView) SetReason(_ dbft.ChangeViewReason) {
66-
}

internal/payload/commit.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,3 @@ func (c *commit) DecodeBinary(r *gob.Decoder) error {
4141
func (c commit) Signature() []byte {
4242
return c.signature[:]
4343
}
44-
45-
// SetSignature implements Commit interface.
46-
func (c *commit) SetSignature(sig []byte) {
47-
copy(c.signature[:], sig)
48-
}

internal/payload/consensus_message.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,27 +98,12 @@ func (m message) ViewNumber() byte {
9898
return m.viewNumber
9999
}
100100

101-
// SetViewNumber implements ConsensusMessage interface.
102-
func (m *message) SetViewNumber(view byte) {
103-
m.viewNumber = view
104-
}
105-
106101
// Type implements ConsensusMessage interface.
107102
func (m message) Type() dbft.MessageType {
108103
return m.cmType
109104
}
110105

111-
// SetType implements ConsensusMessage interface.
112-
func (m *message) SetType(t dbft.MessageType) {
113-
m.cmType = t
114-
}
115-
116106
// Payload implements ConsensusMessage interface.
117107
func (m message) Payload() any {
118108
return m.payload
119109
}
120-
121-
// SetPayload implements ConsensusMessage interface.
122-
func (m *message) SetPayload(p any) {
123-
m.payload = p
124-
}

0 commit comments

Comments
 (0)