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

protocols/horizon/effects: Add UnmarshalJSON to SequenceBumped. #1909

Merged
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
123 changes: 120 additions & 3 deletions clients/horizonclient/effect_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, 2)
}

hmock.On(
Expand All @@ -242,7 +242,48 @@ 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)
}
}

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)

})
}
}

Expand Down Expand Up @@ -304,11 +345,87 @@ var firstEffectsPage = `{
"weight": 1,
"public_key": "GCDIZFWLOTBWHTPODXCBH6XNXPFMSQFRVIDRP3JLEKQZN66G7NF3ANOD",
"key": ""
}
}
]
}
}`

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": {
Expand Down
24 changes: 24 additions & 0 deletions protocols/horizon/effects/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,30 @@ 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 temp struct {
NewSeq json.Number `json:"new_seq"`
}

if err := json.Unmarshal(data, &effect.Base); err != nil {
return err
}

if err := json.Unmarshal(data, &temp); err != nil {
return err
}

newSeq, err := temp.NewSeq.Int64()
if err != nil {
return err
}
effect.NewSeq = newSeq

return nil
}

type SignerCreated struct {
Base
Weight int32 `json:"weight"`
Expand Down