Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(synthetic-chain): Read proposals in sorted order #198

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/synthetic-chain/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agoric/synthetic-chain",
"version": "0.4.1",
"version": "0.4.2",
"description": "Utilities to build a chain and test proposals atop it",
"bin": "dist/cli/cli.js",
"main": "./dist/lib/index.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/synthetic-chain/src/cli/dockerfileGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export function writeDockerfile(
proposalName: fromTag,
proposalIdentifier: fromTag,
// XXX these are bogus
path: 'VIRTUAL',
path: '.',
type: '/agoric.swingset.CoreEvalProposal',
source: 'subdir',
};
Expand Down
25 changes: 24 additions & 1 deletion packages/synthetic-chain/src/cli/proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ export function encodeUpgradeInfo(upgradeInfo: unknown): string {
return upgradeInfo != null ? JSON.stringify(upgradeInfo) : '';
}

export function compareProposalDirNames(a: string, b: string): -1 | 0 | 1 {
// Proposal directories should be named like "$position:$name", and we expect
// $position to be numeric but this logic tolerates deviation.
// Compare by position numerically, then by position lexicographically (by
// code unit for simplicity), then by the full name.
const [_a, aPos, aNumericPos] = a.match(/^(([0-9]+)|[^:]*):.*/) || [];
const [_b, bPos, bNumericPos] = b.match(/^(([0-9]+)|[^:]*):.*/) || [];
if (aNumericPos && !bNumericPos) return -1;
if (!aNumericPos && bNumericPos) return 1;
if (aNumericPos && bNumericPos) {
if (Number(aNumericPos) < Number(bNumericPos)) return -1;
if (Number(aNumericPos) > Number(bNumericPos)) return 1;
}
if (aPos !== undefined && bPos === undefined) return -1;
if (aPos === undefined && bPos !== undefined) return 1;
if (aPos !== undefined && bPos !== undefined) {
if (aPos < bPos) return -1;
if (aPos > bPos) return 1;
}
return a < b ? -1 : a > b ? 1 : 0;
}

export function readProposals(proposalsParent: string): ProposalInfo[] {
const proposalsDir = path.join(proposalsParent, 'proposals');
const proposalPaths = fs
Expand All @@ -82,7 +104,8 @@ export function readProposals(proposalsParent: string): ProposalInfo[] {
}
return hasPackageJson;
})
.map(dirent => dirent.name);
.map(dirent => dirent.name)
.sort(compareProposalDirNames);
return proposalPaths.map(readInfo);
}

Expand Down
35 changes: 35 additions & 0 deletions packages/synthetic-chain/test/test-cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,44 @@
import test from 'ava';
import {
type ProposalInfo,
compareProposalDirNames,
imageNameForProposal,
} from '../src/cli/proposals.js';

test('compareProposalDirNames', t => {
const inputs = [
'1:first',
'9:second',
'10:third',
'99:fourth',
'100:fifth',
'foo:a',
'bar:b',
'baz:c',
'.dot:d',
'Z:e',
'qux',
'quux',
].sort();
t.deepEqual(inputs.slice().sort(compareProposalDirNames), [
// by numeric position
'1:first',
'9:second',
'10:third',
'99:fourth',
'100:fifth',
// lexicographically by whatever precedes the first colon
'.dot:d',
'Z:e',
'bar:b',
'baz:c',
'foo:a',
// lexicographically by full name
'quux',
'qux',
]);
});

test('imageNameForProposal', t => {
const proposal: ProposalInfo = {
type: '/agoric.swingset.CoreEvalProposal',
Expand Down
Loading