Skip to content

Commit 5a4ac8c

Browse files
committed
Address comments
1 parent c91c6e5 commit 5a4ac8c

3 files changed

Lines changed: 22 additions & 17 deletions

File tree

cmd/mtc/mirror/internal/handler/handler.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const (
4242

4343
// Mirror is the interface that the handler uses to interact with the mirror's state.
4444
type Mirror interface {
45-
AddCheckpoint(ctx context.Context, cp []byte, oldSize uint64, proof [][]byte) error
45+
AddCheckpoint(ctx context.Context, oldSize uint64, proof [][]byte, cp []byte) error
4646
AddEntries(ctx context.Context, logOrigin string, uploadStart, uploadEnd uint64, ticket []byte, next func() (*mirror.Package, error)) ([]byte, error)
4747
}
4848

@@ -114,7 +114,7 @@ func addCheckpoint(m Mirror) http.HandlerFunc {
114114
return
115115
}
116116

117-
if err := m.AddCheckpoint(r.Context(), cp, oldSize, proof); err != nil {
117+
if err := m.AddCheckpoint(r.Context(), oldSize, proof, cp); err != nil {
118118
slog.ErrorContext(r.Context(), "AddCheckpoint failed", slog.Any("error", err))
119119
http.Error(w, err.Error(), http.StatusInternalServerError)
120120
return
@@ -127,6 +127,11 @@ func addCheckpoint(m Mirror) http.HandlerFunc {
127127

128128
func addEntries(m Mirror) http.HandlerFunc {
129129
return func(w http.ResponseWriter, r *http.Request) {
130+
// SPEC: The request body MUST have Content-Type of application/octet-stream ...
131+
if t := strings.ToLower(r.Header.Get("Content-Type")); t != "application/octet-stream" {
132+
http.Error(w, fmt.Sprintf("invalid Content-Type %q", t), http.StatusBadRequest)
133+
return
134+
}
130135
req, err := parseAddEntriesPreamble(r.Body)
131136
if err != nil {
132137
http.Error(w, fmt.Sprintf("failed to parse header: %v", err), http.StatusBadRequest)
@@ -227,15 +232,14 @@ func (a *addEntriesRequest) NextPackage() (*mirror.Package, error) {
227232
//
228233
// Returns a struct which represents the request, and provides streaming access to the entry packages.
229234
func parseAddEntriesPreamble(r io.Reader) (*addEntriesRequest, error) {
230-
//SPEC: The request body MUST have Content-Type of application/octet-stream and contain the
231-
// following values, concatenated.
232-
// 2 bytes, encoding a big-endian uint16: log_origin_size
233-
// log_origin_size bytes, containing the log origin: log_origin
234-
// 8 bytes, encoding a big-endian uint64: upload_start
235-
// 8 bytes, encoding a big-endian uint64: upload_end
236-
// 2 bytes, encoding a big-endian uint16: ticket_size
237-
// ticket_size bytes, containing an opaque ticket value, described below
238-
// A sequence of entry packages
235+
// SPEC: The request body MUST ... contain the following values, concatenated:
236+
// 2 bytes, encoding a big-endian uint16: log_origin_size
237+
// log_origin_size bytes, containing the log origin: log_origin
238+
// 8 bytes, encoding a big-endian uint64: upload_start
239+
// 8 bytes, encoding a big-endian uint64: upload_end
240+
// 2 bytes, encoding a big-endian uint16: ticket_size
241+
// ticket_size bytes, containing an opaque ticket value, described below
242+
// A sequence of entry packages
239243
var logOriginSize uint16
240244
if err := binary.Read(r, binary.BigEndian, &logOriginSize); err != nil {
241245
return nil, err

cmd/mtc/mirror/internal/handler/handler_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ import (
2828
)
2929

3030
type mockMirror struct {
31-
addCheckpointFunc func(ctx context.Context, cp []byte, oldSize uint64, proof [][]byte) error
31+
addCheckpointFunc func(ctx context.Context, oldSize uint64, proof [][]byte, cp []byte) error
3232
addEntriesFunc func(ctx context.Context, logOrigin string, uploadStart, uploadEnd uint64, ticket []byte, next func() (*mirror.Package, error)) ([]byte, error)
3333
}
3434

35-
func (m *mockMirror) AddCheckpoint(ctx context.Context, cp []byte, oldSize uint64, proof [][]byte) error {
35+
func (m *mockMirror) AddCheckpoint(ctx context.Context, oldSize uint64, proof [][]byte, cp []byte) error {
3636
if m.addCheckpointFunc != nil {
37-
return m.addCheckpointFunc(ctx, cp, oldSize, proof)
37+
return m.addCheckpointFunc(ctx, oldSize, proof, cp)
3838
}
3939
return nil
4040
}
@@ -50,7 +50,7 @@ func TestAddCheckpoint(t *testing.T) {
5050
const cpOld = 100
5151

5252
mock := &mockMirror{
53-
addCheckpointFunc: func(ctx context.Context, cp []byte, oldSize uint64, proof [][]byte) error {
53+
addCheckpointFunc: func(ctx context.Context, oldSize uint64, proof [][]byte, cp []byte) error {
5454
if oldSize != cpOld {
5555
return fmt.Errorf("want oldSize %d, got %d", cpOld, oldSize)
5656
}
@@ -132,6 +132,7 @@ func TestAddEntries(t *testing.T) {
132132
_, _ = body.Write(make([]byte, 32)) // 1 hash
133133

134134
req := httptest.NewRequest(http.MethodPost, "/add-entries", &body)
135+
req.Header.Add("Content-Type", "application/octet-stream")
135136
w := httptest.NewRecorder()
136137
h.ServeHTTP(w, req)
137138

cmd/mtc/mirror/internal/mirror/mirror.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ type Package struct {
3030
Proof [][]byte
3131
}
3232

33-
func (m *Mirror) AddCheckpoint(ctx context.Context, cp []byte, oldSize uint64, proof [][]byte) error {
34-
slog.InfoContext(ctx, "AddCheckpoint", slog.Int("cp_len", len(cp)), slog.Uint64("old_size", oldSize), slog.Int("proof_len", len(proof)))
33+
func (m *Mirror) AddCheckpoint(ctx context.Context, oldSize uint64, proof [][]byte, cpRaw []byte) error {
34+
slog.InfoContext(ctx, "AddCheckpoint", slog.Uint64("old_size", oldSize), slog.Int("proof_len", len(proof)), slog.String("cp", string(cpRaw)))
3535
return nil
3636
}
3737

0 commit comments

Comments
 (0)