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

[R4R] fix issue669 #168

Merged
merged 1 commit into from
Oct 21, 2019
Merged
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
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