Skip to content

Commit e3c535f

Browse files
committed
adjust ValidateDecideMessage API
1 parent 9c537d2 commit e3c535f

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

consensus.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,13 +745,14 @@ func (c *Consensus) verifyCommitMessage(m *Message) error {
745745

746746
// ValidateDecideMessage validates a <decide> message for non-participants,
747747
// the consensus core must be correctly initialized to validate.
748-
func (c *Consensus) ValidateDecideMessage(bts []byte) error {
748+
// the targetState is to compare the target state enclosed in decide message
749+
func (c *Consensus) ValidateDecideMessage(bts []byte, targetState []byte) error {
749750
signed, err := c.DecodeSignedMessage(bts)
750751
if err != nil {
751752
return err
752753
}
753754

754-
return c.validateDecideMessage(signed)
755+
return c.validateDecideMessage(signed, targetState)
755756
}
756757

757758
// DecodeSignedMessage decodes a binary representation of signed consensus message.
@@ -766,7 +767,7 @@ func (c *Consensus) DecodeSignedMessage(bts []byte) (*SignedProto, error) {
766767

767768
// validateDecideMessage validates a decoded <decide> message for non-participants,
768769
// the consensus core must be correctly initialized to validate.
769-
func (c *Consensus) validateDecideMessage(signed *SignedProto) error {
770+
func (c *Consensus) validateDecideMessage(signed *SignedProto, targetState []byte) error {
770771
// check message version
771772
if signed.Version != ProtocolVersion {
772773
return ErrMessageVersion
@@ -778,6 +779,11 @@ func (c *Consensus) validateDecideMessage(signed *SignedProto) error {
778779
return err
779780
}
780781

782+
// compare state
783+
if !bytes.Equal(m.State, targetState) {
784+
return ErrMismatchedTargetState
785+
}
786+
781787
// verify decide message
782788
if m.Type == MessageType_Decide {
783789
err := c.verifyDecideMessage(m, signed)

errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,7 @@ var (
103103
ErrCommitStatus = errors.New("received <commit> message in non COMMIT state")
104104
ErrCommitHeightMismatch = errors.New("the <commit> messge has another height than expected")
105105
ErrCommitRoundMismatch = errors.New("the <commit> message is from another round")
106+
107+
// <decide> verification
108+
ErrMismatchedTargetState = errors.New("the state in <decide> message does not match the provided target state")
106109
)

message_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -919,27 +919,27 @@ func TestVerifyDecideMessageCorrect(t *testing.T) {
919919
}
920920

921921
func TestValidateDecideMessageCorrect(t *testing.T) {
922-
_, sp, privateKey, proofKeys := createDecideMessage(t, 20, 10, 10, 10, 10)
922+
m, sp, privateKey, proofKeys := createDecideMessage(t, 20, 10, 10, 10, 10)
923923
consensus := createConsensus(t, 9, 10, proofKeys)
924924

925925
consensus.SetLeader(&privateKey.PublicKey)
926926
bts, err := proto.Marshal(sp)
927927
assert.Nil(t, err)
928928

929-
err = consensus.ValidateDecideMessage(bts)
929+
err = consensus.ValidateDecideMessage(bts, m.State)
930930
assert.Nil(t, err)
931931
}
932932

933933
func TestValidateDecideMessageUnknowParticipant(t *testing.T) {
934-
_, sp, privateKey, proofKeys := createDecideMessage(t, 20, 10, 10, 10, 10)
934+
m, sp, privateKey, proofKeys := createDecideMessage(t, 20, 10, 10, 10, 10)
935935
consensus := createConsensus(t, 9, 10, proofKeys)
936936

937937
consensus.SetLeader(&privateKey.PublicKey)
938938
bts, err := proto.Marshal(sp)
939939
assert.Nil(t, err)
940940

941941
consensus = createConsensus(t, 9, 10, nil)
942-
err = consensus.ValidateDecideMessage(bts)
942+
err = consensus.ValidateDecideMessage(bts, m.State)
943943
assert.NotNil(t, err)
944944
}
945945

0 commit comments

Comments
 (0)