Skip to content

Commit

Permalink
Merge branch 'main' into improv
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKurt authored Sep 11, 2024
2 parents 63a9f51 + 8305b2b commit 987ee36
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 88 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ npm run dev -- --from-block=12345 # start indexing from the 12345th block
npm run dev -- --run-once # index and exit without watching for events
npm run dev -- --no-cache # disable cache
npm run dev -- --log-level=trace # set log level
npm run dev -- --port=8081 # start web service on a given port
```

## Running in production
Expand Down
123 changes: 81 additions & 42 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -1,66 +1,105 @@
app = 'indexer-v2'
primary_region = 'den'
kill_signal = 'SIGINT'
kill_timeout = '5s'

app = "indexer-v2"
primary_region = "den"
kill_signal = "SIGINT"
kill_timeout = "5s"

[experimental]
auto_rollback = true

[build]

[deploy]
wait_timeout = '6h0m0s'
wait_timeout = "6h0m0s"

[env]
PINO_PRETTY = 'true'
DEPLOYMENT_ENVIRONMENT = 'production'
ENABLE_RESOURCE_MONITOR = 'false'
ESTIMATES_LINEARQF_WORKER_POOL_SIZE = '10'
INDEXED_CHAINS = 'mainnet,optimism,fantom,pgn-testnet,pgn-mainnet,arbitrum,polygon,sepolia,avalanche,avalanche-fuji,scroll,scroll-sepolia,base,zksync-era-mainnet,lukso-mainnet,lukso-testnet,celo-mainnet,celo-testnet,sei-mainnet,metisAndromeda'
LOG_LEVEL = 'debug'
NODE_OPTIONS = '--max-old-space-size=4096'
PORT = '8080'
STORAGE_DIR = '/mnt/indexer'
PASSPORT_SCORER_ID=335
PINO_PRETTY = "true"
DEPLOYMENT_ENVIRONMENT = "production"
ENABLE_RESOURCE_MONITOR = "false"
ESTIMATES_LINEARQF_WORKER_POOL_SIZE = "10"
INDEXED_CHAINS = "mainnet,optimism,fantom,pgn-testnet,pgn-mainnet,arbitrum,polygon,sepolia,avalanche,avalanche-fuji,scroll,scroll-sepolia,base,zksync-era-mainnet,lukso-mainnet,lukso-testnet,celo-mainnet,celo-testnet,sei-mainnet,metisAndromeda"
LOG_LEVEL = "debug"
NODE_OPTIONS = "--max-old-space-size=4096"
PORT = "8080"
STORAGE_DIR = "/mnt/indexer"
PASSPORT_SCORER_ID = 335

[processes]
indexer = 'npm start -- --indexer --http'
web = 'npm start -- --http --http-wait-for-sync=false'
indexer = "npm start -- --indexer --http"
web = "npm start -- --http --http-wait-for-sync=false"

[[mounts]]
source = 'indexer_staging'
destination = '/mnt/indexer'
initial_size = '50GB'
source = "indexer_staging"
destination = "/mnt/indexer"
initial_size = "50GB"
auto_extend_size_threshold = 80
auto_extend_size_increment = "5GB"
auto_extend_size_limit = "100GB"
processes = ['indexer', 'web']
processes = ["indexer", "web"]

[http_service]
[[services]]
internal_port = 8080
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 2
processes = ['web']

[http_service.concurrency]
type = 'requests'
processes = ["indexer"]
protocol = "tcp"
script_checks = []

[services.concurrency]
hard_limit = 250
soft_limit = 200
type = "requests"

[checks]
[checks.http]
[[services.ports]]
force_https = true
handlers = ["http"]
port = 8080
type = 'http'
interval = '15s'
timeout = '10s'
grace_period = '30s'
method = 'get'
path = '/api/v1/status'
processes = ['web', 'indexer']

[[services.ports]]
handlers = ["tls", "http"]
port = 8081

[[services.tcp_checks]]
grace_period = "30s"
interval = "15s"
restart_limit = 0
timeout = "10s"

[[services]]
internal_port = 8080
processes = ["web"]
protocol = "tcp"
script_checks = []

[services.concurrency]
hard_limit = 250
soft_limit = 200
type = "requests"

[[services.ports]]
force_https = true
handlers = ["http"]
port = 80

[[services.ports]]
handlers = ["tls", "http"]
port = 443

[[services.tcp_checks]]
grace_period = "30s"
interval = "15s"
restart_limit = 0
timeout = "10s"

[checks.http]
port = 8080
type = "http"
interval = "15s"
timeout = "10s"
grace_period = "30s"
method = "get"
path = "/api/v1/status"
processes = ["web", "indexer"]

[[vm]]
memory = '4gb'
cpu_kind = 'performance'
cpus = 2
memory = "4gb"
cpu_kind = "performance"
cpus = 2
94 changes: 49 additions & 45 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,44 @@ export type Config = {
};

export function getConfig(): Config {
const { values: args } = parseArgs({
options: {
"to-block": {
type: "string",
},
"from-block": {
type: "string",
},
"drop-db": {
type: "boolean",
},
"rm-cache": {
type: "boolean",
},
"log-level": {
type: "string",
},
"run-once": {
type: "boolean",
},
"no-cache": {
type: "boolean",
},
"http-wait-for-sync": {
type: "string",
},
http: {
type: "boolean",
},
indexer: {
type: "boolean",
},
port: {
type: "string",
},
},
});

const buildTag = z
.union([z.string(), z.null()])
.default(null)
Expand All @@ -1858,7 +1896,17 @@ export function getConfig(): Config {
.transform((value) => value === "true")
.parse(process.env.ENABLE_RESOURCE_MONITOR);

const apiHttpPort = z.coerce.number().parse(process.env.PORT);
const portSchema = z.coerce.number().int().nonnegative().max(65535);

const portOverride = z
.union([portSchema, z.undefined()])
.optional()
.parse(args["port"]);

const apiHttpPort =
portOverride !== undefined
? portOverride
: portSchema.parse(z.coerce.number().parse(process.env.PORT));

const pinoPretty = z
.enum(["true", "false"])
Expand Down Expand Up @@ -1899,50 +1947,6 @@ export function getConfig(): Config {
.default(path.join(storageDir, "cache"))
.parse(process.env.CACHE_DIR);

const { values: args } = parseArgs({
options: {
"to-block": {
type: "string",
},
"from-block": {
type: "string",
},
"drop-db": {
type: "boolean",
},
"drop-chain-db": {
type: "boolean",
},
"drop-ipfs-db": {
type: "boolean",
},
"drop-price-db": {
type: "boolean",
},
"rm-cache": {
type: "boolean",
},
"log-level": {
type: "string",
},
"run-once": {
type: "boolean",
},
"no-cache": {
type: "boolean",
},
"http-wait-for-sync": {
type: "string",
},
http: {
type: "boolean",
},
indexer: {
type: "boolean",
},
},
});

const chains = z
.string()
.or(z.literal("all"))
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import type { EventHandlerContext } from "./indexer/indexer.js";
import { handleEvent as handleAlloV1Event } from "./indexer/allo/v1/handleEvent.js";
import { handleEvent as handleAlloV2Event } from "./indexer/allo/v2/handleEvent.js";
import { Database } from "./database/index.js";
import { decodeJsonWithBigInts } from "./utils/index.js";
import { decodeJsonWithBigInts, getExternalIP } from "./utils/index.js";
import { Block } from "chainsauce/dist/cache.js";
import { createPublicClient, http } from "viem";
import { IndexerEvents } from "chainsauce/dist/indexer.js";
Expand Down Expand Up @@ -121,6 +121,8 @@ async function main(): Promise<void> {
return decodeJsonWithBigInts(val);
});

await getExternalIP(baseLogger);

if (config.cacheDir) {
if (config.removeCache) {
await fs.rm(config.cacheDir, { recursive: true });
Expand Down
28 changes: 28 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// TODO: why is eslint not recognizing type narrowing?
/* eslint-disable @typescript-eslint/no-unsafe-argument */

import { Logger } from "pino";

/* eslint-disable @typescript-eslint/no-unsafe-member-access */
export function encodeJsonWithBigInts(value: unknown): string {
return JSON.stringify(value, (_key, value) => {
Expand Down Expand Up @@ -31,3 +34,28 @@ export const UINT64_MAX = 18446744073709551615n;
export const getDateFromTimestamp = (timestamp: bigint): Date | null => {
return timestamp < UINT64_MAX ? new Date(Number(timestamp) * 1000) : null;
};

export const getExternalIP = async (logger: Logger): Promise<string> => {
const urls = ["https://api.ipify.org?format=json", "http://ipinfo.io/json"];
for (const url of urls) {
try {
logger.debug(`Attempting to fetch IP address from: ${url}`);
const response = await fetch(url);
if (response.ok) {
const { ip } = (await response.json()) as { ip: string };
logger.info(`Successfully fetched IP address: ${ip}`);
return ip;
}
throw new Error(`Request failed with status: ${response.status}`);
} catch (error) {
if (error instanceof Error) {
logger.error(`Failed to fetch from ${url}: ${error.message}`);
} else {
logger.error(`Failed to fetch from ${url}`);
}
}
}
throw new Error(
"Unable to fetch external IP address from both primary and fallback URLs."
);
};

0 comments on commit 987ee36

Please sign in to comment.