Skip to content

Commit e323875

Browse files
committed
refactor: do not respond with a receipt when invocation has not been executed
1 parent 9620caa commit e323875

1 file changed

Lines changed: 23 additions & 10 deletions

File tree

server/retrieval/server.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
package retrieval
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
67
"fmt"
78
"io"
89
"net/http"
910
"strings"
11+
"time"
1012

1113
"github.com/storacha/go-ucanto/core/dag/blockstore"
1214
"github.com/storacha/go-ucanto/core/delegation"
1315
"github.com/storacha/go-ucanto/core/invocation"
1416
"github.com/storacha/go-ucanto/core/invocation/ran"
1517
"github.com/storacha/go-ucanto/core/ipld"
18+
"github.com/storacha/go-ucanto/core/ipld/codec/json"
1619
"github.com/storacha/go-ucanto/core/message"
1720
"github.com/storacha/go-ucanto/core/receipt"
1821
"github.com/storacha/go-ucanto/core/result"
1922
"github.com/storacha/go-ucanto/core/result/failure"
2023
"github.com/storacha/go-ucanto/principal"
2124
"github.com/storacha/go-ucanto/server"
25+
rdm "github.com/storacha/go-ucanto/server/retrieval/datamodel"
2226
"github.com/storacha/go-ucanto/server/transaction"
2327
"github.com/storacha/go-ucanto/transport"
2428
"github.com/storacha/go-ucanto/transport/headercar"
@@ -66,9 +70,9 @@ type ServiceMethod[O ipld.Builder, X failure.IPLDBuilderFailure] func(
6670
// service implementation.
6771
type Service = map[ucan.Ability]ServiceMethod[ipld.Builder, failure.IPLDBuilderFailure]
6872

69-
// CachingServer is a retrieval that also caches invocations/delegations to
70-
// allow invocations with delegations chains bigger than HTTP header size limits
71-
// to be executed as multiple requests.
73+
// CachingServer is a retrieval server that also caches invocations/delegations
74+
// to allow invocations with delegations chains bigger than HTTP header size
75+
// limits to be executed as multiple requests.
7276
type CachingServer interface {
7377
server.Server[Service]
7478
Cache() delegation.Store
@@ -229,6 +233,12 @@ func Handle(ctx context.Context, srv CachingServer, request transport.HTTPReques
229233
return nil, fmt.Errorf("executing invocations: %w", err)
230234
}
231235

236+
// if there is no agent message to respond with, we simply respond with the
237+
// response from execution (i.e. missing proofs response)
238+
if out == nil {
239+
return thttp.NewResponse(execResp.Status, execResp.Body, execResp.Headers), nil
240+
}
241+
232242
encResp, err := selection.Encoder().Encode(out)
233243
if err != nil {
234244
return nil, fmt.Errorf("encoding response message: %w", err)
@@ -282,15 +292,18 @@ func Execute(ctx context.Context, srv CachingServer, msg message.AgentMessage, r
282292
if err != nil {
283293
mpe := MissingProofs{}
284294
if errors.As(err, &mpe) {
285-
rcpt, err := receipt.Issue(srv.ID(), result.NewFailure(mpe), ran.FromLink(invs[0]))
286-
if err != nil {
287-
return nil, Response{}, fmt.Errorf("issuing missing proofs receipt: %w", err)
288-
}
289-
out, err := message.Build(nil, []receipt.AnyReceipt{rcpt})
295+
body, err := json.Encode(&mpe, rdm.MissingProofsType())
290296
if err != nil {
291-
return nil, Response{}, fmt.Errorf("building missing proofs error message: %w", err)
297+
return nil, Response{}, fmt.Errorf("encoding missing proofs repsonse: %w", err)
292298
}
293-
return out, Response{Status: http.StatusNotExtended}, nil
299+
headers := http.Header{}
300+
expiry := time.Now().Add(10 * time.Minute).Unix() // TODO: honour this?
301+
headers.Set("X-UCAN-Cache-Expiry", fmt.Sprintf("%d", expiry))
302+
return nil, Response{
303+
Status: http.StatusNotExtended,
304+
Body: bytes.NewReader(body),
305+
Headers: headers,
306+
}, nil
294307
}
295308
return nil, Response{}, err
296309
}

0 commit comments

Comments
 (0)