Skip to content

Commit

Permalink
review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
iansuvak committed Aug 11, 2023
1 parent d9a6c30 commit 9ffcee7
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 14 deletions.
2 changes: 1 addition & 1 deletion agreement/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (a SortProposalValue) Less(i, j int) bool {

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

// ProposalLess is necessary for msgp:sort directive
// ProposalValueLess is necessary for msgp:sort directive
// which used to generate UnmarshalValidateMsg generators
func ProposalValueLess(a, b proposalValue) bool {
if a.OriginalPeriod != b.OriginalPeriod {
Expand Down
12 changes: 6 additions & 6 deletions crypto/stateproof/structs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ const (
wrongMapOrder
)

// TestDecodeValidate ensures that DecodeValidate detects non-canonical encodings
// TestDecodeCanonicalMsg ensures that DecodeValidate detects non-canonical encodings
// this is tested here because StateProof contains an example of a map and is itself contained within
// Transaction message which is a relevant message to check canonical encoding for.
func TestDecodeValidate(t *testing.T) {
func TestDecodeCanonicalMsg(t *testing.T) {
partitiontest.PartitionTest(t)
t.Parallel()

Expand All @@ -51,25 +51,25 @@ func TestDecodeValidate(t *testing.T) {
b := protocol.Encode(&z)
var zDecode, zDecodeValidate StateProof
require.NoError(t, protocol.Decode(b, &zDecode))
require.NoError(t, protocol.DecodeValidate(b, &zDecodeValidate))
require.NoError(t, protocol.DecodeCanonicalMsg(b, &zDecodeValidate))

zDecode = StateProof{}
zDecodeValidate = StateProof{}
bCorrect := z.marshalWrongOrder(nil, canonical)
require.NoError(t, protocol.Decode(bCorrect, &zDecode))
require.NoError(t, protocol.DecodeValidate(bCorrect, &zDecodeValidate))
require.NoError(t, protocol.DecodeCanonicalMsg(bCorrect, &zDecodeValidate))

zDecode = StateProof{}
zDecodeValidate = StateProof{}
bWrongMapOrder := z.marshalWrongOrder(nil, wrongMapOrder)
require.NoError(t, protocol.Decode(bWrongMapOrder, &zDecode))
require.ErrorContains(t, protocol.DecodeValidate(bWrongMapOrder, &zDecodeValidate), "msgp: non-canonical encoding detected")
require.ErrorContains(t, protocol.DecodeCanonicalMsg(bWrongMapOrder, &zDecodeValidate), "msgp: non-canonical encoding detected")

zDecode = StateProof{}
zDecodeValidate = StateProof{}
bWrongStructOrder := z.marshalWrongOrder(nil, wrongStructOrder)
require.NoError(t, protocol.Decode(bWrongStructOrder, &zDecode))
require.ErrorContains(t, protocol.DecodeValidate(bWrongStructOrder, &zDecodeValidate), "msgp: non-canonical encoding detected")
require.ErrorContains(t, protocol.DecodeCanonicalMsg(bWrongStructOrder, &zDecodeValidate), "msgp: non-canonical encoding detected")

}

Expand Down
4 changes: 0 additions & 4 deletions data/basics/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ func (a SortUint64) Len() int { return len(a) }
func (a SortUint64) Less(i, j int) bool { return a[i] < a[j] }
func (a SortUint64) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

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

// SortAssetIndex implements sorting by AssetIndex keys for
// canonical encoding of maps in msgpack format.
//
Expand Down
4 changes: 2 additions & 2 deletions protocol/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,12 @@ func DecodeMsgp(b []byte, objptr msgp.Unmarshaler) (err error) {
return nil
}

// DecodeValidate attempts to decode a msgpack-encoded byte buffer into
// DecodeCanonicalMsg attempts to decode a msgpack-encoded byte buffer into
// an object instance pointed to by objptr, requiring that we pre-
// generated the code for doing so using msgp. It also validates that
// the decoded object is canonically encoded. It will return an error
// if struct fields or maps are out of canonical order
func DecodeValidate(b []byte, objptr msgp.UnmarshalerValidator) (err error) {
func DecodeCanonicalMsg(b []byte, objptr msgp.UnmarshalerValidator) (err error) {
defer func() {
if x := recover(); x != nil {
err = fmt.Errorf("DecodeMsgp: %v", x)
Expand Down
2 changes: 1 addition & 1 deletion protocol/codec_tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ func EncodingTest(template msgpMarshalUnmarshal) error {

if _, ok := v1.(msgp.UnmarshalerValidator); ok {
vValidate := reflect.New(reflect.TypeOf(template).Elem()).Interface().(msgp.UnmarshalerValidator)
err = (DecodeValidate(ee1, vValidate))
err = (DecodeCanonicalMsg(ee1, vValidate))
if err != nil {
return err
}
Expand Down

0 comments on commit 9ffcee7

Please sign in to comment.