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: Issue #3476, Include claimable balances in asset stats summaries #3502

Merged
merged 20 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions protocols/horizon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,13 @@ type AssetStat struct {
} `json:"_links"`

base.Asset
PT string `json:"paging_token"`
Accounts AssetStatAccounts `json:"accounts"`
PT string `json:"paging_token"`
Accounts AssetStatAccounts `json:"accounts"`
NumClaimableBalances int32 `json:"num_claimable_balances"`
// Action needed in release: horizon-v3.0.0: deprecated field
Amount string `json:"amount"`
Balances AssetStatBalances `json:"balances"`
Amount string `json:"amount"`
Balances AssetStatBalances `json:"balances"`
ClaimableBalancesAmount string `json:"claimable_balances_amount"`
// Action needed in release: horizon-v3.0.0: deprecated field
NumAccounts int32 `json:"num_accounts"`
Flags AccountFlags `json:"flags"`
Expand Down
4 changes: 4 additions & 0 deletions services/horizon/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this
file. This project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

* Add `claimable_balances` to asset stat summaries at `/assets` ([3502](https://github.com/stellar/go/pull/3502)).

## v2.1.0

### DB State Migration
Expand Down
40 changes: 30 additions & 10 deletions services/horizon/internal/actions/asset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,13 @@ func TestAssetStats(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
ClaimableBalances: 1,
},
Balances: history.ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "10",
},
Amount: "1",
NumAccounts: 2,
Expand All @@ -144,13 +146,15 @@ func TestAssetStats(t *testing.T) {
AuthorizedToMaintainLiabilities: usdAssetStat.Accounts.AuthorizedToMaintainLiabilities,
Unauthorized: usdAssetStat.Accounts.Unauthorized,
},
NumClaimableBalances: usdAssetStat.Accounts.ClaimableBalances,
Balances: horizon.AssetStatBalances{
Authorized: "0.0000001",
AuthorizedToMaintainLiabilities: "0.0000002",
Unauthorized: "0.0000003",
},
Amount: "0.0000001",
NumAccounts: usdAssetStat.NumAccounts,
ClaimableBalancesAmount: "0.0000010",
Amount: "0.0000001",
NumAccounts: usdAssetStat.NumAccounts,
Asset: base.Asset{
Type: "credit_alphanum4",
Code: usdAssetStat.AssetCode,
Expand All @@ -168,11 +172,13 @@ func TestAssetStats(t *testing.T) {
Authorized: 1,
AuthorizedToMaintainLiabilities: 2,
Unauthorized: 3,
ClaimableBalances: 0,
},
Balances: history.ExpAssetStatBalances{
Authorized: "23",
AuthorizedToMaintainLiabilities: "46",
Unauthorized: "92",
ClaimableBalances: "0",
},
Amount: "23",
NumAccounts: 1,
Expand All @@ -183,13 +189,15 @@ func TestAssetStats(t *testing.T) {
AuthorizedToMaintainLiabilities: etherAssetStat.Accounts.AuthorizedToMaintainLiabilities,
Unauthorized: etherAssetStat.Accounts.Unauthorized,
},
NumClaimableBalances: etherAssetStat.Accounts.ClaimableBalances,
Balances: horizon.AssetStatBalances{
Authorized: "0.0000023",
AuthorizedToMaintainLiabilities: "0.0000046",
Unauthorized: "0.0000092",
},
Amount: "0.0000023",
NumAccounts: etherAssetStat.NumAccounts,
ClaimableBalancesAmount: "0.0000000",
Amount: "0.0000023",
NumAccounts: etherAssetStat.NumAccounts,
Asset: base.Asset{
Type: "credit_alphanum4",
Code: etherAssetStat.AssetCode,
Expand All @@ -207,11 +215,13 @@ func TestAssetStats(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
ClaimableBalances: 0,
},
Balances: history.ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "0",
},
Amount: "1",
NumAccounts: 2,
Expand All @@ -222,13 +232,15 @@ func TestAssetStats(t *testing.T) {
AuthorizedToMaintainLiabilities: otherUSDAssetStat.Accounts.AuthorizedToMaintainLiabilities,
Unauthorized: otherUSDAssetStat.Accounts.Unauthorized,
},
NumClaimableBalances: otherUSDAssetStat.Accounts.ClaimableBalances,
Balances: horizon.AssetStatBalances{
Authorized: "0.0000001",
AuthorizedToMaintainLiabilities: "0.0000002",
Unauthorized: "0.0000003",
},
Amount: "0.0000001",
NumAccounts: otherUSDAssetStat.NumAccounts,
ClaimableBalancesAmount: "0.0000000",
Amount: "0.0000001",
NumAccounts: otherUSDAssetStat.NumAccounts,
Asset: base.Asset{
Type: "credit_alphanum4",
Code: otherUSDAssetStat.AssetCode,
Expand All @@ -248,11 +260,13 @@ func TestAssetStats(t *testing.T) {
Authorized: 3,
AuthorizedToMaintainLiabilities: 4,
Unauthorized: 5,
ClaimableBalances: 0,
},
Balances: history.ExpAssetStatBalances{
Authorized: "111",
AuthorizedToMaintainLiabilities: "222",
Unauthorized: "333",
ClaimableBalances: "0",
},
Amount: "111",
NumAccounts: 3,
Expand All @@ -263,13 +277,15 @@ func TestAssetStats(t *testing.T) {
AuthorizedToMaintainLiabilities: eurAssetStat.Accounts.AuthorizedToMaintainLiabilities,
Unauthorized: eurAssetStat.Accounts.Unauthorized,
},
NumClaimableBalances: eurAssetStat.Accounts.ClaimableBalances,
Balances: horizon.AssetStatBalances{
Authorized: "0.0000111",
AuthorizedToMaintainLiabilities: "0.0000222",
Unauthorized: "0.0000333",
},
Amount: "0.0000111",
NumAccounts: eurAssetStat.NumAccounts,
ClaimableBalancesAmount: "0.0000000",
Amount: "0.0000111",
NumAccounts: eurAssetStat.NumAccounts,
Asset: base.Asset{
Type: "credit_alphanum4",
Code: eurAssetStat.AssetCode,
Expand Down Expand Up @@ -419,11 +435,13 @@ func TestAssetStatsIssuerDoesNotExist(t *testing.T) {
Authorized: 2,
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
ClaimableBalances: 0,
},
Balances: history.ExpAssetStatBalances{
Authorized: "1",
AuthorizedToMaintainLiabilities: "2",
Unauthorized: "3",
ClaimableBalances: "0",
},
Amount: "1",
NumAccounts: 2,
Expand All @@ -442,13 +460,15 @@ func TestAssetStatsIssuerDoesNotExist(t *testing.T) {
AuthorizedToMaintainLiabilities: 3,
Unauthorized: 4,
},
NumClaimableBalances: 0,
Balances: horizon.AssetStatBalances{
Authorized: "0.0000001",
AuthorizedToMaintainLiabilities: "0.0000002",
Unauthorized: "0.0000003",
},
Amount: "0.0000001",
NumAccounts: usdAssetStat.NumAccounts,
ClaimableBalancesAmount: "0.0000000",
Amount: "0.0000001",
NumAccounts: usdAssetStat.NumAccounts,
Asset: base.Asset{
Type: "credit_alphanum4",
Code: usdAssetStat.AssetCode,
Expand Down
5 changes: 4 additions & 1 deletion services/horizon/internal/db2/history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ func (e ExpAssetStat) PagingToken() string {
type ExpAssetStatAccounts struct {
Authorized int32 `json:"authorized"`
AuthorizedToMaintainLiabilities int32 `json:"authorized_to_maintain_liabilities"`
ClaimableBalances int32 `json:"claimable_balances"`
Unauthorized int32 `json:"unauthorized"`
}

Expand All @@ -367,18 +368,20 @@ func (a ExpAssetStatAccounts) Add(b ExpAssetStatAccounts) ExpAssetStatAccounts {
return ExpAssetStatAccounts{
Authorized: a.Authorized + b.Authorized,
AuthorizedToMaintainLiabilities: a.AuthorizedToMaintainLiabilities + b.AuthorizedToMaintainLiabilities,
ClaimableBalances: a.ClaimableBalances + b.ClaimableBalances,
Unauthorized: a.Unauthorized + b.Unauthorized,
}
}

func (a ExpAssetStatAccounts) IsZero() bool {
return a.Authorized == 0 && a.AuthorizedToMaintainLiabilities == 0 && a.Unauthorized == 0
return a == ExpAssetStatAccounts{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR: does Exp stand for experimental? If so, I think it's about time to remove it.

Copy link
Contributor Author

@paulbellamy paulbellamy Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like db2 and history are both misnomers as well, given there's only one db, and two "sections" (for lack of a better term) within it, the normal (current? state?) tables and history_* tables.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should try to change exp_asset_stats table name too? Not sure how complicated it is.

}

// ExpAssetStatBalances represents the summarized balances for a single Asset
type ExpAssetStatBalances struct {
Authorized string `json:"authorized"`
AuthorizedToMaintainLiabilities string `json:"authorized_to_maintain_liabilities"`
ClaimableBalances string `json:"claimable_balances"`
Unauthorized string `json:"unauthorized"`
}

Expand Down
18 changes: 12 additions & 6 deletions services/horizon/internal/docs/reference/endpoints/assets-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ If called normally this endpoint responds with a [page](../resources/page.md) of
"accounts": {
"authorized": 2126,
"authorized_to_maintain_liabilities": 32,
"unauthorized": 5
"unauthorized": 5,
"claimable_balances": 18
},
"balances": {
"authorized": "10000.0000000",
"authorized_to_maintain_liabilities": "3000.0000000",
"unauthorized": "4000.0000000"
"unauthorized": "4000.0000000",
"claimable_balances": "2380.0000000"
},
"flags": {
"auth_required": true,
Expand All @@ -106,12 +108,14 @@ If called normally this endpoint responds with a [page](../resources/page.md) of
"accounts": {
"authorized": 32,
"authorized_to_maintain_liabilities": 124,
"unauthorized": 6
"unauthorized": 6,
"claimable_balances": 18
},
"balances": {
"authorized": "5000.0000000",
"authorized_to_maintain_liabilities": "8000.0000000",
"unauthorized": "2000.0000000"
"unauthorized": "2000.0000000",
"claimable_balances": "1200.0000000"
},
"flags": {
"auth_required": false,
Expand All @@ -131,12 +135,14 @@ If called normally this endpoint responds with a [page](../resources/page.md) of
"accounts": {
"authorized": 91547871,
"authorized_to_maintain_liabilities": 45773935,
"unauthorized": 22886967
"unauthorized": 22886967,
"claimable_balances": 11443483
},
"balances": {
"authorized": "1000000000.0000000",
"authorized_to_maintain_liabilities": "500000000.0000000",
"unauthorized": "250000000.0000000"
"unauthorized": "250000000.0000000",
"claimable_balances": "12500000.0000000"
},
"flags": {
"auth_required": false,
Expand Down
10 changes: 6 additions & 4 deletions services/horizon/internal/docs/reference/resources/asset.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ To learn more about the concept of assets in the Stellar network, take a look at
| asset_type | string | The type of this asset: "credit_alphanum4", or "credit_alphanum12". |
| asset_code | string | The code of this asset. |
| asset_issuer | string | The issuer of this asset. |
| accounts | object | The number of accounts holding this asset, summarized by each state of the trust line flags. |
| balances | object | The number of units of credit issued, summarized by each state of the trust line flags. |
| accounts | object | The number of accounts and claimable balances holding this asset. Accounts are summarized by each state of the trust line flags. |
| balances | object | The number of units of credit issued, summarized by each state of the trust line flags, or if they are in a claimable balance. |
| flags | object | The flags denote the enabling/disabling of certain asset issuer privileges. |
| paging_token | string | A [paging token](./page.md) suitable for use as the `cursor` parameter to transaction collection resources. |

Expand Down Expand Up @@ -49,12 +49,14 @@ To learn more about the concept of assets in the Stellar network, take a look at
"accounts": {
"authorized": 91547871,
"authorized_to_maintain_liabilities": 45773935,
"unauthorized": 22886967
"unauthorized": 22886967,
"claimable_balances": 11443483
},
"balances": {
"authorized": "100.0000000",
"authorized_to_maintain_liabilities": "50.0000000",
"unauthorized": "25.0000000"
"unauthorized": "25.0000000",
"claimable_balances": "12.5000000"
},
"flags": {
"auth_required": false,
Expand Down
3 changes: 2 additions & 1 deletion services/horizon/internal/ingest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const (
// - 12: Trigger state rebuild due to `absTime` -> `abs_time` rename
// in ClaimableBalances predicates.
// - 13: Trigger state rebuild to include more than just authorized assets.
CurrentVersion = 13
// - 14: Trigger state rebuild to include claimable balances in the asset stats processor.
CurrentVersion = 14

// MaxDBConnections is the size of the postgres connection pool dedicated to Horizon ingestion:
// * Ledger ingestion,
Expand Down
Loading