-
Notifications
You must be signed in to change notification settings - Fork 42
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
feat(filter)!: return error codes instead of throwing errors #1971
Changes from all commits
dd1ce6d
8226ee3
2303e79
07ed52f
f03e86d
b6c116e
2688ec9
cf9fda2
05b1baa
318bbfd
85cb393
ffb9b2d
891ed66
fc1cacf
4e37405
f2d6d8f
f48c58d
fd7d61c
0f7d3c2
1f6e420
365fae0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,20 @@ | ||
import type { Peer } from "@libp2p/interface"; | ||
import type { Peer, Stream } from "@libp2p/interface"; | ||
import type { IncomingStreamData } from "@libp2p/interface-internal"; | ||
import type { | ||
ContentTopic, | ||
IBaseProtocolCore, | ||
Libp2p, | ||
ProtocolCreateOptions, | ||
PubsubTopic | ||
import { | ||
type ContentTopic, | ||
type CoreProtocolResult, | ||
type IBaseProtocolCore, | ||
type Libp2p, | ||
type ProtocolCreateOptions, | ||
ProtocolError, | ||
type PubsubTopic | ||
} from "@waku/interfaces"; | ||
import { WakuMessage } from "@waku/proto"; | ||
import { Logger } from "@waku/utils"; | ||
import all from "it-all"; | ||
import * as lp from "it-length-prefixed"; | ||
import { pipe } from "it-pipe"; | ||
import { Uint8ArrayList } from "uint8arraylist"; | ||
|
||
import { BaseProtocol } from "../base_protocol.js"; | ||
|
||
|
@@ -90,53 +93,106 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore { | |
pubsubTopic: PubsubTopic, | ||
peer: Peer, | ||
contentTopics: ContentTopic[] | ||
): Promise<void> { | ||
): Promise<CoreProtocolResult> { | ||
const stream = await this.getStream(peer); | ||
|
||
const request = FilterSubscribeRpc.createSubscribeRequest( | ||
pubsubTopic, | ||
contentTopics | ||
); | ||
|
||
const res = await pipe( | ||
[request.encode()], | ||
lp.encode, | ||
stream, | ||
lp.decode, | ||
async (source) => await all(source) | ||
); | ||
|
||
if (!res || !res.length) { | ||
throw Error( | ||
`No response received for request ${request.requestId}: ${res}` | ||
let res: Uint8ArrayList[] | undefined; | ||
try { | ||
res = await pipe( | ||
[request.encode()], | ||
lp.encode, | ||
stream, | ||
lp.decode, | ||
async (source) => await all(source) | ||
); | ||
} catch (error) { | ||
log.error("Failed to send subscribe request", error); | ||
return { | ||
success: null, | ||
failure: { | ||
error: ProtocolError.GENERIC_FAIL, | ||
peerId: peer.id | ||
} | ||
}; | ||
} | ||
|
||
const { statusCode, requestId, statusDesc } = | ||
FilterSubscribeResponse.decode(res[0].slice()); | ||
|
||
if (statusCode < 200 || statusCode >= 300) { | ||
throw new Error( | ||
log.error( | ||
`Filter subscribe request ${requestId} failed with status code ${statusCode}: ${statusDesc}` | ||
); | ||
return { | ||
failure: { | ||
error: ProtocolError.REMOTE_PEER_REJECTED, | ||
peerId: peer.id | ||
}, | ||
success: null | ||
}; | ||
} | ||
|
||
return { | ||
failure: null, | ||
success: peer.id | ||
}; | ||
} | ||
|
||
async unsubscribe( | ||
pubsubTopic: PubsubTopic, | ||
peer: Peer, | ||
contentTopics: ContentTopic[] | ||
): Promise<void> { | ||
const stream = await this.getStream(peer); | ||
): Promise<CoreProtocolResult> { | ||
let stream: Stream | undefined; | ||
try { | ||
stream = await this.getStream(peer); | ||
} catch (error) { | ||
log.error( | ||
`Failed to get a stream for remote peer${peer.id.toString()}`, | ||
error | ||
); | ||
return { | ||
success: null, | ||
failure: { | ||
error: ProtocolError.REMOTE_PEER_FAULT, | ||
peerId: peer.id | ||
} | ||
}; | ||
} | ||
|
||
const unsubscribeRequest = FilterSubscribeRpc.createUnsubscribeRequest( | ||
pubsubTopic, | ||
contentTopics | ||
); | ||
|
||
await pipe([unsubscribeRequest.encode()], lp.encode, stream.sink); | ||
try { | ||
await pipe([unsubscribeRequest.encode()], lp.encode, stream.sink); | ||
} catch (error) { | ||
log.error("Failed to send unsubscribe request", error); | ||
return { | ||
success: null, | ||
failure: { | ||
error: ProtocolError.GENERIC_FAIL, | ||
peerId: peer.id | ||
} | ||
}; | ||
} | ||
|
||
return { | ||
success: peer.id, | ||
failure: null | ||
}; | ||
} | ||
|
||
async unsubscribeAll(pubsubTopic: PubsubTopic, peer: Peer): Promise<void> { | ||
async unsubscribeAll( | ||
pubsubTopic: PubsubTopic, | ||
peer: Peer | ||
): Promise<CoreProtocolResult> { | ||
const stream = await this.getStream(peer); | ||
|
||
const request = FilterSubscribeRpc.createUnsubscribeAllRequest(pubsubTopic); | ||
|
@@ -150,53 +206,105 @@ export class FilterCore extends BaseProtocol implements IBaseProtocolCore { | |
); | ||
|
||
if (!res || !res.length) { | ||
throw Error( | ||
`No response received for request ${request.requestId}: ${res}` | ||
); | ||
return { | ||
failure: { | ||
error: ProtocolError.REMOTE_PEER_FAULT, | ||
peerId: peer.id | ||
}, | ||
success: null | ||
}; | ||
} | ||
|
||
const { statusCode, requestId, statusDesc } = | ||
FilterSubscribeResponse.decode(res[0].slice()); | ||
|
||
if (statusCode < 200 || statusCode >= 300) { | ||
throw new Error( | ||
log.error( | ||
`Filter unsubscribe all request ${requestId} failed with status code ${statusCode}: ${statusDesc}` | ||
); | ||
return { | ||
failure: { | ||
error: ProtocolError.REMOTE_PEER_REJECTED, | ||
peerId: peer.id | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here and in similar places - can we map like in case if full node has no capacity (ddos etc) - it might return some particular code to cover this I think we should do follow up, and sync with @NagyZoltanPeter of what error codes are expected here and in similar places There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agree - that would be useful. we do follow generic error handling based on const { statusCode, requestId, statusDesc } =
FilterSubscribeResponse.decode(res[0].slice());
if (statusCode < 200 || statusCode >= 300) {
log.error(
`Filter unsubscribe all request ${requestId} failed with status code ${statusCode}: ${statusDesc}`
);
return {
failure: {
error: ProtocolError.REMOTE_PEER_REJECTED,
peerId: peer.id
},
success: null
};
} creating an issue to track this: #1999 |
||
}, | ||
success: null | ||
}; | ||
} | ||
|
||
return { | ||
failure: null, | ||
success: peer.id | ||
}; | ||
} | ||
|
||
async ping(peer: Peer): Promise<void> { | ||
const stream = await this.getStream(peer); | ||
async ping(peer: Peer): Promise<CoreProtocolResult> { | ||
let stream: Stream | undefined; | ||
try { | ||
stream = await this.getStream(peer); | ||
} catch (error) { | ||
log.error( | ||
`Failed to get a stream for remote peer${peer.id.toString()}`, | ||
error | ||
); | ||
return { | ||
success: null, | ||
failure: { | ||
error: ProtocolError.REMOTE_PEER_FAULT, | ||
peerId: peer.id | ||
} | ||
}; | ||
} | ||
|
||
const request = FilterSubscribeRpc.createSubscriberPingRequest(); | ||
|
||
let res: Uint8ArrayList[] | undefined; | ||
try { | ||
const res = await pipe( | ||
res = await pipe( | ||
[request.encode()], | ||
lp.encode, | ||
stream, | ||
lp.decode, | ||
async (source) => await all(source) | ||
); | ||
|
||
if (!res || !res.length) { | ||
throw Error( | ||
`No response received for request ${request.requestId}: ${res}` | ||
); | ||
} | ||
|
||
const { statusCode, requestId, statusDesc } = | ||
FilterSubscribeResponse.decode(res[0].slice()); | ||
|
||
if (statusCode < 200 || statusCode >= 300) { | ||
throw new Error( | ||
`Filter ping request ${requestId} failed with status code ${statusCode}: ${statusDesc}` | ||
); | ||
} | ||
log.info(`Ping successful for peer ${peer.id.toString()}`); | ||
} catch (error) { | ||
log.error("Error pinging: ", error); | ||
throw error; // Rethrow the actual error instead of wrapping it | ||
log.error("Failed to send ping request", error); | ||
return { | ||
success: null, | ||
failure: { | ||
error: ProtocolError.GENERIC_FAIL, | ||
peerId: peer.id | ||
} | ||
}; | ||
} | ||
|
||
if (!res || !res.length) { | ||
return { | ||
success: null, | ||
failure: { | ||
error: ProtocolError.REMOTE_PEER_FAULT, | ||
peerId: peer.id | ||
} | ||
}; | ||
} | ||
|
||
const { statusCode, requestId, statusDesc } = | ||
FilterSubscribeResponse.decode(res[0].slice()); | ||
|
||
if (statusCode < 200 || statusCode >= 300) { | ||
log.error( | ||
`Filter ping request ${requestId} failed with status code ${statusCode}: ${statusDesc}` | ||
); | ||
return { | ||
success: null, | ||
failure: { | ||
error: ProtocolError.REMOTE_PEER_REJECTED, | ||
peerId: peer.id | ||
} | ||
}; | ||
} | ||
return { | ||
success: peer.id, | ||
failure: null | ||
}; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't it be more concrete that
generic
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should make errors as informative as possible (without creating a mess) as it is the only thing developer would get, so let's try not to use
GENERIC_FAIL
imhoThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, this is also what we do during any lightpush request.
if
pipe
fails unexpectedly, we can't be sure of what went wrong.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there no response that we can read? I expect full node potentially send some message if it is on the side of it, but if failure is in pipe - then it can be generic (though in that case I still think there is a reason we can root cause)
can you repro unexpected fails of
pipe
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no test vector as such for that
--
this try catch case actually handles error if error is thrown and there is no defined response from the pipe operation
if you follow the next few lines you will see we also handle errors based on bad values of
res
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eg:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe in that case
GENERIC_FAIL
onpipe
means - failed to send a request, as it fails locally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, could it fail while receiving a response for the remote peer?
are we sure about that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dunno, I think we need to investigate it - if it bothers us