diff --git a/core/src/main/java/bisq/core/provider/mempool/TxValidator.java b/core/src/main/java/bisq/core/provider/mempool/TxValidator.java index 7c67343d973..07b7b1ab0cb 100644 --- a/core/src/main/java/bisq/core/provider/mempool/TxValidator.java +++ b/core/src/main/java/bisq/core/provider/mempool/TxValidator.java @@ -190,7 +190,13 @@ private FeeValidationStatus checkFeeAmountBTC(String jsonTxt, Coin tradeAmount, JsonArray jsonVout = getVinAndVout(jsonTxt).second; JsonObject jsonVin0 = jsonVin.get(0).getAsJsonObject(); JsonObject jsonVout0 = jsonVout.get(0).getAsJsonObject(); - JsonElement jsonVIn0Value = jsonVin0.getAsJsonObject("prevout").get("value"); + + JsonObject jsonVin0PreVout = jsonVin0.getAsJsonObject("prevout"); + if (jsonVin0PreVout == null) { + throw new JsonSyntaxException("vin[0].prevout missing"); + } + + JsonElement jsonVIn0Value = jsonVin0PreVout.get("value"); JsonElement jsonFeeValue = jsonVout0.get("value"); if (jsonVIn0Value == null || jsonFeeValue == null) { throw new JsonSyntaxException("vin/vout missing data"); diff --git a/core/src/test/java/bisq/core/fee/MakerTxValidatorSanityCheckTests.java b/core/src/test/java/bisq/core/fee/MakerTxValidatorSanityCheckTests.java index 874b580cf61..34ed78094ef 100644 --- a/core/src/test/java/bisq/core/fee/MakerTxValidatorSanityCheckTests.java +++ b/core/src/test/java/bisq/core/fee/MakerTxValidatorSanityCheckTests.java @@ -23,6 +23,7 @@ import bisq.core.provider.mempool.TxValidator; import com.google.gson.Gson; +import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; @@ -121,6 +122,56 @@ void checkFeeAddressBtcTestVinOrVoutNotJsonArray(String vinOrVout) throws IOExce assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR)); } + @Test + void checkFeeAddressBtcNoTooFewVin() throws IOException { + JsonObject json = MakerTxValidatorSanityCheckTests.getValidBtcMakerFeeMempoolJsonResponse(); + json.add("vin", new JsonArray(0)); + assertThat(json.get("vin").getAsJsonArray().size(), is(0)); + + String jsonContent = new Gson().toJson(json); + TxValidator txValidator1 = txValidator.parseJsonValidateMakerFeeTx(jsonContent, + MakerTxValidatorSanityCheckTests.FEE_RECEIVER_ADDRESSES); + + assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR)); + } + + @ParameterizedTest + @ValueSource(ints = {0, 1}) + void checkFeeAddressBtcNoTooFewVout(int numberOfVouts) throws IOException { + JsonObject json = MakerTxValidatorSanityCheckTests.getValidBtcMakerFeeMempoolJsonResponse(); + + var jsonArray = new JsonArray(numberOfVouts); + for (int i = 0; i < numberOfVouts; i++) { + jsonArray.add(i); + } + json.add("vout", jsonArray); + assertThat(json.get("vout").getAsJsonArray().size(), is(numberOfVouts)); + + String jsonContent = new Gson().toJson(json); + TxValidator txValidator1 = txValidator.parseJsonValidateMakerFeeTx(jsonContent, + MakerTxValidatorSanityCheckTests.FEE_RECEIVER_ADDRESSES); + + assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR)); + } + + @Test + void checkFeeAmountMissingVinPreVout() throws IOException { + JsonObject json = MakerTxValidatorSanityCheckTests.getValidBtcMakerFeeMempoolJsonResponse(); + JsonObject firstInput = json.get("vin").getAsJsonArray().get(0).getAsJsonObject(); + firstInput.remove("prevout"); + + boolean hasPreVout = json.get("vin").getAsJsonArray() + .get(0).getAsJsonObject() + .has("prevout"); + assertThat(hasPreVout, is(false)); + + String jsonContent = new Gson().toJson(json); + TxValidator txValidator1 = txValidator.parseJsonValidateMakerFeeTx(jsonContent, + MakerTxValidatorSanityCheckTests.FEE_RECEIVER_ADDRESSES); + + assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR)); + } + @Test void responseHasDifferentTxId() throws IOException { String differentTxId = "abcde971ead7d03619e3a9eeaa771ed5adba14c448839e0299f857f7bb4ec07"; diff --git a/core/src/test/java/bisq/core/fee/TakerTxValidatorSanityCheckTests.java b/core/src/test/java/bisq/core/fee/TakerTxValidatorSanityCheckTests.java index f74d50592cc..93d4cb62418 100644 --- a/core/src/test/java/bisq/core/fee/TakerTxValidatorSanityCheckTests.java +++ b/core/src/test/java/bisq/core/fee/TakerTxValidatorSanityCheckTests.java @@ -23,6 +23,7 @@ import bisq.core.provider.mempool.TxValidator; import com.google.gson.Gson; +import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonPrimitive; @@ -121,6 +122,56 @@ void checkFeeAddressBtcTestVinOrVoutNotJsonArray(String vinOrVout) throws IOExce assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR)); } + @Test + void checkFeeAddressBtcNoTooFewVin() throws IOException { + JsonObject json = MakerTxValidatorSanityCheckTests.getValidBtcMakerFeeMempoolJsonResponse(); + json.add("vin", new JsonArray(0)); + assertThat(json.get("vin").getAsJsonArray().size(), is(0)); + + String jsonContent = new Gson().toJson(json); + TxValidator txValidator1 = txValidator.parseJsonValidateMakerFeeTx(jsonContent, + MakerTxValidatorSanityCheckTests.FEE_RECEIVER_ADDRESSES); + + assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR)); + } + + @ParameterizedTest + @ValueSource(ints = {0, 1}) + void checkFeeAddressBtcNoTooFewVout(int numberOfVouts) throws IOException { + JsonObject json = MakerTxValidatorSanityCheckTests.getValidBtcMakerFeeMempoolJsonResponse(); + + var jsonArray = new JsonArray(numberOfVouts); + for (int i = 0; i < numberOfVouts; i++) { + jsonArray.add(i); + } + json.add("vout", jsonArray); + assertThat(json.get("vout").getAsJsonArray().size(), is(numberOfVouts)); + + String jsonContent = new Gson().toJson(json); + TxValidator txValidator1 = txValidator.parseJsonValidateMakerFeeTx(jsonContent, + MakerTxValidatorSanityCheckTests.FEE_RECEIVER_ADDRESSES); + + assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR)); + } + + @Test + void checkFeeAmountMissingVinPreVout() throws IOException { + JsonObject json = MakerTxValidatorSanityCheckTests.getValidBtcMakerFeeMempoolJsonResponse(); + JsonObject firstInput = json.get("vin").getAsJsonArray().get(0).getAsJsonObject(); + firstInput.remove("prevout"); + + boolean hasPreVout = json.get("vin").getAsJsonArray() + .get(0).getAsJsonObject() + .has("prevout"); + assertThat(hasPreVout, is(false)); + + String jsonContent = new Gson().toJson(json); + TxValidator txValidator1 = txValidator.parseJsonValidateMakerFeeTx(jsonContent, + MakerTxValidatorSanityCheckTests.FEE_RECEIVER_ADDRESSES); + + assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR)); + } + @Test void responseHasDifferentTxId() throws IOException { String differentTxId = "abcde971ead7d03619e3a9eeaa771ed5adba14c448839e0299f857f7bb4ec07";