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

services/horizon: Include contract asset balances in asset stats #4805

Merged
merged 12 commits into from
Mar 16, 2023
14 changes: 0 additions & 14 deletions services/horizon/internal/db2/history/asset_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,6 @@ func (q *Q) GetAssetStatByContracts(ctx context.Context, contractIDs [][32]byte)
return assetStats, err
}

// CountContractIDs counts all rows in the asset stats table which have a contract id set.
// CountContractIDs is used by the state verification routine.
func (q *Q) CountContractIDs(ctx context.Context) (int, error) {
sql := sq.Select("count(*)").From("exp_asset_stats").
Where("contract_id IS NOT NULL")

var count int
if err := q.Get(ctx, &count, sql); err != nil {
return 0, errors.Wrap(err, "could not run select query")
}

return count, nil
}

func parseAssetStatsCursor(cursor string) (string, string, error) {
parts := strings.SplitN(cursor, "_", 3)
if len(parts) != 3 {
Expand Down
43 changes: 33 additions & 10 deletions services/horizon/internal/db2/history/asset_stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ func TestAssetStatContracts(t *testing.T) {
test.ResetHorizonDB(t, tt.HorizonDB)
q := &Q{tt.HorizonSession()}

// asset stats is empty so count should be 0
count, err := q.CountContractIDs(tt.Ctx)
tt.Assert.NoError(err)
tt.Assert.Equal(0, count)

assetStats := []ExpAssetStat{
{
AssetType: xdr.AssetTypeAssetTypeNative,
Expand All @@ -30,13 +25,15 @@ func TestAssetStatContracts(t *testing.T) {
ClaimableBalances: 0,
LiquidityPools: 0,
Unauthorized: 0,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "0",
AuthorizedToMaintainLiabilities: "0",
ClaimableBalances: "0",
LiquidityPools: "0",
Unauthorized: "0",
Contracts: "0",
},
Amount: "0",
NumAccounts: 0,
Expand All @@ -49,13 +46,15 @@ func TestAssetStatContracts(t *testing.T) {
Authorized: 1,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "23",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "23",
NumAccounts: 1,
Expand All @@ -68,13 +67,15 @@ func TestAssetStatContracts(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "1",
NumAccounts: 2,
Expand All @@ -87,14 +88,10 @@ func TestAssetStatContracts(t *testing.T) {
}
tt.Assert.NoError(q.InsertAssetStats(tt.Ctx, assetStats, 1))

count, err = q.CountContractIDs(tt.Ctx)
tt.Assert.NoError(err)
tt.Assert.Equal(2, count)

contractID[0] = 0
for i := 0; i < 2; i++ {
var assetStat ExpAssetStat
assetStat, err = q.GetAssetStatByContract(tt.Ctx, contractID)
assetStat, err := q.GetAssetStatByContract(tt.Ctx, contractID)
tt.Assert.NoError(err)
tt.Assert.True(assetStat.Equals(assetStats[i]))
contractID[0]++
Expand Down Expand Up @@ -162,13 +159,15 @@ func TestInsertAssetStats(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "1",
NumAccounts: 2,
Expand All @@ -181,13 +180,15 @@ func TestInsertAssetStats(t *testing.T) {
Authorized: 1,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "23",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "23",
NumAccounts: 1,
Expand Down Expand Up @@ -217,13 +218,15 @@ func TestInsertAssetStat(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "1",
NumAccounts: 2,
Expand All @@ -236,13 +239,15 @@ func TestInsertAssetStat(t *testing.T) {
Authorized: 1,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "23",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "23",
NumAccounts: 1,
Expand Down Expand Up @@ -274,13 +279,15 @@ func TestInsertAssetStatAlreadyExistsError(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "1",
NumAccounts: 2,
Expand Down Expand Up @@ -321,13 +328,15 @@ func TestUpdateAssetStatDoesNotExistsError(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "1",
NumAccounts: 2,
Expand Down Expand Up @@ -356,13 +365,15 @@ func TestUpdateStat(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "1",
NumAccounts: 2,
Expand Down Expand Up @@ -402,13 +413,15 @@ func TestGetAssetStatDoesNotExist(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "1",
NumAccounts: 2,
Expand All @@ -433,13 +446,15 @@ func TestRemoveAssetStat(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "4",
LiquidityPools: "5",
Contracts: "0",
},
Amount: "1",
NumAccounts: 2,
Expand Down Expand Up @@ -569,13 +584,15 @@ func TestGetAssetStatsFiltersAndCursor(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "0",
LiquidityPools: "0",
Contracts: "0",
},
Amount: "1",
NumAccounts: 2,
Expand All @@ -588,13 +605,15 @@ func TestGetAssetStatsFiltersAndCursor(t *testing.T) {
Authorized: 1,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "23",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "0",
LiquidityPools: "0",
Contracts: "0",
},
Amount: "23",
NumAccounts: 1,
Expand All @@ -607,13 +626,15 @@ func TestGetAssetStatsFiltersAndCursor(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "0",
LiquidityPools: "0",
Contracts: "0",
},
Amount: "1",
NumAccounts: 2,
Expand All @@ -626,13 +647,15 @@ func TestGetAssetStatsFiltersAndCursor(t *testing.T) {
Authorized: 3,
AuthorizedToMaintainLiabilities: 2,
Unauthorized: 4,
Contracts: 0,
},
Balances: ExpAssetStatBalances{
Authorized: "111",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "1",
LiquidityPools: "2",
Contracts: "0",
},
Amount: "111",
NumAccounts: 3,
Expand Down
18 changes: 17 additions & 1 deletion services/horizon/internal/db2/history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ type ExpAssetStatAccounts struct {
AuthorizedToMaintainLiabilities int32 `json:"authorized_to_maintain_liabilities"`
ClaimableBalances int32 `json:"claimable_balances"`
LiquidityPools int32 `json:"liquidity_pools"`
Contracts int32 `json:"contracts"`
Unauthorized int32 `json:"unauthorized"`
}

Expand Down Expand Up @@ -429,6 +430,7 @@ func (a ExpAssetStatAccounts) Add(b ExpAssetStatAccounts) ExpAssetStatAccounts {
ClaimableBalances: a.ClaimableBalances + b.ClaimableBalances,
LiquidityPools: a.LiquidityPools + b.LiquidityPools,
Unauthorized: a.Unauthorized + b.Unauthorized,
Contracts: a.Contracts + b.Contracts,
}
}

Expand All @@ -443,9 +445,21 @@ type ExpAssetStatBalances struct {
AuthorizedToMaintainLiabilities string `json:"authorized_to_maintain_liabilities"`
ClaimableBalances string `json:"claimable_balances"`
LiquidityPools string `json:"liquidity_pools"`
Contracts string `json:"contracts"`
Unauthorized string `json:"unauthorized"`
}

func (e ExpAssetStatBalances) IsZero() bool {
return e == ExpAssetStatBalances{
Authorized: "0",
AuthorizedToMaintainLiabilities: "0",
ClaimableBalances: "0",
LiquidityPools: "0",
Contracts: "0",
Unauthorized: "0",
}
}

func (e ExpAssetStatBalances) Value() (driver.Value, error) {
return json.Marshal(e)
}
Expand Down Expand Up @@ -477,6 +491,9 @@ func (e *ExpAssetStatBalances) Scan(src interface{}) error {
if e.Unauthorized == "" {
e.Unauthorized = "0"
}
if e.Contracts == "" {
e.Contracts = "0"
}

return nil
}
Expand All @@ -492,7 +509,6 @@ type QAssetStats interface {
RemoveAssetStat(ctx context.Context, assetType xdr.AssetType, assetCode, assetIssuer string) (int64, error)
GetAssetStats(ctx context.Context, assetCode, assetIssuer string, page db2.PageQuery) ([]ExpAssetStat, error)
CountTrustLines(ctx context.Context) (int, error)
CountContractIDs(ctx context.Context) (int, error)
}

type QCreateAccountsHistory interface {
Expand Down
Loading