Skip to content

Commit ad0e115

Browse files
authored
fix: bugs in package caching process (#1859)
1 parent 3c16271 commit ad0e115

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

packages/builder/src/registry.test.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe('registry.ts', () => {
184184
jest.mocked(provider.readContract).mockResolvedValue({
185185
deployUrl: 'ipfs://QmV1kMdjDegcKrvSddsTmRGyCwnYERqN9o1K56g4Mw7F6i',
186186
metaUrl: 'ipfs://QmV1kMdjDegcKrvSddsTmRGyCwnYERqN9o1K56g4Mw7F6j',
187-
mutability: 'foobar',
187+
mutability: viem.stringToHex('foobar', { size: 16 }),
188188
owner: signer.address,
189189
});
190190

@@ -202,6 +202,32 @@ describe('registry.ts', () => {
202202
],
203203
});
204204
});
205+
206+
it('decodes bytes16 mutability field from on-chain hex to string', async () => {
207+
const provider = makeFakeProvider();
208+
const registry = createRegistry({ provider });
209+
210+
// The on-chain registry stores mutability as bytes16 (right-padded hex).
211+
// Before the fix, the raw hex string was returned instead of the decoded value,
212+
// causing mutability checks like `=== 'version'` to always fail.
213+
// Note: empty mutability is not tested here because stringToHex('', {size:16}) produces
214+
// 16 null bytes, which hexToString decodes to '\u0000' rather than ''. In practice,
215+
// the on-chain contract only uses 'version' and 'tag' as meaningful mutability values.
216+
for (const mutability of ['version', 'tag'] as const) {
217+
jest.mocked(provider.readContract).mockResolvedValue({
218+
deployUrl: 'ipfs://QmV1kMdjDegcKrvSddsTmRGyCwnYERqN9o1K56g4Mw7F6i',
219+
metaUrl: '',
220+
mutability: viem.stringToHex(mutability, { size: 16 }),
221+
owner: signer.address,
222+
});
223+
224+
const url = await registry.getUrl('dummy-package:0.0.1@main', 13370);
225+
226+
expect(url.mutability).toBe(mutability);
227+
// Make sure we never leak the raw hex value
228+
expect(url.mutability).not.toMatch(/^0x/);
229+
}
230+
});
205231
});
206232
});
207233
});

packages/builder/src/registry.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import _ from 'lodash';
66
import EventEmitter from 'promise-events';
77
import * as viem from 'viem';
88
import CannonRegistryAbi from './abis/CannonRegistry';
9+
import { getIpfsUrl } from './ipfs';
910
import { prepareMulticall, TxData } from './multicall';
1011
import { PackageReference } from './package-reference';
1112
import { CannonSigner } from './types';
12-
import { getIpfsUrl } from './ipfs';
1313

1414
const debug = Debug('cannon:builder:registry');
1515

@@ -529,7 +529,10 @@ export class OnChainRegistry extends CannonRegistry {
529529
// TODO: I dont understand why viem does not recognize the return type of this function (so I have to use any)
530530
})) as any;
531531

532-
return { url: onChainDeployInfo.deployUrl, mutability: onChainDeployInfo.mutability as 'version' | 'tag' | '' };
532+
return {
533+
url: onChainDeployInfo.deployUrl,
534+
mutability: viem.hexToString(onChainDeployInfo.mutability, { size: 16 }) as 'version' | 'tag' | '',
535+
};
533536
}
534537

535538
async getMetaUrl(packageOrServiceRef: string, chainId: number): Promise<string | null> {

packages/cli/src/registry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as viem from 'viem';
99
import { CliSettings } from './settings';
1010
import { log } from './util/console';
1111
import { isConnectedToInternet } from './util/is-connected-to-internet';
12-
import { resolveRegistryProviders, ProviderAction } from './util/provider';
12+
import { ProviderAction, resolveRegistryProviders } from './util/provider';
1313

1414
const debug = Debug('cannon:cli:registry');
1515

@@ -254,7 +254,7 @@ export async function createDefaultReadRegistry(
254254
// if we had to load this package from the on-chain registry and it was immutable, record
255255
if (event.result.mutability === 'version' && event.registry instanceof OnChainRegistry) {
256256
debug('caching immutable package from on-chain registry', event.fullPackageRef);
257-
await localRegistry.publish(event.fullPackageRef, event.chainId, event.result.url, '', 'version');
257+
await localRegistry.publish([event.fullPackageRef], event.chainId, event.result.url, '', 'version');
258258
}
259259
});
260260

0 commit comments

Comments
 (0)