Skip to content

Commit

Permalink
Merge pull request #436 from stellar/fix-sum-asset-aggregations
Browse files Browse the repository at this point in the history
Fix sum of asset in circulation in all assets endpoint
  • Loading branch information
bartekn authored May 9, 2018
2 parents e25c7b4 + fe74c26 commit 8760b02
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 36 deletions.
15 changes: 15 additions & 0 deletions amount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ func ParseInt64(v string) (int64, error) {
return i, nil
}

// IntStringToAmount converts string integer value and converts it to stellar
// "amount". In other words, it divides the given string integer value by 10^7
// and returns the string representation of that number.
// It is safe to use with values exceeding int64 limits.
func IntStringToAmount(v string) (string, error) {
r := &big.Rat{}
if _, ok := r.SetString(v); !ok {
return "", errors.Errorf("cannot parse amount: %s", v)
}

r.Quo(r, bigOne)

return r.FloatString(7), nil
}

// String returns an "amount string" from the provided raw xdr.Int64 value `v`.
func String(v xdr.Int64) string {
return StringFromInt64(int64(v))
Expand Down
43 changes: 43 additions & 0 deletions amount/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package amount_test

import (
"fmt"
"testing"

"github.com/stellar/go/amount"
Expand Down Expand Up @@ -58,3 +59,45 @@ func TestString(t *testing.T) {
}
}
}

func TestIntStringToAmount(t *testing.T) {
var testCases = []struct {
Output string
Input string
Valid bool
}{
{"100.0000000", "1000000000", true},
{"-100.0000000", "-1000000000", true},
{"100.0000001", "1000000001", true},
{"123.0000001", "1230000001", true},
{"922337203685.4775807", "9223372036854775807", true},
{"922337203685.4775808", "9223372036854775808", true},
{"92233.7203686", "922337203686", true},
{"-922337203685.4775808", "-9223372036854775808", true},
{"-922337203685.4775809", "-9223372036854775809", true},
{"-92233.7203686", "-922337203686", true},
{"1000000000000.0000000", "10000000000000000000", true},
{"0.0000000", "0", true},
{"", "nan", false},
}

for _, tc := range testCases {
t.Run(fmt.Sprintf("%s to %s (valid = %t)", tc.Input, tc.Output, tc.Valid), func(t *testing.T) {
o, err := amount.IntStringToAmount(tc.Input)

if !tc.Valid && err == nil {
t.Errorf("expected err for input %s (output: %s)", tc.Input, tc.Output)
return
}
if tc.Valid && err != nil {
t.Errorf("couldn't parse %s: %v", tc.Input, err)
return
}

if o != tc.Output {
t.Errorf("%s converted to %s, not %s", tc.Input, o, tc.Output)
}
})
}

}
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/assets/asset_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type AssetStatsR struct {
Type string `db:"asset_type"`
Code string `db:"asset_code"`
Issuer string `db:"asset_issuer"`
Amount int64 `db:"amount"`
Amount string `db:"amount"`
NumAccounts int32 `db:"num_accounts"`
Flags int8 `db:"flags"`
Toml string `db:"toml"`
Expand Down
6 changes: 3 additions & 3 deletions services/horizon/internal/db2/assets/asset_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestAssetsStatsQExec(t *testing.T) {
Type: "credit_alphanum4",
Code: "BTC",
Issuer: "GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
Amount: 1009876000,
Amount: "1009876000",
NumAccounts: 1,
Flags: 1,
Toml: "https://test.com/.well-known/stellar.toml",
Expand All @@ -26,7 +26,7 @@ func TestAssetsStatsQExec(t *testing.T) {
Type: "credit_alphanum4",
Code: "SCOT",
Issuer: "GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU",
Amount: 10000000000,
Amount: "10000000000",
NumAccounts: 1,
Flags: 2,
Toml: "",
Expand All @@ -37,7 +37,7 @@ func TestAssetsStatsQExec(t *testing.T) {
Type: "credit_alphanum4",
Code: "USD",
Issuer: "GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
Amount: 3000010434000,
Amount: "3000010434000",
NumAccounts: 2,
Flags: 1,
Toml: "https://test.com/.well-known/stellar.toml",
Expand Down
6 changes: 3 additions & 3 deletions services/horizon/internal/db2/core/trustline.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ func (q *Q) BalancesForAsset(
assetType int32,
assetCode string,
assetIssuer string,
) (int32, int64, error) {
) (int32, string, error) {
sql := selectBalances.Where(sq.Eq{
"assettype": assetType,
"assetcode": assetCode,
"issuer": assetIssuer,
"flags": 1,
})
result := struct {
Count int32 `db:"count"`
Sum int64 `db:"sum"`
Count int32 `db:"count"`
Sum string `db:"sum"`
}{}
err := q.Get(&result, sql)
return result.Count, result.Sum, err
Expand Down
2 changes: 1 addition & 1 deletion services/horizon/internal/db2/history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ type Asset struct {
// AssetStat is a row in the asset_stats table representing the stats per Asset
type AssetStat struct {
ID int64 `db:"id"`
Amount int64 `db:"amount"`
Amount string `db:"amount"`
NumAccounts int32 `db:"num_accounts"`
Flags int8 `db:"flags"`
Toml string `db:"toml"`
Expand Down
51 changes: 37 additions & 14 deletions services/horizon/internal/db2/schema/bindata.go

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion services/horizon/internal/db2/schema/latest.sql
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ SET default_with_oids = false;

CREATE TABLE asset_stats (
id bigint NOT NULL,
amount bigint NOT NULL,
amount character varying NOT NULL,
num_accounts integer NOT NULL,
flags smallint NOT NULL,
toml character varying(64) NOT NULL
Expand Down Expand Up @@ -377,6 +377,7 @@ INSERT INTO gorp_migrations VALUES ('8_create_asset_stats_table.sql', '2018-04-2
INSERT INTO gorp_migrations VALUES ('9_add_header_xdr.sql', '2018-04-23 13:49:41.516755-07');
INSERT INTO gorp_migrations VALUES ('10_add_trades_price.sql', '2018-04-23 13:49:41.522753-07');
INSERT INTO gorp_migrations VALUES ('11_add_trades_account_index.sql', '2018-04-23 13:49:41.533861-07');
INSERT INTO gorp_migrations VALUES ('12_asset_stats_amount_string.sql', '2018-05-09 19:14:41.628472+02');


--
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- +migrate Up
ALTER TABLE asset_stats
ALTER COLUMN amount SET DATA TYPE character varying;

-- +migrate Down
ALTER TABLE asset_stats
ALTER COLUMN amount SET DATA TYPE bigint USING amount::bigint;
2 changes: 1 addition & 1 deletion services/horizon/internal/ingest/asset_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func computeAssetStat(is *Session, asset *xdr.Asset) *history.AssetStat {
}

// statTrustlinesInfo fetches all the stats from the trustlines table
func statTrustlinesInfo(coreQ *core.Q, assetType xdr.AssetType, assetCode string, assetIssuer string) (int32, int64, error) {
func statTrustlinesInfo(coreQ *core.Q, assetType xdr.AssetType, assetCode string, assetIssuer string) (int32, string, error) {
return coreQ.BalancesForAsset(int32(assetType), assetCode, assetIssuer)
}

Expand Down
22 changes: 11 additions & 11 deletions services/horizon/internal/ingest/asset_stat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestStatTrustlinesInfo(t *testing.T) {
assetCode string
assetIssuer string
wantNumAccounts int32
wantAmount int64
wantAmount string
}

testCases := []struct {
Expand All @@ -33,7 +33,7 @@ func TestStatTrustlinesInfo(t *testing.T) {
"USD",
"GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
1,
0,
"0",
}},
}, {
"asset_stat_trustlines_2",
Expand All @@ -42,7 +42,7 @@ func TestStatTrustlinesInfo(t *testing.T) {
"USD",
"GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
1,
0,
"0",
}},
}, {
"asset_stat_trustlines_3",
Expand All @@ -51,13 +51,13 @@ func TestStatTrustlinesInfo(t *testing.T) {
"USD1",
"GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
1,
0,
"0",
}, {
xdr.AssetTypeAssetTypeCreditAlphanum4,
"USD2",
"GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
1,
0,
"0",
}},
}, {
"asset_stat_trustlines_4",
Expand All @@ -66,13 +66,13 @@ func TestStatTrustlinesInfo(t *testing.T) {
"USD",
"GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
1,
0,
"0",
}, {
xdr.AssetTypeAssetTypeCreditAlphanum4,
"USD",
"GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU",
1,
0,
"0",
}},
}, {
"asset_stat_trustlines_5",
Expand All @@ -81,7 +81,7 @@ func TestStatTrustlinesInfo(t *testing.T) {
"USD",
"GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
1,
0,
"0",
}},
}, {
"asset_stat_trustlines_6",
Expand All @@ -90,7 +90,7 @@ func TestStatTrustlinesInfo(t *testing.T) {
"USD",
"GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
1,
1012345000,
"1012345000",
}},
}, {
"asset_stat_trustlines_7",
Expand All @@ -99,7 +99,7 @@ func TestStatTrustlinesInfo(t *testing.T) {
"USD",
"GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
2,
1012345000,
"1012345000",
}},
}, {
"allow_trust",
Expand All @@ -108,7 +108,7 @@ func TestStatTrustlinesInfo(t *testing.T) {
"USD",
"GC23QF2HUE52AMXUFUH3AYJAXXGXXV2VHXYYR6EYXETPKDXZSAW67XO4",
1, // assets with the auth_required flag should only be counted for authorized accounts
0,
"0",
}},
},
}
Expand Down
5 changes: 4 additions & 1 deletion services/horizon/internal/resource/asset_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ func (res *AssetStat) Populate(
res.Asset.Type = row.Type
res.Asset.Code = row.Code
res.Asset.Issuer = row.Issuer
res.Amount = amount.StringFromInt64(row.Amount)
res.Amount, err = amount.IntStringToAmount(row.Amount)
if err != nil {
return err
}
res.NumAccounts = row.NumAccounts
res.Flags = AccountFlags{
(row.Flags & int8(xdr.AccountFlagsAuthRequiredFlag)) != 0,
Expand Down

0 comments on commit 8760b02

Please sign in to comment.