forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestful_client.go
106 lines (89 loc) · 2.74 KB
/
restful_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package das
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/offchainlabs/nitro/arbstate/daprovider"
"github.com/offchainlabs/nitro/das/dastree"
)
// RestfulDasClient implements daprovider.DASReader
type RestfulDasClient struct {
url string
}
func NewRestfulDasClient(protocol string, host string, port int) *RestfulDasClient {
return &RestfulDasClient{
url: fmt.Sprintf("%s://%s:%d", protocol, host, port),
}
}
func NewRestfulDasClientFromURL(url string) (*RestfulDasClient, error) {
if !(strings.HasPrefix(url, "http://") || strings.HasPrefix(url, "https://")) {
return nil, fmt.Errorf("protocol prefix 'http://' or 'https://' must be specified for RestfulDasClient; got '%s'", url)
}
return &RestfulDasClient{
url: url,
}, nil
}
func (c *RestfulDasClient) GetByHash(ctx context.Context, hash common.Hash) ([]byte, error) {
res, err := http.Get(c.url + getByHashRequestPath + EncodeStorageServiceKey(hash))
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP error with status %d returned by server: %s", res.StatusCode, http.StatusText(res.StatusCode))
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}
var response RestfulDasServerResponse
err = json.Unmarshal(body, &response)
if err != nil {
return nil, err
}
decoder := base64.NewDecoder(base64.StdEncoding, bytes.NewReader([]byte(response.Data)))
decodedBytes, err := io.ReadAll(decoder)
if err != nil {
return nil, err
}
if !dastree.ValidHash(hash, decodedBytes) {
return nil, daprovider.ErrHashMismatch
}
return decodedBytes, nil
}
func (c *RestfulDasClient) HealthCheck(ctx context.Context) error {
res, err := http.Get(c.url + healthRequestPath)
if err != nil {
return err
}
if res.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP error with status %d returned by server: %s", res.StatusCode, http.StatusText(res.StatusCode))
}
return nil
}
func (c *RestfulDasClient) ExpirationPolicy(ctx context.Context) (daprovider.ExpirationPolicy, error) {
res, err := http.Get(c.url + expirationPolicyRequestPath)
if err != nil {
return -1, err
}
if res.StatusCode != http.StatusOK {
return -1, err
}
body, err := io.ReadAll(res.Body)
if err != nil {
return -1, fmt.Errorf("HTTP error with status %d returned by server: %s", res.StatusCode, http.StatusText(res.StatusCode))
}
var response RestfulDasServerResponse
err = json.Unmarshal(body, &response)
if err != nil {
return -1, err
}
return daprovider.StringToExpirationPolicy(response.ExpirationPolicy)
}