Skip to content

Commit

Permalink
Merge branch 'feature/bls_aggregation_and_verification' of github.com…
Browse files Browse the repository at this point in the history
…:line/tendermint into feature/bls_aggregation_and_verification
  • Loading branch information
torao committed Dec 3, 2020
2 parents bcd91a1 + a7be6d3 commit f902ba5
Show file tree
Hide file tree
Showing 20 changed files with 864 additions and 408 deletions.
4 changes: 2 additions & 2 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestNodeSetPrivValTCP(t *testing.T) {
signerServer := privval.NewSignerServer(
dialerEndpoint,
config.ChainID(),
types.NewMockPV(),
types.NewMockPV(types.PrivKeyEd25519),
)

go func() {
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestNodeSetPrivValIPC(t *testing.T) {
pvsc := privval.NewSignerServer(
dialerEndpoint,
config.ChainID(),
types.NewMockPV(),
types.NewMockPV(types.PrivKeyEd25519),
)

go func() {
Expand Down
35 changes: 14 additions & 21 deletions privval/signer_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type signerTestCase struct {
signerServer *SignerServer
}

func getSignerTestCases(t *testing.T, start bool) []signerTestCase {
func getSignerTestCases(t *testing.T) []signerTestCase {
testCases := make([]signerTestCase, 0)

// Get test cases for each possible dialer (DialTCP / DialUnix / etc)
Expand All @@ -35,10 +35,8 @@ func getSignerTestCases(t *testing.T, start bool) []signerTestCase {
require.NoError(t, err)
ss := NewSignerServer(sd, chainID, mockPV)

if start {
err = ss.Start()
require.NoError(t, err)
}
err = ss.Start()
require.NoError(t, err)

tc := signerTestCase{
chainID: chainID,
Expand All @@ -54,7 +52,7 @@ func getSignerTestCases(t *testing.T, start bool) []signerTestCase {
}

func TestSignerClose(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
for _, tc := range getSignerTestCases(t) {
err := tc.signerClient.Close()
assert.NoError(t, err)

Expand All @@ -64,7 +62,7 @@ func TestSignerClose(t *testing.T) {
}

func TestSignerPing(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
for _, tc := range getSignerTestCases(t) {
defer tc.signerServer.Stop()
defer tc.signerClient.Close()

Expand All @@ -74,7 +72,7 @@ func TestSignerPing(t *testing.T) {
}

func TestSignerGetPubKey(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
for _, tc := range getSignerTestCases(t) {
defer tc.signerServer.Stop()
defer tc.signerClient.Close()

Expand All @@ -96,7 +94,7 @@ func TestSignerGetPubKey(t *testing.T) {
}

func TestSignerProposal(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
for _, tc := range getSignerTestCases(t) {
ts := time.Now()
want := &types.Proposal{Timestamp: ts}
have := &types.Proposal{Timestamp: ts}
Expand All @@ -113,7 +111,7 @@ func TestSignerProposal(t *testing.T) {

func TestSignerGenerateVRFProof(t *testing.T) {
message := []byte("hello, world")
for _, tc := range getSignerTestCases(t, true) {
for _, tc := range getSignerTestCases(t) {
defer tc.signerServer.Stop()
defer tc.signerClient.Close()

Expand All @@ -127,7 +125,7 @@ func TestSignerGenerateVRFProof(t *testing.T) {
}

func TestSignerVote(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
for _, tc := range getSignerTestCases(t) {
ts := time.Now()
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
have := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
Expand All @@ -143,7 +141,7 @@ func TestSignerVote(t *testing.T) {
}

func TestSignerVoteResetDeadline(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
for _, tc := range getSignerTestCases(t) {
ts := time.Now()
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
have := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
Expand All @@ -169,7 +167,7 @@ func TestSignerVoteResetDeadline(t *testing.T) {
}

func TestSignerVoteKeepAlive(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
for _, tc := range getSignerTestCases(t) {
ts := time.Now()
want := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
have := &types.Vote{Timestamp: ts, Type: types.PrecommitType}
Expand All @@ -194,7 +192,7 @@ func TestSignerVoteKeepAlive(t *testing.T) {
}

func TestSignerSignProposalErrors(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
for _, tc := range getSignerTestCases(t) {
// Replace service with a mock that always fails
tc.signerServer.privVal = types.NewErroringMockPV()
tc.mockPV = types.NewErroringMockPV()
Expand All @@ -216,7 +214,7 @@ func TestSignerSignProposalErrors(t *testing.T) {
}

func TestSignerSignVoteErrors(t *testing.T) {
for _, tc := range getSignerTestCases(t, true) {
for _, tc := range getSignerTestCases(t) {
ts := time.Now()
vote := &types.Vote{Timestamp: ts, Type: types.PrecommitType}

Expand Down Expand Up @@ -263,13 +261,8 @@ func brokenHandler(privVal types.PrivValidator, request SignerMessage, chainID s
}

func TestSignerUnexpectedResponse(t *testing.T) {
for _, tc := range getSignerTestCases(t, false) {
// this should be executed before SignerServer starts to avoid race condition
tc.signerServer.privVal = types.NewMockPV()
tc.mockPV = types.NewMockPV()

for _, tc := range getSignerTestCases(t) {
tc.signerServer.SetRequestHandler(brokenHandler)
tc.signerServer.Start()

defer tc.signerServer.Stop()
defer tc.signerClient.Close()
Expand Down
4 changes: 2 additions & 2 deletions privval/signer_listener_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestSignerRemoteRetryTCPOnly(t *testing.T) {
SignerDialerEndpointConnRetries(retries)(dialerEndpoint)

chainID := tmrand.Str(12)
mockPV := types.NewMockPV()
mockPV := types.NewMockPV(types.PrivKeyEd25519)
signerServer := NewSignerServer(dialerEndpoint, chainID, mockPV)

err = signerServer.Start()
Expand All @@ -88,7 +88,7 @@ func TestRetryConnToRemoteSigner(t *testing.T) {
var (
logger = log.TestingLogger()
chainID = tmrand.Str(12)
mockPV = types.NewMockPV()
mockPV = types.NewMockPV(types.PrivKeyEd25519)
endpointIsOpenCh = make(chan struct{})
thisConnTimeout = testTimeoutReadWrite
listenerEndpoint = newSignerListenerEndpoint(logger, tc.addr, thisConnTimeout)
Expand Down
2 changes: 1 addition & 1 deletion state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
evidence := blockExec.evpool.PendingEvidence(maxNumEvidence)

// Fetch a limited amount of valid txs
maxDataBytes := types.MaxDataBytes(maxBytes, state.Voters.Size(), len(evidence))
maxDataBytes := types.MaxDataBytes(maxBytes, commit, evidence)
txs := blockExec.mempool.ReapMaxBytesMaxGas(maxDataBytes, maxGas)

return state.MakeBlock(height, txs, commit, evidence, proposerAddr, round, proof)
Expand Down
2 changes: 1 addition & 1 deletion state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ func TestProposerFrequency(t *testing.T) {
// make sure votePower > 0
votePower := int64(tmrand.Int()%maxPower) + 1
totalVotePower += votePower
privVal := types.NewMockPV()
privVal := types.NewMockPV(types.PrivKeyEd25519)
pubKey, err := privVal.GetPubKey()
require.NoError(t, err)
val := types.NewValidator(pubKey, votePower)
Expand Down
2 changes: 1 addition & 1 deletion state/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestValidateBlockCommit(t *testing.T) {
)
lastCommit := types.NewCommit(0, 0, types.BlockID{}, nil)
wrongSigsCommit := types.NewCommit(1, 0, types.BlockID{}, nil)
badPrivVal := types.NewMockPV()
badPrivVal := types.NewMockPV(types.PrivKeyEd25519)

for height := int64(1); height < validationTestsStopHeight; height++ {
proposerAddr := state.Validators.SelectProposer([]byte{}, height, 0).Address
Expand Down
59 changes: 54 additions & 5 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/bls"
"github.com/tendermint/tendermint/crypto/composite"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/merkle"
"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/libs/bits"
Expand Down Expand Up @@ -232,12 +233,16 @@ func (b *Block) Unmarshal(bs []byte) error {
// MaxDataBytes returns the maximum size of block's data.
//
// XXX: Panics on negative result.
func MaxDataBytes(maxBytes int64, valsCount, evidenceCount int) int64 {
func MaxDataBytes(maxBytes int64, commit *Commit, evidences []Evidence) int64 {
evidenceBytes := int64(0)
for _, ev := range evidences {
evidenceBytes += MaxEvidenceBytes(PrivKeyTypeByPubKey(ev.PublicKey()))
}
maxDataBytes := maxBytes -
MaxAminoOverheadForBlock -
MaxHeaderBytes -
int64(valsCount)*MaxVoteBytes -
int64(evidenceCount)*MaxEvidenceBytes
commit.MaxCommitBytes() -
evidenceBytes

if maxDataBytes < 0 {
panic(fmt.Sprintf(
Expand All @@ -248,7 +253,6 @@ func MaxDataBytes(maxBytes int64, valsCount, evidenceCount int) int64 {
}

return maxDataBytes

}

// MaxDataBytesUnknownEvidence returns the maximum size of block's data when
Expand All @@ -261,7 +265,7 @@ func MaxDataBytesUnknownEvidence(maxBytes int64, valsCount int) int64 {
maxDataBytes := maxBytes -
MaxAminoOverheadForBlock -
MaxHeaderBytes -
int64(valsCount)*MaxVoteBytes -
int64(valsCount)*MaxCommitBytes -
maxEvidenceBytes

if maxDataBytes < 0 {
Expand Down Expand Up @@ -558,6 +562,15 @@ type CommitSig struct {
Signature []byte `json:"signature"`
}

const (
MaxCommitBytes = 255
BlockIDFlagLen = 4
TimestampMaxLen = 18
Bytes20AminoHeadLen = 2
Bytes64AminoHeadLen = 2
Bytes96AminoHeadLen = 3
)

// NewCommitSigForBlock returns new CommitSig with BlockIDFlagCommit.
func NewCommitSigForBlock(signature []byte, valAddr Address, ts time.Time) CommitSig {
return CommitSig{
Expand All @@ -568,6 +581,19 @@ func NewCommitSigForBlock(signature []byte, valAddr Address, ts time.Time) Commi
}
}

func (cs CommitSig) MaxCommitSigBytes() int64 {
commitSigBytesBase := BlockIDFlagLen + TimestampMaxLen + Bytes20AminoHeadLen + int64(len(cs.ValidatorAddress.Bytes()))
switch len(cs.Signature) {
case 0:
return commitSigBytesBase
case ed25519.SignatureSize:
return commitSigBytesBase + Bytes64AminoHeadLen + int64(len(cs.Signature))
case bls.SignatureSize:
return commitSigBytesBase + Bytes96AminoHeadLen + int64(len(cs.Signature))
}
panic(fmt.Sprintf("unknown signature size"))
}

// ForBlock returns true if CommitSig is for the block.
func (cs CommitSig) ForBlock() bool {
return cs.BlockIDFlag == BlockIDFlagCommit
Expand Down Expand Up @@ -708,6 +734,29 @@ func NewCommit(height int64, round int, blockID BlockID, commitSigs []CommitSig)
}
}

const (
CommitHeighMaxtLen = 11
CommitRoundMaxLen = 6
CommitBlockIDMaxLen = 77
CommitAminoOverhead = 1
CommitAggrSigOverhead = 2
)

func (commit *Commit) MaxCommitBytes() int64 {
sigBytes := int64(0)
for _, s := range commit.Signatures {
sigBytes += CommitAminoOverhead + s.MaxCommitSigBytes()
}
if sigBytes > 0 {
sigBytes += CommitAminoOverhead
}
bytesLen := CommitHeighMaxtLen + CommitRoundMaxLen + CommitAminoOverhead + CommitBlockIDMaxLen + sigBytes
if len(commit.AggregatedSignature) > 0 {
bytesLen += CommitAggrSigOverhead + int64(len(commit.AggregatedSignature))
}
return bytesLen
}

// 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().
Expand Down
Loading

0 comments on commit f902ba5

Please sign in to comment.