Skip to content

Commit

Permalink
fix issue669
Browse files Browse the repository at this point in the history
  • Loading branch information
ackratos committed Oct 15, 2019
1 parent 0200b6f commit ff06e3a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions types/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "fmt"

var UpgradeMgr = NewUpgradeManager(UpgradeConfig{})

const FixSignBytesOverflow = "FixSignBytesOverflow" // fix json unmarshal overflow when build SignBytes

var MainNetConfig = UpgradeConfig{
HeightMap: map[string]int64{},
}
Expand Down
10 changes: 9 additions & 1 deletion types/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"bytes"
"encoding/json"
"time"

Expand All @@ -15,7 +16,14 @@ import (
// If the passed JSON isn't valid it will return an error.
func SortJSON(toSortJSON []byte) ([]byte, error) {
var c interface{}
err := json.Unmarshal(toSortJSON, &c)
var err error
if !IsUpgrade(FixSignBytesOverflow) {
err = json.Unmarshal(toSortJSON, &c)
} else {
decoder := json.NewDecoder(bytes.NewReader(toSortJSON))
decoder.UseNumber()
err = decoder.Decode(&c)
}
if err != nil {
return nil, err
}
Expand Down
30 changes: 30 additions & 0 deletions types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,36 @@ func TestSortJSON(t *testing.T) {
}
}

func TestSortJSONOverflow(t *testing.T) {
UpgradeMgr.AddUpgradeHeight(FixSignBytesOverflow, 100)
UpgradeMgr.SetHeight(100)

cases := []struct {
unsortedJSON string
want string
wantErr bool
}{
// from the TXSpec:
{unsortedJSON: `{"chain_id":"test-chain-1","sequence":1,"msg_bytes":{"inputs":[{"address":"696E707574","coins":[{"amount":9007199326368011,"denom":"atom"}]}],"outputs":[{"address":"6F7574707574","coins":[{"amount":9007199326368011,"denom":"atom"}]}]},"alt_bytes":null}`,
want: `{"alt_bytes":null,"chain_id":"test-chain-1","msg_bytes":{"inputs":[{"address":"696E707574","coins":[{"amount":9007199326368011,"denom":"atom"}]}],"outputs":[{"address":"6F7574707574","coins":[{"amount":9007199326368011,"denom":"atom"}]}]},"sequence":1}`,
wantErr: false},
}

for tcIndex, tc := range cases {
got, err := SortJSON([]byte(tc.unsortedJSON))
if tc.wantErr {
require.NotNil(t, err, "tc #%d", tcIndex)
require.Panics(t, func() { MustSortJSON([]byte(tc.unsortedJSON)) })
} else {
require.Nil(t, err, "tc #%d, err=%s", tcIndex, err)
require.NotPanics(t, func() { MustSortJSON([]byte(tc.unsortedJSON)) })
require.Equal(t, got, MustSortJSON([]byte(tc.unsortedJSON)))
}

require.Equal(t, string(got), tc.want)
}
}

func TestTimeFormatAndParse(t *testing.T) {
cases := []struct {
RFC3339NanoStr string
Expand Down

0 comments on commit ff06e3a

Please sign in to comment.