-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcelestia_server.go
149 lines (128 loc) · 3.15 KB
/
celestia_server.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
package celestia
import (
"context"
"errors"
"fmt"
"io"
"net"
"net/http"
"path"
"strconv"
"time"
plasma "github.com/ethereum-optimism/optimism/op-plasma"
"github.com/ethereum-optimism/optimism/op-service/rpc"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
)
type CelestiaServer struct {
log log.Logger
endpoint string
store *CelestiaStore
tls *rpc.ServerTLSConfig
httpServer *http.Server
listener net.Listener
}
func NewCelestiaServer(host string, port int, store *CelestiaStore, log log.Logger) *CelestiaServer {
endpoint := net.JoinHostPort(host, strconv.Itoa(port))
return &CelestiaServer{
log: log,
endpoint: endpoint,
store: store,
httpServer: &http.Server{
Addr: endpoint,
},
}
}
func (d *CelestiaServer) 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 *CelestiaServer) 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 {
w.WriteHeader(http.StatusBadRequest)
return
}
input, err := d.store.Get(r.Context(), comm)
if err != nil && errors.Is(err, plasma.ErrNotFound) {
w.WriteHeader(http.StatusNotFound)
return
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := w.Write(input); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func (d *CelestiaServer) HandlePut(w http.ResponseWriter, r *http.Request) {
d.log.Debug("PUT", "url", r.URL)
route := path.Base(r.URL.Path)
if route != "put" {
w.WriteHeader(http.StatusBadRequest)
return
}
input, err := io.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
comm, err := d.store.Put(r.Context(), input)
if err != nil {
key := hexutil.Encode(comm)
d.log.Info("Failed to store commitment to the DA server", "err", err, "key", key)
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := w.Write(comm); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func (b *CelestiaServer) Endpoint() string {
return b.listener.Addr().String()
}
func (b *CelestiaServer) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = b.httpServer.Shutdown(ctx)
return nil
}