Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cli options to indexer scripts #138

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@solana/spl-token": "^0.1.8",
"@solana/web3.js": "^1.47.1",
"collections": "^5.1.13",
"commander": "^9.3.0",
"cors": "^2.8.5",
"express": "^4.18.1",
"pg-promise": "^10.11.1",
Expand Down
8 changes: 4 additions & 4 deletions contracts/sdk/indexer/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -862,15 +862,15 @@ export type Proof = {

// this is a top-level await
export async function bootstrap(
dbPath: string = "db",
create: boolean = true
): Promise<NFTDatabaseConnection> {
// open the database
const dir = "db";
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
if (!fs.existsSync(dbPath)) {
fs.mkdirSync(dbPath);
}
const db = await open({
filename: `${dir}/merkle.db`,
filename: `${dbPath}/merkle.db`,
driver: sqlite3.Database,
});

Expand Down
63 changes: 51 additions & 12 deletions contracts/sdk/indexer/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,66 @@ import { handleLogsAtomic } from "./indexer/log/bubblegum";
import { loadPrograms, ParseResult, ParserState } from "./indexer/utils";
import { bootstrap, NFTDatabaseConnection } from "./db";
import { fetchAndPlugGaps, plugGapsFromSlot, validateTree } from "./backfiller";

// const url = "http://api.explorer.mainnet-beta.solana.com";
const url = "http://127.0.0.1:8899";
const { program } = require("commander");

async function handleLogSubscription(
connection: Connection,
db: NFTDatabaseConnection,
logs: Logs,
ctx: Context,
parserState: ParserState,
parserState: ParserState
) {
const result = handleLogsAtomic(db, logs, ctx, parserState);
if (result === ParseResult.LogTruncated) {
console.log("\t\tLOG TRUNCATED\n\n\n\n")
await plugGapsFromSlot(connection, db, parserState, ctx.slot, 0, Number.MAX_SAFE_INTEGER);
console.log("\t\tLOG TRUNCATED\n\n\n\n");
await plugGapsFromSlot(
connection,
db,
parserState,
ctx.slot,
0,
Number.MAX_SAFE_INTEGER
);
}
}

async function main() {
async function main(url: string, dbPath: string) {
const endpoint = url;
const connection = new Connection(endpoint, "confirmed");
const payer = Keypair.generate();
const provider = new anchor.Provider(connection, new NodeWallet(payer), {
commitment: "confirmed",
});
let db = await bootstrap();
let db = await bootstrap(dbPath);
console.log("Finished bootstrapping DB");
const parserState = loadPrograms(provider);
console.log("loaded programs...");
let subscriptionId = connection.onLogs(
BUBBLEGUM_PROGRAM_ID,
(logs, ctx) => handleLogSubscription(connection, db, logs, ctx, parserState),
(logs, ctx) =>
handleLogSubscription(connection, db, logs, ctx, parserState),
"confirmed"
);
while (true) {
try {
const trees = await db.getTrees();
for (const [treeId, depth] of trees) {
console.log("Scanning for gaps");
let maxSeq = await fetchAndPlugGaps(connection, db, 0, treeId, parserState, 5);
let maxSeq = await fetchAndPlugGaps(
connection,
db,
0,
treeId,
parserState,
5
);
console.log("Validation:");
console.log(
` Off-chain tree ${treeId} is consistent: ${await validateTree(
db,
depth,
treeId,
0,
0
)}`
);
console.log("Moving to next tree");
Expand All @@ -66,4 +79,30 @@ async function main() {
}
}

main();
program.option("-u, --url <string>").option("-d, --db-path <string>");

program.parse(process.argv);
const options = program.opts();

let url = "http://127.0.0.1:8899";
if (options.url) {
switch (options.url) {
case "m":
url = "https://api.mainnet-beta.solana.com";
break;
case "d":
url = "https://api.mainnet-beta.solana.com";
break;
case "l":
break;
default:
url = options.url;
break;
}
}
let dbPath = "db";
if (options.dbPath) {
dbPath = options.dbPath;
}

main(url, dbPath);
16 changes: 13 additions & 3 deletions contracts/sdk/indexer/server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import express from "express";
import { bs58 } from "@project-serum/anchor/dist/cjs/utils/bytes";
import { bootstrap, NFTDatabaseConnection, Proof } from "./db";
import cors from 'cors';
import cors from "cors";
const { program } = require("commander");

program.option("-d, --db-path <string>");
program.parse(process.argv);
const options = program.opts();

let dbPath = "db";
if (options.dbPath) {
dbPath = options.dbPath;
}

const app = express();
app.use(cors())
app.use(cors());
app.use(express.json());

let nftDb: NFTDatabaseConnection;
Expand Down Expand Up @@ -62,6 +72,6 @@ app.get("/assets", async (req, res) => {
});

app.listen(port, async () => {
nftDb = await bootstrap(false);
nftDb = await bootstrap(dbPath, false);
console.log(`Tree server listening on port ${port}`);
});
5 changes: 5 additions & 0 deletions contracts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,11 @@ commander@^2.20.3:
resolved "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz"
integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==

commander@^9.3.0:
version "9.3.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-9.3.0.tgz#f619114a5a2d2054e0d9ff1b31d5ccf89255e26b"
integrity sha512-hv95iU5uXPbK83mjrJKuZyFM/LBAoCV/XhVGkS5Je6tl7sxr6A0ITMw5WoRV46/UaJ46Nllm3Xt7IaJhXTIkzw==

component-emitter@~1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0"
Expand Down