Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Modify MakeCommit and AggregateSignature to one operation at once #210

Merged
merged 6 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,6 @@ func (cs *State) createProposalBlock(round int) (block *types.Block, blockParts
case cs.LastCommit.HasTwoThirdsMajority():
// Make the commit from LastCommit
commit = cs.LastCommit.MakeCommit()
commit.AggregateSignatures()
default: // This shouldn't happen.
cs.Logger.Error("enterPropose: Cannot propose anything: No commit for the previous block")
return
Expand Down
45 changes: 37 additions & 8 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,12 @@ func TestCommit(t *testing.T) {
require.NotNil(t, commit.BitArray())
assert.Equal(t, bits.NewBitArray(10).Size(), commit.BitArray().Size())

assert.Equal(t, voteSet.GetByIndex(0), commit.GetByIndex(0))
if len(voteSet.GetByIndex(0).Signature) != bls.SignatureSize {
assert.Equal(t, voteSet.GetByIndex(0), commit.GetByIndex(0))
} else {
assert.NotNil(t, commit.AggregatedSignature)
isEqualVoteWithoutSignature(t, voteSet.GetByIndex(0), commit.GetByIndex(0))
}
}

func TestCommitValidateBasic(t *testing.T) {
Expand Down Expand Up @@ -796,27 +801,51 @@ func TestBlockMaxDataBytesUnknownEvidence(t *testing.T) {
}
}

func isEqualVoteWithoutSignature(t *testing.T, vote1, vote2 *Vote) {
assert.Equal(t, vote1.Type, vote2.Type)
assert.Equal(t, vote1.Height, vote2.Height)
assert.Equal(t, vote1.Round, vote2.Round)
assert.Equal(t, vote1.BlockID, vote2.BlockID)
assert.Equal(t, vote1.Timestamp, vote2.Timestamp)
assert.Equal(t, vote1.ValidatorAddress, vote2.ValidatorAddress)
assert.Equal(t, vote1.ValidatorIndex, vote2.ValidatorIndex)
}

func TestCommitToVoteSet(t *testing.T) {
lastID := makeBlockIDRandom()
h := int64(3)

voteSet, _, valSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
voteSet, _, voterSet, vals := randVoteSet(h-1, 1, PrecommitType, 10, 1)
commit, err := MakeCommit(lastID, h-1, 1, voteSet, vals, time.Now())
assert.NoError(t, err)

chainID := voteSet.ChainID()
voteSet2 := CommitToVoteSet(chainID, commit, valSet)
voteSet2 := CommitToVoteSet(chainID, commit, voterSet)

for i := 0; i < len(vals); i++ {
// This is the vote before `MakeCommit`.
vote1 := voteSet.GetByIndex(i)
// This is the vote created from `CommitToVoteSet`
vote2 := voteSet2.GetByIndex(i)
// This is the vote created from `MakeCommit`
vote3 := commit.GetVote(i)

vote1bz := cdc.MustMarshalBinaryBare(vote1)
vote2bz := cdc.MustMarshalBinaryBare(vote2)
vote3bz := cdc.MustMarshalBinaryBare(vote3)
assert.Equal(t, vote1bz, vote2bz)
assert.Equal(t, vote1bz, vote3bz)
if len(vote1.Signature) != bls.SignatureSize {
vote1bz := cdc.MustMarshalBinaryBare(vote1)
vote2bz := cdc.MustMarshalBinaryBare(vote2)
vote3bz := cdc.MustMarshalBinaryBare(vote3)
assert.Equal(t, vote1bz, vote2bz)
assert.Equal(t, vote1bz, vote3bz)
} else {
vote2bz := cdc.MustMarshalBinaryBare(vote2)
vote3bz := cdc.MustMarshalBinaryBare(vote3)
assert.Equal(t, vote2bz, vote3bz)
assert.NotNil(t, commit.AggregatedSignature)
assert.Nil(t, vote2.Signature)
assert.Nil(t, vote3.Signature)
isEqualVoteWithoutSignature(t, vote1, vote2)
isEqualVoteWithoutSignature(t, vote1, vote3)
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion types/vote_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,19 @@ func (voteSet *VoteSet) MakeCommit() *Commit {
commitSigs := make([]CommitSig, len(voteSet.votes))
for i, v := range voteSet.votes {
commitSig := v.CommitSig()
if !commitSig.Absent() && commitSig.Signature == nil {
panic(fmt.Sprintf("This signature of commitSig %v is already aggregated ", commitSig))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to add a test case for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I forgot it. I will add the test case.

Copy link
Member

@tnasu tnasu Apr 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I think that this error message is difficult to read when commitSig is so big.

I prefer to put the variables to the last.
"This signature of commitSig is already aggregated: commitSig: <%v>"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I see. That's right. I will fix it.

}
// if block ID exists but doesn't match, exclude sig
if commitSig.ForBlock() && !v.BlockID.Equals(*voteSet.maj23) {
commitSig = NewCommitSigAbsent()
}
commitSigs[i] = commitSig
}
newCommit := NewCommit(voteSet.GetHeight(), voteSet.GetRound(), *voteSet.maj23, commitSigs)
newCommit.AggregateSignatures()

return NewCommit(voteSet.GetHeight(), voteSet.GetRound(), *voteSet.maj23, commitSigs)
return newCommit
}

//--------------------------------------------------------------------------------
Expand Down
1 change: 0 additions & 1 deletion types/vote_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,6 @@ func TestMakeCommit(t *testing.T) {
}

commit := voteSet.MakeCommit()
commit.AggregateSignatures()

assert.Equal(t, height, commit.Height)
assert.Equal(t, round, commit.Round)
Expand Down