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

fix: calculate MaxDataBytes accurately by pv key type #139

Merged
merged 7 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
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
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
60 changes: 55 additions & 5 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"time"

"github.com/pkg/errors"
"github.com/tendermint/tendermint/crypto/bls"
"github.com/tendermint/tendermint/crypto/ed25519"

"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/merkle"
Expand Down Expand Up @@ -230,12 +232,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 @@ -246,7 +252,6 @@ func MaxDataBytes(maxBytes int64, valsCount, evidenceCount int) int64 {
}

return maxDataBytes

}

// MaxDataBytesUnknownEvidence returns the maximum size of block's data when
Expand All @@ -259,7 +264,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 @@ -554,6 +559,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 @@ -564,6 +578,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 @@ -706,6 +733,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