Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.39 KB | None | 0 0
  1. const fs = require('fs');
  2.  
  3. const DB = require('./platform/db').default;
  4. const Helpers = require('./util/helpers').default;
  5. const Constant = require('./constant').default;
  6. const Config = require('./config').default;
  7.  
  8. const options = {
  9. noMetaSync: true,
  10. noSync: true
  11. };
  12.  
  13. const blockchainDB = new DB(Config.BLOCKCHAIN_DATABASE, options);
  14. const blockchainReader = blockchainDB.initTxn();
  15. const addressDB = new DB(Config.ADDRESS_DATABASE, options);
  16.  
  17. const getBlockTxs = (hash) => {
  18. const key = Buffer.concat([Buffer.from(Constant.BLOCK_TX_PREFIX), hash]);
  19.  
  20. return blockchainDB.get(blockchainReader, key);
  21. };
  22.  
  23. const saveFile = (filename, data) => {
  24. return new Promise((resolve, reject) => {
  25. fs.writeFile(filename, data, 'utf8', (error) => {
  26. if (error) {
  27. return reject(error);
  28. }
  29.  
  30. return resolve(true);
  31. });
  32. });
  33. };
  34.  
  35. const getBlockByHash = async (hash) => {
  36. const key = Buffer.concat([
  37. Buffer.from(Constant.BLOCK_PREFIX),
  38. hash,
  39. Buffer.from(Constant.BLOCK_SUFFIX)
  40. ]);
  41. const data = blockchainDB.get(blockchainReader, key);
  42.  
  43. if (!data) {
  44. return null;
  45. }
  46.  
  47. const block = Helpers.JSONToObject(data.toString());
  48.  
  49. const txs = getBlockTxs(hash);
  50.  
  51. block.data = await Helpers.decompressData(txs, 'array');
  52. delete block.txCount;
  53. return block;
  54. };
  55.  
  56. const fillBalances = async () => {
  57. const toAddress = {};
  58. const fromAddress = {};
  59. const cursor = blockchainDB.initCursor(blockchainReader);
  60. const prefix = Buffer.from(Constant.CHAIN_PREFIX);
  61. const startKey = Buffer.concat([
  62. prefix,
  63. Helpers.writeVarInt(0)
  64. ]);
  65.  
  66. let cursorValue = cursor.goToRange(startKey);
  67.  
  68. while (cursorValue) {
  69. const key = blockchainDB.get(
  70. blockchainReader,
  71. cursorValue
  72. );
  73. const block = await getBlockByHash(key);
  74.  
  75. if (!block) {
  76. break;
  77. }
  78.  
  79. block.data.forEach((tx) => {
  80. if (tx.type === 'regular') {
  81. // populate from and to objects
  82. }
  83. });
  84.  
  85. cursorValue = cursor.goToNext();
  86. console.log(`Scanned block: ${block.index}`);
  87. }
  88.  
  89. cursor.close();
  90. blockchainReader.abort();
  91. addressDB.close();
  92. blockchainDB.close();
  93.  
  94. await saveFile('fromAddress.json', JSON.stringify(fromAddress, null, 2));
  95. await saveFile('toAddress.json', JSON.stringify(toAddress, null, 2));
  96. };
  97.  
  98. fillBalances().then(() => {
  99. console.log('Completed');
  100. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement