@@ -21,6 +21,7 @@ import (
2121 "github.com/strahe/synapse-go/piece"
2222 "github.com/strahe/synapse-go/signer"
2323 "github.com/strahe/synapse-go/types"
24+ "github.com/strahe/synapse-go/warmstorage"
2425)
2526
2627func mustPieceInfo (t * testing.T ) piece.PieceInfo {
@@ -576,6 +577,148 @@ func TestProviderContextWaitForDataSetCreatedRejectsWrongProvider(t *testing.T)
576577 }
577578}
578579
580+ func TestProviderContextWaitForDataSetCreatedAcceptsZeroProviderID (t * testing.T ) {
581+ txHash := common .HexToHash ("0x1234" )
582+ dataSetID := types .NewBigInt (77 )
583+ clientID := types .NewBigInt (7 )
584+ waitCalls := 0
585+ client := & fakePDPProviderClient {
586+ waitForCreatedFn : func (context.Context , string , time.Duration ) (* pdp.CreateDataSetStatus , error ) {
587+ waitCalls ++
588+ id := copyBigInt (dataSetID )
589+ return & pdp.CreateDataSetStatus {CreateMessageHash : txHash , DataSetID : & id }, nil
590+ },
591+ }
592+ c := mustWritableProviderContext (t , client )
593+ result , err := c .WaitForDataSetCreated (context .Background (), CreateDataSetSubmission {
594+ TransactionID : txHash .Hex (),
595+ StatusURL : "https://sp.example.com/status" ,
596+ ClientDataSetID : & clientID ,
597+ })
598+ if err != nil {
599+ t .Fatalf ("WaitForDataSetCreated: %v" , err )
600+ }
601+ if waitCalls != 1 {
602+ t .Fatalf ("waitCalls=%d want 1" , waitCalls )
603+ }
604+ if ! result .DataSet .ProviderID ().Equal (testProvider ().ID ) ||
605+ ! result .DataSet .DataSetID ().Equal (dataSetID ) ||
606+ ! result .DataSet .ClientDataSetID ().Equal (clientID ) {
607+ t .Fatalf ("result=%+v" , result .DataSet )
608+ }
609+ }
610+
611+ func TestProviderContextWaitForDataSetCreatedRejectsInvalidSubmission (t * testing.T ) {
612+ clientID := types .NewBigInt (7 )
613+ valid := CreateDataSetSubmission {
614+ TransactionID : common .HexToHash ("0x1234" ).Hex (),
615+ StatusURL : "https://sp.example.com/status" ,
616+ ClientDataSetID : & clientID ,
617+ }
618+ tests := map [string ]CreateDataSetSubmission {
619+ "empty transaction" : {TransactionID : "" , StatusURL : valid .StatusURL , ClientDataSetID : valid .ClientDataSetID },
620+ "short transaction" : {TransactionID : "0xbeef" , StatusURL : valid .StatusURL , ClientDataSetID : valid .ClientDataSetID },
621+ "zero transaction" : {TransactionID : common.Hash {}.Hex (), StatusURL : valid .StatusURL , ClientDataSetID : valid .ClientDataSetID },
622+ "empty status URL" : {TransactionID : valid .TransactionID , StatusURL : "" , ClientDataSetID : valid .ClientDataSetID },
623+ "missing client ID" : {TransactionID : valid .TransactionID , StatusURL : valid .StatusURL },
624+ }
625+ for name , submission := range tests {
626+ t .Run (name , func (t * testing.T ) {
627+ waitCalls := 0
628+ client := & fakePDPProviderClient {
629+ waitForCreatedFn : func (context.Context , string , time.Duration ) (* pdp.CreateDataSetStatus , error ) {
630+ waitCalls ++
631+ return nil , errors .New ("wait should not be called" )
632+ },
633+ }
634+ c := mustWritableProviderContext (t , client )
635+ _ , err := c .WaitForDataSetCreated (context .Background (), submission )
636+ if ! errors .Is (err , ErrInvalidArgument ) {
637+ t .Fatalf ("WaitForDataSetCreated error=%v want ErrInvalidArgument" , err )
638+ }
639+ if waitCalls != 0 {
640+ t .Fatalf ("waitCalls=%d want 0" , waitCalls )
641+ }
642+ })
643+ }
644+ }
645+
646+ func TestDataSetContextCommitRejectsEndedAndValidatorFailuresBeforeAddPieces (t * testing.T ) {
647+ info := mustPieceInfo (t )
648+ dataSetID := types .NewBigInt (42 )
649+ want := errors .New ("not live" )
650+
651+ t .Run ("ended rail" , func (t * testing.T ) {
652+ addCalls := 0
653+ reader := & fakeFWSSDataSetReader {
654+ info : & warmstorage.DataSetInfo {DataSetID : dataSetID , PDPEndEpoch : 3778900 },
655+ }
656+ client := & fakePDPProviderClient {
657+ addPiecesFn : func (context.Context , types.BigInt , []pdp.AddPieceInput , []byte ) (* pdp.AddPiecesResult , error ) {
658+ addCalls ++
659+ t .Fatal ("AddPieces must not run for an ended data set" )
660+ return nil , nil
661+ },
662+ }
663+ c := mustWritableDataSetContext (t , client , testDataSetRef (dataSetID , types .NewBigInt (7 )), WithFWSSDataSetReader (reader ))
664+ _ , err := c .Commit (context .Background (), CommitRequest {Pieces : []PieceInput {{PieceCID : info .CIDv2 }}})
665+ requireDataSetPDPPaymentTerminated (t , err , dataSetID , 3778900 )
666+ if addCalls != 0 {
667+ t .Fatalf ("addCalls=%d want 0" , addCalls )
668+ }
669+ if reader .calls != 1 || ! reader .gotID .Equal (dataSetID ) {
670+ t .Fatalf ("reader calls=%d gotID=%s" , reader .calls , reader .gotID .String ())
671+ }
672+ })
673+
674+ t .Run ("validator failure" , func (t * testing.T ) {
675+ addCalls := 0
676+ validator := & fakeDataSetValidator {err : want }
677+ client := & fakePDPProviderClient {
678+ addPiecesFn : func (context.Context , types.BigInt , []pdp.AddPieceInput , []byte ) (* pdp.AddPiecesResult , error ) {
679+ addCalls ++
680+ t .Fatal ("AddPieces must not run after validator failure" )
681+ return nil , nil
682+ },
683+ }
684+ c := mustWritableDataSetContext (t , client , testDataSetRef (dataSetID , types .NewBigInt (7 )), WithDataSetValidator (validator ))
685+ _ , err := c .Commit (context .Background (), CommitRequest {Pieces : []PieceInput {{PieceCID : info .CIDv2 }}})
686+ if ! errors .Is (err , want ) {
687+ t .Fatalf ("Commit error=%v want %v" , err , want )
688+ }
689+ if addCalls != 0 {
690+ t .Fatalf ("addCalls=%d want 0" , addCalls )
691+ }
692+ if len (validator .calls ) != 1 || ! validator .calls [0 ].Equal (dataSetID ) {
693+ t .Fatalf ("validator calls=%v" , validator .calls )
694+ }
695+ })
696+ }
697+
698+ func TestDataSetContextUploadRejectsEndedExistingDataSetBeforeStore (t * testing.T ) {
699+ dataSetID := types .NewBigInt (13269 )
700+ storeCalled := false
701+ reader := & fakeFWSSDataSetReader {
702+ info : & warmstorage.DataSetInfo {DataSetID : dataSetID , PDPEndEpoch : 3778900 },
703+ }
704+ client := & fakePDPProviderClient {
705+ uploadStreamingFn : func (context.Context , io.Reader , pdp.UploadPieceStreamingOptions ) (* pdp.UploadStreamingResult , error ) {
706+ storeCalled = true
707+ t .Fatal ("Store must not run when the existing data set cannot accept uploads" )
708+ return nil , nil
709+ },
710+ }
711+ c := mustWritableDataSetContext (t , client , testDataSetRef (dataSetID , types .NewBigInt (99 )), WithFWSSDataSetReader (reader ))
712+ _ , err := c .Upload (context .Background (), bytes .NewReader ([]byte ("payload" )), nil )
713+ requireDataSetPDPPaymentTerminated (t , err , dataSetID , 3778900 )
714+ if storeCalled {
715+ t .Fatal ("Store was called" )
716+ }
717+ if reader .calls != 1 || ! reader .gotID .Equal (dataSetID ) {
718+ t .Fatalf ("reader calls=%d gotID=%s" , reader .calls , reader .gotID .String ())
719+ }
720+ }
721+
579722func TestContextPullRoutesByConcreteType (t * testing.T ) {
580723 info := mustPieceInfo (t )
581724 dataSetID := types .NewBigInt (42 )
0 commit comments