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

xdr: Add AbsBeforeEpoch to ClaimableBalance:Predicate API resource #4148

Merged
merged 9 commits into from
Dec 20, 2021
3 changes: 1 addition & 2 deletions services/horizon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ file. This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

* Generate Http Status code of 499 for Client Disconnects, should propagate into `horizon_http_requests_duration_seconds_count`
metric key with status=499 label. ([4098](horizon_http_requests_duration_seconds_count))
* Added `absBeforeEpoch` to ClaimableBalance API Resources. It will contain the Unix epoch representation of absolute before date. ([4148](https://github.com/stellar/go/pull/4148))

## v2.12.1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func TestPopulateClaimableBalance(t *testing.T) {
Type: xdr.ClaimableBalanceIdTypeClaimableBalanceIdTypeV0,
V0: &xdr.Hash{1, 2, 3},
}
unconditional := &xdr.ClaimPredicate{
Type: xdr.ClaimPredicateTypeClaimPredicateUnconditional,
}
relBefore := xdr.Int64(12)
absBefore := xdr.Int64(1598440539)

id, err := xdr.MarshalHex(&balanceID)
tt.NoError(err)
claimableBalance := history.ClaimableBalance{
Expand All @@ -32,7 +38,26 @@ func TestPopulateClaimableBalance(t *testing.T) {
{
Destination: "GC3C4AKRBQLHOJ45U4XG35ESVWRDECWO5XLDGYADO6DPR3L7KIDVUMML",
Predicate: xdr.ClaimPredicate{
Type: xdr.ClaimPredicateTypeClaimPredicateUnconditional,
Type: xdr.ClaimPredicateTypeClaimPredicateAnd,
AndPredicates: &[]xdr.ClaimPredicate{
{
Type: xdr.ClaimPredicateTypeClaimPredicateOr,
OrPredicates: &[]xdr.ClaimPredicate{
{
Type: xdr.ClaimPredicateTypeClaimPredicateBeforeRelativeTime,
RelBefore: &relBefore,
},
{
Type: xdr.ClaimPredicateTypeClaimPredicateBeforeAbsoluteTime,
AbsBefore: &absBefore,
},
},
},
{
Type: xdr.ClaimPredicateTypeClaimPredicateNot,
NotPredicate: &unconditional,
},
},
},
},
},
Expand Down Expand Up @@ -74,5 +99,5 @@ func TestPopulateClaimableBalance(t *testing.T) {

predicate, err := json.Marshal(resource.Claimants[0].Predicate)
tt.NoError(err)
tt.JSONEq(`{"unconditional":true}`, string(predicate))
tt.JSONEq(`{"and":[{"or":[{"rel_before":"12"},{"abs_before":"2020-08-26T11:15:39Z","abs_before_epoch":"1598440539"}]},{"not":{"unconditional":true}}]}`, string(predicate))
}
32 changes: 21 additions & 11 deletions xdr/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,22 @@ func (t *iso8601Time) UnmarshalJSON(b []byte) error {
return nil
}

func (t iso8601Time) Epoch() int64 {
sreuland marked this conversation as resolved.
Show resolved Hide resolved
return t.UTC().Unix()
}

func Newiso8601Time(epoch int64) *iso8601Time {
sreuland marked this conversation as resolved.
Show resolved Hide resolved
return &iso8601Time{time.Unix(epoch, 0).UTC()}
}

type claimPredicateJSON struct {
And *[]claimPredicateJSON `json:"and,omitempty"`
Or *[]claimPredicateJSON `json:"or,omitempty"`
Not *claimPredicateJSON `json:"not,omitempty"`
Unconditional bool `json:"unconditional,omitempty"`
AbsBefore *iso8601Time `json:"abs_before,omitempty"`
RelBefore *int64 `json:"rel_before,string,omitempty"`
And *[]claimPredicateJSON `json:"and,omitempty"`
Or *[]claimPredicateJSON `json:"or,omitempty"`
Not *claimPredicateJSON `json:"not,omitempty"`
Unconditional bool `json:"unconditional,omitempty"`
AbsBefore *iso8601Time `json:"abs_before,omitempty"`
AbsBeforeEpoch *int64 `json:"abs_before_epoch,string,omitempty"`
RelBefore *int64 `json:"rel_before,string,omitempty"`
}

func convertPredicatesToXDR(input []claimPredicateJSON) ([]ClaimPredicate, error) {
Expand All @@ -93,7 +102,7 @@ func (c claimPredicateJSON) toXDR() (ClaimPredicate, error) {
result.Type = ClaimPredicateTypeClaimPredicateBeforeRelativeTime
result.RelBefore = &relBefore
case c.AbsBefore != nil:
absBefore := Int64((*c.AbsBefore).UTC().Unix())
absBefore := Int64(c.AbsBefore.UTC().Unix())
result.Type = ClaimPredicateTypeClaimPredicateBeforeAbsoluteTime
result.AbsBefore = &absBefore
case c.Not != nil:
Expand Down Expand Up @@ -152,11 +161,12 @@ func (c ClaimPredicate) toJSON() (claimPredicateJSON, error) {
payload.Not = new(claimPredicateJSON)
*payload.Not, err = c.MustNotPredicate().toJSON()
case ClaimPredicateTypeClaimPredicateBeforeAbsoluteTime:
payload.AbsBefore = new(iso8601Time)
*payload.AbsBefore = iso8601Time{time.Unix(int64(c.MustAbsBefore()), 0).UTC()}
absBeforeEpoch := int64(c.MustAbsBefore())
payload.AbsBefore = Newiso8601Time(absBeforeEpoch)
payload.AbsBeforeEpoch = &absBeforeEpoch
case ClaimPredicateTypeClaimPredicateBeforeRelativeTime:
payload.RelBefore = new(int64)
*payload.RelBefore = int64(c.MustRelBefore())
relBefore := int64(c.MustRelBefore())
payload.RelBefore = &relBefore
default:
err = fmt.Errorf("invalid predicate type: " + c.Type.String())
}
Expand Down
14 changes: 7 additions & 7 deletions xdr/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestClaimPredicateJSON(t *testing.T) {
assert.NoError(t, err)
assert.JSONEq(
t,
`{"and":[{"or":[{"rel_before":"12"},{"abs_before":"2020-08-26T11:15:39Z"}]},{"not":{"unconditional":true}}]}`,
`{"and":[{"or":[{"rel_before":"12"},{"abs_before":"2020-08-26T11:15:39Z","abs_before_epoch":"1598440539"}]},{"not":{"unconditional":true}}]}`,
string(serialized),
)

Expand Down Expand Up @@ -99,28 +99,28 @@ func TestAbsBeforeTimestamps(t *testing.T) {
}{
{
0,
`{"abs_before":"1970-01-01T00:00:00Z"}`,
`{"abs_before":"1970-01-01T00:00:00Z","abs_before_epoch":"0"}`,
},
{
900 * year,
`{"abs_before":"2869-05-27T00:00:00Z"}`,
`{"abs_before":"2869-05-27T00:00:00Z","abs_before_epoch":"28382400000"}`,
},
{
math.MaxInt64,
`{"abs_before":"+292277026596-12-04T15:30:07Z"}`,
`{"abs_before":"+292277026596-12-04T15:30:07Z","abs_before_epoch":"9223372036854775807"}`,
},
{
-10,
`{"abs_before":"1969-12-31T23:59:50Z"}`,
`{"abs_before":"1969-12-31T23:59:50Z","abs_before_epoch":"-10"}`,
},
{
-9000 * year,
`{"abs_before":"-7025-12-23T00:00:00Z"}`,
`{"abs_before":"-7025-12-23T00:00:00Z","abs_before_epoch":"-283824000000"}`,
},
{
math.MinInt64,
// this serialization doesn't make sense but at least it doesn't crash the marshaller
`{"abs_before":"+292277026596-12-04T15:30:08Z"}`,
`{"abs_before":"+292277026596-12-04T15:30:08Z","abs_before_epoch":"-9223372036854775808"}`,
},
} {
xdrSec := Int64(testCase.unix)
Expand Down