From 1890cfd64e964b3b4c827b2fc0100ecaa3fad957 Mon Sep 17 00:00:00 2001 From: jeremy-babylonchain Date: Thu, 25 Jul 2024 20:59:50 +0800 Subject: [PATCH 1/6] additional ordinals tests --- internal/services/ordinals.go | 4 +- tests/assets_checking_test.go | 635 +++++++++++++++++++++++++++++++++- tests/config/config-test.yml | 2 +- 3 files changed, 624 insertions(+), 17 deletions(-) diff --git a/internal/services/ordinals.go b/internal/services/ordinals.go index 3a5ff5c..04d58dd 100644 --- a/internal/services/ordinals.go +++ b/internal/services/ordinals.go @@ -98,11 +98,11 @@ func (s *Services) verifyViaUnisatService( var results []*SafeUTXOPublic for _, utxo := range utxos { key := fmt.Sprintf("%s:%d", utxo.Txid, utxo.Vout) - _, ok := inscriptionsUtxosMap[key] + inscriptions, ok := inscriptionsUtxosMap[key] results = append(results, &SafeUTXOPublic{ TxId: utxo.Txid, Vout: utxo.Vout, - Inscription: ok, + Inscription: ok && len(inscriptions) > 0, }) } return results, nil diff --git a/tests/assets_checking_test.go b/tests/assets_checking_test.go index 765a961..9c51e36 100644 --- a/tests/assets_checking_test.go +++ b/tests/assets_checking_test.go @@ -2,14 +2,18 @@ package tests import ( "bytes" + "context" "encoding/json" "math/rand" "net/http" "testing" + "time" + "github.com/babylonchain/staking-api-service/internal/api" "github.com/babylonchain/staking-api-service/internal/api/handlers" "github.com/babylonchain/staking-api-service/internal/clients" "github.com/babylonchain/staking-api-service/internal/clients/ordinals" + "github.com/babylonchain/staking-api-service/internal/clients/unisat" "github.com/babylonchain/staking-api-service/internal/config" "github.com/babylonchain/staking-api-service/internal/services" "github.com/babylonchain/staking-api-service/internal/types" @@ -22,15 +26,28 @@ import ( const verifyUTXOsPath = "/v1/ordinals/verify-utxos" -func createPayload(t *testing.T, r *rand.Rand, netParam *chaincfg.Params, size int) handlers.VerifyUTXOsRequestPayload { +func createPayload(t *testing.T, r *rand.Rand, netParam *chaincfg.Params, size int, hasInvalid bool) handlers.VerifyUTXOsRequestPayload { var utxos []types.UTXOIdentifier + + numInvalidUTXOs := 0 + if hasInvalid { + numInvalidUTXOs = r.Intn(size) + 1 + } + for i := 0; i < size; i++ { - tx, _, err := generateRandomTx(r) - if err != nil { - t.Fatalf("Failed to generate random tx: %v", err) + var txid string + if hasInvalid && i < numInvalidUTXOs { + randomStr := randomString(r, 10) + txid = randomStr + } else { + tx, _, err := generateRandomTx(r) + if err != nil { + t.Fatalf("Failed to generate random tx: %v", err) + } + txid = tx.TxHash().String() } utxos = append(utxos, types.UTXOIdentifier{ - Txid: tx.TxHash().String(), + Txid: txid, Vout: uint32(r.Intn(10)), }) } @@ -68,12 +85,13 @@ func TestVerifyUtxosEndpointNotAvailableIfAssetsConfigNotSet(t *testing.T) { assert.Equal(t, http.StatusNotFound, resp.StatusCode) } +// Test case 1: Fetching UTXOs return via Ordinal Service func FuzzSuccessfullyVerifyUTXOsAssetsViaOrdinalService(f *testing.F) { attachRandomSeedsToFuzzer(f, 10) f.Fuzz(func(t *testing.T, seed int64) { r := rand.New(rand.NewSource(seed)) numOfUTXOs := randomPositiveInt(r, 100) - payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs) + payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { t.Fatalf("Failed to marshal payload: %v", err) @@ -134,14 +152,567 @@ func FuzzSuccessfullyVerifyUTXOsAssetsViaOrdinalService(f *testing.F) { }) } -// TODO: Test case 2: Fetching more than 100 UTXOs should return an error -// TODO: Test case 3: Invalid UTXO txid should return an error -// TODO: Test case 4: Ordinal service return error, fallback to unisat service and return the result -// TODO: Test case 5: Unisat service return error, return error -// TODO: Test case 6: Ordinal service took too long to respond, fallback to unisat service and return the result -// TODO: Test case 7: Unisat service took too long to respond, return error within the timeout window -// TODO: Test case 8: Send 100 UTXOs, ordinal service fail, unisat service return 100(limit), then fetch again for the remaining 1 item (test the pagination) -// TODO: Test case 9: Fall back to unisat when ordinal service return data that is not in the right order of the request UTXOs +// Test case 2: Fetching more than 100 UTXOs should return an error +func TestVerifyUtxosEndpointExceedMaxAllowedLength(t *testing.T) { + r := rand.New(rand.NewSource(time.Now().Unix())) + numOfUTXOs := 101 // Create 101 UTXOs + payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) + jsonPayload, err := json.Marshal(payload) + if err != nil { + t.Fatalf("Failed to marshal payload: %v", err) + } + + // create some ordinal responses that contains inscriptions + numOfUTXOsWithAsset := r.Intn(numOfUTXOs) + + var txidsWithAsset []string + for i := 0; i < numOfUTXOsWithAsset; i++ { + txidsWithAsset = append(txidsWithAsset, payload.UTXOs[i].Txid) + } + + mockedOrdinalResponse := createOrdinalServiceResponse(t, r, payload.UTXOs, txidsWithAsset) + + mockOrdinal := new(mocks.OrdinalsClientInterface) + mockOrdinal.On("FetchUTXOInfos", mock.Anything, mock.Anything).Return(mockedOrdinalResponse, nil) + mockedClients := &clients.Clients{ + Ordinals: mockOrdinal, + } + + testServer := setupTestServer(t, &TestServerDependency{MockedClients: mockedClients}) + defer testServer.Close() + + url := testServer.Server.URL + verifyUTXOsPath + resp, err := http.Post(url, "application/json", bytes.NewReader(jsonPayload)) + if err != nil { + t.Fatalf("Failed to make POST request to %s: %v", url, err) + } + defer resp.Body.Close() + + assert.Equal(t, http.StatusBadRequest, resp.StatusCode) + + // decode the response body + var response api.ErrorResponse + err = json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + t.Fatalf("Failed to decode response body: %v", err) + } + + assert.Equal(t, types.BadRequest.String(), response.ErrorCode, "expected error code to be BAD_REQUEST") + assert.Equal(t, "too many UTXOs in the request", response.Message, "expected error message to be 'too many UTXOs in the request'") +} + +// Test case 3: Invalid UTXO txid should return an error +func TestVerifyUtxosEndpointWithMixedUTXOs(t *testing.T) { + r := rand.New(rand.NewSource(time.Now().Unix())) + numOfUTXOs := 10 + + payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, true) + jsonPayload, err := json.Marshal(payload) + if err != nil { + t.Fatalf("Failed to marshal payload: %v", err) + } + + // create some ordinal responses that contains inscriptions + numOfUTXOsWithAsset := r.Intn(numOfUTXOs) + + var txidsWithAsset []string + for i := 0; i < numOfUTXOsWithAsset; i++ { + txidsWithAsset = append(txidsWithAsset, payload.UTXOs[i].Txid) + } + + mockedOrdinalResponse := createOrdinalServiceResponse(t, r, payload.UTXOs, txidsWithAsset) + + mockOrdinal := new(mocks.OrdinalsClientInterface) + mockOrdinal.On("FetchUTXOInfos", mock.Anything, mock.Anything).Return(mockedOrdinalResponse, nil) + mockedClients := &clients.Clients{ + Ordinals: mockOrdinal, + } + + testServer := setupTestServer(t, &TestServerDependency{MockedClients: mockedClients}) + defer testServer.Close() + + url := testServer.Server.URL + verifyUTXOsPath + resp, err := http.Post(url, "application/json", bytes.NewReader(jsonPayload)) + if err != nil { + t.Fatalf("Failed to make POST request to %s: %v", url, err) + } + defer resp.Body.Close() + + assert.Equal(t, http.StatusBadRequest, resp.StatusCode) + + var response api.ErrorResponse + err = json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + t.Fatalf("Failed to decode response body: %v", err) + } + + assert.Equal(t, types.BadRequest.String(), response.ErrorCode, "expected error code to be BAD_REQUEST") + assert.Contains(t, response.Message, "invalid UTXO txid", "expected error message to contain 'invalid UTXO txid'") +} + +// Test case 4: Ordinal service return error, fallback to unisat service and return the result +func TestVerifyUtxosEndpointOrdinalServiceErrorFallbackToUnisat(t *testing.T) { + r := rand.New(rand.NewSource(time.Now().Unix())) + numOfUTXOs := randomPositiveInt(r, 100) + payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) + jsonPayload, err := json.Marshal(payload) + if err != nil { + t.Fatalf("Failed to marshal payload: %v", err) + } + + // create some ordinal responses that contains inscriptions + numOfUTXOsWithAsset := r.Intn(numOfUTXOs) + + var txidsWithAsset []string + for i := 0; i < numOfUTXOsWithAsset; i++ { + txidsWithAsset = append(txidsWithAsset, payload.UTXOs[i].Txid) + } + + mockUnisatResponse := createUnisatServiceResponse(t, r, payload.UTXOs, txidsWithAsset) + + mockOrdinal := new(mocks.OrdinalsClientInterface) + mockOrdinal.On("FetchUTXOInfos", mock.Anything, mock.Anything).Return(nil, types.NewErrorWithMsg( + http.StatusInternalServerError, + types.InternalServiceError, + "failed to verify ordinals via ordinals service", + )) + + mockUnisat := new(mocks.UnisatClientInterface) + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything).Return(mockUnisatResponse, nil) + + mockedClients := &clients.Clients{ + Ordinals: mockOrdinal, + Unisat: mockUnisat, + } + testServer := setupTestServer(t, &TestServerDependency{MockedClients: mockedClients}) + defer testServer.Close() + + url := testServer.Server.URL + verifyUTXOsPath + resp, err := http.Post(url, "application/json", bytes.NewReader(jsonPayload)) + if err != nil { + t.Fatalf("Failed to make POST request to %s: %v", url, err) + } + defer resp.Body.Close() + + assert.Equal(t, http.StatusOK, resp.StatusCode) + // decode the response body + var response handlers.PublicResponse[[]services.SafeUTXOPublic] + err = json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + t.Fatalf("Failed to decode response body: %v", err) + } + + // check the response + assert.Equal(t, len(payload.UTXOs), len(response.Data)) + + unisatUTXOMap := make(map[string]*unisat.UnisatUTXO) + for _, u := range mockUnisatResponse { + unisatUTXOMap[u.TxId] = u + } + + for _, u := range response.Data { + unisatUTXO, exists := unisatUTXOMap[u.TxId] + assert.True(t, exists, "UTXO should exist in Unisat response") + + if len(unisatUTXO.Inscriptions) > 0 { + assert.True(t, u.Inscription, "UTXO should be marked as having an inscription") + } else { + assert.False(t, u.Inscription, "UTXO should not be marked as having an inscription") + } + } +} + +// Test case 5: Unisat service return error, return error +func TestVerifyUtxosEndpointUnisatServiceErrorReturnError(t *testing.T) { + r := rand.New(rand.NewSource(time.Now().Unix())) + numOfUTXOs := randomPositiveInt(r, 100) + payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) + jsonPayload, err := json.Marshal(payload) + if err != nil { + t.Fatalf("Failed to marshal payload: %v", err) + } + + // create some ordinal responses that contains inscriptions + numOfUTXOsWithAsset := r.Intn(numOfUTXOs) + + var txidsWithAsset []string + for i := 0; i < numOfUTXOsWithAsset; i++ { + txidsWithAsset = append(txidsWithAsset, payload.UTXOs[i].Txid) + } + + mockOrdinal := new(mocks.OrdinalsClientInterface) + mockOrdinal.On("FetchUTXOInfos", mock.Anything, mock.Anything).Return(nil, types.NewErrorWithMsg( + http.StatusInternalServerError, + types.InternalServiceError, + "failed to verify ordinals via ordinals service", + )) + + mockUnisat := new(mocks.UnisatClientInterface) + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.NewErrorWithMsg( + http.StatusInternalServerError, + types.InternalServiceError, + "failed to verify ordinals via unisat service", + )) + + mockedClients := &clients.Clients{ + Ordinals: mockOrdinal, + Unisat: mockUnisat, + } + testServer := setupTestServer(t, &TestServerDependency{MockedClients: mockedClients}) + defer testServer.Close() + + url := testServer.Server.URL + verifyUTXOsPath + resp, err := http.Post(url, "application/json", bytes.NewReader(jsonPayload)) + if err != nil { + t.Fatalf("Failed to make POST request to %s: %v", url, err) + } + defer resp.Body.Close() + + assert.Equal(t, http.StatusInternalServerError, resp.StatusCode) + + // decode the response body + var response api.ErrorResponse + err = json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + t.Fatalf("Failed to decode response body: %v", err) + } + assert.Equal(t, types.InternalServiceError.String(), response.ErrorCode, "expected error code to be INTERNAL_SERVICE_ERROR") + assert.Contains(t, response.Message, "Internal service error", "expected error message to contain 'Internal service error'") +} + +// Test case 6: Ordinal service took too long to respond, fallback to unisat service and return the result +func TestVerifyUtxosEndpointOrdinalServiceTimeoutFallbackToUnisat(t *testing.T) { + r := rand.New(rand.NewSource(time.Now().Unix())) + numOfUTXOs := randomPositiveInt(r, 100) + payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) + jsonPayload, err := json.Marshal(payload) + if err != nil { + t.Fatalf("Failed to marshal payload: %v", err) + } + + // Create some UTXOs with assets + numOfUTXOsWithAsset := r.Intn(numOfUTXOs) + var txidsWithAsset []string + for i := 0; i < numOfUTXOsWithAsset; i++ { + txidsWithAsset = append(txidsWithAsset, payload.UTXOs[i].Txid) + } + + mockUnisatResponse := createUnisatServiceResponse(t, r, payload.UTXOs, txidsWithAsset) + + mockOrdinal := new(mocks.OrdinalsClientInterface) + mockOrdinal.On("FetchUTXOInfos", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { + time.Sleep(2 * time.Second) + }).Return(nil, types.NewErrorWithMsg(http.StatusRequestTimeout, + types.RequestTimeout, + "request timeout after")) + + mockUnisat := new(mocks.UnisatClientInterface) + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything).Return(mockUnisatResponse, nil) + + mockedClients := &clients.Clients{ + Ordinals: mockOrdinal, + Unisat: mockUnisat, + } + + testServer := setupTestServer(t, &TestServerDependency{ + MockedClients: mockedClients, + }) + + defer testServer.Close() + + url := testServer.Server.URL + verifyUTXOsPath + resp, err := http.Post(url, "application/json", bytes.NewReader(jsonPayload)) + if err != nil { + t.Fatalf("Failed to make POST request to %s: %v", url, err) + } + defer resp.Body.Close() + + assert.Equal(t, http.StatusOK, resp.StatusCode) + + // Decode the response body + var response handlers.PublicResponse[[]services.SafeUTXOPublic] + err = json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + t.Fatalf("Failed to decode response body: %v", err) + } + + // check the response + assert.Equal(t, len(payload.UTXOs), len(response.Data)) + + unisatUTXOMap := make(map[string]*unisat.UnisatUTXO) + for _, u := range mockUnisatResponse { + unisatUTXOMap[u.TxId] = u + } + + for i, u := range response.Data { + assert.Equal(t, payload.UTXOs[i].Txid, u.TxId) + assert.Equal(t, payload.UTXOs[i].Vout, u.Vout) + + unisatUTXO, exists := unisatUTXOMap[u.TxId] + assert.True(t, exists, "UTXO should exist in Unisat response") + + if len(unisatUTXO.Inscriptions) > 0 { + assert.True(t, u.Inscription, "UTXO should be marked as having an inscription") + } else { + assert.False(t, u.Inscription, "UTXO should not be marked as having an inscription") + } + } +} + +// Test case 7: Unisat service took too long to respond, return error within the timeout window +func TestVerifyUtxosEndpointUnisatServiceTimeoutReturnError(t *testing.T) { + r := rand.New(rand.NewSource(time.Now().Unix())) + numOfUTXOs := randomPositiveInt(r, 100) + payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) + jsonPayload, err := json.Marshal(payload) + if err != nil { + t.Fatalf("Failed to marshal payload: %v", err) + } + + // Create some UTXOs with assets + numOfUTXOsWithAsset := r.Intn(numOfUTXOs) + var txidsWithAsset []string + for i := 0; i < numOfUTXOsWithAsset; i++ { + txidsWithAsset = append(txidsWithAsset, payload.UTXOs[i].Txid) + } + + mockOrdinal := new(mocks.OrdinalsClientInterface) + mockOrdinal.On("FetchUTXOInfos", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { + time.Sleep(2 * time.Second) + }).Return(nil, types.NewErrorWithMsg(http.StatusRequestTimeout, + types.RequestTimeout, + "request timeout after")) + + // Mock Unisat service + mockUnisat := new(mocks.UnisatClientInterface) + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.NewErrorWithMsg(http.StatusRequestTimeout, + types.RequestTimeout, + "request timeout after")) + + mockedClients := &clients.Clients{ + Ordinals: mockOrdinal, + Unisat: mockUnisat, + } + + testServer := setupTestServer(t, &TestServerDependency{ + MockedClients: mockedClients, + }) + + defer testServer.Close() + + url := testServer.Server.URL + verifyUTXOsPath + resp, err := http.Post(url, "application/json", bytes.NewReader(jsonPayload)) + if err != nil { + t.Fatalf("Failed to make POST request to %s: %v", url, err) + } + defer resp.Body.Close() + + // decode the response body + var response api.ErrorResponse + err = json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + t.Fatalf("Failed to decode response body: %v", err) + } + assert.Equal(t, types.RequestTimeout.String(), response.ErrorCode, "expected error code to be REQUEST_TIMEOUT") + assert.Contains(t, response.Message, "request timeout after", "expected error message to contain 'request timeout after'") +} + +// Test case 8: Send 100 UTXOs, ordinal service fail, unisat service return 50(limit), then fetch again for the remaining 50 item (test the pagination) +func TestVerifyUtxosEndpointPaginationWithOrdinalServiceFailure(t *testing.T) { + cfg, err := config.New("./config/config-test.yml") + if err != nil { + t.Fatalf("Failed to load config: %v", err) + } + + r := rand.New(rand.NewSource(time.Now().Unix())) + numOfUTXOs := 100 + payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) + jsonPayload, err := json.Marshal(payload) + if err != nil { + t.Fatalf("Failed to marshal payload: %v", err) + } + + // Create some UTXOs with assets + numOfUTXOsWithAsset := r.Intn(numOfUTXOs) + var txidsWithAsset []string + for i := 0; i < numOfUTXOsWithAsset; i++ { + txidsWithAsset = append(txidsWithAsset, payload.UTXOs[i].Txid) + } + + mockOrdinal := new(mocks.OrdinalsClientInterface) + mockOrdinal.On("FetchUTXOInfos", mock.Anything, mock.Anything).Return(nil, types.NewErrorWithMsg( + http.StatusInternalServerError, + types.InternalServiceError, + "failed to verify ordinals via ordinals service", + )) + + callCount := 0 + var mockUnisatResponses [][]*unisat.UnisatUTXO + mockUnisat := new(mocks.UnisatClientInterface) + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). + Run(func(args mock.Arguments) { + callCount++ + if callCount > 10 { + t.Fatalf("Too many calls to Unisat service (> 10), possible infinite loop") + } + }). + Return(func(ctx context.Context, address string, offset uint32) []*unisat.UnisatUTXO { + start := int(offset) + end := start + int(cfg.Assets.Unisat.Limit) + if end > len(payload.UTXOs) { + end = len(payload.UTXOs) + } + if start >= len(payload.UTXOs) { + return []*unisat.UnisatUTXO{} + } + response := createUnisatServiceResponse(t, r, payload.UTXOs[start:end], txidsWithAsset) + mockUnisatResponses = append(mockUnisatResponses, response) + return response + }, nil) + + mockedClients := &clients.Clients{ + Ordinals: mockOrdinal, + Unisat: mockUnisat, + } + + testServer := setupTestServer(t, &TestServerDependency{ + MockedClients: mockedClients, + ConfigOverrides: cfg, + }) + defer testServer.Close() + + url := testServer.Server.URL + verifyUTXOsPath + resp, err := http.Post(url, "application/json", bytes.NewReader(jsonPayload)) + if err != nil { + t.Fatalf("Failed to make POST request to %s: %v", url, err) + } + defer resp.Body.Close() + + assert.Equal(t, http.StatusOK, resp.StatusCode) + + // Decode the response body + var response handlers.PublicResponse[[]services.SafeUTXOPublic] + err = json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + t.Fatalf("Failed to decode response body: %v", err) + } + + assert.Equal(t, numOfUTXOs, len(response.Data), "Response should contain all UTXOs") + + assert.LessOrEqual(t, callCount, 3, "Unisat service should not be called more than 3 times") + + // Create a map of all Unisat UTXOs + unisatUTXOMap := make(map[string]*unisat.UnisatUTXO) + for _, resp := range mockUnisatResponses { + for _, u := range resp { + unisatUTXOMap[u.TxId] = u + } + } + + // Verify the correctness of the response + for i, u := range response.Data { + assert.Equal(t, payload.UTXOs[i].Txid, u.TxId) + assert.Equal(t, payload.UTXOs[i].Vout, u.Vout) + + unisatUTXO, exists := unisatUTXOMap[u.TxId] + assert.True(t, exists, "UTXO should exist in Unisat response") + + if len(unisatUTXO.Inscriptions) > 0 { + assert.True(t, u.Inscription, "UTXO should be marked as having an inscription") + } else { + assert.False(t, u.Inscription, "UTXO should not be marked as having an inscription") + } + } +} + +// Test case 9: Fall back to unisat when ordinal service return data that is not in the right order of the request UTXOs +func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testing.T) { + r := rand.New(rand.NewSource(time.Now().Unix())) + numOfUTXOs := 100 + payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) + jsonPayload, err := json.Marshal(payload) + if err != nil { + t.Fatalf("Failed to marshal payload: %v", err) + } + + // Create some UTXOs with assets + numOfUTXOsWithAsset := r.Intn(numOfUTXOs) + var txidsWithAsset []string + for i := 0; i < numOfUTXOsWithAsset; i++ { + txidsWithAsset = append(txidsWithAsset, payload.UTXOs[i].Txid) + } + t.Logf("Created %d UTXOs with assets", numOfUTXOsWithAsset) + + mockedOrdinalResponse := createOrdinalServiceResponse(t, r, payload.UTXOs, txidsWithAsset) + + // Shuffle the ordinal response to simulate wrong order + r.Shuffle(len(mockedOrdinalResponse), func(i, j int) { + mockedOrdinalResponse[i], mockedOrdinalResponse[j] = mockedOrdinalResponse[j], mockedOrdinalResponse[i] + }) + + mockOrdinal := new(mocks.OrdinalsClientInterface) + mockOrdinal.On("FetchUTXOInfos", mock.Anything, mock.Anything).Return(mockedOrdinalResponse, types.NewErrorWithMsg( + http.StatusInternalServerError, + types.InternalServiceError, + "response does not contain all requested UTXOs or in the wrong order", + )) + + mockUnisatResponse := createUnisatServiceResponse(t, r, payload.UTXOs, txidsWithAsset) + + mockUnisat := new(mocks.UnisatClientInterface) + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). + Return(mockUnisatResponse, nil) + + mockedClients := &clients.Clients{ + Ordinals: mockOrdinal, + Unisat: mockUnisat, + } + + testServer := setupTestServer(t, &TestServerDependency{ + MockedClients: mockedClients, + }) + defer testServer.Close() + + url := testServer.Server.URL + verifyUTXOsPath + resp, err := http.Post(url, "application/json", bytes.NewReader(jsonPayload)) + if err != nil { + t.Fatalf("Failed to make POST request to %s: %v", url, err) + } + defer resp.Body.Close() + + t.Logf("Received response with status code: %d", resp.StatusCode) + assert.Equal(t, http.StatusOK, resp.StatusCode) + + // Decode the response body + var response handlers.PublicResponse[[]services.SafeUTXOPublic] + err = json.NewDecoder(resp.Body).Decode(&response) + if err != nil { + t.Fatalf("Failed to decode response body: %v", err) + } + + assert.Equal(t, numOfUTXOs, len(response.Data), "Response should contain all UTXOs") + + unisatUTXOMap := make(map[string]*unisat.UnisatUTXO) + for _, u := range mockUnisatResponse { + unisatUTXOMap[u.TxId] = u + } + + inscriptionsCount := 0 + for i, u := range response.Data { + assert.Equal(t, payload.UTXOs[i].Txid, u.TxId) + assert.Equal(t, payload.UTXOs[i].Vout, u.Vout) + + unisatUTXO, exists := unisatUTXOMap[u.TxId] + assert.True(t, exists, "UTXO should exist in Unisat response") + + if len(unisatUTXO.Inscriptions) > 0 { + assert.True(t, u.Inscription, "UTXO should be marked as having an inscription") + inscriptionsCount++ + } else { + assert.False(t, u.Inscription, "UTXO should not be marked as having an inscription") + } + } +} func createOrdinalServiceResponse(t *testing.T, r *rand.Rand, utxos []types.UTXOIdentifier, txidsWithAsset []string) []ordinals.OrdinalsOutputResponse { var responses []ordinals.OrdinalsOutputResponse @@ -180,3 +751,39 @@ func createOrdinalServiceResponse(t *testing.T, r *rand.Rand, utxos []types.UTXO } return responses } + +func createUnisatServiceResponse(t *testing.T, r *rand.Rand, utxos []types.UTXOIdentifier, txidsWithAsset []string) []*unisat.UnisatUTXO { + var responses []*unisat.UnisatUTXO + + for _, utxo := range utxos { + withAsset := false + for _, txid := range txidsWithAsset { + if txid == utxo.Txid { + withAsset = true + break + } + } + + unisatUTXO := &unisat.UnisatUTXO{ + TxId: utxo.Txid, + Vout: utxo.Vout, + } + + if withAsset { + if r.Intn(2) == 0 { + numInscriptions := r.Intn(3) + 1 + for i := 0; i < numInscriptions; i++ { + inscription := &unisat.UnisatInscriptions{ + InscriptionId: randomString(r, 64), + Offset: uint32(r.Intn(1000)), + } + unisatUTXO.Inscriptions = append(unisatUTXO.Inscriptions, inscription) + } + } + } + + responses = append(responses, unisatUTXO) + } + + return responses +} diff --git a/tests/config/config-test.yml b/tests/config/config-test.yml index 5cec6fa..2720dc2 100644 --- a/tests/config/config-test.yml +++ b/tests/config/config-test.yml @@ -35,6 +35,6 @@ assets: timeout: 100 unisat: host: "https://open-api-testnet.unisat.io" - limit: 100 + limit: 50 timeout: 100 token: "add your token as ASSETS_UNISAT_TOKEN in .env" From 80b4df82c713a573c15a3304031888a294c3198f Mon Sep 17 00:00:00 2001 From: jeremy-babylonchain Date: Fri, 26 Jul 2024 01:19:59 +0800 Subject: [PATCH 2/6] update test quantity & add config override --- tests/assets_checking_test.go | 47 +++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 10 deletions(-) diff --git a/tests/assets_checking_test.go b/tests/assets_checking_test.go index 9c51e36..1bbf914 100644 --- a/tests/assets_checking_test.go +++ b/tests/assets_checking_test.go @@ -252,8 +252,12 @@ func TestVerifyUtxosEndpointWithMixedUTXOs(t *testing.T) { // Test case 4: Ordinal service return error, fallback to unisat service and return the result func TestVerifyUtxosEndpointOrdinalServiceErrorFallbackToUnisat(t *testing.T) { + cfg, err := config.New("./config/config-test.yml") + if err != nil { + t.Fatalf("Failed to load config: %v", err) + } r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := randomPositiveInt(r, 100) + numOfUTXOs := randomPositiveInt(r, 10) payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { @@ -284,7 +288,10 @@ func TestVerifyUtxosEndpointOrdinalServiceErrorFallbackToUnisat(t *testing.T) { Ordinals: mockOrdinal, Unisat: mockUnisat, } - testServer := setupTestServer(t, &TestServerDependency{MockedClients: mockedClients}) + testServer := setupTestServer(t, &TestServerDependency{ + MockedClients: mockedClients, + ConfigOverrides: cfg, + }) defer testServer.Close() url := testServer.Server.URL + verifyUTXOsPath @@ -324,8 +331,12 @@ func TestVerifyUtxosEndpointOrdinalServiceErrorFallbackToUnisat(t *testing.T) { // Test case 5: Unisat service return error, return error func TestVerifyUtxosEndpointUnisatServiceErrorReturnError(t *testing.T) { + cfg, err := config.New("./config/config-test.yml") + if err != nil { + t.Fatalf("Failed to load config: %v", err) + } r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := randomPositiveInt(r, 100) + numOfUTXOs := randomPositiveInt(r, 10) payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { @@ -358,7 +369,10 @@ func TestVerifyUtxosEndpointUnisatServiceErrorReturnError(t *testing.T) { Ordinals: mockOrdinal, Unisat: mockUnisat, } - testServer := setupTestServer(t, &TestServerDependency{MockedClients: mockedClients}) + testServer := setupTestServer(t, &TestServerDependency{ + MockedClients: mockedClients, + ConfigOverrides: cfg, + }) defer testServer.Close() url := testServer.Server.URL + verifyUTXOsPath @@ -382,8 +396,12 @@ func TestVerifyUtxosEndpointUnisatServiceErrorReturnError(t *testing.T) { // Test case 6: Ordinal service took too long to respond, fallback to unisat service and return the result func TestVerifyUtxosEndpointOrdinalServiceTimeoutFallbackToUnisat(t *testing.T) { + cfg, err := config.New("./config/config-test.yml") + if err != nil { + t.Fatalf("Failed to load config: %v", err) + } r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := randomPositiveInt(r, 100) + numOfUTXOs := randomPositiveInt(r, 10) payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { @@ -416,6 +434,7 @@ func TestVerifyUtxosEndpointOrdinalServiceTimeoutFallbackToUnisat(t *testing.T) testServer := setupTestServer(t, &TestServerDependency{ MockedClients: mockedClients, + ConfigOverrides: cfg, }) defer testServer.Close() @@ -461,8 +480,12 @@ func TestVerifyUtxosEndpointOrdinalServiceTimeoutFallbackToUnisat(t *testing.T) // Test case 7: Unisat service took too long to respond, return error within the timeout window func TestVerifyUtxosEndpointUnisatServiceTimeoutReturnError(t *testing.T) { + cfg, err := config.New("./config/config-test.yml") + if err != nil { + t.Fatalf("Failed to load config: %v", err) + } r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := randomPositiveInt(r, 100) + numOfUTXOs := randomPositiveInt(r, 10) payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { @@ -496,6 +519,7 @@ func TestVerifyUtxosEndpointUnisatServiceTimeoutReturnError(t *testing.T) { testServer := setupTestServer(t, &TestServerDependency{ MockedClients: mockedClients, + ConfigOverrides: cfg, }) defer testServer.Close() @@ -525,7 +549,7 @@ func TestVerifyUtxosEndpointPaginationWithOrdinalServiceFailure(t *testing.T) { } r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := 100 + numOfUTXOs := 10 payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { @@ -627,8 +651,12 @@ func TestVerifyUtxosEndpointPaginationWithOrdinalServiceFailure(t *testing.T) { // Test case 9: Fall back to unisat when ordinal service return data that is not in the right order of the request UTXOs func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testing.T) { + cfg, err := config.New("./config/config-test.yml") + if err != nil { + t.Fatalf("Failed to load config: %v", err) + } r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := 100 + numOfUTXOs := 10 payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { @@ -641,7 +669,6 @@ func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testin for i := 0; i < numOfUTXOsWithAsset; i++ { txidsWithAsset = append(txidsWithAsset, payload.UTXOs[i].Txid) } - t.Logf("Created %d UTXOs with assets", numOfUTXOsWithAsset) mockedOrdinalResponse := createOrdinalServiceResponse(t, r, payload.UTXOs, txidsWithAsset) @@ -670,6 +697,7 @@ func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testin testServer := setupTestServer(t, &TestServerDependency{ MockedClients: mockedClients, + ConfigOverrides: cfg, }) defer testServer.Close() @@ -680,7 +708,6 @@ func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testin } defer resp.Body.Close() - t.Logf("Received response with status code: %d", resp.StatusCode) assert.Equal(t, http.StatusOK, resp.StatusCode) // Decode the response body From 9e542c9f4f5a921c59eedba5c595adc6f23ad4cf Mon Sep 17 00:00:00 2001 From: jeremy-babylonchain Date: Fri, 26 Jul 2024 01:21:51 +0800 Subject: [PATCH 3/6] format --- tests/assets_checking_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/assets_checking_test.go b/tests/assets_checking_test.go index 1bbf914..b37f4fb 100644 --- a/tests/assets_checking_test.go +++ b/tests/assets_checking_test.go @@ -289,7 +289,7 @@ func TestVerifyUtxosEndpointOrdinalServiceErrorFallbackToUnisat(t *testing.T) { Unisat: mockUnisat, } testServer := setupTestServer(t, &TestServerDependency{ - MockedClients: mockedClients, + MockedClients: mockedClients, ConfigOverrides: cfg, }) defer testServer.Close() @@ -331,7 +331,7 @@ func TestVerifyUtxosEndpointOrdinalServiceErrorFallbackToUnisat(t *testing.T) { // Test case 5: Unisat service return error, return error func TestVerifyUtxosEndpointUnisatServiceErrorReturnError(t *testing.T) { - cfg, err := config.New("./config/config-test.yml") + cfg, err := config.New("./config/config-test.yml") if err != nil { t.Fatalf("Failed to load config: %v", err) } @@ -370,7 +370,7 @@ func TestVerifyUtxosEndpointUnisatServiceErrorReturnError(t *testing.T) { Unisat: mockUnisat, } testServer := setupTestServer(t, &TestServerDependency{ - MockedClients: mockedClients, + MockedClients: mockedClients, ConfigOverrides: cfg, }) defer testServer.Close() @@ -433,7 +433,7 @@ func TestVerifyUtxosEndpointOrdinalServiceTimeoutFallbackToUnisat(t *testing.T) } testServer := setupTestServer(t, &TestServerDependency{ - MockedClients: mockedClients, + MockedClients: mockedClients, ConfigOverrides: cfg, }) @@ -518,7 +518,7 @@ func TestVerifyUtxosEndpointUnisatServiceTimeoutReturnError(t *testing.T) { } testServer := setupTestServer(t, &TestServerDependency{ - MockedClients: mockedClients, + MockedClients: mockedClients, ConfigOverrides: cfg, }) @@ -696,7 +696,7 @@ func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testin } testServer := setupTestServer(t, &TestServerDependency{ - MockedClients: mockedClients, + MockedClients: mockedClients, ConfigOverrides: cfg, }) defer testServer.Close() From 08927071662620874bc7d93f3bd813ff8a209e2d Mon Sep 17 00:00:00 2001 From: jeremy-babylonchain Date: Fri, 26 Jul 2024 16:10:28 +0800 Subject: [PATCH 4/6] update --- tests/assets_checking_test.go | 45 ++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/tests/assets_checking_test.go b/tests/assets_checking_test.go index b37f4fb..24b7cbb 100644 --- a/tests/assets_checking_test.go +++ b/tests/assets_checking_test.go @@ -37,7 +37,7 @@ func createPayload(t *testing.T, r *rand.Rand, netParam *chaincfg.Params, size i for i := 0; i < size; i++ { var txid string if hasInvalid && i < numInvalidUTXOs { - randomStr := randomString(r, 10) + randomStr := randomString(r, 100) txid = randomStr } else { tx, _, err := generateRandomTx(r) @@ -87,7 +87,7 @@ func TestVerifyUtxosEndpointNotAvailableIfAssetsConfigNotSet(t *testing.T) { // Test case 1: Fetching UTXOs return via Ordinal Service func FuzzSuccessfullyVerifyUTXOsAssetsViaOrdinalService(f *testing.F) { - attachRandomSeedsToFuzzer(f, 10) + attachRandomSeedsToFuzzer(f, 100) f.Fuzz(func(t *testing.T, seed int64) { r := rand.New(rand.NewSource(seed)) numOfUTXOs := randomPositiveInt(r, 100) @@ -256,17 +256,16 @@ func TestVerifyUtxosEndpointOrdinalServiceErrorFallbackToUnisat(t *testing.T) { if err != nil { t.Fatalf("Failed to load config: %v", err) } + r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := randomPositiveInt(r, 10) + numOfUTXOs := randomPositiveInt(r, 100) payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { t.Fatalf("Failed to marshal payload: %v", err) } - // create some ordinal responses that contains inscriptions numOfUTXOsWithAsset := r.Intn(numOfUTXOs) - var txidsWithAsset []string for i := 0; i < numOfUTXOsWithAsset; i++ { txidsWithAsset = append(txidsWithAsset, payload.UTXOs[i].Txid) @@ -282,12 +281,18 @@ func TestVerifyUtxosEndpointOrdinalServiceErrorFallbackToUnisat(t *testing.T) { )) mockUnisat := new(mocks.UnisatClientInterface) - mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything).Return(mockUnisatResponse, nil) + + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). + Return(mockUnisatResponse, nil) + + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). + Return([]*unisat.UnisatUTXO{}, nil) mockedClients := &clients.Clients{ Ordinals: mockOrdinal, Unisat: mockUnisat, } + testServer := setupTestServer(t, &TestServerDependency{ MockedClients: mockedClients, ConfigOverrides: cfg, @@ -302,14 +307,13 @@ func TestVerifyUtxosEndpointOrdinalServiceErrorFallbackToUnisat(t *testing.T) { defer resp.Body.Close() assert.Equal(t, http.StatusOK, resp.StatusCode) - // decode the response body + var response handlers.PublicResponse[[]services.SafeUTXOPublic] err = json.NewDecoder(resp.Body).Decode(&response) if err != nil { t.Fatalf("Failed to decode response body: %v", err) } - // check the response assert.Equal(t, len(payload.UTXOs), len(response.Data)) unisatUTXOMap := make(map[string]*unisat.UnisatUTXO) @@ -336,7 +340,7 @@ func TestVerifyUtxosEndpointUnisatServiceErrorReturnError(t *testing.T) { t.Fatalf("Failed to load config: %v", err) } r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := randomPositiveInt(r, 10) + numOfUTXOs := randomPositiveInt(r, 100) payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { @@ -401,7 +405,7 @@ func TestVerifyUtxosEndpointOrdinalServiceTimeoutFallbackToUnisat(t *testing.T) t.Fatalf("Failed to load config: %v", err) } r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := randomPositiveInt(r, 10) + numOfUTXOs := randomPositiveInt(r, 100) payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { @@ -425,7 +429,12 @@ func TestVerifyUtxosEndpointOrdinalServiceTimeoutFallbackToUnisat(t *testing.T) "request timeout after")) mockUnisat := new(mocks.UnisatClientInterface) - mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything).Return(mockUnisatResponse, nil) + + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). + Return(mockUnisatResponse, nil) + + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). + Return([]*unisat.UnisatUTXO{}, nil) mockedClients := &clients.Clients{ Ordinals: mockOrdinal, @@ -436,7 +445,6 @@ func TestVerifyUtxosEndpointOrdinalServiceTimeoutFallbackToUnisat(t *testing.T) MockedClients: mockedClients, ConfigOverrides: cfg, }) - defer testServer.Close() url := testServer.Server.URL + verifyUTXOsPath @@ -485,7 +493,7 @@ func TestVerifyUtxosEndpointUnisatServiceTimeoutReturnError(t *testing.T) { t.Fatalf("Failed to load config: %v", err) } r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := randomPositiveInt(r, 10) + numOfUTXOs := randomPositiveInt(r, 100) payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { @@ -549,7 +557,7 @@ func TestVerifyUtxosEndpointPaginationWithOrdinalServiceFailure(t *testing.T) { } r := rand.New(rand.NewSource(time.Now().Unix())) - numOfUTXOs := 10 + numOfUTXOs := 100 payload := createPayload(t, r, &chaincfg.MainNetParams, numOfUTXOs, false) jsonPayload, err := json.Marshal(payload) if err != nil { @@ -684,11 +692,14 @@ func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testin "response does not contain all requested UTXOs or in the wrong order", )) - mockUnisatResponse := createUnisatServiceResponse(t, r, payload.UTXOs, txidsWithAsset) + mockUnisatResponse1 := createUnisatServiceResponse(t, r, payload.UTXOs[:len(payload.UTXOs)/2], txidsWithAsset) + mockUnisatResponse2 := createUnisatServiceResponse(t, r, payload.UTXOs[len(payload.UTXOs)/2:], txidsWithAsset) mockUnisat := new(mocks.UnisatClientInterface) mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). - Return(mockUnisatResponse, nil) + Return(mockUnisatResponse1, nil) + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). + Return(mockUnisatResponse2, nil) mockedClients := &clients.Clients{ Ordinals: mockOrdinal, @@ -720,7 +731,7 @@ func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testin assert.Equal(t, numOfUTXOs, len(response.Data), "Response should contain all UTXOs") unisatUTXOMap := make(map[string]*unisat.UnisatUTXO) - for _, u := range mockUnisatResponse { + for _, u := range append(mockUnisatResponse1, mockUnisatResponse2...) { unisatUTXOMap[u.TxId] = u } From b09720c910abd716a98d2b3fd738bb5dc4572ed2 Mon Sep 17 00:00:00 2001 From: jeremy-babylonchain Date: Fri, 26 Jul 2024 16:47:34 +0800 Subject: [PATCH 5/6] update --- tests/assets_checking_test.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/assets_checking_test.go b/tests/assets_checking_test.go index 24b7cbb..8e6fbee 100644 --- a/tests/assets_checking_test.go +++ b/tests/assets_checking_test.go @@ -281,10 +281,8 @@ func TestVerifyUtxosEndpointOrdinalServiceErrorFallbackToUnisat(t *testing.T) { )) mockUnisat := new(mocks.UnisatClientInterface) - mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). - Return(mockUnisatResponse, nil) - + Return(mockUnisatResponse, nil).Once() mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). Return([]*unisat.UnisatUTXO{}, nil) @@ -431,7 +429,7 @@ func TestVerifyUtxosEndpointOrdinalServiceTimeoutFallbackToUnisat(t *testing.T) mockUnisat := new(mocks.UnisatClientInterface) mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). - Return(mockUnisatResponse, nil) + Return(mockUnisatResponse, nil).Once() mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). Return([]*unisat.UnisatUTXO{}, nil) @@ -692,14 +690,13 @@ func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testin "response does not contain all requested UTXOs or in the wrong order", )) - mockUnisatResponse1 := createUnisatServiceResponse(t, r, payload.UTXOs[:len(payload.UTXOs)/2], txidsWithAsset) - mockUnisatResponse2 := createUnisatServiceResponse(t, r, payload.UTXOs[len(payload.UTXOs)/2:], txidsWithAsset) + mockUnisatResponse := createUnisatServiceResponse(t, r, payload.UTXOs, txidsWithAsset) mockUnisat := new(mocks.UnisatClientInterface) mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). - Return(mockUnisatResponse1, nil) + Return(mockUnisatResponse, nil).Once() mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). - Return(mockUnisatResponse2, nil) + Return(mockUnisatResponse, nil).Once() mockedClients := &clients.Clients{ Ordinals: mockOrdinal, @@ -731,7 +728,7 @@ func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testin assert.Equal(t, numOfUTXOs, len(response.Data), "Response should contain all UTXOs") unisatUTXOMap := make(map[string]*unisat.UnisatUTXO) - for _, u := range append(mockUnisatResponse1, mockUnisatResponse2...) { + for _, u := range append(mockUnisatResponse) { unisatUTXOMap[u.TxId] = u } From 2b52378aeae75cdd0255acca9435e546650a8e0f Mon Sep 17 00:00:00 2001 From: jeremy-babylonchain Date: Fri, 26 Jul 2024 17:02:59 +0800 Subject: [PATCH 6/6] update --- tests/assets_checking_test.go | 4 +--- tests/setup.go | 2 ++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/assets_checking_test.go b/tests/assets_checking_test.go index 8e6fbee..a7ef2bb 100644 --- a/tests/assets_checking_test.go +++ b/tests/assets_checking_test.go @@ -427,7 +427,7 @@ func TestVerifyUtxosEndpointOrdinalServiceTimeoutFallbackToUnisat(t *testing.T) "request timeout after")) mockUnisat := new(mocks.UnisatClientInterface) - + mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). Return(mockUnisatResponse, nil).Once() @@ -695,8 +695,6 @@ func TestVerifyUtxosEndpointFallbackToUnisatOnOrdinalServiceWrongOrder(t *testin mockUnisat := new(mocks.UnisatClientInterface) mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). Return(mockUnisatResponse, nil).Once() - mockUnisat.On("FetchInscriptionsUtxosByAddress", mock.Anything, mock.Anything, mock.Anything). - Return(mockUnisatResponse, nil).Once() mockedClients := &clients.Clients{ Ordinals: mockOrdinal, diff --git a/tests/setup.go b/tests/setup.go index 36b931b..5a1b2fd 100644 --- a/tests/setup.go +++ b/tests/setup.go @@ -336,3 +336,5 @@ func buildActiveStakingEvent(t *testing.T, numOfEvenet int) []*client.ActiveStak } return activeStakingEvents } + +