Minor updates to repair_eth (#1120)
* node/hack/repair_eth: use a cookiejar for http Otherwise, explorers like polygonscan, which implement a cloudflare WAF, will block all requests. * node/hack/repair_eth: add -showError flag When an explorer is having problems or blocks a repair http request, it returns an error like: Failed to get current height: failed to decode response: invalid character '<' looking for beginning of value This allows showing the actual response body to better troubleshoot the problem.
This commit is contained in:
parent
dbc0c56eb5
commit
a4c91d8f1b
|
@ -6,8 +6,10 @@ import (
|
|||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -51,6 +53,7 @@ var (
|
|||
chain = flag.String("chain", "ethereum", "Eth Chain name")
|
||||
dryRun = flag.Bool("dryRun", true, "Dry run")
|
||||
step = flag.Uint64("step", 10000, "Step")
|
||||
showError = flag.Bool("showError", false, "On http error, show the response body")
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -103,7 +106,7 @@ type logResponse struct {
|
|||
Result json.RawMessage `json:"result"`
|
||||
}
|
||||
|
||||
func getCurrentHeight(chainId vaa.ChainID, ctx context.Context, c *http.Client, api, key string) (uint64, error) {
|
||||
func getCurrentHeight(chainId vaa.ChainID, ctx context.Context, c *http.Client, api, key string, showErr bool) (uint64, error) {
|
||||
var req *http.Request
|
||||
var err error
|
||||
if chainId == vaa.ChainIDOasis || chainId == vaa.ChainIDAurora {
|
||||
|
@ -126,15 +129,23 @@ func getCurrentHeight(chainId vaa.ChainID, ctx context.Context, c *http.Client,
|
|||
var r struct {
|
||||
Result string `json:"result"`
|
||||
}
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
|
||||
if resp.StatusCode != http.StatusOK && showErr {
|
||||
fmt.Println(string(body))
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &r); err != nil {
|
||||
return 0, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
|
||||
return hexutil.DecodeUint64(r.Result)
|
||||
}
|
||||
|
||||
func getLogs(chainId vaa.ChainID, ctx context.Context, c *http.Client, api, key, contract, topic0 string, from, to string) ([]*logEntry, error) {
|
||||
func getLogs(chainId vaa.ChainID, ctx context.Context, c *http.Client, api, key, contract, topic0 string, from, to string, showErr bool) ([]*logEntry, error) {
|
||||
var req *http.Request
|
||||
var err error
|
||||
if chainId == vaa.ChainIDOasis || chainId == vaa.ChainIDAurora {
|
||||
|
@ -160,7 +171,16 @@ func getLogs(chainId vaa.ChainID, ctx context.Context, c *http.Client, api, key,
|
|||
|
||||
var r logResponse
|
||||
|
||||
if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response body: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK && showErr {
|
||||
fmt.Println(string(body))
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(body, &r); err != nil {
|
||||
return nil, fmt.Errorf("failed to decode response: %w", err)
|
||||
}
|
||||
|
||||
|
@ -217,7 +237,14 @@ func main() {
|
|||
|
||||
ctx := context.Background()
|
||||
|
||||
currentHeight, err := getCurrentHeight(chainID, ctx, http.DefaultClient, etherscanAPI, *etherscanKey)
|
||||
jar, err := cookiejar.New(nil)
|
||||
if err != nil {
|
||||
log.Fatalf("Error creating http cookiejar: %v", err)
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Jar: jar,
|
||||
}
|
||||
currentHeight, err := getCurrentHeight(chainID, ctx, httpClient, etherscanAPI, *etherscanKey, *showError)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to get current height: %v", err)
|
||||
}
|
||||
|
@ -290,7 +317,10 @@ func main() {
|
|||
|
||||
limiter := rate.NewLimiter(rate.Every(1*time.Second), 1)
|
||||
|
||||
c := &http.Client{Timeout: 5 * time.Second}
|
||||
c := &http.Client{
|
||||
Jar: jar,
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
|
||||
ethAbi, err := abi2.JSON(strings.NewReader(abi.AbiABI))
|
||||
if err != nil {
|
||||
|
@ -317,7 +347,7 @@ func main() {
|
|||
|
||||
log.Printf("Requesting logs from block %s to %s", from, to)
|
||||
|
||||
logs, err := getLogs(chainID, ctx, c, etherscanAPI, *etherscanKey, coreContract, tokenLockupTopic.Hex(), from, to)
|
||||
logs, err := getLogs(chainID, ctx, c, etherscanAPI, *etherscanKey, coreContract, tokenLockupTopic.Hex(), from, to, *showError)
|
||||
if err != nil {
|
||||
log.Fatalf("failed to get logs: %v", err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue