Skip to content

Commit

Permalink
feat(deployment): add scripts to help find nondeterminism
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 1, 2021
1 parent 6f888ba commit a1065c0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/deployment/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@
"prettier": {
"trailingComma": "all",
"singleQuote": true
},
"devDependencies": {
"readline-transform": "^1.0.0"
}
}
74 changes: 74 additions & 0 deletions packages/deployment/scripts/crunch.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#! /usr/bin/env node
// crunch.mjs - crunch a kvstore trace file's writes into TSV

/* eslint-disable no-continue */
/* global process,Buffer */

import fs from 'fs';
// eslint-disable-next-line import/no-extraneous-dependencies
import ReadlineTransform from 'readline-transform';

const [TRACE_FILE, LAST_BLOCK_HEIGHT = 0] = process.argv.slice(2);
if (!TRACE_FILE) {
console.error('usage: crunch trace-file [last-block-height]');
process.exit(1);
}

const escapeCharCode = c => {
switch (c) {
// backslash-escape the following characters
case 92:
return '\\';
case 10:
return '\\n';
case 13:
return '\\r';
case 9:
return '\\t';
case 0:
return '\\0';
default: {
if (c < 0x20 || c > 0x7e) {
return `\\x${c.toString(16).padStart(2, '0')}`;
}
return String.fromCharCode(c);
}
}
};

const decode = b64 => {
const b = Buffer.from(b64, 'base64');
return Array.from(b)
.map(escapeCharCode)
.join('');
};

const main = async () => {
const rl = new ReadlineTransform();
fs.createReadStream(TRACE_FILE).pipe(rl);

for await (const line of rl) {
const { operation, key, value, metadata } = JSON.parse(line);
if (operation !== 'write') {
continue;
}
if (metadata && metadata.blockHeight > LAST_BLOCK_HEIGHT) {
break;
}
const decodedKey = decode(key);
if (decodedKey.match(/^\w+\/fwd\/0x[0-9a-f]+$/)) {
// Probably capability module.
continue;
}

const decodedValue = decode(value);
const obj = { operation, key: decodedKey, value: decodedValue, metadata };

// Write a tab-separated line of key, value, metadata.
process.stdout.write(
`${obj.key}\t${obj.value}\t${JSON.stringify(obj.metadata)}\n`,
);
}
};

await main();
17 changes: 17 additions & 0 deletions packages/deployment/scripts/find-nondeterminism.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#! /bin/bash

set -ueo pipefail

real0=$(readlink "${BASH_SOURCE[0]}" || echo "${BASH_SOURCE[0]}")
thisdir=$(cd "$(dirname -- "$real0")" > /dev/null && pwd -P)

# unzip deployment-test-results.zip

for v in 0 1; do
grep -E '"write"' validator$v-kvstore.trace > v$v.write
grep -E '"metadata":null|blockHeight":1,' v$v.write > v$v.write-block1
"$thisdir/crunch.mjs" validator$v-kvstore.trace > v$v.crunch
sort v$v.crunch > v$v.sorted
done

diff v0.sorted v1.sorted
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17102,6 +17102,11 @@ readdirp@~3.6.0:
dependencies:
picomatch "^2.2.1"

readline-transform@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/readline-transform/-/readline-transform-1.0.0.tgz#3157f97428acaec0f05a5c1ff2c3120f4e6d904b"
integrity sha512-7KA6+N9IGat52d83dvxnApAWN+MtVb1MiVuMR/cf1O4kYsJG+g/Aav0AHcHKsb6StinayfPLne0+fMX2sOzAKg==

[email protected]:
version "2.2.2"
resolved "https://registry.yarnpkg.com/recursive-readdir/-/recursive-readdir-2.2.2.tgz#9946fb3274e1628de6e36b2f6714953b4845094f"
Expand Down

0 comments on commit a1065c0

Please sign in to comment.