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

Feature/cleanup script #3212

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 37 additions & 0 deletions packages/bitcore-node/src/utils/cleanup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { BlockModel } from '../models/block';
import { Transform } from 'stream';
import { TransactionModel } from '../models/transaction';
import { CoinModel } from '../models/coin';
import { Storage } from '../services/storage';

class CleanupTransform extends Transform {
constructor() {
super({ objectMode: true });
}

async _transform(block, _, done) {
const self = this;
const txs = await TransactionModel.collection.find({ blockHash: block.hash }).toArray();
for (let tx of txs) {
let mints = await CoinModel.collection.find({ mintTxid: tx.txid }).toArray();
for (let mint of mints) {
if (mint.mintHeight != block.height && block.height > mint.mintHeight) {
self.push(mint);
}
}
}
done();
}
}

Storage.start({})
.then(() => {
let cursor = BlockModel.collection.find({}).sort({height: -1});
cursor.addCursorFlag('noCursorTimeout', true);
cursor.pipe(new CleanupTransform())
.on('data', console.log)
.on('end', () => console.log('done'));
})
.catch(e => {
console.error('fatal', e);
});