Skip to content

Commit

Permalink
Fix hanging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
charlie-wasp authored Oct 14, 2019
1 parent 8849dc1 commit 2b7db79
Show file tree
Hide file tree
Showing 29 changed files with 1,823 additions and 1,916 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,27 @@ Create assets view in database:

```
CREATE VIEW assets AS
( SELECT (t.assetcode::text || '-'::text) || t.issuer::text AS assetid,
( SELECT (t.assetcode::text || '-'::text) || t.issuer::text AS id,
t.assetcode AS code,
t.issuer,
sum(t.balance) AS total_supply,
sum(t.balance) FILTER (WHERE t.flags = 1) AS circulating_supply,
count(t.accountid) AS holders_count,
count(t.accountid) FILTER (WHERE t.flags = 0) AS unauthorized_holders_count,
max(t.lastmodified) AS last_activity
t.flags,
sum(t.balance) AS "totalSupply",
sum(t.balance) FILTER (WHERE t.flags = 1) AS "circulatingSupply",
count(t.accountid) AS "holdersCount",
count(t.accountid) FILTER (WHERE t.flags = 0) AS "unauthorizedHoldersCount",
max(t.lastmodified) AS "lastActivity"
FROM trustlines t
GROUP BY t.issuer, t.assetcode
ORDER BY (count(t.accountid)) DESC)
UNION
SELECT 'native'::text AS assetid,
SELECT 'native'::text AS id,
'XLM'::character varying AS code,
NULL::character varying AS issuer,
sum(accounts.balance) AS total_supply,
sum(accounts.balance) AS circulating_supply,
count(*) AS holders_count,
0 AS unauthorized_holders_count,
max(accounts.lastmodified) AS last_activity
sum(accounts.balance) AS "totalSupply",
sum(accounts.balance) AS "circulatingSupply",
count(*) AS "holdersCount",
0 AS "unauthorizedHoldersCount",
max(accounts.lastmodified) AS "lastActivity"
FROM accounts;
```

Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"dependencies": {
"@sentry/integrations": "^5.3.1",
"@sentry/node": "^5.3.0",
"@sentry/node": "5.6.2",
"@udia/graphql-postgres-subscriptions": "^1.1.0",
"apollo-datasource-rest": "^0.3.1",
"apollo-server": "^2.4.2",
Expand Down Expand Up @@ -47,19 +47,14 @@
"@types/big.js": "^4.0.5",
"@types/bluebird": "^3.5.25",
"@types/dotenv": "^4.0.3",
"@types/google-protobuf": "^3.2.7",
"@types/graphql": "^14.0.7",
"@types/graphql-resolve-batch": "^1.1.3",
"@types/jest": "^24.0.11",
"@types/lodash": "^4.14.118",
"@types/minimist": "^1.2.0",
"@types/node-fetch": "^2.1.2",
"@types/pg": "^7.4.14",
"@types/progress": "^2.0.3",
"@types/protobufjs": "^6.0.0",
"@types/retry": "^0.12.0",
"@types/rosie": "^0.0.35",
"@types/winston": "^2.3.9",
"apollo-cache-inmemory": "^1.2.9",
"apollo-client": "^2.4.1",
"apollo-link-http": "^1.5.4",
Expand Down
6 changes: 0 additions & 6 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,14 @@ import pgPromise = require("pg-promise");

import { DATABASE_URL } from "./util/secrets";

import AssetsRepo from "./repo/assets";
import LedgerHeadersRepo from "./repo/ledger_headers";
import StoreStateRepo from "./repo/store_state";
import TransactionsRepo from "./repo/transactions";
import TrustLinesRepo from "./repo/trust_lines";

// Database Interface Extensions:
interface IExtensions {
assets: AssetsRepo;
ledgerHeaders: LedgerHeadersRepo;
transactions: TransactionsRepo;
trustLines: TrustLinesRepo;
storeState: StoreStateRepo;
}

Expand All @@ -29,10 +25,8 @@ const initOptions: IOptions<IExtensions> = {
extend(obj: IExtensions) {
// Do not use 'require()' here, because this event occurs for every task
// and transaction being executed, which should be as fast as possible.
obj.assets = new AssetsRepo(obj);
obj.ledgerHeaders = new LedgerHeadersRepo(obj);
obj.transactions = new TransactionsRepo(obj);
obj.trustLines = new TrustLinesRepo(obj);
obj.storeState = new StoreStateRepo(obj);
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/init/db.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { createConnection } from "typeorm";
import { Account, AccountData, Offer, TrustLine } from "../orm/entities";
import { Account, AccountData, Asset, Offer, TrustLine } from "../orm/entities";
import { DATABASE_URL } from "../util/secrets";

export async function initDatabase() {
const queryStart = DATABASE_URL.indexOf("?");

const connectionString = (queryStart !== -1) ? DATABASE_URL.slice(0, queryStart) : DATABASE_URL;
const connectionString = queryStart !== -1 ? DATABASE_URL.slice(0, queryStart) : DATABASE_URL;

return createConnection({
type: "postgres",
url: connectionString,
entities: [Account, AccountData, Offer, TrustLine],
entities: [Account, AccountData, Asset, Offer, TrustLine],
synchronize: false,
logging: process.env.DEBUG_SQL !== undefined
});
Expand Down
3 changes: 3 additions & 0 deletions src/init/graphql_server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getRepository } from "typeorm";
import { Offer } from "../orm/entities/offer";
import { connect as connectPubSub } from "../pubsub";
import { buildOffersGraph } from "../service/dex";
import "../util/asset";
import logger from "../util/logger";
Expand All @@ -18,6 +19,8 @@ export async function initGraphqlServer() {
.then(network => logger.info(`Astrograph will use the network with passphrase "${network}"`))
.then(() => logger.info("Connecting to the database..."))
.then(initDatabase)
.then(() => logger.info("Creating connection for pubsub..."))
.then(connectPubSub)
.then(() => logger.info("Updating base reserve value..."))
.then(updateBaseReserve)
.then(() => logger.info("Building offers graph for path finding..."))
Expand Down
3 changes: 3 additions & 0 deletions src/init/ingestd.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { connect as connectPubSub } from "../pubsub";
import "../util/asset";
import logger from "../util/logger";
import { initDatabase } from "./db";
Expand All @@ -14,5 +15,7 @@ export async function initIngestd() {
.then(network => logger.info(`Astrograph will use ${network}`))
.then(() => logger.info("Connecting to the database..."))
.then(initDatabase)
.then(() => logger.info("Creating connection for pubsub..."))
.then(connectPubSub)
.then(() => logger.info("Ingest daemon is inialized successfully"));
}
54 changes: 0 additions & 54 deletions src/model/asset.ts

This file was deleted.

29 changes: 2 additions & 27 deletions src/model/factories/asset_factory.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
import stellar from "stellar-base";
import { AccountID, Asset, AssetCode, AssetID } from "../";
import { AssetID } from "../";
import { HorizonAssetType } from "../../datasource/types";

export interface IAssetTableRow {
assetid: AssetID;
code: AssetCode;
issuer: AccountID;
total_supply: string;
circulating_supply: string;
holders_count: string;
unauthorized_holders_count: string;
flags: number;
last_activity: number;
}

export class AssetFactory {
public static fromDb(row: IAssetTableRow): Asset {
return new Asset({
code: row.code,
issuer: row.issuer,
totalSupply: row.total_supply,
circulatingSupply: row.circulating_supply,
holdersCount: row.holders_count,
unauthorizedHoldersCount: row.unauthorized_holders_count,
lastModifiedIn: row.last_activity,
flags: row.flags
});
}

public static fromTrustline(type: number, code: string, issuer: string): stellar.Asset {
return type === stellar.xdr.AssetType.assetTypeNative().value
? stellar.Asset.native()
Expand All @@ -38,7 +13,7 @@ export class AssetFactory {
return type === "native" ? stellar.Asset.native() : new stellar.Asset(code!, issuer!);
}

public static fromId(id: string) {
public static fromId(id: AssetID) {
if (id === "native") {
return stellar.Asset.native();
}
Expand Down
4 changes: 2 additions & 2 deletions src/model/factories/balance_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export class BalanceFactory {
return new Balance(data);
}

public static async nativeForAccount(account: Account): Promise<IBalance> {
public static nativeForAccount(account: Account): IBalance {
const balance = new BigNumber(account.balance);
const limit = new BigNumber(MAX_INT64);
const minBalance = await getReservedBalance(account.numSubentries);
const minBalance = getReservedBalance(account.numSubentries);

return new Balance({
account: account.id,
Expand Down
1 change: 0 additions & 1 deletion src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export * from "./account_thresholds";
export * from "./account_flags";
export * from "./account_values";
export * from "./account_subscription_payload";
export * from "./asset";
export * from "./asset_code";
export * from "./asset_id";
export * from "./data_entry_subscription_payload";
Expand Down
89 changes: 89 additions & 0 deletions src/orm/entities/asset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { AfterLoad, ViewColumn, ViewEntity } from "typeorm";
import { AccountID, AssetCode, AssetID } from "../../model";
import { AccountFlagsFactory } from "../../model/factories";

@ViewEntity({
name: "assets",
expression: `
( SELECT (((t.assetcode)::text || '-'::text) || (t.issuer)::text) AS id,
t.assetcode AS code,
t.issuer,
t.flags,
sum(t.balance) AS "totalSupply",
sum(t.balance) FILTER (WHERE (t.flags = 1)) AS "circulatingSupply",
count(t.accountid) AS "holdersCount",
count(t.accountid) FILTER (WHERE (t.flags = 0)) AS "unauthorizedHoldersCount",
max(t.lastmodified) AS "lastActivity"
FROM trustlines t
GROUP BY t.issuer, t.assetcode
ORDER BY (count(t.accountid)) DESC)
UNION
SELECT 'native'::text AS id,
'XLM'::character varying AS code,
4 AS flags,
NULL::character varying AS issuer,
sum(accounts.balance) AS "totalSupply",
sum(accounts.balance) AS "circulatingSupply",
count(*) AS "holdersCount",
0 AS "unauthorizedHoldersCount",
max(accounts.lastmodified) AS "lastActivity"
FROM accounts
`
})
/* tslint:disable */
export class Asset {
@ViewColumn()
id: AssetID;

@ViewColumn()
code: AssetCode;

@ViewColumn()
issuer: AccountID;

@ViewColumn()
totalSupply: string;

@ViewColumn()
circulatingSupply: string;

@ViewColumn()
holdersCount: number;

@ViewColumn()
unauthorizedHoldersCount: number;

@ViewColumn()
lastActivity: number;

@ViewColumn()
flags: number;

public get paging_token() {
return this.id;
}

public get authRequired() {
return this._authRequired;
}

public get authRevocable() {
return this._authRevocable;
}

public get authImmutable() {
return this._authImmutable;
}

private _authRequired: boolean;
private _authRevocable: boolean;
private _authImmutable: boolean;

@AfterLoad()
parseFlags() {
const parsedFlags = AccountFlagsFactory.fromValue(this.flags);
this._authRequired = parsedFlags.authRequired;
this._authRevocable = parsedFlags.authRevocable;
this._authImmutable = parsedFlags.authImmutable;
}
}
2 changes: 2 additions & 0 deletions src/orm/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from "./account";
export * from "./account_data";
export * from "./asset";
export * from "./ledgerheader";
export * from "./offer";
export * from "./trustline";
23 changes: 23 additions & 0 deletions src/orm/entities/ledgerheader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Column, Entity, PrimaryColumn } from "typeorm";

@Entity("ledgerheaders")
/* tslint:disable */
export class LedgerHeader {
@PrimaryColumn({ name: "ledgerhash" })
ledgerHash: string;

@Column({ name: "prevhash" })
prevHash: string;

@Column({ name: "bucketlisthash" })
bucketListHash: string;

@Column({ name: "ledgerseq" })
seq: number;

@Column({ name: "closetime" })
closeTime: Date;

@Column()
data: string;
}
Loading

0 comments on commit 2b7db79

Please sign in to comment.