Merge pull request #7003 from alvasw/fix_crash_on_missing_vin_0_prevout

TxValidator: Fix crash on missing vin[0].prevout
This commit is contained in:
Alejandro García 2024-01-25 11:26:35 +00:00 committed by GitHub
commit e9e62b9e48
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 109 additions and 1 deletions

View File

@ -190,7 +190,13 @@ public class TxValidator {
JsonArray jsonVout = getVinAndVout(jsonTxt).second; JsonArray jsonVout = getVinAndVout(jsonTxt).second;
JsonObject jsonVin0 = jsonVin.get(0).getAsJsonObject(); JsonObject jsonVin0 = jsonVin.get(0).getAsJsonObject();
JsonObject jsonVout0 = jsonVout.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"); JsonElement jsonFeeValue = jsonVout0.get("value");
if (jsonVIn0Value == null || jsonFeeValue == null) { if (jsonVIn0Value == null || jsonFeeValue == null) {
throw new JsonSyntaxException("vin/vout missing data"); throw new JsonSyntaxException("vin/vout missing data");

View File

@ -23,6 +23,7 @@ import bisq.core.provider.mempool.FeeValidationStatus;
import bisq.core.provider.mempool.TxValidator; import bisq.core.provider.mempool.TxValidator;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
@ -121,6 +122,56 @@ public class MakerTxValidatorSanityCheckTests {
assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR)); 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 @Test
void responseHasDifferentTxId() throws IOException { void responseHasDifferentTxId() throws IOException {
String differentTxId = "abcde971ead7d03619e3a9eeaa771ed5adba14c448839e0299f857f7bb4ec07"; String differentTxId = "abcde971ead7d03619e3a9eeaa771ed5adba14c448839e0299f857f7bb4ec07";

View File

@ -23,6 +23,7 @@ import bisq.core.provider.mempool.FeeValidationStatus;
import bisq.core.provider.mempool.TxValidator; import bisq.core.provider.mempool.TxValidator;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
@ -121,6 +122,56 @@ public class TakerTxValidatorSanityCheckTests {
assertThat(txValidator1.getStatus(), is(FeeValidationStatus.NACK_JSON_ERROR)); 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 @Test
void responseHasDifferentTxId() throws IOException { void responseHasDifferentTxId() throws IOException {
String differentTxId = "abcde971ead7d03619e3a9eeaa771ed5adba14c448839e0299f857f7bb4ec07"; String differentTxId = "abcde971ead7d03619e3a9eeaa771ed5adba14c448839e0299f857f7bb4ec07";