Skip to content

Commit

Permalink
fix: cleaning up getOrCreateDB
Browse files Browse the repository at this point in the history
  • Loading branch information
silkroadnomad committed Jan 23, 2025
1 parent de8cda8 commit d3a1169
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 19 deletions.
2 changes: 1 addition & 1 deletion relay/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libp2p-relay",
"version": "0.12.62",
"version": "0.12.63",
"private": true,
"scripts": {
"start:no-restart": "node src/relay.js",
Expand Down
9 changes: 5 additions & 4 deletions relay/src/pinner/nameOpsFileManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import PQueue from 'p-queue';
dotenv.config();

let db = null;
let orbitdb = null;
let isClosing = false;
const dbType = process.env.DB_TYPE || 'leveldb'; // Default to OrbitDB

Expand Down Expand Up @@ -91,14 +92,14 @@ class LevelDBInterface {
}
}

export async function getOrCreateDB(orbitdb) {
console.log("getOrCreateDB", db);
if (db && db.isOpen()) {
export async function getOrCreateDB(_orbitdb) {
console.log("getOrCreateDB", db?.name);
if (db) {
return db;
}

if (dbType === 'orbitdb') {
const orbitdb = new OrbitDBInterface(orbitdb);
const orbitdb = new OrbitDBInterface(_orbitdb);
console.log("orbitdb", orbitdb);
db = await orbitdb.open();
console.log("db", db);
Expand Down
2 changes: 2 additions & 0 deletions relay/src/pinner/pinningService.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const MIN_FEE = 1000000; // 0.01 DOI in swartz

export class PinningService {
constructor(helia, orbitdb, electrumClient) {
console.log("PinningService initialized");
this.helia = helia;
this.orbitdb = orbitdb;
this.electrumClient = electrumClient;
Expand All @@ -28,6 +29,7 @@ export class PinningService {
}

async initializeDatabase() {
console.log("PinningService initializeDatabase");
// If already initialized, return existing instance
if (this.db) return this.db;

Expand Down
17 changes: 9 additions & 8 deletions relay/src/pinner/scanBlockchainForNameOps.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export async function scanBlockchainForNameOps(
helia,
orbitdb,
tip,
pinningService,
_stopToken
) {
try {

pinningService = new PinningService(helia, orbitdb, electrumClient);
console.log("scanBlockchainForNameOps");
stopToken.isStopped = _stopToken;

console.info(
Expand Down Expand Up @@ -64,6 +64,7 @@ export async function scanBlockchainForNameOps(
tip,
state,
orbitdb,
pinningService,
stopToken
);
} finally {
Expand All @@ -79,21 +80,20 @@ async function processBlocks(
tip,
origState,
orbitdb,
pinningService,
stopToken
) {
const MIN_HEIGHT = 0;
let currentDay = null;
let state = null;
const pinQueue = new PQueue({ concurrency: 5 });
const dbQueue = new PQueue({ concurrency: 1 });

for (let height = startHeight; height > MIN_HEIGHT; height--) {
if (stopToken.isStopped) break;
try {
console.info(`Processing block at height ${height}`);
const { nameOpUtxos, blockDate, electrumClient: _electrumClient } = await processBlockAtHeight(height, electrumClient)
electrumClient = _electrumClient
console.info(`nameOpUtxos ${nameOpUtxos} at ${blockDate}`);
// console.info(`nameOpUtxos ${nameOpUtxos} at ${blockDate}`);
const blockDay = moment.utc(blockDate).format('YYYY-MM-DD');
if (blockDay !== currentDay) {
currentDay = blockDay;
Expand Down Expand Up @@ -122,7 +122,8 @@ async function processBlocks(
helia,
nameOp,
nameOp.nameId,
sanitizedValue
sanitizedValue,
pinningService
)
);
} else {
Expand All @@ -134,7 +135,7 @@ async function processBlocks(
// Update scanning state every 10 blocks or when reaching the stored tip height
if (height % 10 === 0 || !origState || height === origState?.tipHeight) {
console.info(`Updating scanning state at height ${height}`);
state = await updateScanningState({
await updateScanningState({
lastBlockHeight: height,
tipHeight: tip.height,
});
Expand Down Expand Up @@ -189,7 +190,7 @@ async function reconnectElectrumClient() {
}
}

async function pinIpfsContent(electrumClient, helia, nameOp, nameId, ipfsUrl) {
async function pinIpfsContent(electrumClient, helia, nameOp, nameId, ipfsUrl, pinningService) {
if (!ipfsUrl.startsWith('ipfs://')) return;

const cid = ipfsUrl.replace('ipfs://', '');
Expand Down
4 changes: 2 additions & 2 deletions relay/src/pubsubHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export function setupPubsub(
electrumClient,
fsHelia,
contentTopic,
tipWatcher
tipWatcher,
pinningService
) {
const pinningService = new PinningService(helia, orbitdb, electrumClient);
helia.libp2p.services.pubsub.subscribe(contentTopic);
helia.libp2p.services.pubsub.subscribe("doichain._peer-discovery._p2p._pubsub")

Expand Down
9 changes: 5 additions & 4 deletions relay/src/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,19 @@ logger.info('Helia and OrbitDB are running');
const fsHelia = unixfs(helia);

const tipWatcher = new TipWatcher(electrumClient);

const pinningService = new PinningService(helia, orbitdb, electrumClient);
setupPubsub(
helia,
orbitdb,
electrumClient,
fsHelia,
CONTENT_TOPIC,
tipWatcher,
pinningService
);
// createHttpServr(helia, orbitdb, electrumClient, tipWatcher);

const pinningService = new PinningService(helia, orbitdb, electrumClient);

let isScanning = false;

tipWatcher.on('newTip', async (tip) => {
Expand All @@ -117,7 +118,7 @@ tipWatcher.on('newTip', async (tip) => {
}

isScanning = true;
await scanBlockchainForNameOps(electrumClient, helia, orbitdb, tip);
await scanBlockchainForNameOps(electrumClient, helia, orbitdb, tip, pinningService);
isScanning = false;

// Then check for expired pins
Expand Down Expand Up @@ -164,7 +165,7 @@ if (!argv['disable-scanning']) {
logger.info('Scan already in progress, skipping initial scan');
} else {
isScanning = true;
await scanBlockchainForNameOps(electrumClient, helia, orbitdb);
await scanBlockchainForNameOps(electrumClient, helia, orbitdb, null, pinningService);
isScanning = false;
}
} else {
Expand Down

0 comments on commit d3a1169

Please sign in to comment.