Beta, add webserver (socket, event, dirlisting), tx ordering, etc.

This commit is contained in:
Wietse Wind
2023-10-06 00:58:05 +02:00
parent 986705326a
commit f58015933b
11 changed files with 1144 additions and 63 deletions

View File

@@ -2,6 +2,8 @@ import { writeFile } from 'fs'
import { ledgerIndexToFolders } from './ledgerIndexToFolders.mjs'
import { computeBinaryTransactionHash } from './computeBinaryTransactionHash.mjs'
import { dirExists } from './dirExists.mjs'
import { ledgerReady, waitForLedgerReady } from './events/ledgerReady.mjs'
import { onTransaction } from './onTransaction.mjs'
import 'dotenv/config'
const obtainedHumanReadableLedgers = []
@@ -18,7 +20,7 @@ const onLedger = async ({
const storeDir = new URL('../' + relativeStoreDir, import.meta.url).pathname
if (await dirExists(storeDir)) {
;[
const ledgerData = [
...(
obtainedBinaryTxLedgers.indexOf(ledger.ledger_index) < 0
? [
@@ -37,6 +39,9 @@ const onLedger = async ({
connection.send({
command: 'ledger',
ledger_index: ledger.ledger_index,
transactions: true,
expand: true,
binary: false,
})
]
: []),
@@ -71,6 +76,8 @@ const onLedger = async ({
writeFile(storeDir + '/ledger_binary_transactions.json', Buffer.from(JSON.stringify(results.ledger), 'utf8'), err => {
if (err) {
console.log('Error writing file @ ' + storeDir)
} else {
ledgerReady(results.ledger_index, 'ledger_binary_transactions')
}
})
}
@@ -87,16 +94,54 @@ const onLedger = async ({
console.log('Obtained ledger (JSON object)', relativeStoreDir, results.ledger_index, 'Hash', results.ledger.ledger_hash)
writeFile(storeDir + '/ledger_info.json', Buffer.from(JSON.stringify(results.ledger), 'utf8'), err => {
writeFile(storeDir + '/ledger_info.json', Buffer.from(JSON.stringify({ ...results.ledger, transactions: undefined, }), 'utf8'), err => {
if (err) {
console.log('Error writing file @ ' + storeDir)
} else {
ledgerReady(ledger.ledger_index, 'ledger_info')
}
})
}
}
}))
}
return results.ledger
}))
/**
* Deal with transactions & fire events
*/
waitForLedgerReady(ledger.ledger_index).then(async () => {
if (ledgerData.length > 0) {
const [binary, json] = await Promise.all(ledgerData)
const sequetiallyMappedLedgerTxEvents = (json?.transactions || []).map(tx => {
return {
validated: true,
ledger_index: ledger.ledger_index,
transaction: tx,
}
})
.sort((a, b) => a.transaction.Sequence - b.transaction.Sequence)
.reduce((promiseChain, current) => {
return promiseChain.then(() => {
// console.log(' » Tx events: Processing', current.transaction.Sequence)
return onTransaction({
networkId,
transaction: current,
connection,
})
}).then(() => {
// console.log(' » Tx events: Done ', current.transaction.Sequence)
})
}, Promise.resolve())
sequetiallyMappedLedgerTxEvents.then(() => {
// console.log(' « « « « All transactions in ledger processed', ledger.ledger_index)
});
}
})
}
}
}
}