Skip to content

Commit

Permalink
Merge pull request #380 from stellar/effects-createdat
Browse files Browse the repository at this point in the history
Add `created_at` property to effect resources
  • Loading branch information
nullstyle authored Mar 28, 2018
2 parents 3a84b17 + e9b4546 commit 50e3c42
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 72 deletions.
34 changes: 31 additions & 3 deletions services/horizon/internal/actions_effects.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package horizon

import (
"errors"
"fmt"
"regexp"

"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/render/hal"
"github.com/stellar/go/services/horizon/internal/render/sse"
"github.com/stellar/go/services/horizon/internal/resource"
"github.com/stellar/go/support/errors"
halRender "github.com/stellar/go/support/render/hal"
)

Expand All @@ -29,6 +30,7 @@ type EffectIndexAction struct {
PagingParams db2.PageQuery
Records []history.Effect
Page hal.Page
Ledgers history.LedgerCache
}

// JSON is a method for actions.JSON
Expand All @@ -38,6 +40,7 @@ func (action *EffectIndexAction) JSON() {
action.loadParams,
action.ValidateCursorWithinHistory,
action.loadRecords,
action.loadLedgers,
action.loadPage,
)

Expand All @@ -56,12 +59,20 @@ func (action *EffectIndexAction) SSE(stream sse.Stream) {

action.Do(
action.loadRecords,
action.loadLedgers,
func() {
stream.SetLimit(int(action.PagingParams.Limit))
records := action.Records[stream.SentCount():]

for _, record := range records {
res, err := resource.NewEffect(action.Ctx, record)
ledger, found := action.Ledgers.Records[record.LedgerSequence()]
if !found {
msg := fmt.Sprintf("could not find ledger data for sequence %d", record.LedgerSequence())
stream.Err(errors.New(msg))
return
}

res, err := resource.NewEffect(action.Ctx, record, ledger)

if err != nil {
stream.Err(action.Err)
Expand All @@ -77,6 +88,15 @@ func (action *EffectIndexAction) SSE(stream sse.Stream) {
)
}

// loadLedgers populates the ledger cache for this action
func (action *EffectIndexAction) loadLedgers() {
for _, eff := range action.Records {
action.Ledgers.Queue(eff.LedgerSequence())
}

action.Err = action.Ledgers.Load(action.HistoryQ())
}

func (action *EffectIndexAction) loadParams() {
action.ValidateCursor()
action.PagingParams = action.GetPageQuery()
Expand Down Expand Up @@ -107,8 +127,16 @@ func (action *EffectIndexAction) loadRecords() {
// loadPage populates action.Page
func (action *EffectIndexAction) loadPage() {
for _, record := range action.Records {

ledger, found := action.Ledgers.Records[record.LedgerSequence()]
if !found {
msg := fmt.Sprintf("could not find ledger data for sequence %d", record.LedgerSequence())
action.Err = errors.New(msg)
return
}

var res hal.Pageable
res, action.Err = resource.NewEffect(action.Ctx, record)
res, action.Err = resource.NewEffect(action.Ctx, record, ledger)
if action.Err != nil {
return
}
Expand Down
149 changes: 88 additions & 61 deletions services/horizon/internal/actions_effects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,96 @@ package horizon
import (
"testing"

"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/resource/effects"
"github.com/stellar/go/services/horizon/internal/test"
)

func TestEffectActions_Index(t *testing.T) {
ht := StartHTTPTest(t, "base")
defer ht.Finish()

w := ht.Get("/effects?limit=20")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(11, w.Body)
}

// test streaming, regression for https://github.com/stellar/go/services/horizon/internal/issues/147
w = ht.Get("/effects?limit=2", test.RequestHelperStreaming)
ht.Assert.Equal(200, w.Code)

// filtered by ledger
w = ht.Get("/ledgers/1/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(0, w.Body)
}

w = ht.Get("/ledgers/2/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(9, w.Body)
}

w = ht.Get("/ledgers/3/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(2, w.Body)
}

// filtered by account
w = ht.Get("/accounts/GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(3, w.Body)
}

w = ht.Get("/accounts/GA5WBPYA5Y4WAEHXWR2UKO2UO4BUGHUQ74EUPKON2QHV4WRHOIRNKKH2/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(2, w.Body)
}

w = ht.Get("/accounts/GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(3, w.Body)
}

// filtered by transaction
w = ht.Get("/transactions/2374e99349b9ef7dba9a5db3339b78fda8f34777b1af33ba468ad5c0df946d4d/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(3, w.Body)
}

// filtered by operation
w = ht.Get("/operations/8589938689/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(3, w.Body)
}

// before history
ht.ReapHistory(1)
w = ht.Get("/effects?order=desc&cursor=8589938689-1")
ht.Assert.Equal(410, w.Code)
ht.Logger.Error(w.Body.String())

t.Run("omnibus test", func(t *testing.T) {

ht := StartHTTPTest(t, "base")
defer ht.Finish()

w := ht.Get("/effects?limit=20")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(11, w.Body)
}

// test streaming, regression for https://github.com/stellar/go/services/horizon/internal/issues/147
w = ht.Get("/effects?limit=2", test.RequestHelperStreaming)
ht.Assert.Equal(200, w.Code)

// filtered by ledger
w = ht.Get("/ledgers/1/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(0, w.Body)
}

w = ht.Get("/ledgers/2/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(9, w.Body)
}

w = ht.Get("/ledgers/3/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(2, w.Body)
}

// filtered by account
w = ht.Get("/accounts/GBRPYHIL2CI3FNQ4BXLFMNDLFJUNPU2HY3ZMFSHONUCEOASW7QC7OX2H/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(3, w.Body)
}

w = ht.Get("/accounts/GA5WBPYA5Y4WAEHXWR2UKO2UO4BUGHUQ74EUPKON2QHV4WRHOIRNKKH2/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(2, w.Body)
}

w = ht.Get("/accounts/GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(3, w.Body)
}

// filtered by transaction
w = ht.Get("/transactions/2374e99349b9ef7dba9a5db3339b78fda8f34777b1af33ba468ad5c0df946d4d/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(3, w.Body)
}

// filtered by operation
w = ht.Get("/operations/8589938689/effects")
if ht.Assert.Equal(200, w.Code) {
ht.Assert.PageOf(3, w.Body)
}

// before history
ht.ReapHistory(1)
w = ht.Get("/effects?order=desc&cursor=8589938689-1")
ht.Assert.Equal(410, w.Code)
ht.Logger.Error(w.Body.String())
})

t.Run("Effect resource props", func(t *testing.T) {
ht := StartHTTPTest(t, "base")
defer ht.Finish()

// created_at
w := ht.Get("/ledgers/2/effects")
if ht.Assert.Equal(200, w.Code) {
var result []effects.Base
_ = ht.UnmarshalPage(w.Body, &result)
ht.Require.NotEmpty(result, "unexpected empty response")

e1 := result[0]

var ledger2 history.Ledger
err := ht.HorizonDB.Get(&ledger2, "SELECT * FROM history_ledgers WHERE sequence = 2")
ht.Require.NoError(err, "failed to load ledger")

ht.Assert.Equal(ledger2.ClosedAt.UTC(), e1.LedgerCloseTime.UTC())
}
})
}
3 changes: 2 additions & 1 deletion services/horizon/internal/resource/effects/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ func (this Base) PagingToken() string {
}

// Populate loads this resource from `row`
func (this *Base) Populate(ctx context.Context, row history.Effect) {
func (this *Base) Populate(ctx context.Context, row history.Effect, ledger history.Ledger) {
this.ID = row.ID()
this.PT = row.PagingToken()
this.Account = row.Account
this.populateType(row)
this.LedgerCloseTime = ledger.ClosedAt

lb := hal.LinkBuilder{httpx.BaseURL(ctx)}
this.Links.Operation = lb.Linkf("/operations/%d", row.HistoryOperationID)
Expand Down
15 changes: 9 additions & 6 deletions services/horizon/internal/resource/effects/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package effects

import (
"time"
"context"

"github.com/stellar/go/services/horizon/internal/db2/history"
Expand Down Expand Up @@ -39,10 +40,11 @@ var TypeNames = map[history.EffectType]string{
func New(
ctx context.Context,
row history.Effect,
ledger history.Ledger,
) (result hal.Pageable, err error) {

basev := Base{}
basev.Populate(ctx, row)
basev.Populate(ctx, row, ledger)

switch row.Type {
case history.EffectAccountCreated:
Expand Down Expand Up @@ -130,11 +132,12 @@ type Base struct {
Precedes hal.Link `json:"precedes"`
} `json:"_links"`

ID string `json:"id"`
PT string `json:"paging_token"`
Account string `json:"account"`
Type string `json:"type"`
TypeI int32 `json:"type_i"`
ID string `json:"id"`
PT string `json:"paging_token"`
Account string `json:"account"`
Type string `json:"type"`
TypeI int32 `json:"type_i"`
LedgerCloseTime time.Time `json:"created_at"`
}

type AccountCreated struct {
Expand Down
3 changes: 2 additions & 1 deletion services/horizon/internal/resource/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,9 @@ type TransactionSuccess struct {
func NewEffect(
ctx context.Context,
row history.Effect,
ledger history.Ledger,
) (result hal.Pageable, err error) {
return effects.New(ctx, row)
return effects.New(ctx, row, ledger)
}

// NewOperation returns a resource of the appropriate sub-type for the provided
Expand Down

0 comments on commit 50e3c42

Please sign in to comment.