Process opReturn txOutput before remaining outputs

This commit is contained in:
sqrrm 2018-09-05 16:34:59 +02:00
parent a623fd3852
commit f31bf0d5cf
No known key found for this signature in database
GPG key ID: 45235F9EF87089EC
2 changed files with 21 additions and 36 deletions

View file

@ -59,8 +59,6 @@ public class TxOutputParser {
@Getter
private boolean bsqOutputFound;
@Getter
private Optional<OpReturnType> optionalOpReturnTypeCandidate = Optional.empty();
@Getter
private Optional<OpReturnType> optionalVerifiedOpReturnType = Optional.empty();
@Getter
private Optional<TempTxOutput> optionalIssuanceCandidate = Optional.empty();
@ -82,9 +80,17 @@ public class TxOutputParser {
}
}
boolean isOpReturnCandidate(TempTxOutput txOutput) {
optionalOpReturnTypeCandidate = OpReturnParser.getOptionalOpReturnTypeCandidate(txOutput);
return optionalOpReturnTypeCandidate.isPresent();
boolean isOpReturnOutput(TempTxOutput txOutput) {
return txOutput.getOpReturnData() != null;
}
void processOpReturnOutput(boolean isLastOutput, TempTxOutput tempTxOutput) {
byte[] opReturnData = tempTxOutput.getOpReturnData();
if (opReturnData != null) {
handleOpReturnOutput(tempTxOutput, isLastOutput);
} else {
log.error("This should be an opReturn output");
}
}
/**
@ -110,14 +116,10 @@ public class TxOutputParser {
handleBtcOutput(tempTxOutput, index);
}
} else {
handleOpReturnOutput(tempTxOutput, isLastOutput);
log.error("This should not be an opReturn output");
}
}
boolean isOpReturnOutput(TempTxOutput txOutput) {
return txOutput.getOpReturnData() != null;
}
/**
* Whether a transaction is a valid unlock bond transaction or not.
*
@ -154,8 +156,8 @@ public class TxOutputParser {
boolean isFirstOutput = index == 0;
OpReturnType opReturnTypeCandidate = null;
if (optionalOpReturnTypeCandidate.isPresent())
opReturnTypeCandidate = optionalOpReturnTypeCandidate.get();
if (optionalVerifiedOpReturnType.isPresent())
opReturnTypeCandidate = optionalVerifiedOpReturnType.get();
TxOutputType bsqOutput;
if (isFirstOutput && opReturnTypeCandidate == OpReturnType.BLIND_VOTE) {
@ -182,8 +184,8 @@ public class TxOutputParser {
// candidate to the parsingModel and we don't apply the TxOutputType as we do that later as the OpReturn check.
if (availableInputValue > 0 &&
index == 1 &&
optionalOpReturnTypeCandidate.isPresent() &&
optionalOpReturnTypeCandidate.get() == OpReturnType.COMPENSATION_REQUEST) {
optionalVerifiedOpReturnType.isPresent() &&
optionalVerifiedOpReturnType.get() == OpReturnType.COMPENSATION_REQUEST) {
// We don't set the txOutputType yet as we have not fully validated the tx but put the candidate
// into our local optionalIssuanceCandidate.

View file

@ -116,8 +116,9 @@ public class TxParser {
checkArgument(!outputs.isEmpty(), "outputs must not be empty");
int lastIndex = outputs.size() - 1;
int lastNonOpReturnIndex = lastIndex;
if (txOutputParser.isOpReturnCandidate(outputs.get(lastIndex))) {
txOutputParser.processTxOutput(true, outputs.get(lastIndex), lastIndex);
if (txOutputParser.isOpReturnOutput(outputs.get(lastIndex))){
// TODO(SQ): perhaps the check for isLastOutput could be skipped
txOutputParser.processOpReturnOutput(true, outputs.get(lastIndex));
lastNonOpReturnIndex -= 1;
}
@ -145,7 +146,7 @@ public class TxParser {
// use RawTx?
TxType txType = TxParser.getBisqTxType(
tempTx,
txOutputParser.getOptionalOpReturnTypeCandidate().isPresent(),
txOutputParser.getOptionalVerifiedOpReturnType().isPresent(),
remainingInputValue,
getOptionalOpReturnType()
);
@ -395,25 +396,7 @@ public class TxParser {
*/
private Optional<OpReturnType> getOptionalOpReturnType() {
if (txOutputParser.isBsqOutputFound()) {
// We want to be sure that the initial assumption of the opReturn type was matching the result after full
// validation.
Optional<OpReturnType> optionalOpReturnTypeCandidate = txOutputParser.getOptionalOpReturnTypeCandidate();
if (optionalOpReturnTypeCandidate.isPresent()) {
OpReturnType opReturnTypeCandidate = optionalOpReturnTypeCandidate.get();
Optional<OpReturnType> optionalVerifiedOpReturnType = txOutputParser.getOptionalVerifiedOpReturnType();
if (optionalVerifiedOpReturnType.isPresent()) {
OpReturnType verifiedOpReturnType = optionalVerifiedOpReturnType.get();
if (opReturnTypeCandidate == verifiedOpReturnType) {
return optionalVerifiedOpReturnType;
}
}
}
String msg = "We got a different opReturn type after validation as we expected initially. " +
"optionalOpReturnTypeCandidate=" + optionalOpReturnTypeCandidate +
", optionalVerifiedOpReturnType=" + txOutputParser.getOptionalVerifiedOpReturnType();
log.error(msg);
return txOutputParser.getOptionalVerifiedOpReturnType();
} else {
String msg = "We got a tx without any valid BSQ output but with burned BSQ. " +
"Burned fee=" + remainingInputValue / 100D + " BSQ.";