forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validium support for L1 recovery (DAC) (#560)
* Support validium mode in L1 recovery * Retry data fetching from DA when the response error is 429 (too many requests) * Unit tests and minor fixes * Test decode validium batch data * Revert unrelated changes * Address CR feedbacks * Remove unused functions * Update zk/da/client.go Co-authored-by: Rachit Sonthalia <[email protected]> --------- Co-authored-by: Igor Mandrigin <[email protected]> Co-authored-by: Rachit Sonthalia <[email protected]>
- Loading branch information
1 parent
79cc4e6
commit 18960e5
Showing
14 changed files
with
308 additions
and
20 deletions.
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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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/erigon/zkevm/jsonrpc/client" | ||
) | ||
|
||
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 { | ||
return nil, err | ||
} | ||
|
||
if response.Error != nil { | ||
return nil, fmt.Errorf("%v %v", response.Error.Code, response.Error.Message) | ||
} | ||
|
||
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) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package da | ||
|
||
import ( | ||
"context" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gateway-fm/cdk-erigon-lib/common" | ||
"github.com/ledgerwatch/erigon/zkevm/jsonrpc/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestClient_GetOffChainData(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
hash common.Hash | ||
result string | ||
data []byte | ||
statusCode int | ||
err string | ||
}{ | ||
{ | ||
name: "successfully got offhcain data", | ||
hash: common.BytesToHash([]byte("hash")), | ||
result: fmt.Sprintf(`{"result":"0x%s"}`, hex.EncodeToString([]byte("offchaindata"))), | ||
data: []byte("offchaindata"), | ||
}, | ||
{ | ||
name: "error returned by server", | ||
hash: common.BytesToHash([]byte("hash")), | ||
result: `{"error":{"code":123,"message":"test error"}}`, | ||
err: "123 test error", | ||
}, | ||
{ | ||
name: "invalid offchain data returned by server", | ||
hash: common.BytesToHash([]byte("hash")), | ||
result: `{"result":"invalid-signature"}`, | ||
err: "hex string without 0x prefix", | ||
}, | ||
{ | ||
name: "unsuccessful status code returned by server", | ||
hash: common.BytesToHash([]byte("hash")), | ||
statusCode: http.StatusUnauthorized, | ||
err: "invalid status code, expected: 200, found: 401", | ||
}, | ||
{ | ||
name: "handle retry on 429", | ||
hash: common.BytesToHash([]byte("hash")), | ||
statusCode: http.StatusTooManyRequests, | ||
err: "max attempts of data fetching reached", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
var res types.Request | ||
require.NoError(t, json.NewDecoder(r.Body).Decode(&res)) | ||
require.Equal(t, "sync_getOffChainData", res.Method) | ||
|
||
var params []common.Hash | ||
require.NoError(t, json.Unmarshal(res.Params, ¶ms)) | ||
require.Equal(t, tt.hash, params[0]) | ||
|
||
if tt.statusCode > 0 { | ||
w.WriteHeader(tt.statusCode) | ||
} | ||
|
||
_, err := fmt.Fprint(w, tt.result) | ||
require.NoError(t, err) | ||
})) | ||
defer svr.Close() | ||
|
||
got, err := GetOffChainData(context.Background(), svr.URL, tt.hash) | ||
if tt.err != "" { | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), tt.err) | ||
} else { | ||
require.NoError(t, err) | ||
require.Equal(t, tt.data, got) | ||
} | ||
}) | ||
} | ||
} |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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