Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TxValidator: Fix crash on missing vin[0].prevout #7003

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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";
Expand Down