Files
xahau.js/tools/createValidateTests.js
Mayukha Vadari 91e7369f1b feat: add support for current sidechain design (#2039)
* Update definitions.json

* add new st types

* add tests

* add XChainClaim tx

* add XChainCommit tx

* add XChainCreateBridge tx

* add XChainCreateClaimID tx

* update definitions.json

* rename Bridge -> XChainBridge in binary codec, fix tests

* rename Bridge -> XChainBridge in models

* add codec support for XChainAddAttestation

* add XChainAddAttestation model

* undo debugging change

* fix linting issues

* update definitions.json for new rippled code, add new tests/update old tests

* add/update models

* update history

* update binary-codec

* add XChainModifyBridge model

* update RPCs

* update to latest rippled

* more fixes

* fix definitions.json

* fix spacing

* update definitions.json to avoid conflict with amm

* update definitions.json to resolve amm conflicts

* audit code

* more updates

* update rpcs

* switch to beta version

* add destination tag to XChainClaim

* rename IssuedCurrency -> Issue to match rippled

* update Issue form

* fix account object filters

* fix issue from typing

* fix LedgerEntry types

* fix attestation destination type

* Update definitions.json

* rename XChainAddAttestation -> XChainAddAttestationBatch

* add XChainAddClaimAttestation

* add XChainAddAccountCreateAttestation

* remove XChainAddAttestationBatch

* update definitions

* fix attestation txns

* fix attestation object

* add validate for new txs

* add Bridge ledger object

* add XChainOwnedClaimID ledger object

* add XChainOwnedCreateAccountClaimID ledger object

* update account_objects

* update models to latest rippled

* fix minor issues

* fix bridge ledger_entry

* add XChainModifyBridge flag

* Update definitions.json

* add rbc tests for the new txs

* update validateXChainModifyBridge

* add validate methods to other xchain txs

* fix isXChainBridge

* fix isIssue typing

* fix model types

* update changelog

* switch prepare to prepublishOnly

* add docs

* fix AccountObjectsType filter

* export common types

* fix account_objects filter

* update LedgerEntry

* add sidechain faucet info

* add snippet

* improve snippet

* fix spacing issues

* update ledger_entry

* remove AMMDelete tests for now

* Update definitions.json

* fix unit tests

* convert createValidate script to JS
* remove unneeded linter ignores

* respond to comments

* more snippet fixes

* make validate functions more readable

* add getXChainClaimID method to parse metadata

* re-add linter rules

* clean up common

* fix getXChainClaimID test

* return undefined for failed tx

* test: add model tests for new sidechain transactions (#2059)

* add XChainAddAttestation tests, fix model

* add XChainClaim model tests

* add XChainCommit model tests, fix typo

* add XChainCreateBridge model tests

* add XChainCreateClaimID model tests

* add XChainModifyBridge tests

* update to most recent version of code

* remove XChainAddAttestationBatch tests

* add more validation tests

* switch createValidateTests to JS
2023-09-26 10:01:21 -04:00

188 lines
4.5 KiB
JavaScript

const fs = require("fs");
const fixtures = require("../packages/ripple-binary-codec/test/fixtures/codec-fixtures.json");
const NORMAL_TYPES = ["number", "string"];
const NUMBERS = ["0", "1"];
function getTx(txName) {
transactions = fixtures.transactions;
const validTxs = fixtures.transactions
.filter((tx) => tx.json.TransactionType === txName)
.map((tx) => tx.json);
const validTx = validTxs[0];
delete validTx.TxnSignature;
delete validTx.SigningPubKey;
return JSON.stringify(validTx, null, 2);
}
function main() {
const modelName = process.argv[2];
const filename = `./packages/xrpl/src/models/transactions/${modelName}.ts`;
const [model, txName] = getModel(filename);
return processModel(model, txName);
}
// Extract just the model from the file
function getModel(filename) {
let model = "";
let started = false;
let ended = false;
const data = fs.readFileSync(filename, "utf8");
const lines = data.split("\n");
for (let line of lines) {
if (ended) {
continue;
}
if (!started && !line.startsWith("export")) {
continue;
}
if (!started && line.includes("Flags")) {
continue;
}
if (!started) {
started = true;
}
model += line + "\n";
if (line === "}") {
ended = true;
}
}
const name_line = model.split("\n")[0].split(" ");
const txName = name_line[2];
return [model, txName];
}
function getInvalidValue(paramTypes) {
if (paramTypes.length === 1) {
const paramType = paramTypes[0];
if (paramType == "number") {
return "'number'";
} else if (paramType == "string") {
return 123;
} else if (paramType == "IssuedCurrency") {
return JSON.stringify({ test: "test" });
} else if (paramType == "Amount") {
return JSON.stringify({ currency: "ETH" });
} else if (paramType == "XChainBridge") {
return JSON.stringify({ XChainDoor: "test" });
} else {
throw Error(`${paramType} not supported yet`);
}
}
const simplifiedParamTypes = paramTypes.filter(
(_paramType, index) => index % 2 == 0
);
if (JSON.stringify(simplifiedParamTypes) === '["0","1"]') {
return 2;
} else if (JSON.stringify(simplifiedParamTypes) === '["number","string"]') {
return JSON.stringify({ currency: "ETH" });
} else {
throw Error(`${simplifiedParamTypes} not supported yet`);
}
}
// Process the model and build the tests
function processModel(model, txName) {
let output = "";
for (let line of model.split("\n")) {
if (line == "") {
continue;
}
if (line.startsWith("export")) {
continue;
}
if (line == "}") {
continue;
}
line = line.trim();
if (line.startsWith("TransactionType")) {
continue;
}
if (line.startsWith("Flags")) {
// TODO: support flag checking
continue;
}
if (line.startsWith("/**")) {
continue;
}
if (line.startsWith("*")) {
continue;
}
const split = line.split(" ");
const param = split[0].replace("?:", "").replace(":", "").trim();
const paramTypes = split.slice(1);
const optional = split[0].endsWith("?:");
if (!optional) {
output += ` it("throws w/ missing ${param}", function () {
delete tx.${param}
assert.throws(
() => validate${txName}(tx),
ValidationError,
'${txName}: missing field ${param}',
)
assert.throws(
() => validate(tx),
ValidationError,
'${txName}: missing field ${param}',
)
})
`;
}
const fakeValue = getInvalidValue(paramTypes);
output += ` it('throws w/ invalid ${param}', function () {
tx.${param} = ${fakeValue}
assert.throws(
() => validate${txName}(tx),
ValidationError,
'${txName}: invalid field ${param}',
)
assert.throws(
() => validate(tx),
ValidationError,
'${txName}: invalid field ${param}',
)
})
`;
}
output = output.substring(0, output.length - 2);
output += "\n})\n";
output =
`import { assert } from 'chai'
import { validate, ValidationError } from '../../src'
import { validate${txName} } from '../../src/models/transactions/${txName}'
/**
* ${txName} Transaction Verification Testing.
*
* Providing runtime verification testing for each specific transaction type.
*/
describe('${txName}', function () {
let tx
beforeEach(function () {
tx = ${getTx(txName)} as any
})
it('verifies valid ${txName}', function () {
assert.doesNotThrow(() => validate${txName}(tx))
assert.doesNotThrow(() => validate(tx))
})
` + output;
return output;
}
console.log(main());