Skip to content

Commit

Permalink
make get_transactions place TXs in the staging folder
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgaray committed Oct 10, 2024
1 parent 89579d1 commit 903e406
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ shell.nix
.direnv
blockfrost-mainnet.txt
tests/reports/
tests/serialization/staging
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"codegen": "tsx conway-cddl/codegen/main.ts",
"generate-grammar-bundle": "npx ohm generateBundles -e -t tests/api/grammar.ohm",
"generate-cdl-definitions": "make typedefs",
"get-transactions": "tsx tests/serialization/get_transactions.ts",
"cleanup-compilation-artifacts": "make clean; npm run generate-grammar-bundle"
},
"author": "",
Expand Down
37 changes: 17 additions & 20 deletions tests/serialization/get_transactions.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { BlockFrostAPI } from "@blockfrost/blockfrost-js";
import fs from "node:fs";

type TransactionInfo = { hash: string, cbor: string };
const stagingPath = "tests/serialization/staging";

// Hashes of txs to test
// Hashes of txs to test. This is not normally used, but it can be handy for
// retrieving specific transactions one might be interested in.
const transactionHashes: Array<string> = [
'fd236cb770bf5ceb67ff9ec8478a8ae9ff9f0f84fc5db9cfec391528ac054459',
'469a2f934ef014b55181a1282cac0fe35d041bf7c298fdca9115aafa4ab1e506',
'0e5bc1e21e86d011f47ba2f406337e15cc8076b9c5fc0d7283e836de93bafeaf',
'f4e2cf9b0ca03b9e344c17ddd0e2c0520830bb56b00640ce6b40deab2f3463e0',
'4caa06fe5aa051124d58261962c88ab334136996ee1976723bae43e528bf40eb',
'85e695c228d27c480c74cb9114e44857d9f66ec7611750f5f568a7382942c9ef',
'b2c7b33c780ca6b4f8a7aef57b35c8c3afa066753e5c0b311e753cea98912026',
'433927e357e1ae76f35439650045ebe0ac7701023c708c41b5fb839a5dc5a51e',
'e72ff052f0eefb5f8d0736a896e12040ccbe090f5a2838cd2d02bb7f05eb07d9',
'24da9e4e230e9c12fd3adb9ecdef55cfd0318df8c7e3882cb0cd892acd74e1fb'
];
// number of txs to retrieve if `transactionIds` is empty

// Number of txs to retrieve if `transactionIds` is empty
const transactionCount: number = 10;

// Set up blockfrost API
const projectId = fs.readFileSync("blockfrost-mainnet.txt", { encoding: "utf8" }).trimEnd();
const bf = new BlockFrostAPI({ projectId: projectId, network: "mainnet" });

// Create staging directory if it doesn't exist yet. If it exists, delete
// all files in it.
if (!fs.existsSync(stagingPath)) {
fs.mkdirSync(stagingPath);
} else {
fs.rmSync(stagingPath, { "recursive": true, "force": true })
fs.mkdirSync(stagingPath);
}

async function main(): Promise<void> {
console.log("(get_transactions) Starting");
// transactions in hex
Expand All @@ -34,16 +35,12 @@ async function main(): Promise<void> {
transactionsHex = await retrieveTxs();
};

let fd = fs.openSync("transaction_fifo", "w");
for (const [index, cbor] of transactionsHex.entries()) {
// type TransactionInfo = { hash: string, cbor: string };
const info: TransactionInfo = { hash: transactionHashes[index], cbor: cbor };
fs.writeSync(fd, `${JSON.stringify(info)}\n`);
fs.writeFileSync(`${stagingPath}/${index.toString().padStart(3, "0")}-${transactionHashes[index]}.cbor`, cbor);
}
console.log("(get_transactions) Finished writing to FIFO. Closing it.")
fs.closeSync(fd);
console.log("(get_transactions) FIFO closed. Exiting.")
console.log("(get_transactions) Finished writing transactions to staging")
}

// Retrieve the required number of TXs from the latest blocks
async function retrieveNewTxs(): Promise<Array<string>> {
let latest_block = await bf.blocksLatest();
Expand Down

0 comments on commit 903e406

Please sign in to comment.