metachris / flashbotsrpc

Golang client for Flashbots Relay, mev-geth and standard Ethereum JSON-RPC API endpoints
https://docs.flashbots.net/flashbots-auction/searchers/advanced/rpc-endpoint/
MIT License
165 stars 55 forks source link

Return both error and success responses from a broadcast #27

Closed hazim-j closed 11 months ago

hazim-j commented 11 months ago

Thanks for the amazing work on this package @metachris and also @TitanBuilder for adding the broadcaster middleware in #19.

We are currently trying to integrate flashbotsrpc into stackup-bundler to enable broadcasting of ERC-4337 UserOperations to a list of known builders. I ran into some issues with error handling which I think this PR would fix.

In a success scenario, if I broadcast a batch to 3 builders with no errors, I would expect a response like this:

{BundleResponse:{BundleHash:0x48529d4d335ffe9a0cc322edb7f8e78585ee0587eb346d6e54a884ed4bea6970} Err:<nil>}
{BundleResponse:{BundleHash:0x0923989048a5efaa098d75ce4d281335d086beea70b5085e71ead16a4a6958c2} Err:<nil>}
{BundleResponse:{BundleHash:0xbd7dd6a2c84900ab933686e418c85cd520b991f36277553e34365444e0e31941} Err:<nil>}

But in the case where one builder returns an error, we end up with a response like this:

{BundleResponse:{BundleHash:} Err:...}
{BundleResponse:{BundleHash:} Err:unexpected end of JSON input}

In this case the error from a bad builder is causing all the successful responses to be thrown away and we are adding an additional response with the error unexpected end of JSON input. Ideally the response should look like this:

{BundleResponse:{BundleHash:0x48529d4d335ffe9a0cc322edb7f8e78585ee0587eb346d6e54a884ed4bea6970} Err:<nil>}
{BundleResponse:{BundleHash:} Err:...}
{BundleResponse:{BundleHash:0xbd7dd6a2c84900ab933686e418c85cd520b991f36277553e34365444e0e31941} Err:<nil>}

The reason for this, which I address in the PR, is because:

  1. In the first loop we exit on error rather than continuing to process all other remaining HTTP responses.
  2. In the second loop we try to json.Unmarshal an error response with a nil msg which is causing the additional unexpected end of JSON input error.
metachris commented 11 months ago

lgtm. any comment @TitanBuilder ?