Skip to content

Commit

Permalink
Merge pull request #141 from line/feature/bls_voteset_and_commit
Browse files Browse the repository at this point in the history
fix: remove aggregatedSignature from VoteSet and make Commit.Aggregat…
  • Loading branch information
torao authored Nov 27, 2020
2 parents 7ab6dc6 + a5e6f91 commit 5b56290
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 125 deletions.
52 changes: 36 additions & 16 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ jobs:
name: Build
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v2-beta
- name: Set GOBIN
run: |
echo "::add-path::$(go env GOPATH)/bin"
- uses: actions/setup-go@v2
with:
go-version: "^1.15.4"
- uses: actions/checkout@v2
- uses: technote-space/get-diff-action@v4
with:
PATTERNS: |
**/**.go
go.mod
go.sum
- name: install
run: make install install_abci
# Cache bin
Expand All @@ -35,11 +40,16 @@ jobs:
runs-on: ubuntu-latest
needs: Build
steps:
- uses: actions/setup-go@v2-beta
- name: Set GOBIN
run: |
echo "::add-path::$(go env GOPATH)/bin"
- uses: actions/setup-go@v2
with:
go-version: "^1.15.4"
- uses: actions/checkout@v2
- uses: technote-space/get-diff-action@v4
with:
PATTERNS: |
**/**.go
go.mod
go.sum
- uses: actions/cache@v1
with:
path: ~/go/bin
Expand All @@ -52,11 +62,16 @@ jobs:
runs-on: ubuntu-latest
needs: Build
steps:
- uses: actions/setup-go@v2-beta
- name: Set GOBIN
run: |
echo "::add-path::$(go env GOPATH)/bin"
- uses: actions/setup-go@v2
with:
go-version: "^1.15.4"
- uses: actions/checkout@v2
- uses: technote-space/get-diff-action@v4
with:
PATTERNS: |
**/**.go
go.mod
go.sum
- uses: actions/cache@v1
with:
path: ~/go/bin
Expand All @@ -68,11 +83,16 @@ jobs:
runs-on: ubuntu-latest
needs: Build
steps:
- uses: actions/setup-go@v2-beta
- name: Set GOBIN
run: |
echo "::add-path::$(go env GOPATH)/bin"
- uses: actions/setup-go@v2
with:
go-version: "^1.15.4"
- uses: actions/checkout@v2
- uses: technote-space/get-diff-action@v4
with:
PATTERNS: |
**/**.go
go.mod
go.sum
- uses: actions/cache@v1
with:
path: ~/go/bin
Expand Down
1 change: 0 additions & 1 deletion blockchain/v1/reactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func makeVote(
Timestamp: tmtime.Now(),
Type: types.PrecommitType,
BlockID: blockID,
Signature: []byte{},
}

_ = privVal.SignVote(header.ChainID, vote)
Expand Down
1 change: 0 additions & 1 deletion consensus/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ func (vs *validatorStub) signVote(
Timestamp: tmtime.Now(),
Type: voteType,
BlockID: types.BlockID{Hash: hash, PartsHeader: header},
Signature: []byte{},
}

err = vs.PrivValidator.SignVote(config.ChainID(), vote)
Expand Down
1 change: 0 additions & 1 deletion consensus/invalid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func invalidDoPrevoteFunc(t *testing.T, height int64, round int, cs *State, sw *
BlockID: types.BlockID{
Hash: blockHash,
PartsHeader: types.PartSetHeader{Total: 1, Hash: tmrand.Bytes(32)}},
Signature: []byte{},
}
if err = cs.privValidator.SignVote(cs.state.ChainID, precommit); err != nil {
panic(err)
Expand Down
6 changes: 4 additions & 2 deletions consensus/reactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,11 @@ OUTER_LOOP:
// Catchup logic
// If peer is lagging by more than 1, send Commit.
if prs.Height != 0 && rs.Height >= prs.Height+2 {
// Load the block commit for prs.Height,
// Load the seen commit for prs.Height,
// which contains precommit signatures for prs.Height.
commit := conR.conS.blockStore.LoadBlockCommit(prs.Height)
// Originally the block commit was used, but with the addition of the BLS signature-aggregation,
// we use seen commit instead of the block commit because block commit has no no individual signature.
commit := conR.conS.blockStore.LoadSeenCommit(prs.Height)
if ps.PickSendVote(commit) {
logger.Debug("Picked Catchup commit to send", "height", prs.Height)
continue OUTER_LOOP
Expand Down
2 changes: 1 addition & 1 deletion consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,7 @@ 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 Expand Up @@ -2047,7 +2048,6 @@ func (cs *State) signVote(
Timestamp: cs.voteTime(),
Type: msgType,
BlockID: types.BlockID{Hash: hash, PartsHeader: header},
Signature: []byte{},
}

err = cs.privValidator.SignVote(cs.state.ChainID, vote)
Expand Down
1 change: 0 additions & 1 deletion consensus/types/height_vote_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func makeVoteHR(t *testing.T, height int64, round int, privVals []types.PrivVali
Timestamp: tmtime.Now(),
Type: types.PrecommitType,
BlockID: types.BlockID{Hash: []byte("fakehash"), PartsHeader: types.PartSetHeader{}},
Signature: []byte{},
}
chainID := config.ChainID()
err = privVal.SignVote(chainID, vote)
Expand Down
1 change: 0 additions & 1 deletion crypto/ed25519/ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

amino "github.com/tendermint/go-amino"
"golang.org/x/crypto/ed25519"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/tmhash"
)
Expand Down
1 change: 0 additions & 1 deletion lite/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func makeVote(header *types.Header, voterSet *types.VoterSet, key crypto.PrivKey
Timestamp: tmtime.Now(),
Type: types.PrecommitType,
BlockID: blockID,
Signature: []byte{},
}
// Sign it
signBytes := vote.SignBytes(header.ChainID)
Expand Down
1 change: 0 additions & 1 deletion lite2/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func makeVote(header *types.Header, voterIdx int, key crypto.PrivKey, blockID ty
Timestamp: tmtime.Now(),
Type: types.PrecommitType,
BlockID: blockID,
Signature: []byte{},
}
// Sign it
signBytes := vote.SignBytes(header.ChainID)
Expand Down
1 change: 0 additions & 1 deletion privval/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ func newVote(addr types.Address, idx int, height int64, round int, typ byte, blo
Type: types.SignedMsgType(typ),
Timestamp: tmtime.Now(),
BlockID: blockID,
Signature: []byte{},
}
}

Expand Down
16 changes: 8 additions & 8 deletions privval/signer_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ func TestSignerGenerateVRFProof(t *testing.T) {
func TestSignerVote(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
ts := time.Now()
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType, Signature: []byte{}}
have := &types.Vote{Timestamp: ts, Type: types.PrecommitType, Signature: []byte{}}
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
have := &types.Vote{Timestamp: ts, Type: types.PrecommitType}

defer tc.signerServer.Stop()
defer tc.signerClient.Close()
Expand All @@ -145,8 +145,8 @@ func TestSignerVote(t *testing.T) {
func TestSignerVoteResetDeadline(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
ts := time.Now()
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType, Signature: []byte{}}
have := &types.Vote{Timestamp: ts, Type: types.PrecommitType, Signature: []byte{}}
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
have := &types.Vote{Timestamp: ts, Type: types.PrecommitType}

defer tc.signerServer.Stop()
defer tc.signerClient.Close()
Expand All @@ -171,8 +171,8 @@ func TestSignerVoteResetDeadline(t *testing.T) {
func TestSignerVoteKeepAlive(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
ts := time.Now()
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType, Signature: []byte{}}
have := &types.Vote{Timestamp: ts, Type: types.PrecommitType, Signature: []byte{}}
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
have := &types.Vote{Timestamp: ts, Type: types.PrecommitType}

defer tc.signerServer.Stop()
defer tc.signerClient.Close()
Expand Down Expand Up @@ -218,7 +218,7 @@ func TestSignerSignProposalErrors(t *testing.T) {
func TestSignerSignVoteErrors(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
ts := time.Now()
vote := &types.Vote{Timestamp: ts, Type: types.PrecommitType, Signature: []byte{}}
vote := &types.Vote{Timestamp: ts, Type: types.PrecommitType}

// Replace signer service privval with one that always fails
tc.signerServer.privVal = types.NewErroringMockPV()
Expand Down Expand Up @@ -275,7 +275,7 @@ func TestSignerUnexpectedResponse(t *testing.T) {
defer tc.signerClient.Close()

ts := time.Now()
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType, Signature: []byte{}}
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}

e := tc.signerClient.SignVote(tc.chainID, want)
assert.EqualError(t, e, "received unexpected response")
Expand Down
9 changes: 1 addition & 8 deletions rpc/client/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,16 +564,10 @@ func deepcpVote(vote *types.Vote) (res *types.Vote) {
Hash: make([]byte, len(vote.BlockID.Hash)),
PartsHeader: vote.BlockID.PartsHeader,
},
Signature: []byte{},
}
copy(res.ValidatorAddress, vote.ValidatorAddress)
copy(res.BlockID.Hash, vote.BlockID.Hash)
if vote.Signature == nil {
res.Signature = nil
} else if len(vote.Signature) > 0 {
res.Signature = make([]byte, len(vote.Signature))
copy(res.Signature, vote.Signature)
}
copy(res.Signature, vote.Signature)
return
}

Expand Down Expand Up @@ -611,7 +605,6 @@ func makeEvidences(
Hash: tmhash.Sum([]byte("partset")),
},
},
Signature: []byte{},
}

var err error
Expand Down
1 change: 0 additions & 1 deletion state/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ func TestValidateBlockCommit(t *testing.T) {
Timestamp: tmtime.Now(),
Type: types.PrecommitType,
BlockID: blockID,
Signature: []byte{},
}
err = badPrivVal.SignVote(chainID, goodVote)
require.NoError(t, err, "height %d", height)
Expand Down
1 change: 0 additions & 1 deletion tools/tm-signer-harness/internal/test_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,6 @@ func (th *TestHarness) TestSignVote() error {
ValidatorIndex: 0,
ValidatorAddress: tmhash.SumTruncated([]byte("addr")),
Timestamp: time.Now(),
Signature: []byte{},
}
voteBytes := vote.SignBytes(th.chainID)
// sign the vote
Expand Down
40 changes: 28 additions & 12 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,25 +701,21 @@ type Commit struct {

// NewCommit returns a new Commit.
func NewCommit(height int64, round int, blockID BlockID, commitSigs []CommitSig) *Commit {
return NewCommitWithAggregatedSignature(height, round, blockID, commitSigs, nil)
}

// NewCommitWithAggregatedSignature returns a new Commit with .
func NewCommitWithAggregatedSignature(
height int64, round int, blockID BlockID, commitSigs []CommitSig, aggrSig []byte) *Commit {
return &Commit{
Height: height,
Round: round,
BlockID: blockID,
Signatures: commitSigs,
AggregatedSignature: aggrSig,
Height: height,
Round: round,
BlockID: blockID,
Signatures: commitSigs,
}
}

// CommitToVoteSet constructs a VoteSet from the Commit and validator set.
// Panics if signatures from the commit can't be added to the voteset.
// Inverse of VoteSet.MakeCommit().
func CommitToVoteSet(chainID string, commit *Commit, voters *VoterSet) *VoteSet {
if commit.AggregatedSignature != nil {
panic("Aggregated commit cannot make a VoteSet")
}
voteSet := NewVoteSet(chainID, commit.Height, commit.Round, PrecommitType, voters)
for idx, commitSig := range commit.Signatures {
if commitSig.Absent() {
Expand All @@ -730,10 +726,30 @@ func CommitToVoteSet(chainID string, commit *Commit, voters *VoterSet) *VoteSet
panic(fmt.Sprintf("Failed to reconstruct LastCommit: %v", err))
}
}
voteSet.aggregatedSignature = commit.AggregatedSignature
return voteSet
}

func (commit *Commit) AggregateSignatures() {
if commit.AggregatedSignature != nil {
panic("The commit is already aggregated")
}
var err error
for i := 0; i < len(commit.Signatures); i++ {
if !commit.Signatures[i].Absent() && len(commit.Signatures[i].Signature) == bls.SignatureSize {
if commit.AggregatedSignature == nil {
commit.AggregatedSignature = commit.Signatures[i].Signature
} else {
commit.AggregatedSignature, err = bls.AddSignature(commit.AggregatedSignature,
commit.Signatures[i].Signature)
if err != nil {
panic(fmt.Sprintf("fail to aggregate signature: %s\n", err))
}
}
commit.Signatures[i].Signature = nil
}
}
}

// GetVote converts the CommitSig for the given valIdx to a Vote.
// Returns nil if the precommit at valIdx is nil.
// Panics if valIdx >= commit.Size().
Expand Down
17 changes: 1 addition & 16 deletions types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,17 +290,7 @@ func TestCommit(t *testing.T) {
require.NotNil(t, commit.BitArray())
assert.Equal(t, bits.NewBitArray(10).Size(), commit.BitArray().Size())

vote1, vote2 := voteSet.GetByIndex(0), commit.GetByIndex(0)
assert.Equal(t, vote1.BlockID, vote2.BlockID)
assert.Equal(t, vote1.Height, vote2.Height)
assert.Equal(t, vote1.Round, vote2.Round)
assert.Equal(t, vote1.Timestamp, vote2.Timestamp)
assert.Equal(t, vote1.Type, vote2.Type)
assert.Equal(t, vote1.ValidatorAddress, vote2.ValidatorAddress)
assert.Equal(t, vote1.ValidatorIndex, vote2.ValidatorIndex)
assert.NotNil(t, vote1.Signature)
assert.Nil(t, vote2.Signature)
assert.True(t, commit.IsCommit())
assert.Equal(t, voteSet.GetByIndex(0), commit.GetByIndex(0))
}

func TestCommitValidateBasic(t *testing.T) {
Expand Down Expand Up @@ -518,10 +508,6 @@ func TestCommitToVoteSet(t *testing.T) {
chainID := voteSet.ChainID()
voteSet2 := CommitToVoteSet(chainID, commit, valSet)

assert.Nil(t, voteSet.aggregatedSignature)
assert.NotNil(t, commit.AggregatedSignature)
assert.Equal(t, commit.AggregatedSignature, voteSet2.aggregatedSignature)

for i := 0; i < len(vals); i++ {
vote1 := voteSet2.GetByIndex(i)
vote2 := commit.GetVote(i)
Expand Down Expand Up @@ -567,7 +553,6 @@ func TestCommitToVoteSetWithVotesForNilBlock(t *testing.T) {
Type: PrecommitType,
BlockID: tc.blockIDs[n],
Timestamp: tmtime.Now(),
Signature: []byte{},
}

added, err := signAddVote(vals[vi], vote, voteSet)
Expand Down
1 change: 0 additions & 1 deletion types/evidence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func makeVote(
Round: round,
Type: SignedMsgType(step),
BlockID: blockID,
Signature: []byte{},
}
err = val.SignVote(chainID, v)
if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions types/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func MakeCommit(blockID BlockID, height int64, round int,
Type: PrecommitType,
BlockID: blockID,
Timestamp: now,
Signature: []byte{},
}

_, err = signAddVote(validators[i], vote, voteSet)
Expand Down Expand Up @@ -65,7 +64,6 @@ func MakeVote(
Timestamp: now,
Type: PrecommitType,
BlockID: blockID,
Signature: []byte{},
}
if err := privVal.SignVote(chainID, vote); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 5b56290

Please sign in to comment.