Skip to content

Commit

Permalink
Added neighbor info to blocks and phased out CHAIN_COUNT and NETWORK_…
Browse files Browse the repository at this point in the history
…ID env vars (#2086)

* Added neighbor info to blocks and phased out CHAIN_COUNT and NETWORK_ID env vars

* Fixed default values

* fixed simulation

* linting

* fixed network config
  • Loading branch information
MRVDH committed May 15, 2024
1 parent f68ba2b commit 7e19fd3
Show file tree
Hide file tree
Showing 34 changed files with 189 additions and 76 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-plums-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kadena/graph": patch
---

Added neighbor info to blocks and phased out CHAIN_COUNT and NETWORK_ID env vars.
3 changes: 0 additions & 3 deletions packages/apps/graph/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
CHAIN_COUNT=20

# Complexity
COMPLEXITY_LIMIT=500
COMPLEXITY_ENABLED=false
Expand All @@ -13,7 +11,6 @@ PRISMA_LOG_FILENAME="prisma.log"

# The following variables are used to configure the network endpoints for the GraphQL API.
NETWORK_HOST="http://localhost:8080"
NETWORK_ID="development"
MEMPOOL_HOST="localhost:1789"
NETWORK_STATISTICS_URL="http://localhost:8080/stats"

Expand Down
29 changes: 22 additions & 7 deletions packages/apps/graph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,21 @@
- [Method 1: Using the published npm package](#method-1-using-the-published-npm-package)
- [Method 2: Building and running from source](#method-2-building-and-running-from-source)
- [Running your first query](#running-your-first-query)
- [Example queries per use case](#example-queries-per-use-case)
- [Wallet related](#wallet-related)
- [Explorer related](#explorer-related)
- [Event related](#event-related)
- [Fungible related](#fungible-related)
- [Non-fungible related](#non-fungible-related)
- [Features](#features)
- [Tracing and trace analysis](#tracing-and-trace-analysis)
- [Query Complexity](#query-complexity)
- [Prisma JSON field queries](#prisma-json-field-queries)
- [Paginated results](#paginated-results)
- [Useful extra's](#useful-extras)
- [Network Information](#network-information)
- [Running devnet](#running-devnet)
- [GraphQL](#graphql)
- [Connecting to the database](#connecting-to-the-database)
- [Simulating traffic on the devnet](#simulating-traffic-on-the-devnet)
- [Coin simulation](#coin-simulation)
Expand Down Expand Up @@ -99,25 +107,26 @@ We've curated a list of useful queries and subscriptions tailored for different

To get started, ensure you have a local instance of the graph service running on `localhost:4000`.

#### Wallet related


##### Wallet related
- [Get account balance](http://localhost:4000/graphql?query=query+GetAccountBalance+%7B%0A++fungibleAccount%28accountName%3A+%22k%3A123456789...%22%29%7B%0A++++accountName%0A++++totalBalance%0A++++fungibleName%0A++++chainAccounts%7B%0A++++++chainId%0A++++++balance%0A++++%7D%0A++%7D%0A%7D)
- [Getting account transactions](http://localhost:4000/graphql?query=query+GetAccountBalance%7B%0A++fungibleAccount%28accountName%3A+%22k%3A123456789...%22%29+%7B%0A++++transactions%28first%3A+10%29+%7B%0A++++++edges+%7B%0A++++++++node+%7B%0A++++++++++hash%0A++++++++++cmd+%7B%0A++++++++++++meta+%7B%0A++++++++++++++chainId%0A++++++++++++++creationTime%0A++++++++++++++gasLimit%0A++++++++++++++gasPrice%0A++++++++++++++sender%0A++++++++++++++ttl%0A++++++++++++%7D%0A++++++++++++payload+%7B%0A++++++++++++++...+on+ContinuationPayload+%7B%0A++++++++++++++++data%0A++++++++++++++++pactId%0A++++++++++++++++proof%0A++++++++++++++++rollback%0A++++++++++++++++step%0A++++++++++++++%7D%0A++++++++++++++...+on+ExecutionPayload+%7B%0A++++++++++++++++code%0A++++++++++++++++data%0A++++++++++++++%7D%0A++++++++++++%7D%0A++++++++++%7D%0A++++++++%7D%0A++++++%7D%0A++++%7D%0A++%7D%0A%7D)

##### Explorer related
#### Explorer related

- [Listen for a transaction](http://localhost:4000/graphql?query=subscription+ListenTransaction%7B%0A++transaction%28requestKey%3A+%22EBS5rExXr7ndvMp6nK_ie-372oIXWVX5JmmKMXkiD4Q%22%29%7B%0A++++sigs%7B%0A++++++sig%0A++++%7D%0A++++cmd%7B%0A++++++meta%7B%0A++++++++chainId%0A++++++++creationTime%0A++++++++gasLimit%0A++++++++gasPrice%0A++++++++sender%0A++++++++ttl%0A++++++%7D%0A++++++networkId%0A++++++nonce%0A++++++payload%7B%0A++++++++...on+ContinuationPayload%7B%0A++++++++++data%0A++++++++++pactId%0A++++++++++proof%0A++++++++++rollback%0A++++++++++step%0A++++++++%7D%0A++++++++...on+ExecutionPayload%7B%0A++++++++++code%0A++++++++++data%0A++++++++%7D%0A++++++%7D%0A++++++signers%7B%0A++++++++address%0A++++++++clist%7B%0A++++++++++args%0A++++++++++name%0A++++++++%7D%0A++++++++id%0A++++++++orderIndex%0A++++++++pubkey%0A++++++++scheme%0A++++++%7D%0A++++%7D%0A++++hash%0A++++id%0A++++result%7B%0A++++++...on+TransactionMempoolInfo%7B%0A++++++++status%0A++++++%7D%0A++++%7D%0A++%7D%0A%7D)
- [Get the 5 latest confirmed blocks on chain 0 and 1](http://localhost:4000/graphql?query=query+GetLatestConfirmedBlocks+%7B%0A++blocksFromDepth%28first%3A+5%2C+minimumDepth%3A+6%2C+chainIds%3A+%5B%220%22%2C+%221%22%5D%29+%7B%0A++++edges+%7B%0A++++++node+%7B%0A++++++++height%0A++++++++hash%0A++++++%7D%0A++++%7D%0A++%7D%0A%7D)

#### Event related

##### Event related
- [Listen to events](http://localhost:4000/graphql?query=subscription+GetLatestEvents%7B%0A++events%28qualifiedEventName%3A+%22coin.TRANSFER%22%29%7B%0A++++name%0A++++requestKey%0A++++parameters%0A++++orderIndex%0A++%7D%0A%7D)

##### Fungible related
#### Fungible related

- [Get data on a given account for a given fungible](http://localhost:4000/graphql?query=query+GetAccountInfoOnFungible%7B%0A++fungibleAccount%28accountName%3A%22test-coin-account%22%2C+fungibleName%3A+%22test-coin%22%29%7B%0A++++accountName%0A++++fungibleName%0A++++totalBalance%0A++++transactions%7B%0A++++++totalCount%0A++++%7D%0A++%7D%0A%7D)

#### Non-fungible related

##### Non-fungible related
- [Get token balances for a given account](http://localhost:4000/graphql?query=query+GetNFTBalances+%7B%0A++nonFungibleAccount%28accountName%3A+%22k%3A123456789...%22%29%7B%0A++++accountName%0A++++nonFungibleTokenBalances%7B%0A++++++balance%0A++++++chainId%0A++++++id%0A++++++info%7B%0A++++++++precision%0A++++++++supply%0A++++++++uri%0A++++++%7D%0A++++++version%0A++++%7D%0A++++transactions%7B%0A++++++totalCount%0A++++%7D%0A++%7D%0A%7D)

Remember, the GraphiQL Explorer is a powerful tool for understanding and interacting with our GraphQL API. Don't hesitate to experiment and learn!
Expand Down Expand Up @@ -178,6 +187,12 @@ Note that `first` can only be used with `after`, and `last` can only be used wit

## Useful extra's

### Network Information

On startup, the GraphQL server will fetch the network information from the Chainweb node. This will store it in memory and contains information about the chain ids, the network id, the network version and the neighbours of each chain.

> ⚠️ **Note:** The network information is only fetched once on startup. If you want to update the network information, you will need to restart the server.
### Running devnet

Prerequisites:
Expand All @@ -204,8 +219,8 @@ pnpm run fund -- -k <key> -a <amount>

An alternative is to run a full simulation of traffic on the devnet, see [Simulating traffic on the devnet](#simulating-traffic-on-the-devnet).

#### GraphQL

#### GraphQL
The devnet includes a built-in GraphQL server operating on port `4000`. For convenience, the HTTP API proxies the GraphQL endpoint via the `/graphql` route. Additional information and resources can be accessed on the splash page at port `8080`.

Please note: This built-in version is intended for consumption when not actively developing. If you're making changes to your project, be aware that the built-in GraphQL server will not reflect these updates. In a development scenario, you should set up your own instance of the GraphQL server to test your changes.
Expand Down
9 changes: 8 additions & 1 deletion packages/apps/graph/generated-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Block implements Node {
height: BigInt!
id: ID!
minerAccount: FungibleChainAccount!
neighbours: [BlockNeighbor!]!
parent: Block
payloadHash: String!

Expand All @@ -43,6 +44,12 @@ type BlockEventsConnectionEdge {
node: Event!
}

"""The neighbor of a block."""
type BlockNeighbor {
blockHash: String!
chainId: String!
}

type BlockTransactionsConnection {
edges: [BlockTransactionsConnectionEdge!]!
pageInfo: PageInfo!
Expand Down Expand Up @@ -390,7 +397,7 @@ type Query {
before: String

"""Default: all chains"""
chainIds: [String!] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
chainIds: [String!]
completedHeights: Boolean = false
first: Int
heightCount: Int = 3
Expand Down
3 changes: 2 additions & 1 deletion packages/apps/graph/src/devnet/deployment/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@kadena/client';
import { dotenv } from '@utils/dotenv';
import { logger } from '@utils/logger';
import { networkData } from '@utils/network';

export interface IAccount {
account: string;
Expand All @@ -27,7 +28,7 @@ const getClient = (): IClient => {
if (!client) {
client = createClient(
({ chainId }) =>
`${dotenv.NETWORK_HOST}/chainweb/0.0/${dotenv.NETWORK_ID}/chain/${chainId}/pact`,
`${dotenv.NETWORK_HOST}/chainweb/${networkData.apiVersion}/${networkData.networkId}/chain/${chainId}/pact`,
);
}
return client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const argumentConfig = {
is_upgrade: 'false',
// eslint-disable-next-line @typescript-eslint/naming-convention
upgrade_version_1: 'false',
network: dotenv.NETWORK_ID,
network: 'development', // Gets overwritten with the actual network id when called
chain: dotenv.SIMULATE_DEFAULT_CHAIN_ID,
sender: sender00.account,
kip_namespace: 'kip',
Expand Down
5 changes: 5 additions & 0 deletions packages/apps/graph/src/devnet/deployment/marmalade/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { join, relative } from 'path';
import { downloadGitFiles } from '@services/git/download-git-files';
import { flattenFolder } from '@services/git/path';
import { logger } from '@utils/logger';
import { initializeNetworkConfig, networkData } from '@utils/network';
import { validateObjectProperties } from '@utils/validate-object';
import type { IAccount } from '../helper';
import { inspect, listen, signAndAssertTransaction, submit } from '../helper';
Expand Down Expand Up @@ -36,6 +37,10 @@ export async function deployMarmaladeContracts(
codeFileDestinationPath: string = marmaladeLocalConfig.codeFilesPath,
nsDestinationPath: string = marmaladeLocalConfig.namespacePath,
): Promise<void> {
await initializeNetworkConfig();

argumentConfig.network = networkData.networkId;

logger.info('Validating repository data...');
validateConfig(
marmaladeRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { IAccount } from '@devnet/utils';
import { sender00 } from '@devnet/utils';
import type { ChainId, ICommand, IKeyPair } from '@kadena/client';
import { Pact } from '@kadena/client';
import { dotenv } from '@utils/dotenv';
import { logger } from '@utils/logger';
import { networkData } from '@utils/network';
import { readFileSync, readdirSync } from 'fs';
import { join } from 'path';
import { inspect, listen, signAndAssertTransaction, submit } from '../helper';
Expand Down Expand Up @@ -106,7 +106,7 @@ export async function createPactCommandFromFile(
filepath: string,
{
chainId,
networkId = dotenv.NETWORK_ID,
networkId = networkData.networkId,
signers = sender00.keys,
meta = {
gasLimit: 70000,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createSignWithKeypair } from '@kadena/client';
import { transferCrossChain } from '@kadena/client-utils/coin';
import { dotenv } from '@utils/dotenv';
import { logger } from '@utils/logger';
import { networkData } from '@utils/network';

export async function crossChainTransfer({
sender,
Expand Down Expand Up @@ -60,7 +61,7 @@ export async function crossChainTransfer({
{
host: dotenv.NETWORK_HOST,
defaults: {
networkId: dotenv.NETWORK_ID,
networkId: networkData.networkId,
},
sign: createSignWithKeypair([...sender.keys, ...gasPayer.keys]),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { PactNumber } from '@kadena/pactjs';
import type { ChainId } from '@kadena/types';
import { dotenv } from '@utils/dotenv';
import { logger } from '@utils/logger';
import { networkData } from '@utils/network';
import { stringifyProperty } from '../helper';

export async function safeTransfer({
Expand Down Expand Up @@ -37,7 +38,7 @@ export async function safeTransfer({
host: dotenv.NETWORK_HOST,
sign: createSignWithKeypair([...sender.keys, ...receiver.keys]),
defaults: {
networkId: dotenv.NETWORK_ID,
networkId: networkData.networkId,
},
})(
Pact.builder
Expand Down Expand Up @@ -87,7 +88,7 @@ export async function safeTransfer({
senderAccount: sender.account,
ttl: 8 * 60 * 60, //8 hours
})
.setNetworkId(dotenv.NETWORK_ID)
.setNetworkId(networkData.networkId)
.getCommand(),
).executeTo('listen');
}
10 changes: 8 additions & 2 deletions packages/apps/graph/src/devnet/simulation/coin/simulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { sender00 } from '@devnet/utils';
import type { ChainId } from '@kadena/client';
import { dotenv } from '@utils/dotenv';
import { logger } from '@utils/logger';
import { initializeNetworkConfig, networkData } from '@utils/network';
import type { TransferType } from '../file';
import { appendToFile, createFile } from '../file';
import {
Expand Down Expand Up @@ -41,6 +42,8 @@ export async function simulateCoin({
}: ISimulationOptions): Promise<void> {
const accounts: IAccount[] = [];

await initializeNetworkConfig();

// Parameters validation
if (tokenPool < maxAmount) {
logger.error(
Expand Down Expand Up @@ -172,13 +175,16 @@ export async function simulateCoin({
let result;

// This is to simulate cross chain transfers
if (transferType === 'cross-chain-transfer' && dotenv.CHAIN_COUNT > 1) {
if (
transferType === 'cross-chain-transfer' &&
networkData.chainIds.length > 1
) {
if (account.chainId === nextAccount.chainId) {
nextAccount = {
...nextAccount,
chainId: `${getRandomNumber(
seededRandomNo,
dotenv.CHAIN_COUNT,
networkData.chainIds.length,
)}` as ChainId,
};
}
Expand Down
3 changes: 2 additions & 1 deletion packages/apps/graph/src/devnet/simulation/coin/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { transferCreate } from '@kadena/client-utils/coin';
import { PactNumber } from '@kadena/pactjs';
import { dotenv } from '@utils/dotenv';
import { logger } from '@utils/logger';
import { networkData } from '@utils/network';
import { stringifyProperty } from '../helper';

export async function transfer({
Expand Down Expand Up @@ -48,7 +49,7 @@ export async function transfer({
{
host: dotenv.NETWORK_HOST,
defaults: {
networkId: dotenv.NETWORK_ID,
networkId: networkData.networkId,
},
sign: createSignWithKeypair(sender.keys),
},
Expand Down
7 changes: 5 additions & 2 deletions packages/apps/graph/src/devnet/simulation/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import { getBalance } from '@kadena/client-utils/coin';
import { genKeyPair } from '@kadena/cryptography-utils';
import type { ChainId } from '@kadena/types';
import { dotenv } from '@utils/dotenv';
import { initializeNetworkConfig, networkData } from '@utils/network';
import seedrandom from 'seedrandom';

export const generateAccount = async (
keys: number = 1,
chainId: ChainId = dotenv.SIMULATE_DEFAULT_CHAIN_ID,
): Promise<IAccount> => {
await initializeNetworkConfig();

const keyPairs = Array.from({ length: keys }, () => genKeyPair());
const account = await createPrincipal(
{
Expand All @@ -20,7 +23,7 @@ export const generateAccount = async (
{
host: dotenv.NETWORK_HOST,
defaults: {
networkId: dotenv.NETWORK_ID,
networkId: networkData.networkId,
meta: { chainId },
},
},
Expand Down Expand Up @@ -101,7 +104,7 @@ export const getAccountBalance = async ({
}): Promise<string> => {
const result = await getBalance(
account,
dotenv.NETWORK_ID,
networkData.networkId,
chainId || dotenv.SIMULATE_DEFAULT_CHAIN_ID,
dotenv.NETWORK_HOST,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
setMeta,
} from '@kadena/client/fp';
import { dotenv } from '@utils/dotenv';
import { networkData } from '@utils/network';

interface ICreateTokenIdInput {
policies?: string[];
Expand All @@ -25,7 +26,7 @@ export async function createTokenId({
return (await dirtyReadClient({
host: dotenv.NETWORK_HOST,
defaults: {
networkId: dotenv.NETWORK_ID,
networkId: networkData.networkId,
},
})(
composePactCommand(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@kadena/client/fp';
import { PactNumber } from '@kadena/pactjs';
import { dotenv } from '@utils/dotenv';
import { networkData } from '@utils/network';

interface ICreateTokenInput {
policies?: string[];
Expand Down Expand Up @@ -60,7 +61,7 @@ export async function createToken({
const config = {
host: dotenv.NETWORK_HOST,
defaults: {
networkId: dotenv.NETWORK_ID,
networkId: networkData.networkId,
},
sign: createSignWithKeypair(creator.keys),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Pact } from '@kadena/client';
import { dirtyReadClient } from '@kadena/client-utils/core';
import { composePactCommand, execution, setMeta } from '@kadena/client/fp';
import { dotenv } from '@utils/dotenv';
import { networkData } from '@utils/network';

interface ITokenInfo {
supply: number;
Expand Down Expand Up @@ -67,7 +68,7 @@ export const getTokenInfo = async (
const config = {
host: dotenv.NETWORK_HOST,
defaults: {
networkId: dotenv.NETWORK_ID,
networkId: networkData.networkId,
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@kadena/client/fp';
import type { IPactDecimal } from '@kadena/types';
import { dotenv } from '@utils/dotenv';
import { networkData } from '@utils/network';

export interface ICreateTokenInput {
tokenId: string;
Expand Down Expand Up @@ -48,7 +49,7 @@ export async function mintToken({
const config = {
host: dotenv.NETWORK_HOST,
defaults: {
networkId: dotenv.NETWORK_ID,
networkId: networkData.networkId,
},
sign: createSignWithKeypair(guard.keys),
};
Expand Down
Loading

0 comments on commit 7e19fd3

Please sign in to comment.