-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathhandlers.go
More file actions
1405 lines (1229 loc) · 45.1 KB
/
Copy pathhandlers.go
File metadata and controls
1405 lines (1229 loc) · 45.1 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package pdp
import (
"context"
"database/sql"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"math/big"
"net/http"
"strconv"
"strings"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/yugabyte/pgx/v5"
"github.com/filecoin-project/curio/alertmanager"
"github.com/filecoin-project/curio/api"
"github.com/filecoin-project/curio/harmony/harmonydb"
"github.com/filecoin-project/curio/lib/ethchain"
"github.com/filecoin-project/curio/lib/paths"
ipni_provider "github.com/filecoin-project/curio/market/ipni/ipni-provider"
"github.com/filecoin-project/curio/pdp/contract"
"github.com/filecoin-project/curio/tasks/indexing"
types2 "github.com/filecoin-project/lotus/chain/types"
)
func httpServerError(w http.ResponseWriter, statusCode int, msg string, err error) {
if chainErr, ok := errors.AsType[*api.ChainError](err); ok {
http.Error(w, chainErr.Error(), statusCode)
return
}
eid := uuid.New()
log.Errorf("%s [eid=%s]: %+v", msg, eid, err)
http.Error(w, fmt.Sprintf("%s [eid: %s]", msg, eid), statusCode)
}
// PDPRoutePath is the base path for PDP routes
const PDPRoutePath = "/pdp"
// PingOKBody is the exact success body for GET /pdp/ping.
// Reachability probes match this to confirm they hit Curio PDP, not a proxy.
const PingOKBody = "curio-pdp"
const (
// MaxCreateDataSetExtraDataSize defines the limit for extraData size in CreateDataSet calls (4KB).
MaxCreateDataSetExtraDataSize = 4096
// MaxAddPiecesBatchSize caps pieces per AddPieces (or CreateDataSetAndAddPieces)
// call to reject early rather than revert on-chain.
MaxAddPiecesBatchSize = 40
// MaxDeletePieceExtraDataSize defines the limit for extraData size in DeletePiece calls (1KiB).
MaxDeletePieceExtraDataSize = 1024
MaxDeletePiecesBatchSize = contract.ConservativeEnqueuedRemovalsLimit
)
// ETHTxSender enqueues (and eventually sends) an Ethereum transaction.
// *message.SenderETH implements this; tests may substitute an instant mock.
type ETHTxSender interface {
Send(ctx context.Context, fromAddress common.Address, tx *types.Transaction, reason string) (common.Hash, error)
}
// PDPService represents the service for managing data sets and pieces
type PDPService struct {
Auth
db *harmonydb.DB
storage paths.StashStore
sender ETHTxSender
ethClient ethchain.EthClient
filClient PDPServiceNodeApi
alertTask *alertmanager.AlertTask
pullHandler *PullHandler
ipp *ipni_provider.Provider
ipOffenseThrottle *IPOffenseThrottle
}
type PDPServiceNodeApi interface {
ChainHead(ctx context.Context) (*types2.TipSet, error)
}
// NewPDPService creates a new instance of PDPService with the provided stores.
func NewPDPService(
ctx context.Context,
db *harmonydb.DB,
stor paths.StashStore,
ec ethchain.EthClient,
fc PDPServiceNodeApi,
sn ETHTxSender,
alertTask *alertmanager.AlertTask,
ipp *ipni_provider.Provider) *PDPService {
auth := &NullAuth{}
pullStore := NewDBPullStore(db)
pullValidator := NewEthCallValidator(ec, db)
p := &PDPService{
Auth: auth,
db: db,
storage: stor,
sender: sn,
ethClient: ec,
filClient: fc,
alertTask: alertTask,
pullHandler: NewPullHandler(auth, pullStore, pullValidator, db),
ipp: ipp,
ipOffenseThrottle: NewIPOffenseThrottle(defaultIPOffensePolicies()),
}
go p.ipOffenseThrottle.RunCleanup(ctx)
go p.cleanup(ctx)
return p
}
// kvDataSetID extracts the dataset URL param for lifecycle logging.
func kvDataSetID(r *http.Request) []any {
return []any{"dataset", chi.URLParam(r, "dataSetId")}
}
// kvDataSetPiece extracts dataset + piece URL params for lifecycle logging.
func kvDataSetPiece(r *http.Request) []any {
return []any{"dataset", chi.URLParam(r, "dataSetId"), "piece_id", chi.URLParam(r, "pieceID")}
}
// kvUploadUUID extracts the upload UUID URL param for lifecycle logging.
func kvUploadUUID(r *http.Request) []any {
return []any{"upload_uuid", chi.URLParam(r, "uploadUUID")}
}
// Routes registers the HTTP routes with the provided router.
func Routes(r chi.Router, p *PDPService) {
mountExploreRoutes(r, p)
r.Route(PDPRoutePath, func(r chi.Router) {
r.Use(p.ipOffenseThrottle.Middleware)
// Routes for data sets
r.Route("/data-sets", func(r chi.Router) {
// POST /pdp/data-sets - Create a new data set
r.Post("/", instrument("dataSetCreate", p.handleCreateDataSet, nil))
// POST /pdp/data-sets/create-and-add - Create a new data set and add pieces at the same time
r.Post("/create-and-add", instrument("dataSetCreateAndAdd", p.handleCreateDataSetAndAddPieces, nil))
// GET /pdp/data-sets/created/{txHash} - Get the status of a data set creation
r.Get("/created/{txHash}", p.handleGetDataSetCreationStatus)
// Individual data set routes
r.Route("/{dataSetId}", func(r chi.Router) {
// GET /pdp/data-sets/{set-id}
r.Get("/", p.handleGetDataSet)
// POST /pdp/data-sets/{set-id}/terminate
r.Post("/terminate", instrument("dataSetTerminate", p.handleTerminateDataSet, kvDataSetID))
// GET /pdp/data-sets/{set-id}/terminate
r.Get("/terminate", p.handleGetDataSetTerminationStatus)
// Routes for pieces within a data set
r.Route("/pieces", func(r chi.Router) {
// POST /pdp/data-sets/{set-id}/pieces
r.Post("/", instrument("pieceAdd", p.handleAddPieceToDataSet, kvDataSetID))
// GET /pdp/data-sets/{set-id}/pieces/added/{txHash}
r.Get("/added/{txHash}", p.handleGetPieceAdditionStatus)
// Individual piece routes
r.Route("/{pieceID}", func(r chi.Router) {
// GET /pdp/data-sets/{set-id}/pieces/{piece-id}
r.Get("/", p.handleGetDataSetPiece)
// DEL /pdp/data-sets/{set-id}/pieces/{piece-id}
r.Delete("/", instrument("pieceDelete", p.handleDeleteDataSetPiece, kvDataSetPiece))
})
})
})
})
r.Get("/ping", p.handlePing)
// GET /pdp/piece/{pieceCid}/status - Get indexing/IPNI status for a piece
r.Get("/piece/{pieceCid}/status", p.handleGetPieceStatus)
// Routes for piece storage and retrieval
// POST /pdp/piece
r.Post("/piece", instrument("pieceUploadInit", p.handlePiecePost, nil))
// GET /pdp/piece
r.Get("/piece", p.handleFindPiece)
// PUT /pdp/piece/upload/{uploadUUID}
r.Put("/piece/upload/{uploadUUID}", instrument("pieceUpload", p.handlePieceUpload, kvUploadUUID))
// POST /pdp/piece/uploads
r.Post("/piece/uploads", instrument("pieceStreamInit", p.handleStreamingUploadURL, nil))
// PUT /pdp/piece/uploads/{uploadUUID}
r.Put("/piece/uploads/{uploadUUID}", instrument("pieceStreamUpload", p.handleStreamingUpload, kvUploadUUID))
// POST /pdp/piece/uploads/{uploadUUID}
r.Post("/piece/uploads/{uploadUUID}", instrument("pieceStreamFinalize", p.handleFinalizeStreamingUpload, kvUploadUUID))
// POST /pdp/piece/pull - Pull pieces from other SPs
r.Post("/piece/pull", instrument("piecePull", p.pullHandler.HandlePull, nil))
})
}
// Handler functions
func (p *PDPService) handlePing(w http.ResponseWriter, r *http.Request) {
_, err := p.AuthService(r)
if err != nil {
httpServerError(w, http.StatusUnauthorized, "Failed to authorize request", err)
return
}
if p.alertTask != nil && p.alertTask.Problems() {
httpServerError(w, http.StatusServiceUnavailable, "Service Unavailable", nil)
return
}
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(PingOKBody))
}
// handleGetPieceStatus returns the indexing and IPNI status for a piece
func (p *PDPService) handleGetPieceStatus(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Verify authorization
serviceLabel, err := p.AuthService(r)
if err != nil {
httpServerError(w, http.StatusUnauthorized, "Failed to authorize request", err)
return
}
// Extract pieceCid from URL and convert to v1 for DB query
pieceCidStr := chi.URLParam(r, "pieceCid")
if pieceCidStr == "" {
http.Error(w, "Missing pieceCid in URL", http.StatusBadRequest)
return
}
// Convert to v1 format (database stores v1)
info, err := ParsePieceCid(pieceCidStr)
if err != nil {
http.Error(w, "Invalid pieceCid format: "+err.Error(), http.StatusBadRequest)
return
}
pieceCidV1Str := info.CidV1.String()
// Query status from database
var results []struct {
PieceCID string `db:"piece_cid"`
PieceRawSize uint64 `db:"piece_raw_size"`
CreatedAt time.Time `db:"created_at"`
Indexed bool `db:"indexed"`
IndexedAt sql.NullTime `db:"indexed_at"`
AdvertisementCreated bool `db:"advertisement_created"`
AdvertisementCreatedAt sql.NullTime `db:"advertisement_created_at"`
AdCID sql.NullString `db:"ad_cid"`
AdvertisementRetrieved bool `db:"advertisement_retrieved"`
AdvertisementRetrievedAt sql.NullTime `db:"advertisement_retrieved_at"`
Status string `db:"status"`
Provider sql.NullString `db:"provider"`
}
err = p.db.Select(ctx, &results, `
SELECT
pr.piece_cid,
pp.piece_raw_size,
pr.created_at,
-- Indexing status (true when CAR indexing completed and ready for/in IPNI)
(pr.needs_ipni OR pr.ipni_task_id IS NOT NULL OR ia.ad_cid IS NOT NULL) as indexed,
pr.indexed_at,
-- Advertisement status
ia.ad_cid IS NOT NULL as advertisement_created,
pr.advertisement_created_at as advertisement_created_at,
ia.ad_cid,
-- Advertisement Fetch status
ia.fetched_at IS NOT NULL as advertisement_retrieved,
ia.fetched_at as advertisement_retrieved_at,
-- Determine overall status
CASE
WHEN ia.fetched_at IS NOT NULL THEN 'retrieved'
WHEN ia.ad_cid IS NOT NULL THEN 'announced'
WHEN pr.ipni_task_id IS NOT NULL THEN 'creating_ad'
WHEN pr.indexing_task_id IS NOT NULL THEN 'indexing'
ELSE 'pending'
END as status,
ia.provider
FROM pdp_piecerefs pr
JOIN parked_piece_refs pprf ON pprf.ref_id = pr.piece_ref
JOIN parked_pieces pp ON pp.id = pprf.piece_id
LEFT JOIN LATERAL (
SELECT
MIN(i.ad_cid) as ad_cid,
MIN(i.provider) as provider,
MIN((SELECT MIN(af.fetched_at) FROM ipni_ad_fetches af WHERE af.ad_cid = i.ad_cid)) as fetched_at
FROM ipni i
WHERE i.piece_cid = pr.piece_cid
AND i.provider = (SELECT peer_id FROM ipni_peerid WHERE sp_id = $3)
AND i.is_rm = FALSE
) ia ON true
WHERE pr.piece_cid = $1 AND pr.service = $2
LIMIT 1
`, pieceCidV1Str, serviceLabel, indexing.PDP_v0_SP_ID)
if err != nil {
httpServerError(w, http.StatusInternalServerError, "Failed to query piece status", err)
return
}
if len(results) == 0 {
http.Error(w, "Piece not found or does not belong to service", http.StatusNotFound)
return
}
result := results[0]
// Convert authoritative PieceCID back from v1 to v2 for external API
pieceInfo, err := PieceCidV2FromV1Str(result.PieceCID, result.PieceRawSize)
if err != nil {
httpServerError(w, http.StatusInternalServerError, "Failed to convert PieceCID to v2", err)
return
}
// Prepare response
response := struct {
PieceCID string `json:"pieceCid"`
Status string `json:"status"`
Indexed bool `json:"indexed"`
IndexedAt *time.Time `json:"indexedAt,omitempty"`
AdCreated bool `json:"adCreated"`
AdCreatedAt *time.Time `json:"adCreatedAt,omitempty"`
Advertised bool `json:"advertised"`
AdvertisedAt *time.Time `json:"advertisedAt,omitempty"`
Retrieved bool `json:"retrieved"`
RetrievedAt *time.Time `json:"retrievedAt,omitempty"`
}{
PieceCID: pieceInfo.CidV2.String(),
Status: result.Status,
Indexed: result.Indexed,
AdCreated: result.AdvertisementCreated,
Retrieved: result.AdvertisementRetrieved,
}
if !result.IndexedAt.Valid {
response.IndexedAt = nil
} else {
response.IndexedAt = &result.IndexedAt.Time
}
if !result.AdvertisementCreatedAt.Valid {
response.AdCreatedAt = nil
} else {
response.AdCreatedAt = &result.AdvertisementCreatedAt.Time
}
if !result.AdvertisementRetrievedAt.Valid {
response.RetrievedAt = nil
} else {
response.RetrievedAt = &result.AdvertisementRetrievedAt.Time
}
// Advertised and AdvertisedAt are derived from three signals, in order:
// 1. A recorded fetch of this ad in ipni_ad_fetches is the strongest per-ad
// signal. If an indexer fetched the ad, it must have been advertised
// already. Since we do not store the actual first publish time for each
// ad, AdvertisedAt is estimated as the earlier of
// advertisement_created_at + PublishInterval and the first fetch time.
// This keeps AdvertisedAt from appearing after RetrievedAt.
// 2. The in-process IPNI provider exposes LastPublishTime per provider, not
// per ad. It is useful only when there is no fetch record and we know
// when this ad was created. A provider publish after this ad was created
// means the ad should have been included in the announced head; an older
// provider publish means it was not, and we do not fall through to the
// timing heuristic.
// 3. Without either signal, fall back to the old timing heuristic: after
// PublishInterval has elapsed from ad creation, assume the ad was
// announced.
if result.AdvertisementRetrieved {
response.Advertised = true
if result.AdvertisementRetrievedAt.Valid {
advertisedAt := result.AdvertisementRetrievedAt.Time
if result.AdvertisementCreatedAt.Valid {
createdAtEstimate := result.AdvertisementCreatedAt.Time.Add(ipni_provider.PublishInterval)
if createdAtEstimate.Before(advertisedAt) {
advertisedAt = createdAtEstimate
}
}
response.AdvertisedAt = &advertisedAt
} else {
response.AdvertisedAt = nil
}
}
advertisedFromProvider := false
if !response.Advertised && result.AdvertisementCreatedAt.Valid && p.ipp != nil && result.Provider.Valid {
publishedAt := p.ipp.LastPublishTime(result.Provider.String)
if publishedAt != nil {
advertisedFromProvider = true
if publishedAt.After(result.AdvertisementCreatedAt.Time) {
response.Advertised = true
response.AdvertisedAt = new(*publishedAt)
if publishedAt.After(time.Now().Add(ipni_provider.PublishInterval)) {
response.AdvertisedAt = new(result.AdvertisementCreatedAt.Time.Add(ipni_provider.PublishInterval))
}
} else {
response.Advertised = false
response.AdvertisedAt = nil
}
}
}
if !advertisedFromProvider && !response.Advertised {
if result.AdvertisementCreated && result.AdvertisementCreatedAt.Valid {
if time.Since(result.AdvertisementCreatedAt.Time) > ipni_provider.PublishInterval {
// More than 5 seconds since advertisement was created, assume it's published
response.Advertised = true
response.AdvertisedAt = new(result.AdvertisementCreatedAt.Time.Add(ipni_provider.PublishInterval))
} else {
response.Advertised = false
response.AdvertisedAt = nil
}
}
}
// Return JSON response
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
return
}
}
// getSenderAddress retrieves the sender address from the database where role = 'pdp' limit 1
func (p *PDPService) getSenderAddress(ctx context.Context) (common.Address, error) {
var addressStr string
err := p.db.QueryRow(ctx, `SELECT address FROM eth_keys WHERE role = 'pdp' LIMIT 1`).Scan(&addressStr)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return common.Address{}, errors.New("no sender address with role 'pdp' found")
}
return common.Address{}, err
}
address := common.HexToAddress(addressStr)
return address, nil
}
// handleGetDataSetCreationStatus handles the GET request to retrieve the status of a data set creation
func (p *PDPService) handleGetDataSetCreationStatus(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Step 1: Verify that the request is authorized using ECDSA JWT
serviceLabel, err := p.AuthService(r)
if err != nil {
http.Error(w, "Unauthorized: "+err.Error(), http.StatusUnauthorized)
return
}
// Step 2: Extract txHash from the URL
txHash := chi.URLParam(r, "txHash")
if txHash == "" {
http.Error(w, "Missing txHash in URL", http.StatusBadRequest)
return
}
// Clean txHash (ensure it starts with '0x' and is lowercase)
if !strings.HasPrefix(txHash, "0x") {
txHash = "0x" + txHash
}
txHash = strings.ToLower(txHash)
log.Debugw("GetDataSetCreationStatus request",
"txHash", txHash,
"service", serviceLabel)
// Validate txHash is a valid hash
if len(txHash) != 66 { // '0x' + 64 hex chars
http.Error(w, "Invalid txHash length", http.StatusBadRequest)
return
}
if _, err := hex.DecodeString(txHash[2:]); err != nil {
http.Error(w, "Invalid txHash format", http.StatusBadRequest)
return
}
// Step 3: Lookup pdp_data_set_creates by create_message_hash (which is txHash)
var dataSetCreate struct {
CreateMessageHash string `db:"create_message_hash"`
OK *bool `db:"ok"` // Pointer to handle NULL
DataSetCreated bool `db:"data_set_created"`
Service string `db:"service"`
}
err = p.db.QueryRow(ctx, `
SELECT create_message_hash, ok, data_set_created, service
FROM pdp_data_set_creates
WHERE create_message_hash = $1
`, txHash).Scan(&dataSetCreate.CreateMessageHash, &dataSetCreate.OK, &dataSetCreate.DataSetCreated, &dataSetCreate.Service)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
http.Error(w, "Data set creation not found for given txHash", http.StatusNotFound)
return
}
httpServerError(w, http.StatusInternalServerError, "Failed to query data set creation", err)
return
}
// Step 4: Check that the service matches the requesting service
if dataSetCreate.Service != serviceLabel {
http.Error(w, "Unauthorized: service label mismatch", http.StatusUnauthorized)
return
}
// Step 5: Prepare the response
response := struct {
CreateMessageHash string `json:"createMessageHash"`
DataSetCreated bool `json:"dataSetCreated"`
Service string `json:"service"`
TxStatus string `json:"txStatus"`
OK *bool `json:"ok"`
DataSetId *uint64 `json:"dataSetId,omitempty"`
// ConfirmedTxHash is the hash that landed on chain. It differs from
// createMessageHash when Curio replaced the original send by fee.
ConfirmedTxHash string `json:"confirmedTxHash,omitempty"`
}{
CreateMessageHash: dataSetCreate.CreateMessageHash,
DataSetCreated: dataSetCreate.DataSetCreated,
Service: dataSetCreate.Service,
OK: dataSetCreate.OK,
}
// Now get the tx_status (and confirmed hash, if any) from message_waits_eth.
// Wait rows stay keyed by the original Location hash; confirmed_tx_hash is
// the included transaction after optional replace-by-fee.
var txStatus string
var confirmedTxHash sql.NullString
err = p.db.QueryRow(ctx, `
SELECT tx_status, confirmed_tx_hash
FROM message_waits_eth
WHERE signed_tx_hash = $1
`, txHash).Scan(&txStatus, &confirmedTxHash)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
// This should not happen as per foreign key constraints
http.Error(w, "Message status not found for given txHash", http.StatusInternalServerError)
return
}
httpServerError(w, http.StatusInternalServerError, "Failed to query message status", err)
return
}
response.TxStatus = txStatus
if confirmedTxHash.Valid {
response.ConfirmedTxHash = confirmedTxHash.String
}
if dataSetCreate.DataSetCreated {
// The data set has been created, get the dataSetId from pdp_data_sets
var dataSetId uint64
err = p.db.QueryRow(ctx, `
SELECT id
FROM pdp_data_sets
WHERE create_message_hash = $1
`, txHash).Scan(&dataSetId)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
// Should not happen, but handle gracefully
http.Error(w, "Data set not found despite data_set_created = true", http.StatusInternalServerError)
return
}
httpServerError(w, http.StatusInternalServerError, "Failed to query data set", err)
return
}
response.DataSetId = &dataSetId
}
log.Debugw("GetDataSetCreationStatus response",
"txHash", txHash,
"txStatus", response.TxStatus,
"confirmedTxHash", response.ConfirmedTxHash,
"dataSetCreated", response.DataSetCreated,
"ok", response.OK,
"dataSetId", response.DataSetId)
// Step 6: Return the response as JSON
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, "Failed to write response: "+err.Error(), http.StatusInternalServerError)
return
}
}
// handleGetDataSet handles the GET request to retrieve the details of a data set
func (p *PDPService) handleGetDataSet(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Step 1: Verify that the request is authorized using ECDSA JWT
serviceLabel, err := p.AuthService(r)
if err != nil {
http.Error(w, "Unauthorized: "+err.Error(), http.StatusUnauthorized)
return
}
// Step 2: Extract dataSetId from the URL
dataSetIdStr := chi.URLParam(r, "dataSetId")
if dataSetIdStr == "" {
http.Error(w, "Missing data set ID in URL", http.StatusBadRequest)
return
}
// Convert dataSetId to uint64
dataSetId, err := strconv.ParseUint(dataSetIdStr, 10, 64)
if err != nil {
http.Error(w, "Invalid data set ID format", http.StatusBadRequest)
return
}
// Step 3: Retrieve the data set from the database
var dataSet struct {
ID uint64 `db:"id"`
Service string `db:"service"`
}
err = p.db.QueryRow(ctx, `
SELECT id, service
FROM pdp_data_sets
WHERE id = $1
`, dataSetId).Scan(&dataSet.ID, &dataSet.Service)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
http.Error(w, "Data set not found", http.StatusNotFound)
return
}
httpServerError(w, http.StatusInternalServerError, "Failed to retrieve data set", err)
return
}
// Step 4: Check that the data set belongs to the requesting service
if dataSet.Service != serviceLabel {
http.Error(w, "Unauthorized: data set does not belong to your service", http.StatusUnauthorized)
return
}
// Step 5: Retrieve the pieces associated with the data set
// Join with parked_pieces to get the raw size for sub-pieces
// Note: aggregate pieces are not stored, only sub-pieces
var pieces []struct {
PieceID uint64 `db:"piece_id"`
PieceCid string `db:"piece"`
Removed bool `db:"removed"`
SubPieceCID string `db:"sub_piece"`
SubPieceOffset int64 `db:"sub_piece_offset"`
SubPieceSize int64 `db:"sub_piece_size"`
SubPieceRawSize uint64 `db:"sub_piece_raw_size"`
}
err = p.db.Select(ctx, &pieces, `
SELECT
dsp.piece_id,
dsp.piece,
dsp.removed,
dsp.sub_piece,
dsp.sub_piece_offset,
dsp.sub_piece_size,
pp.piece_raw_size AS sub_piece_raw_size
FROM pdp_data_set_pieces dsp
-- Use pdp_pieceref to get to the sub-piece's raw size
JOIN pdp_piecerefs ppr ON ppr.id = dsp.pdp_pieceref
JOIN parked_piece_refs pprf ON pprf.ref_id = ppr.piece_ref
JOIN parked_pieces pp ON pp.id = pprf.piece_id
WHERE dsp.data_set = $1
ORDER BY dsp.piece_id, dsp.sub_piece_offset
`, dataSetId)
if err != nil {
httpServerError(w, http.StatusInternalServerError, "Failed to retrieve data set pieces", err)
return
}
// Step 6: Get the next challenge epoch (can be NULL for uninitialized data sets)
var nextChallengeEpoch *int64
err = p.db.QueryRow(ctx, `
SELECT prove_at_epoch
FROM pdp_data_sets
WHERE id = $1
`, dataSetId).Scan(&nextChallengeEpoch)
if err != nil {
httpServerError(w, http.StatusInternalServerError, "Failed to retrieve next challenge epoch", err)
return
}
// Step 7: Prepare the response
// Use 0 to indicate uninitialized data set (no challenge epoch set yet)
// This maintains compatibility with SDK expectations
epochValue := int64(0)
if nextChallengeEpoch != nil {
epochValue = *nextChallengeEpoch
}
response := struct {
ID uint64 `json:"id"`
Pieces []PieceEntry `json:"pieces"`
NextChallengeEpoch int64 `json:"nextChallengeEpoch"`
}{
ID: dataSet.ID,
NextChallengeEpoch: epochValue,
Pieces: []PieceEntry{}, // Initialize as empty array, not nil
}
// Calculate aggregate piece raw sizes by summing sub-piece raw sizes (group by piece_id)
pieceRawSizes := make(map[uint64]uint64)
for _, piece := range pieces {
pieceRawSizes[piece.PieceID] += piece.SubPieceRawSize
}
aggregatePieceCIDs := make(map[uint64]string)
showRemoved := chi.URLParam(r, "XXXshowRemoved") == "true"
// Convert pieces to the desired JSON format
for _, piece := range pieces {
if !showRemoved && piece.Removed {
continue
}
// Calculate aggregate piece CID on first use for this piece_id
pcv2Str, exists := aggregatePieceCIDs[piece.PieceID]
if !exists {
aggregateRawSize := pieceRawSizes[piece.PieceID]
pcInfo, err := PieceCidV2FromV1Str(piece.PieceCid, aggregateRawSize)
if err != nil {
http.Error(w, "Invalid PieceCID: "+err.Error(), http.StatusBadRequest)
return
}
pcv2Str = pcInfo.CidV2.String()
aggregatePieceCIDs[piece.PieceID] = pcv2Str
}
// Use the raw size for the sub piece
spcInfo, err := PieceCidV2FromV1Str(piece.SubPieceCID, piece.SubPieceRawSize)
if err != nil {
http.Error(w, "Invalid SubPieceCID: "+err.Error(), http.StatusBadRequest)
return
}
response.Pieces = append(response.Pieces, PieceEntry{
PieceID: piece.PieceID,
PieceCID: pcv2Str,
SubPieceCID: spcInfo.CidV2.String(),
SubPieceOffset: piece.SubPieceOffset,
})
}
// Step 8: Return the response as JSON
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
http.Error(w, "Failed to write response: "+err.Error(), http.StatusInternalServerError)
return
}
}
// PieceEntry represents a piece in the data set for JSON serialization
type PieceEntry struct {
PieceID uint64 `json:"pieceId"`
PieceCID string `json:"pieceCid"`
SubPieceCID string `json:"subPieceCid"`
SubPieceOffset int64 `json:"subPieceOffset"`
}
// handleGetPieceAdditionStatus handles GET /pdp/data-sets/{dataSetId}/pieces/added/{txHash}
func (p *PDPService) handleGetPieceAdditionStatus(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
// Step 1: Verify that the request is authorized using ECDSA JWT
serviceLabel, err := p.AuthService(r)
if err != nil {
http.Error(w, "Unauthorized: "+err.Error(), http.StatusUnauthorized)
return
}
// Step 2: Extract parameters from the URL
dataSetIdStr := chi.URLParam(r, "dataSetId")
txHash := chi.URLParam(r, "txHash")
if dataSetIdStr == "" {
http.Error(w, "Missing data set ID in URL", http.StatusBadRequest)
return
}
if txHash == "" {
http.Error(w, "Missing transaction hash in URL", http.StatusBadRequest)
return
}
// Convert dataSetId to uint64
dataSetId, err := strconv.ParseUint(dataSetIdStr, 10, 64)
if err != nil {
http.Error(w, "Invalid data set ID format", http.StatusBadRequest)
return
}
// Clean txHash (ensure it starts with '0x' and is lowercase)
if !strings.HasPrefix(txHash, "0x") {
txHash = "0x" + txHash
}
txHash = strings.ToLower(txHash)
// Validate txHash is a valid hash
if len(txHash) != 66 { // '0x' + 64 hex chars
http.Error(w, "Invalid txHash length", http.StatusBadRequest)
return
}
if _, err := hex.DecodeString(txHash[2:]); err != nil {
http.Error(w, "Invalid txHash format", http.StatusBadRequest)
return
}
// Step 3: Verify data set ownership
var dataSetService string
err = p.db.QueryRow(ctx, `
SELECT service
FROM pdp_data_sets
WHERE id = $1
`, dataSetId).Scan(&dataSetService)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
http.Error(w, "Data set not found", http.StatusNotFound)
return
}
httpServerError(w, http.StatusInternalServerError, "Failed to retrieve data set", err)
return
}
if dataSetService != serviceLabel {
// Same response as not found to avoid leaking information
http.Error(w, "Data set not found", http.StatusNotFound)
return
}
// Step 4: Query pdp_data_set_piece_adds for this transaction
type PieceAddInfo struct {
Piece string `db:"piece"`
AddMessageIndex int `db:"add_message_index"`
SubPiece string `db:"sub_piece"`
SubPieceOffset int64 `db:"sub_piece_offset"`
SubPieceSize int64 `db:"sub_piece_size"`
AddMessageOK *bool `db:"add_message_ok"`
PiecesAdded bool `db:"pieces_added"`
}
var pieceAdds []PieceAddInfo
err = p.db.Select(ctx, &pieceAdds, `
SELECT piece, add_message_index, sub_piece, sub_piece_offset,
sub_piece_size, add_message_ok, pieces_added
FROM pdp_data_set_piece_adds
WHERE data_set = $1 AND add_message_hash = $2
ORDER BY add_message_index, sub_piece_offset
`, dataSetId, txHash)
if err != nil {
httpServerError(w, http.StatusInternalServerError, "Failed to query piece additions", err)
return
}
if len(pieceAdds) == 0 {
http.Error(w, "Piece addition not found for given transaction", http.StatusNotFound)
return
}
// Step 5: Get transaction status from message_waits_eth.
// Wait rows stay keyed by the original Location hash; confirmed_tx_hash is
// the included transaction after optional replace-by-fee.
var txStatus string
var confirmedTxHash sql.NullString
err = p.db.QueryRow(ctx, `
SELECT tx_status, confirmed_tx_hash FROM message_waits_eth WHERE signed_tx_hash = $1
`, txHash).Scan(&txStatus, &confirmedTxHash)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
http.Error(w, "Transaction status not found", http.StatusNotFound)
return
}
httpServerError(w, http.StatusInternalServerError, "Failed to query transaction status", err)
return
}
// Determine unique pieces list
uniquePieceMap := make(map[string]bool)
for _, ra := range pieceAdds {
uniquePieceMap[ra.Piece] = true
}
// Step 6: If transaction is confirmed and successful, get assigned piece IDs
var confirmedPieceIds []uint64
if txStatus == "confirmed" && len(pieceAdds) > 0 && pieceAdds[0].AddMessageOK != nil && *pieceAdds[0].AddMessageOK {
// Query pdp_data_set_pieces directly using the transaction hash
// This gives us the exact pieces added in THIS transaction even if there are duplicate pieces
err = p.db.Select(ctx, &confirmedPieceIds, `
SELECT DISTINCT piece_id
FROM pdp_data_set_pieces
WHERE data_set = $1
AND add_message_hash = $2
ORDER BY piece_id
`, dataSetId, txHash)
if err != nil {
httpServerError(w, http.StatusInternalServerError, "Failed to query confirmed pieces", err)
return
}
}
if confirmedPieceIds != nil && len(confirmedPieceIds) != len(pieceAdds) {
msg := fmt.Sprintf("Mismatch in confirmed piece IDs count (%d) vs number of pieces added (%d) for tx %s", len(confirmedPieceIds), len(pieceAdds), txHash)
log.Error(msg)
http.Error(w, msg, http.StatusInternalServerError)
return
} // else confirmedPieceIds is nil because they haven't landed yet, or we got the right number of confirmed pieces
// Step 7: Build and send response
// Check that all pieces have the same PiecesAdded value (consistency check)
if len(pieceAdds) > 0 {
firstPiecesAdded := pieceAdds[0].PiecesAdded
for _, pa := range pieceAdds[1:] {
if pa.PiecesAdded != firstPiecesAdded {
http.Error(w, "Inconsistent piecesAdded state for this transaction's pieces", http.StatusInternalServerError)
return
}
}
}
allPiecesProcessed := false
if len(pieceAdds) > 0 {
allPiecesProcessed = pieceAdds[0].PiecesAdded
}
response := struct {
TxHash string `json:"txHash"`
TxStatus string `json:"txStatus"`
DataSetId uint64 `json:"dataSetId"`
PieceCount int `json:"pieceCount"`
AddMessageOK *bool `json:"addMessageOk"`
PiecesAdded bool `json:"piecesAdded"`
ConfirmedPieceIds []uint64 `json:"confirmedPieceIds,omitempty"`
// ConfirmedTxHash is the hash that landed on chain. It differs from
// txHash when Curio replaced the original send by fee.
ConfirmedTxHash string `json:"confirmedTxHash,omitempty"`
}{
TxHash: txHash,
TxStatus: txStatus,
DataSetId: dataSetId,
PieceCount: len(uniquePieceMap),
AddMessageOK: pieceAdds[0].AddMessageOK,
PiecesAdded: allPiecesProcessed,
ConfirmedPieceIds: confirmedPieceIds,
}
if confirmedTxHash.Valid {
response.ConfirmedTxHash = confirmedTxHash.String
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(response); err != nil {
http.Error(w, "Failed to encode response: "+err.Error(), http.StatusInternalServerError)
return
}
}
func normalizeDeletePieceIDs(ids []uint64) ([]int64, error) {
if len(ids) > MaxDeletePiecesBatchSize {
return nil, fmt.Errorf("piece count (%d) exceeds the maximum allowed per DeletePiece call (%d)", len(ids), MaxDeletePiecesBatchSize)
}
seen := make(map[uint64]struct{}, len(ids))
out := make([]int64, 0, len(ids))
for _, id := range ids {
if id > math.MaxInt64 {
return nil, fmt.Errorf("piece ID %d is out of range", id)
}
if _, ok := seen[id]; ok {