Skip to content

Commit fff0739

Browse files
authored
fix(pxe): warn and return first log instead of throwing on ambiguous log retrieval (#22067)
2 parents cf97fb4 + 90014f3 commit fff0739

2 files changed

Lines changed: 48 additions & 8 deletions

File tree

yarn-project/pxe/src/logs/log_service.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,48 @@ describe('LogService', () => {
8484
expect(responses[0]).not.toBeNull();
8585
});
8686

87+
it('returns first log when multiple public logs are found for a single tag', async () => {
88+
const scopedLog1 = randomTxScopedPrivateL2Log();
89+
const scopedLog2 = randomTxScopedPrivateL2Log();
90+
91+
aztecNode.getPublicLogsByTagsFromContract.mockResolvedValue([[scopedLog1, scopedLog2]]);
92+
aztecNode.getPrivateLogsByTags.mockResolvedValue([[]]);
93+
94+
const request = new LogRetrievalRequest(contractAddress, tag);
95+
const responses = await logService.fetchLogsByTag([request]);
96+
97+
expect(responses.length).toEqual(1);
98+
expect(responses[0]).not.toBeNull();
99+
});
100+
101+
it('returns first log when multiple private logs are found for a single tag', async () => {
102+
const scopedLog1 = randomTxScopedPrivateL2Log();
103+
const scopedLog2 = randomTxScopedPrivateL2Log();
104+
105+
aztecNode.getPublicLogsByTagsFromContract.mockResolvedValue([[]]);
106+
aztecNode.getPrivateLogsByTags.mockResolvedValue([[scopedLog1, scopedLog2]]);
107+
108+
const request = new LogRetrievalRequest(contractAddress, tag);
109+
const responses = await logService.fetchLogsByTag([request]);
110+
111+
expect(responses.length).toEqual(1);
112+
expect(responses[0]).not.toBeNull();
113+
});
114+
115+
it('returns first log when both a public and private log are found for a single tag', async () => {
116+
const publicLog = randomTxScopedPrivateL2Log();
117+
const privateLog = randomTxScopedPrivateL2Log();
118+
119+
aztecNode.getPublicLogsByTagsFromContract.mockResolvedValue([[publicLog]]);
120+
aztecNode.getPrivateLogsByTags.mockResolvedValue([[privateLog]]);
121+
122+
const request = new LogRetrievalRequest(contractAddress, tag);
123+
const responses = await logService.fetchLogsByTag([request]);
124+
125+
expect(responses.length).toEqual(1);
126+
expect(responses[0]).not.toBeNull();
127+
});
128+
87129
it('returns a private log if one is found', async () => {
88130
const scopedLog = randomTxScopedPrivateL2Log();
89131

yarn-project/pxe/src/logs/log_service.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ export class LogService {
5050
]);
5151

5252
if (publicLog !== null && privateLog !== null) {
53-
throw new Error(
54-
`Found both a public and private log when searching for tag ${request.tag} from contract ${request.contractAddress}`,
53+
this.log.warn(
54+
`Found both a public and private log for tag ${request.tag} from contract ${request.contractAddress}. This may indicate a contract bug. Returning the public log.`,
5555
);
5656
}
5757

@@ -73,9 +73,8 @@ export class LogService {
7373
if (logsForTag.length === 0) {
7474
return null;
7575
} else if (logsForTag.length > 1) {
76-
// TODO(#11627): handle this case
77-
throw new Error(
78-
`Got ${logsForTag.length} logs for tag ${tag} and contract ${contractAddress.toString()}. getPublicLogByTag currently only supports a single log per tag`,
76+
this.log.warn(
77+
`Expected at most 1 public log for tag ${tag} and contract ${contractAddress.toString()}, got ${logsForTag.length}. This may indicate a contract bug. Returning the first log.`,
7978
);
8079
}
8180

@@ -97,9 +96,8 @@ export class LogService {
9796
if (logsForTag.length === 0) {
9897
return null;
9998
} else if (logsForTag.length > 1) {
100-
// TODO(#11627): handle this case
101-
throw new Error(
102-
`Got ${logsForTag.length} logs for tag ${siloedTag}. getPrivateLogByTag currently only supports a single log per tag`,
99+
this.log.warn(
100+
`Expected at most 1 private log for tag ${siloedTag}, got ${logsForTag.length}. This may indicate a contract bug. Returning the first log.`,
103101
);
104102
}
105103

0 commit comments

Comments
 (0)