generated from flashbots/go-template
-
Notifications
You must be signed in to change notification settings - Fork 118
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
Use SSZ encoding for block validation #600
Open
avalonche
wants to merge
2
commits into
main
Choose a base branch
from
ssz-block-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
@@ -35,17 +36,19 @@ type IBlockSimRateLimiter interface { | |
} | ||
|
||
type BlockSimulationRateLimiter struct { | ||
cv *sync.Cond | ||
counter int64 | ||
blockSimURL string | ||
client http.Client | ||
cv *sync.Cond | ||
counter int64 | ||
blockSimURL string | ||
blockSimHTTPURL string | ||
client http.Client | ||
} | ||
|
||
func NewBlockSimulationRateLimiter(blockSimURL string) *BlockSimulationRateLimiter { | ||
func NewBlockSimulationRateLimiter(blockSimURL, blockSimHTTPURL string) *BlockSimulationRateLimiter { | ||
return &BlockSimulationRateLimiter{ | ||
cv: sync.NewCond(&sync.Mutex{}), | ||
counter: 0, | ||
blockSimURL: blockSimURL, | ||
cv: sync.NewCond(&sync.Mutex{}), | ||
counter: 0, | ||
blockSimURL: blockSimURL, | ||
blockSimHTTPURL: blockSimHTTPURL, | ||
client: http.Client{ //nolint:exhaustruct | ||
Timeout: simRequestTimeout, | ||
}, | ||
|
@@ -71,7 +74,6 @@ func (b *BlockSimulationRateLimiter) Send(context context.Context, payload *comm | |
return fmt.Errorf("%w, %w", ErrRequestClosed, err), nil | ||
} | ||
|
||
var simReq *jsonrpc.JSONRPCRequest | ||
if payload.Version == spec.DataVersionCapella && payload.Capella == nil { | ||
return ErrNoCapellaPayload, nil | ||
} | ||
|
@@ -95,7 +97,14 @@ func (b *BlockSimulationRateLimiter) Send(context context.Context, payload *comm | |
headers.Add("X-Fast-Track", "true") | ||
} | ||
|
||
if (b.blockSimHTTPURL != "") && (payload.Version == spec.DataVersionDeneb) { | ||
// Create and fire off HTTP request | ||
requestErr, validationErr = SendHTTPRequest(&b.client, payload, b.blockSimHTTPURL, headers) | ||
return requestErr, validationErr | ||
} | ||
|
||
// Create and fire off JSON-RPC request | ||
var simReq *jsonrpc.JSONRPCRequest | ||
if payload.Version == spec.DataVersionDeneb { | ||
simReq = jsonrpc.NewJSONRPCRequest("1", "flashbots_validateBuilderSubmissionV3", payload) | ||
} else { | ||
|
@@ -110,6 +119,49 @@ func (b *BlockSimulationRateLimiter) CurrentCounter() int64 { | |
return atomic.LoadInt64(&b.counter) | ||
} | ||
|
||
func SendHTTPRequest(client *http.Client, req *common.BuilderBlockValidationRequest, url string, headers http.Header) (requestErr, validationErr error) { | ||
payloadBytes, err := req.MarshalSSZ() | ||
if err != nil { | ||
return fmt.Errorf("could not marshal request: %w", err), nil | ||
} | ||
httpReq, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(payloadBytes)) | ||
if err != nil { | ||
return fmt.Errorf("invalid request for %s: %w", url, err), nil | ||
} | ||
|
||
httpReq.Header.Add("Content-Type", "application/octet-stream") | ||
httpReq.Header.Add("Eth-Consensus-Version", strings.ToLower(req.Version.String())) | ||
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. allow future forks to reuse the same endpoint instead of a new endpoint incremented by version number, like in the JSON RPC endpoint. |
||
httpReq.Header.Set("Accept", "application/json") | ||
for k, v := range headers { | ||
httpReq.Header.Add(k, v[0]) | ||
} | ||
|
||
// execute request | ||
resp, err := client.Do(httpReq) | ||
if err != nil { | ||
return err, nil | ||
} | ||
defer resp.Body.Close() | ||
|
||
bodyBytes, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("could not read response body for %s: %w", url, err), nil | ||
} | ||
|
||
if resp.StatusCode >= http.StatusMultipleChoices { | ||
ec := &struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
}{} | ||
if err = json.Unmarshal(bodyBytes, ec); err != nil { | ||
return fmt.Errorf("could not unmarshal error response from validation node for %s from %s: %w", url, string(bodyBytes), err), nil | ||
} | ||
return nil, fmt.Errorf("%w: %s", ErrSimulationFailed, ec.Message) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
// SendJSONRPCRequest sends the request to URL and returns the general JsonRpcResponse, or an error (note: not the JSONRPCError) | ||
func SendJSONRPCRequest(client *http.Client, req jsonrpc.JSONRPCRequest, url string, headers http.Header) (res *jsonrpc.JSONRPCResponse, requestErr, validationErr error) { | ||
buf, err := json.Marshal(req) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
please add comments to the two
BLOCKSIM_URI
to clarify what they are used for