@@ -551,10 +551,10 @@ func TestWaitForDataSetCreated(t *testing.T) {
551551 calls ++
552552 w .Header ().Set ("Content-Type" , "application/json" )
553553 if calls == 1 {
554- _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x1 ","service":"svc","txStatus":"pending","dataSetCreated":false,"ok":null}` )
554+ _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","service":"svc","txStatus":"pending","dataSetCreated":false,"ok":null}` )
555555 return
556556 }
557- _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x1 ","service":"svc","txStatus":"confirmed","dataSetCreated":true,"ok":true,"dataSetId":42}` )
557+ _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","service":"svc","txStatus":"confirmed","dataSetCreated":true,"ok":true,"dataSetId":42}` )
558558 }))
559559 status , err := c .WaitForDataSetCreated (context .Background (), c .BaseURL ().String ()+ "pdp/data-sets/created/0x1" , 10 * time .Millisecond )
560560 if err != nil {
@@ -569,7 +569,7 @@ func TestGetDataSetCreationStatus_Accepts202(t *testing.T) {
569569 c , _ := newTestClient (t , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
570570 w .Header ().Set ("Content-Type" , "application/json" )
571571 w .WriteHeader (http .StatusAccepted )
572- _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x1 ","service":"svc","txStatus":"pending","dataSetCreated":false,"ok":null}` )
572+ _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","service":"svc","txStatus":"pending","dataSetCreated":false,"ok":null}` )
573573 }))
574574 status , err := c .GetDataSetCreationStatus (context .Background (), c .BaseURL ().String ()+ "pdp/data-sets/created/0x1" )
575575 if err != nil {
@@ -580,33 +580,30 @@ func TestGetDataSetCreationStatus_Accepts202(t *testing.T) {
580580 }
581581}
582582
583- func TestWaitForDataSetCreated_ConfirmedFalseStillPending (t * testing.T ) {
583+ func TestWaitForDataSetCreated_ConfirmedWithoutResultStillPending (t * testing.T ) {
584584 var calls int
585585 c , _ := newTestClient (t , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
586586 calls ++
587587 w .Header ().Set ("Content-Type" , "application/json" )
588588 if calls == 1 {
589- _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x1 ","service":"svc","txStatus":"confirmed","dataSetCreated":false,"ok":null}` )
589+ _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","service":"svc","txStatus":"confirmed","dataSetCreated":false,"ok":null}` )
590590 return
591591 }
592- _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x1 ","service":"svc","txStatus":"confirmed","dataSetCreated":true,"ok":true,"dataSetId":42}` )
592+ _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","service":"svc","txStatus":"confirmed","dataSetCreated":true,"ok":true,"dataSetId":42}` )
593593 }))
594- status , err := c .WaitForDataSetCreated (context .Background (), c .BaseURL ().String ()+ "pdp/data-sets/created/0x1" , 10 * time .Millisecond )
594+ status , err := c .WaitForDataSetCreated (context .Background (), c .BaseURL ().String ()+ "pdp/data-sets/created/0x1" , time .Millisecond )
595595 if err != nil {
596596 t .Fatal (err )
597597 }
598- if calls < 2 {
599- t .Fatalf ("expected multiple polls, got %d" , calls )
600- }
601- if status .DataSetID == nil || ! status .DataSetID .Equal (types .NewBigInt (42 )) {
602- t .Fatalf ("id=%v" , status .DataSetID )
598+ if calls != 2 || status .DataSetID == nil || ! status .DataSetID .Equal (types .NewBigInt (42 )) {
599+ t .Fatalf ("calls=%d status=%+v" , calls , status )
603600 }
604601}
605602
606603func TestWaitForDataSetCreated_Rejected (t * testing.T ) {
607604 c , _ := newTestClient (t , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
608605 w .Header ().Set ("Content-Type" , "application/json" )
609- _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x1 ","service":"svc","txStatus":"rejected","dataSetCreated":false,"ok":false}` )
606+ _ , _ = fmt .Fprint (w , `{"createMessageHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","service":"svc","txStatus":"rejected","dataSetCreated":false,"ok":false}` )
610607 }))
611608 _ , err := c .WaitForDataSetCreated (context .Background (), c .BaseURL ().String ()+ "pdp/data-sets/created/0x1" , 10 * time .Millisecond )
612609 if ! errors .Is (err , ErrTxRejected ) {
@@ -687,13 +684,13 @@ func TestAddPieces(t *testing.T) {
687684}
688685
689686func TestAddPieces_MaxBatchSizeAccepted (t * testing.T ) {
690- pcInfo , err := piece .CalculateFromBytes ([]byte ("hi" ))
691- if err != nil {
692- t .Fatalf ("CalculateFromBytes: %v" , err )
693- }
694687 pieces := make ([]AddPieceInput , MaxAddPiecesBatchSize )
695688 for i := range pieces {
696- pieces [i ] = AddPieceInput {PieceCID : pcInfo .CIDv1 }
689+ info , err := piece .CalculateFromBytes ([]byte {byte (i ), 0xa5 })
690+ if err != nil {
691+ t .Fatalf ("CalculateFromBytes(%d): %v" , i , err )
692+ }
693+ pieces [i ] = AddPieceInput {PieceCID : info .CIDv1 }
697694 }
698695 var gotPieces int
699696 c , _ := newTestClient (t , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
@@ -713,6 +710,42 @@ func TestAddPieces_MaxBatchSizeAccepted(t *testing.T) {
713710 }
714711}
715712
713+ func TestAddPiecesRejectsDuplicateCanonicalCIDBeforeRequest (t * testing.T ) {
714+ info := testPieceInfoV2 (t )
715+ requests := 0
716+ c , _ := newTestClient (t , http .HandlerFunc (func (http.ResponseWriter , * http.Request ) {
717+ requests ++
718+ }))
719+ _ , err := c .AddPieces (context .Background (), types .NewBigInt (5 ), []AddPieceInput {
720+ {PieceCID : info .CIDv1 },
721+ {PieceCID : info .CIDv2 },
722+ }, []byte {1 })
723+ if err == nil || ! strings .Contains (err .Error (), "duplicate pieceCID" ) {
724+ t .Fatalf ("error=%v want duplicate pieceCID" , err )
725+ }
726+ if requests != 0 {
727+ t .Fatalf ("requests=%d want 0" , requests )
728+ }
729+ }
730+
731+ func TestAddPiecesAllowsSameCIDAcrossRequests (t * testing.T ) {
732+ info := testPieceInfoV2 (t )
733+ requests := 0
734+ c , _ := newTestClient (t , http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
735+ requests ++
736+ w .Header ().Set ("Location" , "/pdp/data-sets/5/pieces/added/0xdead000000000000000000000000000000000000000000000000000000000000" )
737+ w .WriteHeader (http .StatusCreated )
738+ }))
739+ for range 2 {
740+ if _ , err := c .AddPieces (context .Background (), types .NewBigInt (5 ), []AddPieceInput {{PieceCID : info .CIDv2 }}, []byte {1 }); err != nil {
741+ t .Fatal (err )
742+ }
743+ }
744+ if requests != 2 {
745+ t .Fatalf ("requests=%d want 2" , requests )
746+ }
747+ }
748+
716749func TestAddPieces_TooManyPieces (t * testing.T ) {
717750 c , _ := newTestClient (t , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
718751 t .Fatal ("should not reach server" )
@@ -756,10 +789,10 @@ func TestWaitForPiecesAdded(t *testing.T) {
756789 calls ++
757790 w .Header ().Set ("Content-Type" , "application/json" )
758791 if calls == 1 {
759- _ , _ = fmt .Fprint (w , `{"txHash":"0x1 ","txStatus":"pending","dataSetId":5,"pieceCount":1,"addMessageOk":null,"piecesAdded":false}` )
792+ _ , _ = fmt .Fprint (w , `{"txHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","txStatus":"pending","dataSetId":5,"pieceCount":1,"addMessageOk":null,"piecesAdded":false}` )
760793 return
761794 }
762- _ , _ = fmt .Fprint (w , `{"txHash":"0x1 ","txStatus":"confirmed","dataSetId":5,"pieceCount":1,"addMessageOk":true,"piecesAdded":true,"confirmedPieceIds":[10,11]}` )
795+ _ , _ = fmt .Fprint (w , `{"txHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","txStatus":"confirmed","dataSetId":5,"pieceCount":1,"addMessageOk":true,"piecesAdded":true,"confirmedPieceIds":[10,11]}` )
763796 }))
764797 status , err := c .WaitForPiecesAdded (context .Background (), c .BaseURL ().String ()+ "status" , 10 * time .Millisecond )
765798 if err != nil {
@@ -774,7 +807,7 @@ func TestGetAddPiecesStatus_Accepts202(t *testing.T) {
774807 c , _ := newTestClient (t , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
775808 w .Header ().Set ("Content-Type" , "application/json" )
776809 w .WriteHeader (http .StatusAccepted )
777- _ , _ = fmt .Fprint (w , `{"txHash":"0x1 ","txStatus":"pending","dataSetId":5,"pieceCount":1,"addMessageOk":null,"piecesAdded":false}` )
810+ _ , _ = fmt .Fprint (w , `{"txHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","txStatus":"pending","dataSetId":5,"pieceCount":1,"addMessageOk":null,"piecesAdded":false}` )
778811 }))
779812 status , err := c .GetAddPiecesStatus (context .Background (), c .BaseURL ().String ()+ "status" )
780813 if err != nil {
@@ -785,26 +818,23 @@ func TestGetAddPiecesStatus_Accepts202(t *testing.T) {
785818 }
786819}
787820
788- func TestWaitForPiecesAdded_ConfirmedFalseStillPending (t * testing.T ) {
821+ func TestWaitForPiecesAdded_ConfirmedWithoutResultStillPending (t * testing.T ) {
789822 var calls int
790823 c , _ := newTestClient (t , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
791824 calls ++
792825 w .Header ().Set ("Content-Type" , "application/json" )
793826 if calls == 1 {
794- _ , _ = fmt .Fprint (w , `{"txHash":"0x1 ","txStatus":"confirmed","dataSetId":5,"pieceCount":1,"addMessageOk":null,"piecesAdded":false}` )
827+ _ , _ = fmt .Fprint (w , `{"txHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","txStatus":"confirmed","dataSetId":5,"pieceCount":1,"addMessageOk":null,"piecesAdded":false}` )
795828 return
796829 }
797- _ , _ = fmt .Fprint (w , `{"txHash":"0x1 ","txStatus":"confirmed","dataSetId":5,"pieceCount":1,"addMessageOk":true,"piecesAdded":true,"confirmedPieceIds":[10,11 ]}` )
830+ _ , _ = fmt .Fprint (w , `{"txHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","txStatus":"confirmed","dataSetId":5,"pieceCount":1,"addMessageOk":true,"piecesAdded":true,"confirmedPieceIds":[10]}` )
798831 }))
799- status , err := c .WaitForPiecesAdded (context .Background (), c .BaseURL ().String ()+ "status" , 10 * time .Millisecond )
832+ status , err := c .WaitForPiecesAdded (context .Background (), c .BaseURL ().String ()+ "status" , time .Millisecond )
800833 if err != nil {
801834 t .Fatal (err )
802835 }
803- if calls < 2 {
804- t .Fatalf ("expected multiple polls, got %d" , calls )
805- }
806- if len (status .ConfirmedPieceIDs ) != 2 {
807- t .Fatalf ("len=%d" , len (status .ConfirmedPieceIDs ))
836+ if calls != 2 || len (status .ConfirmedPieceIDs ) != 1 {
837+ t .Fatalf ("calls=%d status=%+v" , calls , status )
808838 }
809839}
810840
@@ -825,7 +855,7 @@ func TestWaitForPiecesAdded_404ReturnsHTTPError(t *testing.T) {
825855func TestGetAddPiecesStatus_LargeUint64DataSetID (t * testing.T ) {
826856 c , _ := newTestClient (t , http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
827857 w .Header ().Set ("Content-Type" , "application/json" )
828- _ , _ = fmt .Fprint (w , `{"txHash":"0x1 ","txStatus":"confirmed","dataSetId":9223372036854775808,"pieceCount":1,"addMessageOk":true,"piecesAdded":true,"confirmedPieceIds":[10]}` )
858+ _ , _ = fmt .Fprint (w , `{"txHash":"0x0000000000000000000000000000000000000000000000000000000000000001 ","txStatus":"confirmed","dataSetId":9223372036854775808,"pieceCount":1,"addMessageOk":true,"piecesAdded":true,"confirmedPieceIds":[10]}` )
829859 }))
830860 status , err := c .GetAddPiecesStatus (context .Background (), c .BaseURL ().String ()+ "status" )
831861 if err != nil {
@@ -923,7 +953,7 @@ func TestIsRetryable(t *testing.T) {
923953 }
924954 for _ , tc := range tests {
925955 t .Run (tc .name , func (t * testing.T ) {
926- if got := isRetryable (tc .err ); got != tc .want {
956+ if got := isRetryable (context . Background (), tc .err ); got != tc .want {
927957 t .Errorf ("isRetryable(%v) = %v, want %v" , tc .err , got , tc .want )
928958 }
929959 })
0 commit comments