-
Notifications
You must be signed in to change notification settings - Fork 5
/
daserver.go
168 lines (143 loc) · 3.91 KB
/
daserver.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package plasma0g
import (
"context"
"encoding/hex"
"errors"
"fmt"
"io"
"net"
"net/http"
"path"
"strconv"
"time"
"github.com/ethereum-optimism/optimism/op-service/rpc"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
)
var ErrNotFound = errors.New("not found")
type KVStore interface {
// Get retrieves the given key if it's present in the key-value data store.
Get(ctx context.Context, key []byte) ([]byte, error)
// Put inserts the given value into the key-value data store.
Put(ctx context.Context, value []byte) ([]byte, error)
}
type DAServer struct {
log log.Logger
endpoint string
store KVStore
tls *rpc.ServerTLSConfig
httpServer *http.Server
listener net.Listener
useGenericComm bool
}
func NewDAServer(host string, port int, store KVStore, log log.Logger, useGenericComm bool) *DAServer {
endpoint := net.JoinHostPort(host, strconv.Itoa(port))
return &DAServer{
log: log,
endpoint: endpoint,
store: store,
httpServer: &http.Server{
Addr: endpoint,
},
useGenericComm: useGenericComm,
}
}
func (d *DAServer) Start() error {
mux := http.NewServeMux()
mux.HandleFunc("/get/", d.HandleGet)
mux.HandleFunc("/put/", d.HandlePut)
d.httpServer.Handler = mux
listener, err := net.Listen("tcp", d.endpoint)
if err != nil {
return fmt.Errorf("failed to listen: %w", err)
}
d.listener = listener
d.endpoint = listener.Addr().String()
errCh := make(chan error, 1)
go func() {
if d.tls != nil {
if err := d.httpServer.ServeTLS(d.listener, "", ""); err != nil {
errCh <- err
}
} else {
if err := d.httpServer.Serve(d.listener); err != nil {
errCh <- err
}
}
}()
// verify that the server comes up
tick := time.NewTimer(10 * time.Millisecond)
defer tick.Stop()
select {
case err := <-errCh:
return fmt.Errorf("http server failed: %w", err)
case <-tick.C:
return nil
}
}
func (d *DAServer) HandleGet(w http.ResponseWriter, r *http.Request) {
d.log.Debug("GET", "url", r.URL)
route := path.Dir(r.URL.Path)
if route != "/get" {
w.WriteHeader(http.StatusBadRequest)
return
}
key := path.Base(r.URL.Path)
comm, err := hexutil.Decode(key)
if err != nil {
d.log.Error("Failed to decode commitment", "err", err, "key", key)
w.WriteHeader(http.StatusBadRequest)
return
}
input, err := d.store.Get(r.Context(), comm)
if err != nil && errors.Is(err, ErrNotFound) {
d.log.Error("Commitment not found", "key", key, "error", err)
w.WriteHeader(http.StatusNotFound)
return
}
if err != nil {
d.log.Error("Failed to read commitment", "err", err, "key", key)
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := w.Write(input); err != nil {
d.log.Error("Failed to write pre-image", "err", err, "key", key)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func (d *DAServer) HandlePut(w http.ResponseWriter, r *http.Request) {
d.log.Info("PUT", "url", r.URL)
route := path.Dir(r.URL.Path)
if route != "/put" {
w.WriteHeader(http.StatusBadRequest)
return
}
input, err := io.ReadAll(r.Body)
if err != nil {
d.log.Error("Failed to read request body", "err", err)
w.WriteHeader(http.StatusBadRequest)
return
}
comm, err := d.store.Put(r.Context(), input)
if err != nil {
d.log.Error("Failed to store commitment to the DA server", "err", err, "comm", comm)
w.WriteHeader(http.StatusInternalServerError)
return
}
d.log.Info("stored commitment", "key", hex.EncodeToString(comm), "input_len", len(input))
if _, err := w.Write(comm); err != nil {
d.log.Error("Failed to write commitment request body", "err", err, "comm", comm)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func (b *DAServer) Endpoint() string {
return b.listener.Addr().String()
}
func (b *DAServer) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = b.httpServer.Shutdown(ctx)
return nil
}