diff --git a/consensus_message.go b/consensus_message.go index 5552e469..7e72029f 100644 --- a/consensus_message.go +++ b/consensus_message.go @@ -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 diff --git a/consensus_payload.go b/consensus_payload.go index af6bf533..e537d3e0 100644 --- a/consensus_payload.go +++ b/consensus_payload.go @@ -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 diff --git a/dbft_test.go b/dbft_test.go index 4aa9511d..951e5265 100644 --- a/dbft_test.go +++ b/dbft_test.go @@ -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()) }) @@ -735,18 +734,12 @@ 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) - + p := payload.NewConsensusPayload(dbft.ChangeViewType, s.currHeight+1, from, view, 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()) return p } @@ -754,10 +747,7 @@ 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) - + p := payload.NewConsensusPayload(dbft.CommitType, s.currHeight+1, from, 0, c) return p } @@ -765,30 +755,20 @@ 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) - + p := payload.NewConsensusPayload(dbft.PrepareResponseType, s.currHeight+1, from, 0, resp) return p } func (s testState) getPrepareRequest(from uint16, hashes ...crypto.Uint256) Payload { + return s.getPrepareRequestWithHeight(from, s.currHeight+1, hashes...) +} + +func (s testState) getPrepareRequestWithHeight(from uint16, height uint32, 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 -} - -func (s testState) getPayload(from uint16) Payload { - p := payload.NewConsensusPayload() - p.SetHeight(s.currHeight + 1) - p.SetValidatorIndex(from) - + p := payload.NewConsensusPayload(dbft.PrepareRequestType, height, from, 0, req) return p } @@ -867,7 +847,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 @@ -898,13 +880,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 } diff --git a/internal/payload/consensus_message.go b/internal/payload/consensus_message.go index 88dded51..dd0e2436 100644 --- a/internal/payload/consensus_message.go +++ b/internal/payload/consensus_message.go @@ -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 -} diff --git a/internal/payload/constructors.go b/internal/payload/constructors.go index ac1ecd0a..d4d29093 100644 --- a/internal/payload/constructors.go +++ b/internal/payload/constructors.go @@ -6,8 +6,16 @@ import ( ) // NewConsensusPayload returns minimal ConsensusPayload implementation. -func NewConsensusPayload() dbft.ConsensusPayload[crypto.Uint256, crypto.Uint160] { - return &Payload{} +func NewConsensusPayload(t dbft.MessageType, height uint32, validatorIndex uint16, viewNumber byte, consensusMessage any) dbft.ConsensusPayload[crypto.Uint256, crypto.Uint160] { + return &Payload{ + message: message{ + cmType: t, + viewNumber: viewNumber, + payload: consensusMessage, + }, + validatorIndex: validatorIndex, + height: height, + } } // NewPrepareRequest returns minimal prepareRequest implementation. @@ -36,8 +44,9 @@ func NewRecoveryRequest() dbft.RecoveryRequest { } // NewRecoveryMessage returns minimal RecoveryMessage implementation. -func NewRecoveryMessage() dbft.RecoveryMessage[crypto.Uint256, crypto.Uint160] { +func NewRecoveryMessage(preparationHash *crypto.Uint256) dbft.RecoveryMessage[crypto.Uint256, crypto.Uint160] { return &recoveryMessage{ + preparationHash: preparationHash, preparationPayloads: make([]preparationCompact, 0), commitPayloads: make([]commitCompact, 0), changeViewPayloads: make([]changeViewCompact, 0), diff --git a/internal/payload/message.go b/internal/payload/message.go index e7de8274..a39c49a4 100644 --- a/internal/payload/message.go +++ b/internal/payload/message.go @@ -100,11 +100,6 @@ func (p Payload) Version() uint32 { return p.version } -// SetVersion implements ConsensusPayload interface. -func (p *Payload) SetVersion(v uint32) { - p.version = v -} - // ValidatorIndex implements ConsensusPayload interface. func (p Payload) ValidatorIndex() uint16 { return p.validatorIndex @@ -120,17 +115,7 @@ func (p Payload) PrevHash() crypto.Uint256 { return p.prevHash } -// SetPrevHash implements ConsensusPayload interface. -func (p *Payload) SetPrevHash(h crypto.Uint256) { - p.prevHash = h -} - // Height implements ConsensusPayload interface. func (p Payload) Height() uint32 { return p.height } - -// SetHeight implements ConsensusPayload interface. -func (p *Payload) SetHeight(h uint32) { - p.height = h -} diff --git a/internal/payload/message_test.go b/internal/payload/message_test.go index 8d76185e..d9de377e 100644 --- a/internal/payload/message_test.go +++ b/internal/payload/message_test.go @@ -13,16 +13,12 @@ import ( ) func TestPayload_EncodeDecode(t *testing.T) { - m := NewConsensusPayload().(*Payload) - m.SetValidatorIndex(10) - m.SetHeight(77) - m.SetPrevHash(crypto.Uint256{1}) - m.SetVersion(8) - m.SetViewNumber(3) + generateMessage := func(typ dbft.MessageType, payload any) *Payload { + return NewConsensusPayload(typ, 77, 10, 3, payload).(*Payload) + } t.Run("PrepareRequest", func(t *testing.T) { - m.SetType(dbft.PrepareRequestType) - m.SetPayload(&prepareRequest{ + m := generateMessage(dbft.PrepareRequestType, &prepareRequest{ nonce: 123, timestamp: 345, transactionHashes: []crypto.Uint256{ @@ -36,8 +32,7 @@ func TestPayload_EncodeDecode(t *testing.T) { }) t.Run("PrepareResponse", func(t *testing.T) { - m.SetType(dbft.PrepareResponseType) - m.SetPayload(&prepareResponse{ + m := generateMessage(dbft.PrepareResponseType, &prepareResponse{ preparationHash: crypto.Uint256{3}, }) @@ -46,18 +41,16 @@ func TestPayload_EncodeDecode(t *testing.T) { }) t.Run("Commit", func(t *testing.T) { - m.SetType(dbft.CommitType) var cc commit fillRandom(t, cc.signature[:]) - m.SetPayload(&cc) + m := generateMessage(dbft.CommitType, &cc) testEncodeDecode(t, m, new(Payload)) testMarshalUnmarshal(t, m, new(Payload)) }) t.Run("ChangeView", func(t *testing.T) { - m.SetType(dbft.ChangeViewType) - m.SetPayload(&changeView{ + m := generateMessage(dbft.ChangeViewType, &changeView{ timestamp: 12345, newViewNumber: 4, }) @@ -67,8 +60,7 @@ func TestPayload_EncodeDecode(t *testing.T) { }) t.Run("RecoveryMessage", func(t *testing.T) { - m.SetType(dbft.RecoveryMessageType) - m.SetPayload(&recoveryMessage{ + m := generateMessage(dbft.RecoveryMessageType, &recoveryMessage{ changeViewPayloads: []changeViewCompact{ { Timestamp: 123, @@ -97,8 +89,7 @@ func TestPayload_EncodeDecode(t *testing.T) { }) t.Run("RecoveryRequest", func(t *testing.T) { - m.SetType(dbft.RecoveryRequestType) - m.SetPayload(&recoveryRequest{ + m := generateMessage(dbft.RecoveryRequestType, &recoveryRequest{ timestamp: 17334, }) @@ -108,13 +99,7 @@ func TestPayload_EncodeDecode(t *testing.T) { } func TestRecoveryMessage_NoPayloads(t *testing.T) { - m := NewConsensusPayload().(*Payload) - m.SetValidatorIndex(0) - m.SetHeight(77) - m.SetPrevHash(crypto.Uint256{1}) - m.SetVersion(8) - m.SetViewNumber(3) - m.SetPayload(&recoveryMessage{}) + m := NewConsensusPayload(dbft.RecoveryRequestType, 77, 0, 3, &recoveryMessage{}).(*Payload) validators := make([]dbft.PublicKey, 1) _, validators[0] = crypto.Generate(rand.Reader) @@ -186,9 +171,8 @@ func TestPayload_Setters(t *testing.T) { }) t.Run("RecoveryMessage", func(t *testing.T) { - r := NewRecoveryMessage() + r := NewRecoveryMessage(&crypto.Uint256{1, 2, 3}) - r.SetPreparationHash(&crypto.Uint256{1, 2, 3}) require.Equal(t, &crypto.Uint256{1, 2, 3}, r.PreparationHash()) }) } diff --git a/internal/payload/recovery_message.go b/internal/payload/recovery_message.go index 870f402c..f98647f3 100644 --- a/internal/payload/recovery_message.go +++ b/internal/payload/recovery_message.go @@ -31,11 +31,6 @@ func (m *recoveryMessage) PreparationHash() *crypto.Uint256 { return m.preparationHash } -// SetPreparationHash implements RecoveryMessage interface. -func (m *recoveryMessage) SetPreparationHash(h *crypto.Uint256) { - m.preparationHash = h -} - // AddPayload implements RecoveryMessage interface. func (m *recoveryMessage) AddPayload(p dbft.ConsensusPayload[crypto.Uint256, crypto.Uint160]) { switch p.Type() { diff --git a/internal/simulation/main.go b/internal/simulation/main.go index e4587935..8cc655db 100644 --- a/internal/simulation/main.go +++ b/internal/simulation/main.go @@ -121,14 +121,7 @@ func newBlockFromContext(ctx *dbft.Context[crypto.Uint256, crypto.Uint160]) dbft // defaultNewConsensusPayload is default function for creating // consensus payload of specific type. func defaultNewConsensusPayload(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) - - return cp + return payload.NewConsensusPayload(t, c.BlockIndex, uint16(c.MyIndex), c.ViewNumber, msg) } func initSimNode(nodes []*simNode, i int, log *zap.Logger) error { @@ -164,7 +157,9 @@ func initSimNode(nodes []*simNode, i int, log *zap.Logger) error { dbft.WithNewPrepareResponse[crypto.Uint256, crypto.Uint160](payload.NewPrepareResponse), dbft.WithNewChangeView[crypto.Uint256, crypto.Uint160](payload.NewChangeView), dbft.WithNewCommit[crypto.Uint256, crypto.Uint160](payload.NewCommit), - 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) + }), dbft.WithNewRecoveryRequest[crypto.Uint256, crypto.Uint160](payload.NewRecoveryRequest), ) diff --git a/recovery_message.go b/recovery_message.go index be14aa2c..8e6f760d 100644 --- a/recovery_message.go +++ b/recovery_message.go @@ -16,6 +16,4 @@ type RecoveryMessage[H Hash, A Address] interface { // PreparationHash returns has of PrepareRequest payload for this epoch. // It can be useful in case only PrepareResponse payloads were received. PreparationHash() *H - // SetPreparationHash sets preparation hash. - SetPreparationHash(h *H) }