Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const DB = require('./platform/db').default;
- const Helpers = require('./util/helpers').default;
- const Constant = require('./constant').default;
- const Config = require('./config').default;
- const options = {
- noMetaSync: true,
- noSync: true
- };
- const blockchainDB = new DB(Config.BLOCKCHAIN_DATABASE, options);
- const blockchainReader = blockchainDB.initTxn();
- const addressDB = new DB(Config.ADDRESS_DATABASE, options);
- const getBlockTxs = (hash) => {
- const key = Buffer.concat([Buffer.from(Constant.BLOCK_TX_PREFIX), hash]);
- return blockchainDB.get(blockchainReader, key);
- };
- const saveFile = (filename, data) => {
- return new Promise((resolve, reject) => {
- fs.writeFile(filename, data, 'utf8', (error) => {
- if (error) {
- return reject(error);
- }
- return resolve(true);
- });
- });
- };
- const getBlockByHash = async (hash) => {
- const key = Buffer.concat([
- Buffer.from(Constant.BLOCK_PREFIX),
- hash,
- Buffer.from(Constant.BLOCK_SUFFIX)
- ]);
- const data = blockchainDB.get(blockchainReader, key);
- if (!data) {
- return null;
- }
- const block = Helpers.JSONToObject(data.toString());
- const txs = getBlockTxs(hash);
- block.data = await Helpers.decompressData(txs, 'array');
- delete block.txCount;
- return block;
- };
- const fillBalances = async () => {
- const toAddress = {};
- const fromAddress = {};
- const cursor = blockchainDB.initCursor(blockchainReader);
- const prefix = Buffer.from(Constant.CHAIN_PREFIX);
- const startKey = Buffer.concat([
- prefix,
- Helpers.writeVarInt(0)
- ]);
- let cursorValue = cursor.goToRange(startKey);
- while (cursorValue) {
- const key = blockchainDB.get(
- blockchainReader,
- cursorValue
- );
- const block = await getBlockByHash(key);
- if (!block) {
- break;
- }
- block.data.forEach((tx) => {
- if (tx.type === 'regular') {
- // populate from and to objects
- }
- });
- cursorValue = cursor.goToNext();
- console.log(`Scanned block: ${block.index}`);
- }
- cursor.close();
- blockchainReader.abort();
- addressDB.close();
- blockchainDB.close();
- await saveFile('fromAddress.json', JSON.stringify(fromAddress, null, 2));
- await saveFile('toAddress.json', JSON.stringify(toAddress, null, 2));
- };
- fillBalances().then(() => {
- console.log('Completed');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement