Skip to content

Commit

Permalink
Add custom timeout for http request precompile (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt authored Jun 27, 2024
1 parent ad144b8 commit 9972736
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 5 deletions.
3 changes: 2 additions & 1 deletion core/types/suave_structs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion core/vm/contracts_suave.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,15 @@ func (s *suaveRuntime) doHTTPRequest(request types.HttpRequest) ([]byte, error)
req.Header.Add("X-Flashbots-Signature", signature)
}

var timeout time.Duration
if request.Timeout == 0 {
timeout = 5 * time.Second
} else {
timeout = time.Duration(request.Timeout) * time.Millisecond
}

client := &http.Client{
Timeout: 5 * time.Second, // TODO: test
Timeout: timeout,
}
resp, err := client.Do(req)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion core/vm/contracts_suave_runtime_adapter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions core/vm/contracts_suave_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http/httptest"
"strings"
"testing"
"time"

"github.com/ethereum/go-ethereum/beacon/dencun"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -375,3 +376,42 @@ func TestSuave_HttpRequest_Cookies(t *testing.T) {
_, err = s.doHTTPRequest(req)
require.NoError(t, err)
}

func TestSuave_HttpRequest_Timeout(t *testing.T) {
var requestTimeout time.Duration

srv := httptest.NewServer(&httpTestHandler{
fn: func(w http.ResponseWriter, r *http.Request) {
time.Sleep(requestTimeout)
},
})

s := &suaveRuntime{
suaveContext: &SuaveContext{
Context: map[string][]byte{},
Backend: &SuaveExecutionBackend{
ExternalWhitelist: []string{"127.0.0.1"},
ServiceAliasRegistry: map[string]string{"goerli": srv.URL},
},
},
}

defer srv.Close()

call := func(timeout time.Duration) error {
req := types.HttpRequest{Url: srv.URL, Method: "GET", Timeout: uint64(timeout.Milliseconds())}
_, err := s.doHTTPRequest(req)
return err
}

requestTimeout = 2 * time.Second
require.NoError(t, call(3*time.Second))
require.Error(t, call(1*time.Second))

// check default timeout of 5 seconds
requestTimeout = 10 * time.Second
start := time.Now()
require.Error(t, call(0))
end := time.Now()
require.True(t, end.Sub(start) < 6*time.Second)
}
2 changes: 1 addition & 1 deletion suave/artifacts/SuaveLib.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion suave/artifacts/addresses.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions suave/gen/suave_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ structs:
- name: withFlashbotsSignature
description: "Whether to include the Flashbots signature"
type: bool
- name: timeout
description: "Timeout of the request in milliseconds"
type: uint64
- name: SimulateTransactionResult
description: "Result of a simulated transaction."
fields:
Expand Down
2 changes: 2 additions & 0 deletions suave/sol/libraries/Suave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ library Suave {
/// @param headers HTTP Headers
/// @param body Body of the request (if Post or Put)
/// @param withFlashbotsSignature Whether to include the Flashbots signature
/// @param timeout Timeout of the request in milliseconds
struct HttpRequest {
string url;
string method;
string[] headers;
bytes body;
bool withFlashbotsSignature;
uint64 timeout;
}

/// @notice Result of a simulated transaction.
Expand Down

0 comments on commit 9972736

Please sign in to comment.