Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into 4160/exclude_dust_t…
Browse files Browse the repository at this point in the history
…rades_from_aggregations
  • Loading branch information
Paul Bellamy committed Feb 9, 2022
2 parents 59586d2 + 995905d commit 12f1bd4
Show file tree
Hide file tree
Showing 43 changed files with 177 additions and 132 deletions.
2 changes: 1 addition & 1 deletion go.list
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ github.com/google/go-querystring v0.0.0-20160401233042-9235644dd9e5
github.com/google/uuid v1.2.0
github.com/googleapis/gax-go/v2 v2.0.5
github.com/gorilla/schema v1.1.0
github.com/graph-gophers/graphql-go v0.0.0-20190225005345-3e8838d4614c
github.com/graph-gophers/graphql-go v1.3.0
github.com/guregu/null v2.1.3-0.20151024101046-79c5bd36b615+incompatible
github.com/hashicorp/golang-lru v0.5.1
github.com/holiman/uint256 v1.2.0
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
github.com/google/go-querystring v0.0.0-20160401233042-9235644dd9e5 // indirect
github.com/google/uuid v1.2.0
github.com/gorilla/schema v1.1.0
github.com/graph-gophers/graphql-go v0.0.0-20190225005345-3e8838d4614c
github.com/graph-gophers/graphql-go v1.3.0
github.com/guregu/null v2.1.3-0.20151024101046-79c5bd36b615+incompatible
github.com/holiman/uint256 v1.2.0
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c
Expand All @@ -43,7 +43,6 @@ require (
github.com/moul/http2curl v0.0.0-20161031194548-4e24498b31db // indirect
github.com/onsi/ginkgo v1.7.0
github.com/onsi/gomega v1.4.3
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/pelletier/go-toml v1.9.0
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGa
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY=
github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/graph-gophers/graphql-go v0.0.0-20190225005345-3e8838d4614c h1:YyFUsspLqAt3noyPCLz7EFK/o1LpC1j/6MjU0bSVOQ4=
github.com/graph-gophers/graphql-go v0.0.0-20190225005345-3e8838d4614c/go.mod h1:uJhtPXrcJLqyi0H5IuMFh+fgW+8cMMakK3Txrbk/WJE=
github.com/graph-gophers/graphql-go v1.3.0 h1:Eb9x/q6MFpCLz7jBCiP/WTxjSDrYLR1QY41SORZyNJ0=
github.com/graph-gophers/graphql-go v1.3.0/go.mod h1:9CQHMSxwO4MprSdzoIEobiHpoLtHm77vfxsvsIN5Vuc=
github.com/guregu/null v2.1.3-0.20151024101046-79c5bd36b615+incompatible h1:SZmF1M6CdAm4MmTPYYTG+x9EC8D3FOxUq9S4D37irQg=
github.com/guregu/null v2.1.3-0.20151024101046-79c5bd36b615+incompatible/go.mod h1:ePGpQaN9cw0tj45IR5E5ehMvsFlLlQZAkkOXZurJ3NM=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
Expand Down Expand Up @@ -267,7 +267,6 @@ github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs=
github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
github.com/pelletier/go-toml v1.9.0 h1:NOd0BRdOKpPf0SxkL3HxSQOG7rNh+4kl6PHcBPFs7Q0=
Expand Down Expand Up @@ -421,7 +420,6 @@ golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190125091013-d26f9f9a57f3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
16 changes: 5 additions & 11 deletions ingest/ledgerbackend/captive_core_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,16 +398,14 @@ func (c *CaptiveStellarCore) IsPrepared(ctx context.Context, ledgerRange Range)
}

func (c *CaptiveStellarCore) isPrepared(ledgerRange Range) bool {
if c.isClosed() {
if c.closed {
return false
}

if c.stellarCoreRunner == nil {
return false
}
if c.closed {
if c.stellarCoreRunner == nil || c.stellarCoreRunner.context().Err() != nil {
return false
}

lastLedger := uint32(0)
if c.lastLedger != nil {
lastLedger = *c.lastLedger
Expand Down Expand Up @@ -464,7 +462,7 @@ func (c *CaptiveStellarCore) GetLedger(ctx context.Context, sequence uint32) (xd
return *c.cachedMeta, nil
}

if c.isClosed() {
if c.closed {
return xdr.LedgerCloseMeta{}, errors.New("stellar-core is no longer usable")
}

Expand Down Expand Up @@ -607,7 +605,7 @@ func (c *CaptiveStellarCore) GetLatestLedgerSequence(ctx context.Context) (uint3
c.stellarCoreLock.RLock()
defer c.stellarCoreLock.RUnlock()

if c.isClosed() {
if c.closed {
return 0, errors.New("stellar-core is no longer usable")
}
if c.prepared == nil {
Expand All @@ -626,10 +624,6 @@ func (c *CaptiveStellarCore) GetLatestLedgerSequence(ctx context.Context) (uint3
return *c.lastLedger, nil
}

func (c *CaptiveStellarCore) isClosed() bool {
return c.closed
}

// Close closes existing Stellar-Core process, streaming sessions and removes all
// temporary files. Note, once a CaptiveStellarCore instance is closed it can no longer be used and
// all subsequent calls to PrepareRange(), GetLedger(), etc will fail.
Expand Down
44 changes: 36 additions & 8 deletions ingest/ledgerbackend/captive_core_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,12 @@ func TestCaptivePrepareRangeCloseNotFullyTerminated(t *testing.T) {

ctx, cancel := context.WithCancel(context.Background())
mockRunner := &stellarCoreRunnerMock{}
mockRunner.On("catchup", uint32(100), uint32(200)).Return(nil).Once()
mockRunner.On("catchup", uint32(100), uint32(200)).Return(nil).Twice()
mockRunner.On("getMetaPipe").Return((<-chan metaResult)(metaChan))
mockRunner.On("context").Return(ctx)
mockRunner.On("close").Return(nil)
mockRunner.On("getProcessExitError").Return(true, nil)
mockRunner.On("getProcessExitError").Return(false, nil)

mockArchive := &historyarchive.MockArchive{}
mockArchive.
Expand Down Expand Up @@ -313,6 +316,7 @@ func TestCaptivePrepareRange_ErrClosingSession(t *testing.T) {
ctx := context.Background()
mockRunner := &stellarCoreRunnerMock{}
mockRunner.On("close").Return(fmt.Errorf("transient error"))
mockRunner.On("context").Return(ctx)

captiveBackend := CaptiveStellarCore{
nextLedger: 300,
Expand Down Expand Up @@ -603,12 +607,12 @@ func TestCaptiveGetLedger(t *testing.T) {

ledgerRange := BoundedRange(65, 66)
tt.False(captiveBackend.isPrepared(ledgerRange), "core is not prepared until explicitly prepared")
tt.False(captiveBackend.isClosed())
tt.False(captiveBackend.closed)
err = captiveBackend.PrepareRange(ctx, ledgerRange)
assert.NoError(t, err)

tt.True(captiveBackend.isPrepared(ledgerRange))
tt.False(captiveBackend.isClosed())
tt.False(captiveBackend.closed)

_, err = captiveBackend.GetLedger(ctx, 64)
tt.Error(err, "requested ledger 64 is behind the captive core stream (expected=66)")
Expand All @@ -634,12 +638,12 @@ func TestCaptiveGetLedger(t *testing.T) {
tt.NoError(err)

tt.False(captiveBackend.isPrepared(ledgerRange))
tt.False(captiveBackend.isClosed())
tt.False(captiveBackend.closed)
_, err = captiveBackend.GetLedger(ctx, 66)
tt.NoError(err)

// core is not closed unless it's explicitly closed
tt.False(captiveBackend.isClosed())
tt.False(captiveBackend.closed)

mockArchive.AssertExpectations(t)
mockRunner.AssertExpectations(t)
Expand Down Expand Up @@ -903,14 +907,14 @@ func TestCaptiveGetLedger_ErrReadingMetaResult(t *testing.T) {
tt.NoError(err)
tt.Equal(xdr.Uint32(65), meta.V0.LedgerHeader.Header.LedgerSeq)

tt.False(captiveBackend.isClosed())
tt.False(captiveBackend.closed)

// try reading from an empty buffer
_, err = captiveBackend.GetLedger(ctx, 66)
tt.EqualError(err, "unmarshalling error")

// not closed even if there is an error getting ledger
tt.False(captiveBackend.isClosed())
tt.False(captiveBackend.closed)

mockArchive.AssertExpectations(t)
mockRunner.AssertExpectations(t)
Expand Down Expand Up @@ -997,7 +1001,7 @@ func TestCaptiveAfterClose(t *testing.T) {
assert.NoError(t, err)

assert.NoError(t, captiveBackend.Close())
assert.True(t, captiveBackend.isClosed())
assert.True(t, captiveBackend.closed)

_, err = captiveBackend.GetLedger(ctx, boundedRange.to)
assert.EqualError(t, err, "stellar-core is no longer usable")
Expand Down Expand Up @@ -1340,6 +1344,30 @@ func TestCaptiveIsPrepared(t *testing.T) {
}
}

// TestCaptiveIsPreparedCoreContextCancelled checks if IsPrepared returns false
// if the stellarCoreRunner.context() is cancelled. This can happen when
// stellarCoreRunner was closed, ex. when binary file was updated.
func TestCaptiveIsPreparedCoreContextCancelled(t *testing.T) {
mockRunner := &stellarCoreRunnerMock{}
ctx, cancel := context.WithCancel(context.Background())
mockRunner.On("context").Return(ctx).Maybe()

rang := UnboundedRange(100)
captiveBackend := CaptiveStellarCore{
nextLedger: 100,
prepared: &rang,
stellarCoreRunner: mockRunner,
}

result := captiveBackend.isPrepared(UnboundedRange(100))
assert.True(t, result)

cancel()

result = captiveBackend.isPrepared(UnboundedRange(100))
assert.False(t, result)
}

// TestCaptivePreviousLedgerCheck checks if previousLedgerHash is set in PrepareRange
// and then checked and updated in GetLedger.
func TestCaptivePreviousLedgerCheck(t *testing.T) {
Expand Down
45 changes: 38 additions & 7 deletions ingest/ledgerbackend/database_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
)

const (
latestLedgerSeqQuery = "select ledgerseq, closetime from ledgerheaders order by ledgerseq desc limit 1"
txHistoryQuery = "select txbody, txresult, txmeta, txindex from txhistory where ledgerseq = ? "
ledgerHeaderQuery = "select ledgerhash, data from ledgerheaders where ledgerseq = ? "
txFeeHistoryQuery = "select txchanges, txindex from txfeehistory where ledgerseq = ? "
upgradeHistoryQuery = "select ledgerseq, upgradeindex, upgrade, changes from upgradehistory where ledgerseq = ? order by upgradeindex asc"
orderBy = "order by txindex asc"
dbDriver = "postgres"
latestLedgerSeqQuery = "select ledgerseq, closetime from ledgerheaders order by ledgerseq desc limit 1"
txHistoryQuery = "select txbody, txresult, txmeta, txindex from txhistory where ledgerseq = ? "
ledgerHeaderQuery = "select ledgerhash, data from ledgerheaders where ledgerseq = ? "
ledgerSequenceAfterQuery = "select ledgerseq from ledgerheaders where ledgerseq > ? "
txFeeHistoryQuery = "select txchanges, txindex from txfeehistory where ledgerseq = ? "
upgradeHistoryQuery = "select ledgerseq, upgradeindex, upgrade, changes from upgradehistory where ledgerseq = ? order by upgradeindex asc"
orderBy = "order by txindex asc"
dbDriver = "postgres"
)

// Ensure DatabaseBackend implements LedgerBackend
Expand Down Expand Up @@ -128,11 +129,41 @@ func (dbb *DatabaseBackend) GetLedger(ctx context.Context, sequence uint32) (xdr
if exists {
return meta, nil
} else {
// Check if there are ledgers after `sequence`. If so, it's likely
// the requested sequence was removed during maintenance so return
// error.
ledgerAfterExist, oldestSequenceAfter, err := dbb.getLedgerAfterExist(ctx, sequence)
if err != nil {
return xdr.LedgerCloseMeta{}, err
}

if ledgerAfterExist {
return xdr.LedgerCloseMeta{}, errors.Errorf("requested ledger already removed (oldest sequence after %d is %d)", sequence, oldestSequenceAfter)
}
time.Sleep(time.Second)
}
}
}

// getLedgerAfterExist returns true (and sequence number) if there's a ledger in
// the Stellar-Core DB with the sequence number higher than sequence.
func (dbb *DatabaseBackend) getLedgerAfterExist(ctx context.Context, sequence uint32) (bool, uint32, error) {
var fetchedSequence uint32
err := dbb.session.GetRaw(ctx, &fetchedSequence, ledgerSequenceAfterQuery, sequence)
// Return errors...
if err != nil {
switch err {
case sql.ErrNoRows:
// Ledger was not found
return false, fetchedSequence, nil
default:
return false, fetchedSequence, errors.Wrapf(err, "Error getting ledger after %d", sequence)
}
}

return true, fetchedSequence, nil
}

// getLedgerQuery returns the LedgerCloseMeta for the given ledger sequence number.
// The first returned value is false when the ledger does not exist in the database.
func (dbb *DatabaseBackend) getLedgerQuery(ctx context.Context, sequence uint32) (bool, xdr.LedgerCloseMeta, error) {
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/ledger"
hProblem "github.com/stellar/go/services/horizon/internal/render/problem"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/render/problem"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)

Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/ledger"
"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/support/db"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/render/problem"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)

Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/actions/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"github.com/stellar/go/services/horizon/internal/ledger"
"github.com/stellar/go/services/horizon/internal/render/problem"
"github.com/stellar/go/services/horizon/internal/resourceadapter"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/render/hal"
supportProblem "github.com/stellar/go/support/render/problem"
"github.com/stellar/go/toid"
)

// Joinable query struct for join query parameter
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/history/effect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/stellar/go/protocols/horizon/effects"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/toid"
)

// UnmarshalDetails unmarshals the details of this effect into `dest`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/guregu/null"
"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/toid"
)

func TestAddEffect(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/history/effect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/stellar/go/protocols/horizon/effects"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/toid"
)

func TestEffectsForLiquidityPool(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/history/fee_bump_scenario.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/stellar/go/ingest"
"github.com/stellar/go/network"
"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/assert"
)
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/history/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
sq "github.com/Masterminds/squirrel"
"github.com/guregu/null"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)

Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/history/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/guregu/null"
"github.com/stellar/go/ingest/ledgerbackend"
"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)

Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/history/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

sq "github.com/Masterminds/squirrel"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/guregu/null"
"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)

Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/history/operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/guregu/null"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/services/horizon/internal/toid"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"
)

Expand Down
Loading

0 comments on commit 12f1bd4

Please sign in to comment.