- Changed test-cases README.md examples from python-cli to node-cli

- added input handling from --stdin
- wrapped some logic in callback function in main file as new --stind option works asynchronously
This commit is contained in:
AlexanderBuzz
2023-01-20 08:00:24 +01:00
parent a71665cca4
commit de56a1f7ad
2 changed files with 29 additions and 12 deletions

View File

@@ -636,10 +636,19 @@ class TxSerializer {
// Startup stuff begin // Startup stuff begin
function main(rawJson) {
const json = JSON.parse(rawJson)
const serializer = new TxSerializer(json)
const serializedTx = serializer.serializeTx(json)
console.log(serializedTx.toUpperCase())
}
const args = parseArgs(process.argv.slice(2), { const args = parseArgs(process.argv.slice(2), {
alias: { alias: {
'f': 'filename', 'f': 'filename',
'j': 'json', 'j': 'json',
's': 'stdin',
'v': 'verbose', 'v': 'verbose',
}, },
default: { default: {
@@ -659,12 +668,20 @@ const logger = function(verbose, value) {
let rawJson let rawJson
if (args.json) { if (args.json) {
rawJson = args.json rawJson = args.json
main(rawJson)
} else if (args.stdin) {
const stdin = process.openStdin();
let data = ""
stdin.on('data', function(chunk) {
data += chunk
});
stdin.on('end', function() {
main(data)
});
} else { } else {
rawJson = fs.readFileSync(args.filename, 'utf8') rawJson = fs.readFileSync(args.filename, 'utf8')
} main(rawJson)
}
const json = JSON.parse(rawJson)
const serializer = new TxSerializer(json)
const serializedTx = serializer.serializeTx(json)
console.log(serializedTx.toUpperCase())

View File

@@ -3,14 +3,14 @@
This folder contains several transactions in their JSON and binary forms, which This folder contains several transactions in their JSON and binary forms, which
you can use to verify the behavior of transaction serialization code. you can use to verify the behavior of transaction serialization code.
For example (starting from the `tx-serialization/` dir above this one): For example (starting from the `tx-serialization/js/` dir above this one):
```bash ```bash
$ python3 serialize.py -f test-cases/tx2.json | \ $ node index.js -f test-cases/tx2.json | \
diff - test-cases/tx2-binary.txt diff - test-cases/tx2-binary.txt
``` ```
The expected result is no output because the output of `serialize.py` matches The expected result is no output because the output of `node index.js` matches
the contents of `test-cases/tx2-binary.txt` exactly. the contents of `test-cases/tx2-binary.txt` exactly.
For an example of how the output is different if you change the `Fee` parameter of sample transaction 1, we can pipe a modified version of the file into the serializer: For an example of how the output is different if you change the `Fee` parameter of sample transaction 1, we can pipe a modified version of the file into the serializer:
@@ -18,7 +18,7 @@ For an example of how the output is different if you change the `Fee` parameter
```bash ```bash
$ cat test-cases/tx1.json | \ $ cat test-cases/tx1.json | \
sed -e 's/"Fee": "10"/"Fee": "100"/' | \ sed -e 's/"Fee": "10"/"Fee": "100"/' | \
python3 serialize.py --stdin | \ node index.js --json | \
diff - test-cases/tx1-binary.txt --color diff - test-cases/tx1-binary.txt --color
``` ```
@@ -46,7 +46,7 @@ CDC63E1DEE7FE3744630440220143759437C04F7B61F012563AFE90D8DAFC46E86035E1D965A9CED
For a friendlier display, you could pipe the output of the serializer to a file and use a visual tool like [Meld](http://meldmerge.org/) that shows intra-line differences: For a friendlier display, you could pipe the output of the serializer to a file and use a visual tool like [Meld](http://meldmerge.org/) that shows intra-line differences:
```bash ```bash
$ cat test-cases/tx1.json | sed -e 's/"Fee": "10"/"Fee": "100"/' | python3 serialize.py --stdin > /tmp/tx1-modified.txt && meld /tmp/tx1-modified.txt test-cases/tx1-binary.txt $ cat test-cases/tx1.json | sed -e 's/"Fee": "10"/"Fee": "100"/' | node index.js --stdin --stdin > /tmp/tx1-modified.txt && meld /tmp/tx1-modified.txt test-cases/tx1-binary.txt
``` ```
![Meld screenshot showing the `0A` / `64` difference](meld-example.png) ![Meld screenshot showing the `0A` / `64` difference](meld-example.png)