Skip to content

Commit

Permalink
services/horizon: Remove Core DB access from web, actions, and resour…
Browse files Browse the repository at this point in the history
…ceadapters. (#2677)

Remove Core DB access from `web`, `actions`, and `resourceadapters`.

## Why
This is part of the work to drop db2/core (#2643).
  • Loading branch information
abuiles authored Jun 8, 2020
1 parent 954ddeb commit e08171f
Show file tree
Hide file tree
Showing 10 changed files with 8 additions and 164 deletions.
11 changes: 0 additions & 11 deletions services/horizon/internal/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/stellar/go/services/horizon/internal/actions"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/httpx"
"github.com/stellar/go/services/horizon/internal/ledger"
Expand All @@ -28,16 +27,6 @@ type Action struct {
Log *log.Entry

hq *history.Q
cq *core.Q
}

// CoreQ provides access to queries that access the stellar core database.
func (action *Action) CoreQ() *core.Q {
if action.cq == nil {
action.cq = &core.Q{Session: action.App.CoreSession(action.R.Context())}
}

return action.cq
}

// HistoryQ provides access to queries that access the history portion of
Expand Down
3 changes: 1 addition & 2 deletions services/horizon/internal/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/test"
"github.com/stellar/go/support/render/hal"
Expand All @@ -23,7 +22,7 @@ func TestGetTransactionPage(t *testing.T) {
defer tt.Finish()

ctx := context.Background()
w := mustInitWeb(ctx, &history.Q{tt.HorizonSession()}, &core.Q{tt.CoreSession()}, time.Duration(5), 0, true)
w := mustInitWeb(ctx, &history.Q{tt.HorizonSession()}, time.Duration(5), 0, true)

// filter by account
params := &indexActionQueryParams{
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (a *App) init() {
a.reaper = reap.New(a.config.HistoryRetentionCount, a.HorizonSession(context.Background()))

// web.init
a.web = mustInitWeb(a.ctx, a.historyQ, a.coreQ, a.config.SSEUpdateFrequency, a.config.StaleThreshold, a.config.IngestFailedTransactions)
a.web = mustInitWeb(a.ctx, a.historyQ, a.config.SSEUpdateFrequency, a.config.StaleThreshold, a.config.IngestFailedTransactions)

// web.rate-limiter
a.web.rateLimiter = maybeInitWebRateLimiter(a.config.RateQuota)
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/resourceadapter/account_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func PopulateAccountEntry(
// populate balances
dest.Balances = make([]protocol.Balance, len(trustLines)+1)
for i, tl := range trustLines {
err := PopulateHistoryBalance(&dest.Balances[i], tl)
err := PopulateBalance(&dest.Balances[i], tl)
if err != nil {
return errors.Wrap(err, "populating balance")
}
Expand Down
12 changes: 0 additions & 12 deletions services/horizon/internal/resourceadapter/account_flags.go

This file was deleted.

12 changes: 0 additions & 12 deletions services/horizon/internal/resourceadapter/account_thresholds.go

This file was deleted.

26 changes: 1 addition & 25 deletions services/horizon/internal/resourceadapter/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,12 @@ import (
"github.com/stellar/go/amount"
protocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/assets"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)

func PopulateBalance(dest *protocol.Balance, row core.Trustline) (err error) {
dest.Type, err = assets.String(row.Assettype)
if err != nil {
return errors.Wrap(err, "getting the string representation from the provided xdr asset type")
}

dest.Balance = amount.String(row.Balance)
dest.BuyingLiabilities = amount.String(row.BuyingLiabilities)
dest.SellingLiabilities = amount.String(row.SellingLiabilities)
dest.Limit = amount.String(row.Tlimit)
dest.Issuer = row.Issuer
dest.Code = row.Assetcode
dest.LastModifiedLedger = row.LastModified
isAuthorized := row.IsAuthorized()
dest.IsAuthorized = &isAuthorized
dest.IsAuthorizedToMaintainLiabilities = &isAuthorized
isAuthorizedToMaintainLiabilities := row.IsAuthorizedToMaintainLiabilities()
if isAuthorizedToMaintainLiabilities {
dest.IsAuthorizedToMaintainLiabilities = &isAuthorizedToMaintainLiabilities
}
return
}

func PopulateHistoryBalance(dest *protocol.Balance, row history.TrustLine) (err error) {
func PopulateBalance(dest *protocol.Balance, row history.TrustLine) (err error) {
dest.Type, err = assets.String(row.AssetType)
if err != nil {
return errors.Wrap(err, "getting the string representation from the provided xdr asset type")
Expand Down
72 changes: 3 additions & 69 deletions services/horizon/internal/resourceadapter/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,12 @@ import (
"testing"

. "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/xdr"
"github.com/stretchr/testify/assert"
)

func TestPopulateBalance(t *testing.T) {
testAssetCode1 := "TEST_ASSET_1"
testAssetCode2 := "TEST_ASSET_2"
authorizedTrustline := core.Trustline{
Accountid: "testID",
Assettype: xdr.AssetTypeAssetTypeCreditAlphanum12,
Issuer: "",
Assetcode: testAssetCode1,
Tlimit: 100,
Balance: 10,
Flags: 1,
}
authorizedToMaintainLiabilitiesTrustline := core.Trustline{
Accountid: "testID",
Assettype: xdr.AssetTypeAssetTypeCreditAlphanum12,
Issuer: "",
Assetcode: testAssetCode1,
Tlimit: 100,
Balance: 10,
Flags: 2,
}
unauthorizedTrustline := core.Trustline{
Accountid: "testID",
Assettype: xdr.AssetTypeAssetTypeCreditAlphanum12,
Issuer: "",
Assetcode: testAssetCode2,
Tlimit: 100,
Balance: 10,
Flags: 0,
}

want := Balance{}
err := PopulateBalance(&want, authorizedTrustline)
assert.NoError(t, err)
assert.Equal(t, "credit_alphanum12", want.Type)
assert.Equal(t, "0.0000010", want.Balance)
assert.Equal(t, "0.0000100", want.Limit)
assert.Equal(t, "", want.Issuer)
assert.Equal(t, testAssetCode1, want.Code)
assert.Equal(t, true, *want.IsAuthorized)
assert.Equal(t, true, *want.IsAuthorizedToMaintainLiabilities)

want = Balance{}
err = PopulateBalance(&want, authorizedToMaintainLiabilitiesTrustline)
assert.NoError(t, err)
assert.Equal(t, "credit_alphanum12", want.Type)
assert.Equal(t, "0.0000010", want.Balance)
assert.Equal(t, "0.0000100", want.Limit)
assert.Equal(t, "", want.Issuer)
assert.Equal(t, testAssetCode1, want.Code)
assert.Equal(t, false, *want.IsAuthorized)
assert.Equal(t, true, *want.IsAuthorizedToMaintainLiabilities)

want = Balance{}
err = PopulateBalance(&want, unauthorizedTrustline)
assert.NoError(t, err)
assert.Equal(t, "credit_alphanum12", want.Type)
assert.Equal(t, "0.0000010", want.Balance)
assert.Equal(t, "0.0000100", want.Limit)
assert.Equal(t, "", want.Issuer)
assert.Equal(t, testAssetCode2, want.Code)
assert.Equal(t, false, *want.IsAuthorized)
assert.Equal(t, false, *want.IsAuthorizedToMaintainLiabilities)
}

func TestPopulateHistoryBalance(t *testing.T) {
testAssetCode1 := "TEST_ASSET_1"
testAssetCode2 := "TEST_ASSET_2"
authorizedTrustline := history.TrustLine{
Expand Down Expand Up @@ -107,7 +41,7 @@ func TestPopulateHistoryBalance(t *testing.T) {
}

want := Balance{}
err := PopulateHistoryBalance(&want, authorizedTrustline)
err := PopulateBalance(&want, authorizedTrustline)
assert.NoError(t, err)
assert.Equal(t, "credit_alphanum12", want.Type)
assert.Equal(t, "0.0000010", want.Balance)
Expand All @@ -118,7 +52,7 @@ func TestPopulateHistoryBalance(t *testing.T) {
assert.Equal(t, true, *want.IsAuthorizedToMaintainLiabilities)

want = Balance{}
err = PopulateHistoryBalance(&want, authorizedToMaintainLiabilitiesTrustline)
err = PopulateBalance(&want, authorizedToMaintainLiabilitiesTrustline)
assert.NoError(t, err)
assert.Equal(t, "credit_alphanum12", want.Type)
assert.Equal(t, "0.0000010", want.Balance)
Expand All @@ -129,7 +63,7 @@ func TestPopulateHistoryBalance(t *testing.T) {
assert.Equal(t, true, *want.IsAuthorizedToMaintainLiabilities)

want = Balance{}
err = PopulateHistoryBalance(&want, unauthorizedTrustline)
err = PopulateBalance(&want, unauthorizedTrustline)
assert.NoError(t, err)
assert.Equal(t, "credit_alphanum12", want.Type)
assert.Equal(t, "0.0000010", want.Balance)
Expand Down
24 changes: 0 additions & 24 deletions services/horizon/internal/resourceadapter/signer.go

This file was deleted.

8 changes: 1 addition & 7 deletions services/horizon/internal/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (

"github.com/stellar/go/services/horizon/internal/actions"
"github.com/stellar/go/services/horizon/internal/db2"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/ledger"
"github.com/stellar/go/services/horizon/internal/paths"
Expand Down Expand Up @@ -47,7 +46,6 @@ type web struct {
ingestFailedTx bool

historyQ *history.Q
coreQ *core.Q

requestTimer metrics.Timer
failureMeter metrics.Meter
Expand All @@ -68,20 +66,16 @@ func init() {
}

// mustInitWeb installed a new Web instance onto the provided app object.
func mustInitWeb(ctx context.Context, hq *history.Q, cq *core.Q, updateFreq time.Duration, threshold uint, ingestFailedTx bool) *web {
func mustInitWeb(ctx context.Context, hq *history.Q, updateFreq time.Duration, threshold uint, ingestFailedTx bool) *web {
if hq == nil {
log.Fatal("missing history DB for installing the web instance")
}
if cq == nil {
log.Fatal("missing core DB for installing the web instance")
}

return &web{
appCtx: ctx,
router: chi.NewRouter(),
internalRouter: chi.NewRouter(),
historyQ: hq,
coreQ: cq,
sseUpdateFrequency: updateFreq,
staleThreshold: threshold,
ingestFailedTx: ingestFailedTx,
Expand Down

0 comments on commit e08171f

Please sign in to comment.