Skip to content

Commit 6ca8aca

Browse files
authored
Merge pull request #130 from hack-a-chain-software/streaming-fix
General fixes
2 parents fe01e9e + 9452c36 commit 6ca8aca

29 files changed

+593
-144
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ COPY --from=builder /app/src/config/global-bundle.pem ./dist/config/global-bundl
1414
COPY --from=builder /app/src/kadena-server/config/schema.graphql ./dist/kadena-server/config/schema.graphql
1515
EXPOSE 3001
1616

17-
CMD ["node", "dist/index.js", "--graphqlServer"]
17+
CMD ["node", "dist/index.js", "--graphql"]

indexer/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"dev:streaming": "ts-node src/index.ts --streaming",
6868
"dev:old-graphql": "ts-node src/index.ts --oldGraphql",
6969
"dev:graphql": "nodemon src/index.ts --graphql",
70+
"dev:guards": "nodemon src/index.ts --guards",
7071
"prod:start": "docker-compose up --build indexer && docker-compose logs -f indexer",
7172
"prod:streaming": "node dist/index.js --streaming",
7273
"prod:backfill": "node dist/index.js --backfill",
@@ -75,4 +76,4 @@
7576
"migrate:up": "dotenv -e .env npx sequelize-cli db:migrate",
7677
"migrate:down": "dotenv -e .env npx sequelize-cli db:migrate:undo"
7778
}
78-
}
79+
}

indexer/src/cache/init.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const MEMORY_CACHE = new NodeCache({ stdTTL: 0 });
1111

1212
const CACHE_TTL = 1000 * 60 * 5; // 5 minutes
1313

14-
export default function initCache(context: ResolverContext) {
14+
export default async function initCache(context: ResolverContext) {
1515
const { blockRepository, networkRepository } = context;
1616

1717
async function getHashRateAndTotalDifficulty() {
@@ -30,7 +30,7 @@ export default function initCache(context: ResolverContext) {
3030
};
3131
MEMORY_CACHE.set(HASH_RATE_AND_TOTAL_DIFFICULTY_KEY, newValue);
3232
} catch (err) {
33-
console.log("Error getting hash rate and total difficulty");
33+
console.log("Error getting hash rate and total difficulty", err);
3434
}
3535
}
3636

@@ -39,7 +39,7 @@ export default function initCache(context: ResolverContext) {
3939
const networkStatistics = await networkRepository.getNetworkStatistics();
4040
MEMORY_CACHE.set(NETWORK_STATISTICS_KEY, networkStatistics);
4141
} catch (err) {
42-
console.log("Error getting network statistics");
42+
console.log("Error getting network statistics", err);
4343
}
4444
}
4545

@@ -48,18 +48,18 @@ export default function initCache(context: ResolverContext) {
4848
const nodeInfo = await networkRepository.getNodeInfo();
4949
MEMORY_CACHE.set(NODE_INFO_KEY, nodeInfo);
5050
} catch (err) {
51-
console.log("Error getting node info");
51+
console.log("Error getting node info", err);
5252
}
5353
}
5454

5555
MEMORY_CACHE.set(HASH_RATE_AND_TOTAL_DIFFICULTY_KEY, {
5656
totalDifficulty: -1,
5757
});
5858

59-
const getAllInfo = () => {
60-
getHashRateAndTotalDifficulty();
61-
getNetworkStatistics();
62-
getNodeInfo();
59+
const getAllInfo = async () => {
60+
await getNetworkStatistics();
61+
await getNodeInfo();
62+
await getHashRateAndTotalDifficulty();
6363
};
6464

6565
getAllInfo();

indexer/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import { usePostgraphile } from "./server/metrics";
88
import { useKadenaGraphqlServer } from "./kadena-server/server";
99
import { closeDatabase } from "./config/database";
1010
import { initializeDatabase } from "./config/init";
11+
import { startGuardsBackfill } from "./services/sync/guards";
1112

1213
program
1314
.option("-s, --streaming", "Start streaming blockchain data")
1415
.option("-g, --oldGraphql", "Start GraphQL server based on Postgraphile")
1516
.option("-t, --graphql", "Start GraphQL server based on kadena schema")
17+
.option("-f, --guards", "Backfill the guards")
1618
.option("-z, --database", "Init the database");
1719

1820
program.parse(process.argv);
@@ -32,8 +34,9 @@ async function main() {
3234
}
3335

3436
if (options.streaming) {
35-
await initializeDatabase();
3637
await startStreaming();
38+
} else if (options.guards) {
39+
await startGuardsBackfill();
3740
} else if (options.oldGraphql) {
3841
await usePostgraphile();
3942
} else if (options.graphql) {

indexer/src/jobs/publisher-job.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import zod from "zod";
22
import { getRequiredEnvString } from "../utils/helpers";
33

4-
const eventsToPublish: Array<DispatchInfo> = [];
5-
64
const KADENA_GRAPHQL_URL = getRequiredEnvString("KADENA_GRAPHQL_API_URL");
75
const KADENA_GRAPHQL_PORT = getRequiredEnvString("KADENA_GRAPHQL_API_PORT");
86

9-
const DISPATCH_INTERVAL = 1000; // 1 second
10-
117
export const dispatchInfoSchema = zod.object({
128
hash: zod.string(),
139
chainId: zod.string(),
@@ -18,20 +14,7 @@ export const dispatchInfoSchema = zod.object({
1814

1915
export type DispatchInfo = zod.infer<typeof dispatchInfoSchema>;
2016

21-
export function startPublisher() {
22-
setInterval(() => {
23-
const [first] = eventsToPublish.splice(0, 1);
24-
if (first) {
25-
dispatch(first);
26-
}
27-
}, DISPATCH_INTERVAL);
28-
}
29-
30-
export const addPublishEvents = (events: DispatchInfo[]) => {
31-
// eventsToPublish.push(...events);
32-
};
33-
34-
const dispatch = async (dispatchInfo: DispatchInfo) => {
17+
export const dispatch = async (dispatchInfo: DispatchInfo) => {
3518
try {
3619
const url = `${KADENA_GRAPHQL_URL}:${KADENA_GRAPHQL_PORT}/new-block`;
3720
const response = await fetch(url, {

indexer/src/kadena-server/config/graphql-types.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export type FungibleChainAccount = Node & {
182182
balance: Scalars['Float']['output'];
183183
chainId: Scalars['String']['output'];
184184
fungibleName: Scalars['String']['output'];
185+
guard: Guard;
185186
id: Scalars['ID']['output'];
186187
transactions: FungibleChainAccountTransactionsConnection;
187188
transfers: FungibleChainAccountTransfersConnection;
@@ -250,6 +251,13 @@ export type GraphConfiguration = {
250251
minimumBlockHeight?: Maybe<Scalars['BigInt']['output']>;
251252
};
252253

254+
export type Guard = {
255+
__typename?: 'Guard';
256+
keys: Array<Scalars['String']['output']>;
257+
predicate: Scalars['String']['output'];
258+
raw: Scalars['String']['output'];
259+
};
260+
253261
/** Information about the network. */
254262
export type NetworkInfo = {
255263
__typename?: 'NetworkInfo';
@@ -922,7 +930,7 @@ export type ResolversUnionTypes<_RefType extends Record<string, unknown>> = {
922930

923931
/** Mapping of interface types */
924932
export type ResolversInterfaceTypes<_RefType extends Record<string, unknown>> = {
925-
Node: ( Omit<Block, 'events' | 'minerAccount' | 'parent' | 'transactions'> & { events: _RefType['BlockEventsConnection'], minerAccount: _RefType['FungibleChainAccount'], parent?: Maybe<_RefType['Block']>, transactions: _RefType['BlockTransactionsConnection'] } ) | ( Omit<Event, 'block' | 'transaction'> & { block: _RefType['Block'], transaction?: Maybe<_RefType['Transaction']> } ) | ( Omit<FungibleAccount, 'chainAccounts' | 'transactions' | 'transfers'> & { chainAccounts: Array<_RefType['FungibleChainAccount']>, transactions: _RefType['FungibleAccountTransactionsConnection'], transfers: _RefType['FungibleAccountTransfersConnection'] } ) | ( Omit<FungibleChainAccount, 'transactions' | 'transfers'> & { transactions: _RefType['FungibleChainAccountTransactionsConnection'], transfers: _RefType['FungibleChainAccountTransfersConnection'] } ) | ( Omit<NonFungibleAccount, 'transactions'> & { transactions: _RefType['NonFungibleAccountTransactionsConnection'] } ) | ( Omit<NonFungibleChainAccount, 'transactions'> & { transactions: _RefType['NonFungibleChainAccountTransactionsConnection'] } ) | ( NonFungibleTokenBalance ) | ( Signer ) | ( Omit<Transaction, 'cmd' | 'orphanedTransactions' | 'result'> & { cmd: _RefType['TransactionCommand'], orphanedTransactions?: Maybe<Array<Maybe<_RefType['Transaction']>>>, result: _RefType['TransactionInfo'] } ) | ( Omit<Transfer, 'block' | 'crossChainTransfer' | 'transaction'> & { block: _RefType['Block'], crossChainTransfer?: Maybe<_RefType['Transfer']>, transaction?: Maybe<_RefType['Transaction']> } );
933+
Node: ( Omit<Block, 'events' | 'minerAccount' | 'parent' | 'transactions'> & { events: _RefType['BlockEventsConnection'], minerAccount: _RefType['FungibleChainAccount'], parent?: Maybe<_RefType['Block']>, transactions: _RefType['BlockTransactionsConnection'] } ) | ( Omit<Event, 'block' | 'transaction'> & { block: _RefType['Block'], transaction?: Maybe<_RefType['Transaction']> } ) | ( Omit<FungibleAccount, 'chainAccounts' | 'transactions' | 'transfers'> & { chainAccounts: Array<_RefType['FungibleChainAccount']>, transactions: _RefType['FungibleAccountTransactionsConnection'], transfers: _RefType['FungibleAccountTransfersConnection'] } ) | ( Omit<FungibleChainAccount, 'guard' | 'transactions' | 'transfers'> & { guard: _RefType['Guard'], transactions: _RefType['FungibleChainAccountTransactionsConnection'], transfers: _RefType['FungibleChainAccountTransfersConnection'] } ) | ( Omit<NonFungibleAccount, 'transactions'> & { transactions: _RefType['NonFungibleAccountTransactionsConnection'] } ) | ( Omit<NonFungibleChainAccount, 'transactions'> & { transactions: _RefType['NonFungibleChainAccountTransactionsConnection'] } ) | ( NonFungibleTokenBalance ) | ( Signer ) | ( Omit<Transaction, 'cmd' | 'orphanedTransactions' | 'result'> & { cmd: _RefType['TransactionCommand'], orphanedTransactions?: Maybe<Array<Maybe<_RefType['Transaction']>>>, result: _RefType['TransactionInfo'] } ) | ( Omit<Transfer, 'block' | 'crossChainTransfer' | 'transaction'> & { block: _RefType['Block'], crossChainTransfer?: Maybe<_RefType['Transfer']>, transaction?: Maybe<_RefType['Transaction']> } );
926934
};
927935

928936
/** Mapping between all available schema types and the resolvers types */
@@ -946,14 +954,15 @@ export type ResolversTypes = {
946954
FungibleAccountTransactionsConnectionEdge: ResolverTypeWrapper<Omit<FungibleAccountTransactionsConnectionEdge, 'node'> & { node: ResolversTypes['Transaction'] }>;
947955
FungibleAccountTransfersConnection: ResolverTypeWrapper<Omit<FungibleAccountTransfersConnection, 'edges'> & { edges: Array<ResolversTypes['FungibleAccountTransfersConnectionEdge']> }>;
948956
FungibleAccountTransfersConnectionEdge: ResolverTypeWrapper<Omit<FungibleAccountTransfersConnectionEdge, 'node'> & { node: ResolversTypes['Transfer'] }>;
949-
FungibleChainAccount: ResolverTypeWrapper<Omit<FungibleChainAccount, 'transactions' | 'transfers'> & { transactions: ResolversTypes['FungibleChainAccountTransactionsConnection'], transfers: ResolversTypes['FungibleChainAccountTransfersConnection'] }>;
957+
FungibleChainAccount: ResolverTypeWrapper<Omit<FungibleChainAccount, 'guard' | 'transactions' | 'transfers'> & { guard: ResolversTypes['Guard'], transactions: ResolversTypes['FungibleChainAccountTransactionsConnection'], transfers: ResolversTypes['FungibleChainAccountTransfersConnection'] }>;
950958
FungibleChainAccountTransactionsConnection: ResolverTypeWrapper<Omit<FungibleChainAccountTransactionsConnection, 'edges'> & { edges: Array<ResolversTypes['FungibleChainAccountTransactionsConnectionEdge']> }>;
951959
FungibleChainAccountTransactionsConnectionEdge: ResolverTypeWrapper<Omit<FungibleChainAccountTransactionsConnectionEdge, 'node'> & { node: ResolversTypes['Transaction'] }>;
952960
FungibleChainAccountTransfersConnection: ResolverTypeWrapper<Omit<FungibleChainAccountTransfersConnection, 'edges'> & { edges: Array<ResolversTypes['FungibleChainAccountTransfersConnectionEdge']> }>;
953961
FungibleChainAccountTransfersConnectionEdge: ResolverTypeWrapper<Omit<FungibleChainAccountTransfersConnectionEdge, 'node'> & { node: ResolversTypes['Transfer'] }>;
954962
GasLimitEstimation: ResolverTypeWrapper<GasLimitEstimation>;
955963
GenesisHeight: ResolverTypeWrapper<GenesisHeight>;
956964
GraphConfiguration: ResolverTypeWrapper<GraphConfiguration>;
965+
Guard: ResolverTypeWrapper<Guard>;
957966
ID: ResolverTypeWrapper<Scalars['ID']['output']>;
958967
Int: ResolverTypeWrapper<Scalars['Int']['output']>;
959968
NetworkInfo: ResolverTypeWrapper<NetworkInfo>;
@@ -1025,14 +1034,15 @@ export type ResolversParentTypes = {
10251034
FungibleAccountTransactionsConnectionEdge: Omit<FungibleAccountTransactionsConnectionEdge, 'node'> & { node: ResolversParentTypes['Transaction'] };
10261035
FungibleAccountTransfersConnection: Omit<FungibleAccountTransfersConnection, 'edges'> & { edges: Array<ResolversParentTypes['FungibleAccountTransfersConnectionEdge']> };
10271036
FungibleAccountTransfersConnectionEdge: Omit<FungibleAccountTransfersConnectionEdge, 'node'> & { node: ResolversParentTypes['Transfer'] };
1028-
FungibleChainAccount: Omit<FungibleChainAccount, 'transactions' | 'transfers'> & { transactions: ResolversParentTypes['FungibleChainAccountTransactionsConnection'], transfers: ResolversParentTypes['FungibleChainAccountTransfersConnection'] };
1037+
FungibleChainAccount: Omit<FungibleChainAccount, 'guard' | 'transactions' | 'transfers'> & { guard: ResolversParentTypes['Guard'], transactions: ResolversParentTypes['FungibleChainAccountTransactionsConnection'], transfers: ResolversParentTypes['FungibleChainAccountTransfersConnection'] };
10291038
FungibleChainAccountTransactionsConnection: Omit<FungibleChainAccountTransactionsConnection, 'edges'> & { edges: Array<ResolversParentTypes['FungibleChainAccountTransactionsConnectionEdge']> };
10301039
FungibleChainAccountTransactionsConnectionEdge: Omit<FungibleChainAccountTransactionsConnectionEdge, 'node'> & { node: ResolversParentTypes['Transaction'] };
10311040
FungibleChainAccountTransfersConnection: Omit<FungibleChainAccountTransfersConnection, 'edges'> & { edges: Array<ResolversParentTypes['FungibleChainAccountTransfersConnectionEdge']> };
10321041
FungibleChainAccountTransfersConnectionEdge: Omit<FungibleChainAccountTransfersConnectionEdge, 'node'> & { node: ResolversParentTypes['Transfer'] };
10331042
GasLimitEstimation: GasLimitEstimation;
10341043
GenesisHeight: GenesisHeight;
10351044
GraphConfiguration: GraphConfiguration;
1045+
Guard: Guard;
10361046
ID: Scalars['ID']['output'];
10371047
Int: Scalars['Int']['output'];
10381048
NetworkInfo: NetworkInfo;
@@ -1221,6 +1231,7 @@ export type FungibleChainAccountResolvers<ContextType = any, ParentType extends
12211231
balance?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
12221232
chainId?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
12231233
fungibleName?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
1234+
guard?: Resolver<ResolversTypes['Guard'], ParentType, ContextType>;
12241235
id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>;
12251236
transactions?: Resolver<ResolversTypes['FungibleChainAccountTransactionsConnection'], ParentType, ContextType, RequireFields<FungibleChainAccountTransactionsArgs, 'first' | 'last'>>;
12261237
transfers?: Resolver<ResolversTypes['FungibleChainAccountTransfersConnection'], ParentType, ContextType, RequireFields<FungibleChainAccountTransfersArgs, 'first' | 'last'>>;
@@ -1273,6 +1284,13 @@ export type GraphConfigurationResolvers<ContextType = any, ParentType extends Re
12731284
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
12741285
};
12751286

1287+
export type GuardResolvers<ContextType = any, ParentType extends ResolversParentTypes['Guard'] = ResolversParentTypes['Guard']> = {
1288+
keys?: Resolver<Array<ResolversTypes['String']>, ParentType, ContextType>;
1289+
predicate?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
1290+
raw?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
1291+
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
1292+
};
1293+
12761294
export type NetworkInfoResolvers<ContextType = any, ParentType extends ResolversParentTypes['NetworkInfo'] = ResolversParentTypes['NetworkInfo']> = {
12771295
apiVersion?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
12781296
coinsInCirculation?: Resolver<ResolversTypes['Float'], ParentType, ContextType>;
@@ -1639,6 +1657,7 @@ export type Resolvers<ContextType = any> = {
16391657
GasLimitEstimation?: GasLimitEstimationResolvers<ContextType>;
16401658
GenesisHeight?: GenesisHeightResolvers<ContextType>;
16411659
GraphConfiguration?: GraphConfigurationResolvers<ContextType>;
1660+
Guard?: GuardResolvers<ContextType>;
16421661
NetworkInfo?: NetworkInfoResolvers<ContextType>;
16431662
Node?: NodeResolvers<ContextType>;
16441663
NonFungibleAccount?: NonFungibleAccountResolvers<ContextType>;

indexer/src/kadena-server/config/schema.graphql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ type FungibleChainAccount implements Node {
432432
balance: Float!
433433
chainId: String!
434434
fungibleName: String!
435+
guard: Guard!
435436

436437
transactions(
437438
after: String
@@ -790,3 +791,9 @@ type NonFungibleChainAccountTransactionsConnectionEdge {
790791
cursor: String!
791792
node: Transaction!
792793
}
794+
795+
type Guard {
796+
keys: [String!]!
797+
predicate: String!
798+
raw: String!
799+
}

indexer/src/kadena-server/repository/application/balance-repository.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,27 @@ export default interface BalanceRepository {
7171
chainId: string,
7272
tokenId: string,
7373
): Promise<INonFungibleTokenBalance | null>;
74+
75+
/** methods below use balance from node */
76+
getAccountInfo_NODE(
77+
accountName: string,
78+
fungibleName?: string | null,
79+
): Promise<FungibleAccountOutput>;
80+
81+
getChainsAccountInfo_NODE(
82+
accountName: string,
83+
fungibleName: string,
84+
chainIds?: string[],
85+
): Promise<FungibleChainAccountOutput[]>;
86+
87+
getAccountsByPublicKey_NODE(
88+
publicKey: string,
89+
fungibleName: string,
90+
): Promise<FungibleAccountOutput[]>;
91+
92+
getChainAccountsByPublicKey_NODE(
93+
publicKey: string,
94+
fungibleName: string,
95+
chainId: string,
96+
): Promise<FungibleChainAccountOutput[]>;
7497
}

indexer/src/kadena-server/repository/application/block-repository.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export default interface BlockRepository {
4848
pageInfo: PageInfo;
4949
edges: ConnectionEdge<BlockOutput>[];
5050
}>;
51-
getMinerData(hash: string): Promise<FungibleChainAccountOutput>;
51+
getMinerData(
52+
hash: string,
53+
chainId: string,
54+
): Promise<FungibleChainAccountOutput>;
5255

5356
getLowestBlockHeight(): Promise<number>;
5457

0 commit comments

Comments
 (0)