Skip to content

Commit

Permalink
sort JSON after marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Sep 17, 2024
1 parent c9f0e2e commit c2f94a3
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion tests/integration/tx/aminojson/aminojson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aminojson

import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -214,7 +215,7 @@ func TestAminoJSON_LegacyParity(t *testing.T) {
vesting.AppModule{}, gov.AppModule{})
legacytx.RegressionTestingAminoCodec = encCfg.Amino

aj := aminojson.NewEncoder(aminojson.EncoderOptions{DoNotSortFields: true})
aj := aminojson.NewEncoder(aminojson.EncoderOptions{})
addr1 := types.AccAddress("addr1")
now := time.Now()

Expand Down Expand Up @@ -393,14 +394,18 @@ func TestAminoJSON_LegacyParity(t *testing.T) {
},
"staking/stake_authorization_allow": {
gogo: &stakingtypes.StakeAuthorization{
MaxTokens: &types.Coin{Denom: "foo", Amount: math.NewInt(123)},
Validators: &stakingtypes.StakeAuthorization_AllowList{
AllowList: &stakingtypes.StakeAuthorization_Validators{Address: []string{"foo"}},
},
AuthorizationType: stakingtypes.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE,
},
pulsar: &stakingapi.StakeAuthorization{
MaxTokens: &v1beta1.Coin{Denom: "foo", Amount: "123"},
Validators: &stakingapi.StakeAuthorization_AllowList{
AllowList: &stakingapi.StakeAuthorization_Validators{Address: []string{"foo"}},
},
AuthorizationType: stakingapi.AuthorizationType_AUTHORIZATION_TYPE_DELEGATE,
},
},
"vesting/base_account_empty": {
Expand Down Expand Up @@ -432,6 +437,8 @@ func TestAminoJSON_LegacyParity(t *testing.T) {
t.Run(name, func(t *testing.T) {
gogoBytes, err := encCfg.Amino.MarshalJSON(tc.gogo)
require.NoError(t, err)
gogoBytes, err = sortJson(gogoBytes)
require.NoError(t, err)

pulsarBytes, err := aj.Marshal(tc.pulsar)
if tc.pulsarMarshalFails {
Expand Down Expand Up @@ -459,6 +466,8 @@ func TestAminoJSON_LegacyParity(t *testing.T) {

newGogoBytes, err := encCfg.Amino.MarshalJSON(newGogo)
require.NoError(t, err)
newGogoBytes, err = sortJson(newGogoBytes)
require.NoError(t, err)
if tc.roundTripUnequal {
require.NotEqual(t, string(gogoBytes), string(newGogoBytes))
return
Expand Down Expand Up @@ -598,3 +607,20 @@ func postFixPulsarMessage(msg proto.Message) {
}
}
}

// sortJson sorts the JSON bytes by way of the side effect of unmarshalling and remarshalling the JSON
// using encoding/json. This hacky way of sorting JSON fields was used by the legacy amino JSON encoding in
// x/auth/migrations/legacytx.StdSignBytes. It is used here ensure the x/tx JSON encoding is equivalent to
// the legacy amino JSON encoding.
func sortJson(bz []byte) ([]byte, error) {
var c any
err := json.Unmarshal(bz, &c)
if err != nil {
return nil, err
}
js, err := json.Marshal(c)
if err != nil {
return nil, err
}
return js, nil
}

0 comments on commit c2f94a3

Please sign in to comment.