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

msgp: Add canonical validation to msgp unmarshalling #5605

Draft
wants to merge 19 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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,072 changes: 3,233 additions & 839 deletions agreement/msgp_gen.go

Large diffs are not rendered by default.

42 changes: 36 additions & 6 deletions agreement/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
"github.com/algorand/go-algorand/data/basics"
)

// These types are defined to satisfy SortInterface used by
// These types are defined to satisfy SortInterface used by msgp

// SortAddress is re-exported from basics.Address since the interface is already defined there
//
//msgp:sort basics.Address SortAddress
//msgp:sort basics.Address SortAddress basics.AddressLess
type SortAddress = basics.SortAddress

// SortUint64 is re-exported from basics since the interface is already defined there
Expand All @@ -36,38 +36,50 @@
// SortStep defines SortInterface used by msgp to consistently sort maps with this type as key.
//
//msgp:ignore SortStep
//msgp:sort step SortStep
//msgp:sort step SortStep StepLess
type SortStep []step

func (a SortStep) Len() int { return len(a) }
func (a SortStep) Less(i, j int) bool { return a[i] < a[j] }
func (a SortStep) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// StepLess is necessary for msgp:sort directive
// which used to generate UnmarshalValidateMsg generators
func StepLess(a, b step) bool { return a < b }

// SortPeriod defines SortInterface used by msgp to consistently sort maps with this type as key.
//
//msgp:ignore SortPeriod
//msgp:sort period SortPeriod
//msgp:sort period SortPeriod PeriodLess
type SortPeriod []period

func (a SortPeriod) Len() int { return len(a) }
func (a SortPeriod) Less(i, j int) bool { return a[i] < a[j] }
func (a SortPeriod) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// PeriodLess is necessary for msgp:sort directive
// which used to generate UnmarshalValidateMsg generators
func PeriodLess(a, b period) bool { return a < b }

// SortRound defines SortInterface used by msgp to consistently sort maps with this type as key.
// note, for type aliases the base type is used for the interface
//
//msgp:ignore SortRound
//msgp:sort basics.Round SortRound
//msgp:sort basics.Round SortRound RoundLess
type SortRound []basics.Round

func (a SortRound) Len() int { return len(a) }
func (a SortRound) Less(i, j int) bool { return a[i] < a[j] }
func (a SortRound) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// RoundLess is necessary for msgp:sort directive
// which used to generate UnmarshalValidateMsg generators
func RoundLess(a, b round) bool { return a < b }

// SortProposalValue defines SortInterface used by msgp to consistently sort maps with this type as key.
//
//msgp:ignore SortProposalValue
//msgp:sort proposalValue SortProposalValue
//msgp:sort proposalValue SortProposalValue ProposalValueLess
type SortProposalValue []proposalValue

func (a SortProposalValue) Len() int { return len(a) }
Expand All @@ -88,3 +100,21 @@
}

func (a SortProposalValue) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// ProposalValueLess is necessary for msgp:sort directive
// which used to generate UnmarshalValidateMsg generators
func ProposalValueLess(a, b proposalValue) bool {
if a.OriginalPeriod != b.OriginalPeriod {
return a.OriginalPeriod < b.OriginalPeriod
}
cmp := bytes.Compare(a.OriginalProposer[:], b.OriginalProposer[:])
if cmp != 0 {
return cmp < 0

Check warning on line 112 in agreement/sort.go

View check run for this annotation

Codecov / codecov/patch

agreement/sort.go#L110-L112

Added lines #L110 - L112 were not covered by tests
}
cmp = bytes.Compare(a.BlockDigest[:], b.BlockDigest[:])
if cmp != 0 {
return cmp < 0

Check warning on line 116 in agreement/sort.go

View check run for this annotation

Codecov / codecov/patch

agreement/sort.go#L114-L116

Added lines #L114 - L116 were not covered by tests
}
cmp = bytes.Compare(a.EncodingDigest[:], b.EncodingDigest[:])
return cmp < 0

Check warning on line 119 in agreement/sort.go

View check run for this annotation

Codecov / codecov/patch

agreement/sort.go#L118-L119

Added lines #L118 - L119 were not covered by tests
}
Loading