Skip to content
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

Force to get the offchain data #1115

Open
wants to merge 4 commits into
base: zkevm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions zk/da/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,40 @@ package da
import (
"context"
"fmt"
"net/http"
"strings"
"time"

"github.com/gateway-fm/cdk-erigon-lib/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/zkevm/jsonrpc/client"
"github.com/ledgerwatch/erigon/zkevm/jsonrpc/types"
)

const maxAttempts = 10
const retryDelay = 500 * time.Millisecond

func GetOffChainData(ctx context.Context, url string, hash common.Hash) ([]byte, error) {
attemp := 0

for attemp < maxAttempts {
response, err := client.JSONRPCCall(url, "sync_getOffChainData", hash)

if httpErr, ok := err.(*client.HTTPError); ok && httpErr.StatusCode == http.StatusTooManyRequests {
time.Sleep(retryDelay)
attemp += 1
continue
}

if err != nil {
var err error
var response types.Response
for {
select {
case <-ctx.Done():
log.Error(fmt.Sprintf("GetOffChainData hash:%v, context done", hash.String()))
if response.Error != nil {
return nil, fmt.Errorf("%v %v", response.Error.Code, response.Error.Message)
}
return nil, err
default:
}

if response.Error != nil {
return nil, fmt.Errorf("%v %v", response.Error.Code, response.Error.Message)
response, err = client.JSONRPCCall(url, "sync_getOffChainData", hash)
if err != nil || response.Error != nil {
log.Error(fmt.Sprintf("GetOffChainData hash:%v, error:%v, response err:%v", hash.String(), err, response.Error))
time.Sleep(retryDelay)
continue
}

return hexutil.Decode(strings.Trim(string(response.Result), "\""))
}

return nil, fmt.Errorf("max attempts of data fetching reached, attempts: %v, DA url: %s", maxAttempts, url)
}
7 changes: 5 additions & 2 deletions zk/da/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/gateway-fm/cdk-erigon-lib/common"
"github.com/ledgerwatch/erigon/zkevm/jsonrpc/types"
Expand Down Expand Up @@ -51,7 +52,7 @@ func TestClient_GetOffChainData(t *testing.T) {
name: "handle retry on 429",
hash: common.BytesToHash([]byte("hash")),
statusCode: http.StatusTooManyRequests,
err: "max attempts of data fetching reached",
err: "invalid status code, expected: 200, found: 429",
},
}
for _, tt := range tests {
Expand All @@ -78,7 +79,9 @@ func TestClient_GetOffChainData(t *testing.T) {
}))
defer svr.Close()

got, err := GetOffChainData(context.Background(), svr.URL, tt.hash)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
got, err := GetOffChainData(ctx, svr.URL, tt.hash)
if tt.err != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tt.err)
Expand Down
Loading