From 8dec252e24b3710e3a88f6108c080756d7ac539c Mon Sep 17 00:00:00 2001 From: Adolfo Builes Date: Mon, 11 Nov 2019 15:13:16 -0500 Subject: [PATCH 1/4] Add UnmarshalJSON to SequenceBumped. --- clients/horizonclient/effect_request_test.go | 44 +++++++++++++++++++- protocols/horizon/effects/main.go | 37 ++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/clients/horizonclient/effect_request_test.go b/clients/horizonclient/effect_request_test.go index eb5640182c..715d29c213 100644 --- a/clients/horizonclient/effect_request_test.go +++ b/clients/horizonclient/effect_request_test.go @@ -232,7 +232,7 @@ func TestNextEffectsPage(t *testing.T) { efp, err := client.Effects(effectRequest) if assert.NoError(t, err) { - assert.Equal(t, len(efp.Embedded.Records), 2) + assert.Len(t, efp.Embedded.Records, 4) } hmock.On( @@ -242,7 +242,7 @@ func TestNextEffectsPage(t *testing.T) { nextPage, err := client.NextEffectsPage(efp) if assert.NoError(t, err) { - assert.Equal(t, len(nextPage.Embedded.Records), 0) + assert.Len(t, nextPage.Embedded.Records, 0) } } @@ -304,6 +304,46 @@ var firstEffectsPage = `{ "weight": 1, "public_key": "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD", "key": "" + }, + { + "_links": { + "operation": { + "href": "https://horizon-testnet.stellar.org/operations/249108107265" + }, + "succeeds": { + "href": "https://horizon-testnet.stellar.org/effects?order=desc\u0026cursor=249108107265-1" + }, + "precedes": { + "href": "https://horizon-testnet.stellar.org/effects?order=asc\u0026cursor=249108107265-1" + } + }, + "id": "0000000249108107265-0000000001", + "paging_token": "249108107265-1", + "account": "GCQZP3IU7XU6EJ63JZXKCQOYT2RNXN3HB5CNHENNUEUHSMA4VUJJJSEN", + "type": "sequence_bumped", + "type_i": 43, + "created_at": "2019-06-03T16:36:24Z", + "new_seq": 300000000000 + }, + { + "_links": { + "operation": { + "href": "https://horizon-testnet.stellar.org/operations/249108107266" + }, + "succeeds": { + "href": "https://horizon-testnet.stellar.org/effects?order=desc\u0026cursor=249108107266-1" + }, + "precedes": { + "href": "https://horizon-testnet.stellar.org/effects?order=asc\u0026cursor=249108107266-1" + } + }, + "id": "0000000249108107266-0000000001", + "paging_token": "249108107266-1", + "account": "GCQZP3IU7XU6EJ63JZXKCQOYT2RNXN3HB5CNHENNUEUHSMA4VUJJJSEN", + "type": "sequence_bumped", + "type_i": 43, + "created_at": "2019-06-03T16:36:24Z", + "new_seq": "300000000000" } ] } diff --git a/protocols/horizon/effects/main.go b/protocols/horizon/effects/main.go index 2f45418cc5..df6366a6aa 100644 --- a/protocols/horizon/effects/main.go +++ b/protocols/horizon/effects/main.go @@ -2,6 +2,7 @@ package effects import ( "encoding/json" + "strconv" "time" "github.com/stellar/go/protocols/horizon/base" @@ -201,6 +202,42 @@ type SequenceBumped struct { NewSeq int64 `json:"new_seq"` } +// UnmarshalJSON is the custom unmarshal method for SequenceBumped. It allows +// parsing of new_seq as a string or an int64. +func (effect *SequenceBumped) UnmarshalJSON(data []byte) error { + var raw map[string]interface{} + var temp struct { + NewSeq int64 `json:"new_seq"` + } + + if err := json.Unmarshal(data, &raw); err != nil { + return err + } + + if s, ok := raw["new_seq"].(string); ok { + ns, err := strconv.ParseInt(s, 10, 64) + if err != nil { + return err + } + + effect.NewSeq = ns + } else { + // letting Unmarshal handle the convertion so we don't have to convert + // from interface{} to int64 which requires multiple checks. + if err := json.Unmarshal(data, &temp); err != nil { + return err + } + + effect.NewSeq = temp.NewSeq + } + + if err := json.Unmarshal(data, &effect.Base); err != nil { + return err + } + + return nil +} + type SignerCreated struct { Base Weight int32 `json:"weight"` From 3fcfd008a087fe51f4182d7f0ceb25c2661ad643 Mon Sep 17 00:00:00 2001 From: Adolfo Builes Date: Tue, 12 Nov 2019 13:16:20 -0500 Subject: [PATCH 2/4] Update protocols/horizon/effects/main.go Co-Authored-By: Eric Saunders --- protocols/horizon/effects/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/protocols/horizon/effects/main.go b/protocols/horizon/effects/main.go index df6366a6aa..6445f390e7 100644 --- a/protocols/horizon/effects/main.go +++ b/protocols/horizon/effects/main.go @@ -222,7 +222,7 @@ func (effect *SequenceBumped) UnmarshalJSON(data []byte) error { effect.NewSeq = ns } else { - // letting Unmarshal handle the convertion so we don't have to convert + // letting Unmarshal handle the conversion so we don't have to convert // from interface{} to int64 which requires multiple checks. if err := json.Unmarshal(data, &temp); err != nil { return err From 7665d24f01e03b3178c6837ce3bcb80d3cc68a09 Mon Sep 17 00:00:00 2001 From: Adolfo Builes Date: Tue, 12 Nov 2019 13:29:06 -0500 Subject: [PATCH 3/4] Use json.Number. --- protocols/horizon/effects/main.go | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/protocols/horizon/effects/main.go b/protocols/horizon/effects/main.go index 6445f390e7..7bccb9bb2f 100644 --- a/protocols/horizon/effects/main.go +++ b/protocols/horizon/effects/main.go @@ -2,7 +2,6 @@ package effects import ( "encoding/json" - "strconv" "time" "github.com/stellar/go/protocols/horizon/base" @@ -205,35 +204,23 @@ type SequenceBumped struct { // UnmarshalJSON is the custom unmarshal method for SequenceBumped. It allows // parsing of new_seq as a string or an int64. func (effect *SequenceBumped) UnmarshalJSON(data []byte) error { - var raw map[string]interface{} var temp struct { - NewSeq int64 `json:"new_seq"` + NewSeq json.Number `json:"new_seq"` } - if err := json.Unmarshal(data, &raw); err != nil { + if err := json.Unmarshal(data, &effect.Base); err != nil { return err } - if s, ok := raw["new_seq"].(string); ok { - ns, err := strconv.ParseInt(s, 10, 64) - if err != nil { - return err - } - - effect.NewSeq = ns - } else { - // letting Unmarshal handle the conversion so we don't have to convert - // from interface{} to int64 which requires multiple checks. - if err := json.Unmarshal(data, &temp); err != nil { - return err - } - - effect.NewSeq = temp.NewSeq + if err := json.Unmarshal(data, &temp); err != nil { + return err } - if err := json.Unmarshal(data, &effect.Base); err != nil { + newSeq, err := temp.NewSeq.Int64() + if err != nil { return err } + effect.NewSeq = newSeq return nil } From a9e3597181d4156eb38c34f222301eb9394667cc Mon Sep 17 00:00:00 2001 From: Adolfo Builes Date: Tue, 12 Nov 2019 13:40:21 -0500 Subject: [PATCH 4/4] split test for sequence number in both formats. --- clients/horizonclient/effect_request_test.go | 161 ++++++++++++++----- 1 file changed, 119 insertions(+), 42 deletions(-) diff --git a/clients/horizonclient/effect_request_test.go b/clients/horizonclient/effect_request_test.go index 715d29c213..f3f34b6c3c 100644 --- a/clients/horizonclient/effect_request_test.go +++ b/clients/horizonclient/effect_request_test.go @@ -232,7 +232,7 @@ func TestNextEffectsPage(t *testing.T) { efp, err := client.Effects(effectRequest) if assert.NoError(t, err) { - assert.Len(t, efp.Embedded.Records, 4) + assert.Len(t, efp.Embedded.Records, 2) } hmock.On( @@ -246,6 +246,47 @@ func TestNextEffectsPage(t *testing.T) { } } +func TestSequenceBumpedNewSeq(t *testing.T) { + hmock := httptest.NewClient() + client := &Client{ + HorizonURL: "https://localhost/", + HTTP: hmock, + } + effectRequest := EffectRequest{ForAccount: "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD"} + testCases := []struct { + desc string + payload string + }{ + { + desc: "new_seq as a string", + payload: sequenceBumpedAsStringPage, + }, + { + desc: "new_seq as a number", + payload: sequenceBumpedAsNumberPage, + }, + } + for _, tc := range testCases { + t.Run(tc.desc, func(t *testing.T) { + hmock.On( + "GET", + "https://localhost/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects", + ).ReturnString(200, tc.payload) + + efp, err := client.Effects(effectRequest) + + if assert.NoError(t, err) { + assert.Len(t, efp.Embedded.Records, 1) + } + + effect, ok := efp.Embedded.Records[0].(effects.SequenceBumped) + assert.True(t, ok) + assert.Equal(t, int64(300000000000), effect.NewSeq) + + }) + } +} + var effectStreamResponse = `data: {"_links":{"operation":{"href":"https://horizon-testnet.stellar.org/operations/2531135896703017"},"succeeds":{"href":"https://horizon-testnet.stellar.org/effects?order=desc\u0026cursor=2531135896703017-1"},"precedes":{"href":"https://horizon-testnet.stellar.org/effects?order=asc\u0026cursor=2531135896703017-1"}},"id":"0002531135896703017-0000000001","paging_token":"2531135896703017-1","account":"GBNZN27NAOHRJRCMHQF2ZN2F6TAPVEWKJIGZIRNKIADWIS2HDENIS6CI","type":"account_credited","type_i":2,"created_at":"2019-04-03T10:14:17Z","asset_type":"credit_alphanum4","asset_code":"qwop","asset_issuer":"GBM4HXXNDBWWQBXOL4QCTZIUQAP6XFUI3FPINUGUPBMULMTEHJPIKX6T","amount":"0.0460000"} ` @@ -304,51 +345,87 @@ var firstEffectsPage = `{ "weight": 1, "public_key": "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD", "key": "" - }, - { - "_links": { - "operation": { - "href": "https://horizon-testnet.stellar.org/operations/249108107265" - }, - "succeeds": { - "href": "https://horizon-testnet.stellar.org/effects?order=desc\u0026cursor=249108107265-1" - }, - "precedes": { - "href": "https://horizon-testnet.stellar.org/effects?order=asc\u0026cursor=249108107265-1" - } - }, - "id": "0000000249108107265-0000000001", - "paging_token": "249108107265-1", - "account": "GCQZP3IU7XU6EJ63JZXKCQOYT2RNXN3HB5CNHENNUEUHSMA4VUJJJSEN", - "type": "sequence_bumped", - "type_i": 43, - "created_at": "2019-06-03T16:36:24Z", - "new_seq": 300000000000 - }, - { - "_links": { - "operation": { - "href": "https://horizon-testnet.stellar.org/operations/249108107266" - }, - "succeeds": { - "href": "https://horizon-testnet.stellar.org/effects?order=desc\u0026cursor=249108107266-1" - }, - "precedes": { - "href": "https://horizon-testnet.stellar.org/effects?order=asc\u0026cursor=249108107266-1" - } - }, - "id": "0000000249108107266-0000000001", - "paging_token": "249108107266-1", - "account": "GCQZP3IU7XU6EJ63JZXKCQOYT2RNXN3HB5CNHENNUEUHSMA4VUJJJSEN", - "type": "sequence_bumped", - "type_i": 43, - "created_at": "2019-06-03T16:36:24Z", - "new_seq": "300000000000" - } + } ] } }` +var sequenceBumpedAsNumberPage = `{ + "_links": { + "self": { + "href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=&limit=10&order=asc" + }, + "next": { + "href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-3&limit=10&order=asc" + }, + "prev": { + "href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-1&limit=10&order=desc" + } + }, + "_embedded": { + "records": [ + { + "_links": { + "operation": { + "href": "https://horizon-testnet.stellar.org/operations/249108107265" + }, + "succeeds": { + "href": "https://horizon-testnet.stellar.org/effects?order=desc\u0026cursor=249108107265-1" + }, + "precedes": { + "href": "https://horizon-testnet.stellar.org/effects?order=asc\u0026cursor=249108107265-1" + } + }, + "id": "0000000249108107265-0000000001", + "paging_token": "249108107265-1", + "account": "GCQZP3IU7XU6EJ63JZXKCQOYT2RNXN3HB5CNHENNUEUHSMA4VUJJJSEN", + "type": "sequence_bumped", + "type_i": 43, + "created_at": "2019-06-03T16:36:24Z", + "new_seq": 300000000000 + } + ] + } + }` + +var sequenceBumpedAsStringPage = `{ + "_links": { + "self": { + "href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=&limit=10&order=asc" + }, + "next": { + "href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-3&limit=10&order=asc" + }, + "prev": { + "href": "https://horizon-testnet.stellar.org/accounts/GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD/effects?cursor=1557363731492865-1&limit=10&order=desc" + } + }, + "_embedded": { + "records": [ + { + "_links": { + "operation": { + "href": "https://horizon-testnet.stellar.org/operations/249108107265" + }, + "succeeds": { + "href": "https://horizon-testnet.stellar.org/effects?order=desc\u0026cursor=249108107265-1" + }, + "precedes": { + "href": "https://horizon-testnet.stellar.org/effects?order=asc\u0026cursor=249108107265-1" + } + }, + "id": "0000000249108107265-0000000001", + "paging_token": "249108107265-1", + "account": "GCQZP3IU7XU6EJ63JZXKCQOYT2RNXN3HB5CNHENNUEUHSMA4VUJJJSEN", + "type": "sequence_bumped", + "type_i": 43, + "created_at": "2019-06-03T16:36:24Z", + "new_seq": "300000000000" + } + ] + } + }` + var emptyEffectsPage = `{ "_links": { "self": {