evidence: use bytes instead of quantity to limit size (#5449)(#5476)

This commit is contained in:
Callum Waters
2020-10-08 14:38:11 +02:00
committed by GitHub
parent dac18d73a7
commit 7d5d417dc9
26 changed files with 227 additions and 274 deletions

View File

@@ -273,12 +273,12 @@ func BlockFromProto(bp *tmproto.Block) (*Block, 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, evidenceBytes int64, valsCount int) int64 {
maxDataBytes := maxBytes -
MaxOverheadForBlock -
MaxHeaderBytes -
int64(valsCount)*MaxVoteBytes -
int64(evidenceCount)*MaxEvidenceBytes
evidenceBytes
if maxDataBytes < 0 {
panic(fmt.Sprintf(
@@ -292,18 +292,16 @@ func MaxDataBytes(maxBytes int64, valsCount, evidenceCount int) int64 {
}
// MaxDataBytesUnknownEvidence returns the maximum size of block's data when
// 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 MaxDataBytesUnknownEvidence(maxBytes int64, valsCount int, maxNumEvidence uint32) int64 {
maxEvidenceBytes := int64(maxNumEvidence) * MaxEvidenceBytes
func MaxDataBytesNoEvidence(maxBytes int64, valsCount int) int64 {
maxDataBytes := maxBytes -
MaxOverheadForBlock -
MaxHeaderBytes -
int64(valsCount)*MaxVoteBytes -
maxEvidenceBytes
int64(valsCount)*MaxVoteBytes
if maxDataBytes < 0 {
panic(fmt.Sprintf(
@@ -1073,8 +1071,9 @@ func DataFromProto(dp *tmproto.Data) (Data, error) {
type EvidenceData struct {
Evidence EvidenceList `json:"evidence"`
// Volatile
hash tmbytes.HexBytes
// Volatile. Used as cache
hash tmbytes.HexBytes
byteSize int64
}
// Hash returns the hash of the data.
@@ -1085,6 +1084,20 @@ func (data *EvidenceData) Hash() tmbytes.HexBytes {
return data.hash
}
// ByteSize returns the total byte size of all the evidence
func (data *EvidenceData) ByteSize() int64 {
if data.byteSize == 0 && len(data.Evidence) != 0 {
for _, ev := range data.Evidence {
pb, err := EvidenceToProto(ev)
if err != nil {
panic(err)
}
data.byteSize += int64(pb.Size())
}
}
return data.byteSize
}
// StringIndented returns a string representation of the evidence.
func (data *EvidenceData) StringIndented(indent string) string {
if data == nil {
@@ -1142,11 +1155,10 @@ func (data *EvidenceData) FromProto(eviData *tmproto.EvidenceData) error {
return err
}
eviBzs[i] = evi
data.byteSize += int64(eviData.Evidence[i].Size())
}
data.Evidence = eviBzs
data.hash = eviData.GetHash()
return nil
}