add mechanism to change the number of bytes sent to the application

This commit is contained in:
William Banfield
2022-03-28 12:09:10 -04:00
parent 14f5588ce2
commit 62fab24cdb
10 changed files with 205 additions and 68 deletions
+9 -13
View File
@@ -262,9 +262,8 @@ func BlockFromProto(bp *tmproto.Block) (*Block, error) {
//-----------------------------------------------------------------------------
// MaxDataBytes returns the maximum size of block's data.
//
// XXX: Panics on negative result.
func MaxDataBytes(maxBytes, evidenceBytes int64, valsCount int) int64 {
// Returns an error if the max data is negative.
func MaxDataBytes(maxBytes, evidenceBytes int64, valsCount int) (int64, error) {
maxDataBytes := maxBytes -
MaxOverheadForBlock -
MaxHeaderBytes -
@@ -272,36 +271,33 @@ func MaxDataBytes(maxBytes, evidenceBytes int64, valsCount int) int64 {
evidenceBytes
if maxDataBytes < 0 {
panic(fmt.Sprintf(
return maxDataBytes, fmt.Errorf(
"Negative MaxDataBytes. Block.MaxBytes=%d is too small to accommodate header&lastCommit&evidence=%d",
maxBytes,
-(maxDataBytes - maxBytes),
))
)
}
return maxDataBytes
return maxDataBytes, nil
}
// MaxDataBytesNoEvidence returns the maximum size of block's data when
// evidence count is unknown. MaxEvidencePerBlock will be used for the size
// of evidence.
//
// XXX: Panics on negative result.
func MaxDataBytesNoEvidence(maxBytes int64, valsCount int) int64 {
func MaxDataBytesNoEvidence(maxBytes int64, valsCount int) (int64, error) {
maxDataBytes := maxBytes -
MaxOverheadForBlock -
MaxHeaderBytes -
MaxCommitBytes(valsCount)
if maxDataBytes < 0 {
panic(fmt.Sprintf(
return maxDataBytes, fmt.Errorf(
"Negative MaxDataBytesUnknownEvidence. Block.MaxBytes=%d is too small to accommodate header&lastCommit&evidence=%d",
maxBytes,
-(maxDataBytes - maxBytes),
))
)
}
return maxDataBytes
return maxDataBytes, nil
}
// MakeBlock returns a new block with an empty header, except what can be
+12 -18
View File
@@ -499,7 +499,7 @@ func TestBlockMaxDataBytes(t *testing.T) {
maxBytes int64
valsCount int
evidenceBytes int64
panics bool
errors bool
result int64
}{
0: {-10, 1, 0, true, 0},
@@ -513,15 +513,12 @@ func TestBlockMaxDataBytes(t *testing.T) {
for i, tc := range testCases {
tc := tc
if tc.panics {
assert.Panics(t, func() {
MaxDataBytes(tc.maxBytes, tc.evidenceBytes, tc.valsCount)
}, "#%v", i)
b, err := MaxDataBytes(tc.maxBytes, tc.evidenceBytes, tc.valsCount)
if tc.errors {
require.Error(t, err, "#%v", i)
} else {
assert.Equal(t,
tc.result,
MaxDataBytes(tc.maxBytes, tc.evidenceBytes, tc.valsCount),
"#%v", i)
require.NoError(t, err)
assert.Equal(t, tc.result, b, "#%v", i)
}
}
}
@@ -530,7 +527,7 @@ func TestBlockMaxDataBytesNoEvidence(t *testing.T) {
testCases := []struct {
maxBytes int64
valsCount int
panics bool
errors bool
result int64
}{
0: {-10, 1, true, 0},
@@ -542,15 +539,12 @@ func TestBlockMaxDataBytesNoEvidence(t *testing.T) {
for i, tc := range testCases {
tc := tc
if tc.panics {
assert.Panics(t, func() {
MaxDataBytesNoEvidence(tc.maxBytes, tc.valsCount)
}, "#%v", i)
b, err := MaxDataBytesNoEvidence(tc.maxBytes, tc.valsCount)
if tc.errors {
assert.Error(t, err, "#%v", i)
} else {
assert.Equal(t,
tc.result,
MaxDataBytesNoEvidence(tc.maxBytes, tc.valsCount),
"#%v", i)
require.NoError(t, err)
assert.Equal(t, tc.result, b, "#%v", i)
}
}
}