Skip to content

Commit

Permalink
fix(synthetic-chain): Read proposals in sorted order
Browse files Browse the repository at this point in the history
Fixes #197
  • Loading branch information
gibson042 committed Nov 19, 2024
1 parent be44746 commit 03ebcf1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
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

0 comments on commit 03ebcf1

Please sign in to comment.