|
| 1 | +package gateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "strings" |
| 10 | + |
| 11 | + weaveVMtypes "github.com/dymensionxyz/dymint/da/weave_vm/types" |
| 12 | + "github.com/dymensionxyz/dymint/types" |
| 13 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 14 | +) |
| 15 | + |
| 16 | +type Gateway struct { |
| 17 | + config *weaveVMtypes.Config |
| 18 | + logger types.Logger |
| 19 | +} |
| 20 | + |
| 21 | +func NewGatewayClient(config *weaveVMtypes.Config, logger types.Logger) *Gateway { |
| 22 | + return &Gateway{config, logger} |
| 23 | +} |
| 24 | + |
| 25 | +const weaveVMGatewayURL = "https://gateway.wvm.dev/v1/calldata/%s" |
| 26 | + |
| 27 | +// Modified get function with improved error handling |
| 28 | +func (g *Gateway) RetrieveFromGateway(ctx context.Context, txHash string) (*weaveVMtypes.WvmDymintBlob, error) { |
| 29 | + type WvmRetrieverResponse struct { |
| 30 | + ArweaveBlockHash string `json:"arweave_block_hash"` |
| 31 | + Calldata string `json:"calldata"` |
| 32 | + WarDecodedCalldata string `json:"war_decoded_calldata"` |
| 33 | + WvmBlockHash string `json:"weavevm_block_hash"` |
| 34 | + WvmBlockNumber uint64 `json:"wvm_block_id"` |
| 35 | + } |
| 36 | + r, err := http.NewRequestWithContext(ctx, http.MethodGet, |
| 37 | + fmt.Sprintf(weaveVMGatewayURL, |
| 38 | + txHash), nil) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + r.Header.Set("Accept", "application/json") |
| 44 | + client := &http.Client{ |
| 45 | + Timeout: g.config.Timeout, |
| 46 | + } |
| 47 | + |
| 48 | + g.logger.Debug("sending request to WeaveVM data retriever", |
| 49 | + "url", r.URL.String(), |
| 50 | + "headers", r.Header) |
| 51 | + |
| 52 | + resp, err := client.Do(r) |
| 53 | + if err != nil { |
| 54 | + return nil, fmt.Errorf("failed to call weaveVM-data-retriever: %w", err) |
| 55 | + } |
| 56 | + defer resp.Body.Close() |
| 57 | + |
| 58 | + body, err := io.ReadAll(resp.Body) |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("failed to read response body: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + if err := validateResponse(resp, body); err != nil { |
| 64 | + g.logger.Error("invalid response from WeaveVM data retriever", |
| 65 | + "status", resp.Status, |
| 66 | + "content_type", resp.Header.Get("Content-Type"), |
| 67 | + "body", string(body)) |
| 68 | + return nil, fmt.Errorf("invalid response: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + var weaveVMData WvmRetrieverResponse |
| 72 | + if err = json.Unmarshal(body, &weaveVMData); err != nil { |
| 73 | + g.logger.Error("failed to unmarshal response", |
| 74 | + "error", err, |
| 75 | + "body", string(body), |
| 76 | + "content_type", resp.Header.Get("Content-Type")) |
| 77 | + return nil, fmt.Errorf("failed to unmarshal response: %w, body: %s", err, string(body)) |
| 78 | + } |
| 79 | + |
| 80 | + g.logger.Info("weaveVM backend: get data from weaveVM", |
| 81 | + "arweave_block_hash", weaveVMData.ArweaveBlockHash, |
| 82 | + "weavevm_block_hash", weaveVMData.WvmBlockHash, |
| 83 | + "calldata_length", len(weaveVMData.Calldata)) |
| 84 | + |
| 85 | + blob, err := hexutil.Decode(weaveVMData.Calldata) |
| 86 | + if err != nil { |
| 87 | + return nil, fmt.Errorf("failed to decode calldata: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + if len(blob) == 0 { |
| 91 | + return nil, fmt.Errorf("decoded blob has length zero") |
| 92 | + } |
| 93 | + |
| 94 | + return &weaveVMtypes.WvmDymintBlob{ArweaveBlockHash: weaveVMData.ArweaveBlockHash, WvmBlockHash: weaveVMData.WvmBlockHash, WvmTxHash: txHash, Blob: blob}, nil |
| 95 | +} |
| 96 | + |
| 97 | +func validateResponse(resp *http.Response, body []byte) error { |
| 98 | + if resp.StatusCode != http.StatusOK { |
| 99 | + return fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, string(body)) |
| 100 | + } |
| 101 | + |
| 102 | + contentType := resp.Header.Get("Content-Type") |
| 103 | + if !strings.Contains(contentType, "application/json") { |
| 104 | + return fmt.Errorf("unexpected content type: %s, body: %s", contentType, string(body)) |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
0 commit comments