mirror of
https://github.com/Xahau/Validation-Ledger-Tx-Store-to-xPOP.git
synced 2025-12-06 17:27:57 +00:00
Add ledger file persist
This commit is contained in:
16
index.mjs
16
index.mjs
@@ -1,7 +1,7 @@
|
||||
import { XrplClient } from 'xrpl-client'
|
||||
import { createDirectory } from './lib/createDirectory.mjs'
|
||||
import { onValidation } from './lib/onValidation.mjs'
|
||||
// import { onLedger } from './lib/onLedger.mjs'
|
||||
import { onLedger } from './lib/onLedger.mjs'
|
||||
// import { onTransaction } from './lib/onTransaction.mjs'
|
||||
import 'dotenv/config'
|
||||
import assert from 'assert'
|
||||
@@ -20,7 +20,7 @@ process.env.NODES.split(',').map(h => h.trim())
|
||||
|
||||
c.send({ command: "subscribe", streams: [
|
||||
"validations",
|
||||
// "ledger",
|
||||
"ledger",
|
||||
// "transactions",
|
||||
// "transactions_proposed"
|
||||
] })
|
||||
@@ -31,12 +31,12 @@ process.env.NODES.split(',').map(h => h.trim())
|
||||
validation,
|
||||
}))
|
||||
|
||||
// c.on("ledger", ledger => onLedger({
|
||||
// connectionUrl: c.getState()?.server?.uri,
|
||||
// networkId: c.getState()?.server?.networkId,
|
||||
// ledger,
|
||||
// connection: c,
|
||||
// }))
|
||||
c.on("ledger", ledger => onLedger({
|
||||
connectionUrl: c.getState()?.server?.uri,
|
||||
networkId: c.getState()?.server?.networkId,
|
||||
ledger,
|
||||
connection: c,
|
||||
}))
|
||||
|
||||
// c.on("transaction", transaction => onTransaction({
|
||||
// connectionUrl: c.getState()?.server?.uri,
|
||||
|
||||
78
lib/onLedger.mjs
Normal file
78
lib/onLedger.mjs
Normal file
@@ -0,0 +1,78 @@
|
||||
import { writeFile } from 'fs'
|
||||
import { stat } from 'fs'
|
||||
import { ledgerIndexToFolders } from './ledgerIndexToFolders.mjs'
|
||||
import 'dotenv/config'
|
||||
|
||||
const obtainedHumanReadableLedgers = []
|
||||
const obtainedBinaryTxLedgers = []
|
||||
|
||||
const onLedger = async ({
|
||||
networkId,
|
||||
ledger,
|
||||
connection,
|
||||
}) => {
|
||||
if ((ledger?.type || '').toUpperCase() === 'LEDGERCLOSED') {
|
||||
if (ledger?.txn_count > 0 && ledger?.ledger_index) {
|
||||
const relativeStoreDir = 'store/' + networkId + '/' + ledgerIndexToFolders(ledger.ledger_index)
|
||||
const storeDir = new URL('../' + relativeStoreDir, import.meta.url).pathname
|
||||
|
||||
const dirExists = await new Promise(resolve => stat(storeDir, staterr => resolve(!staterr)))
|
||||
|
||||
if (dirExists) {
|
||||
;[
|
||||
...(
|
||||
obtainedBinaryTxLedgers.indexOf(ledger.ledger_index) < 0
|
||||
? [
|
||||
connection.send({
|
||||
command: 'ledger',
|
||||
ledger_index: ledger.ledger_index,
|
||||
transactions: true,
|
||||
expand: true,
|
||||
binary: true,
|
||||
})
|
||||
]
|
||||
: []),
|
||||
...(
|
||||
obtainedHumanReadableLedgers.indexOf(ledger.ledger_index) < 0
|
||||
? [
|
||||
connection.send({
|
||||
command: 'ledger',
|
||||
ledger_index: ledger.ledger_index,
|
||||
})
|
||||
]
|
||||
: []),
|
||||
].map(query => query.then(results => {
|
||||
if (results?.validated && results?.ledger_index === ledger?.ledger_index && results?.ledger_hash === ledger?.ledger_hash) {
|
||||
if (results?.ledger?.transactions && obtainedBinaryTxLedgers.indexOf(ledger.ledger_index) < 0) {
|
||||
obtainedBinaryTxLedgers.unshift(results.ledger_index)
|
||||
obtainedBinaryTxLedgers.length = 250
|
||||
|
||||
console.log('Obtained ledger (binary)', results.ledger_index, results.ledger.transactions.length)
|
||||
writeFile(storeDir + '/ledger_binary_transactions.json', Buffer.from(JSON.stringify(results.ledger), 'utf8'), err => {
|
||||
if (err) {
|
||||
console.log('Error writing file @ ' + storeDir)
|
||||
}
|
||||
})
|
||||
}
|
||||
if (results?.ledger?.parent_hash && obtainedHumanReadableLedgers.indexOf(ledger.ledger_index) < 0) {
|
||||
obtainedHumanReadableLedgers.unshift(results.ledger_index)
|
||||
obtainedHumanReadableLedgers.length = 250
|
||||
|
||||
console.log('Obtained ledger (JSON object)', results.ledger_index, results.ledger.ledger_hash)
|
||||
writeFile(storeDir + '/ledger.json', Buffer.from(JSON.stringify(results.ledger), 'utf8'), err => {
|
||||
if (err) {
|
||||
console.log('Error writing file @ ' + storeDir)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
onLedger,
|
||||
}
|
||||
Reference in New Issue
Block a user