From a528907b0810d033e31996871296fcb5dedca31a Mon Sep 17 00:00:00 2001 From: Matthew Slipper Date: Sun, 10 Mar 2019 17:49:44 -0700 Subject: [PATCH 1/3] Serialize Uint64 to string in JSON Closes #1924. --- types/uint64.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/types/uint64.go b/types/uint64.go index dc76b0fe9c..48927b08b6 100644 --- a/types/uint64.go +++ b/types/uint64.go @@ -1,12 +1,11 @@ package types import ( - "encoding/base64" - "strings" - "gx/ipfs/QmSKyB5faguXT4NqbrXpnRXqaVj5DhSm7x9BtzFydBY1UK/go-leb128" cbor "gx/ipfs/QmcZLyosDwMKdB6NLRsiss9HXzDPhVhhRtPy67JFKTDQDX/go-ipld-cbor" "gx/ipfs/QmdBzoMxsBpojBfN1cv5GnKtB7sfYBMoLH7p9qSyEVYXcu/refmt/obj/atlas" + "strconv" + "strings" ) func init() { @@ -29,17 +28,16 @@ type Uint64 uint64 // MarshalJSON converts a Uint64 to a json string and returns it. func (u Uint64) MarshalJSON() ([]byte, error) { - encoded := base64.StdEncoding.EncodeToString(leb128.FromUInt64(uint64(u))) - return []byte(`"` + encoded + `"`), nil + return []byte(`"` + strconv.FormatUint(uint64(u), 10) + `"`), nil } // UnmarshalJSON converts a json string to a Uint64. func (u *Uint64) UnmarshalJSON(b []byte) error { - jd, err := base64.StdEncoding.DecodeString(strings.Trim(string(b), `"`)) + val, err := strconv.ParseUint(strings.Trim(string(b), `"`), 10, 64) if err != nil { return err } - *u = Uint64(leb128.ToUInt64(jd)) + *u = Uint64(val) return nil } From bf8a5ce56d1edaef5f488118594d5e2c82f50660 Mon Sep 17 00:00:00 2001 From: Matthew Slipper Date: Tue, 12 Mar 2019 19:50:25 -0700 Subject: [PATCH 2/3] Code review updates; add test --- commands/chain_daemon_test.go | 14 ++++++++++++++ types/uint64.go | 5 +++-- types/uint64_test.go | 3 ++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/commands/chain_daemon_test.go b/commands/chain_daemon_test.go index 586f9f34ab..9fada71397 100644 --- a/commands/chain_daemon_test.go +++ b/commands/chain_daemon_test.go @@ -110,4 +110,18 @@ func TestChainDaemon(t *testing.T) { assert.Contains(chainLsResult, "1") assert.Contains(chainLsResult, "0") }) + + t.Run("chain ls --long with JSON encoding returns integer string block height and nonce", func(t *testing.T) { + t.Parallel() + assert := assert.New(t) + + daemon := th.NewDaemon(t, th.WithMiner(fixtures.TestMiners[0])).Start() + defer daemon.ShutdownSuccess() + + daemon.RunSuccess("mining", "once", "--enc", "text") + chainLsResult := daemon.RunSuccess("chain", "ls", "--long", "--enc", "json").ReadStdoutTrimNewlines() + assert.Contains(chainLsResult, `"height":"0"`) + assert.Contains(chainLsResult, `"height":"1"`) + assert.Contains(chainLsResult, `"nonce":"0"`) + }) } diff --git a/types/uint64.go b/types/uint64.go index 48927b08b6..5f5ae70eca 100644 --- a/types/uint64.go +++ b/types/uint64.go @@ -1,11 +1,12 @@ package types import ( + "strconv" + "strings" + "gx/ipfs/QmSKyB5faguXT4NqbrXpnRXqaVj5DhSm7x9BtzFydBY1UK/go-leb128" cbor "gx/ipfs/QmcZLyosDwMKdB6NLRsiss9HXzDPhVhhRtPy67JFKTDQDX/go-ipld-cbor" "gx/ipfs/QmdBzoMxsBpojBfN1cv5GnKtB7sfYBMoLH7p9qSyEVYXcu/refmt/obj/atlas" - "strconv" - "strings" ) func init() { diff --git a/types/uint64_test.go b/types/uint64_test.go index 57d4e1be54..1748ef61db 100644 --- a/types/uint64_test.go +++ b/types/uint64_test.go @@ -27,8 +27,9 @@ func TestUint64Json(t *testing.T) { v := Uint64(64) m, err := json.Marshal(v) assert.NoError(err) + assert.Equal(`"64"`, string(m)) var got Uint64 err = json.Unmarshal(m, &got) assert.NoError(err) assert.Equal(v, got) -} +} \ No newline at end of file From 7f3151b3f5e489901a3f246bde8d565b3f86dbab Mon Sep 17 00:00:00 2001 From: Matthew Slipper Date: Tue, 12 Mar 2019 20:17:24 -0700 Subject: [PATCH 3/3] fix lint --- types/uint64_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/uint64_test.go b/types/uint64_test.go index 1748ef61db..2dd7963ca4 100644 --- a/types/uint64_test.go +++ b/types/uint64_test.go @@ -32,4 +32,4 @@ func TestUint64Json(t *testing.T) { err = json.Unmarshal(m, &got) assert.NoError(err) assert.Equal(v, got) -} \ No newline at end of file +}