Refactor TxOutputParser

- Remove unneeded isLastOutput
- Rename bsqOutputs to tempTxOutputs
- Rename commitTxOutputs to commitTxOutputsForValidTx
This commit is contained in:
Manfred Karrer 2018-10-03 17:59:34 -05:00
parent 0c8d475c24
commit 744f0cc516
No known key found for this signature in database
GPG Key ID: 401250966A6B2C46
3 changed files with 28 additions and 33 deletions

View File

@ -58,17 +58,16 @@ class OpReturnParser {
* Parse the type of OP_RETURN data and validate it.
*
* @param tempTxOutput The temporary transaction output to parse.
* @param isLastOutput If true, the output being parsed has a non-zero value.
* @return The type of the transaction output, which will be either one of the
* {@code *_OP_RETURN_OUTPUT} values, or {@code UNDEFINED} in case of
* unexpected state.
*/
static TxOutputType getTxOutputType(TempTxOutput tempTxOutput, boolean isLastOutput) {
static TxOutputType getTxOutputType(TempTxOutput tempTxOutput) {
boolean nonZeroOutput = tempTxOutput.getValue() != 0;
byte[] opReturnData = tempTxOutput.getOpReturnData();
checkNotNull(opReturnData, "opReturnData must not be null");
if (nonZeroOutput || !isLastOutput || opReturnData.length < 22) {
if (nonZeroOutput || opReturnData.length < 22) {
log.warn("OP_RETURN data does not match our rules. opReturnData={}",
Utils.HEX.encode(opReturnData));
return TxOutputType.INVALID_OUTPUT;

View File

@ -51,7 +51,7 @@ public class TxOutputParser {
@Setter
private long availableInputValue = 0;
private int lockTime;
private List<TempTxOutput> bsqOutputs = new ArrayList<>();
private List<TempTxOutput> tempTxOutputs = new ArrayList<>();
@Setter
private int unlockBlockHeight;
@Setter
@ -77,38 +77,34 @@ public class TxOutputParser {
this.bsqStateService = bsqStateService;
}
public void commitTxOutputs() {
bsqOutputs.forEach(bsqOutput -> {
bsqStateService.addUnspentTxOutput(TxOutput.fromTempOutput(bsqOutput));
});
}
/**
* This sets all outputs to BTC_OUTPUT and doesn't add any txOutputs to the bsqStateService
*/
public void commitTxOutputsForInvalidTx() {
bsqOutputs.forEach(bsqOutput -> {
bsqOutput.setTxOutputType(TxOutputType.BTC_OUTPUT);
});
}
public void processGenesisTxOutput(TempTx genesisTx) {
for (int i = 0; i < genesisTx.getTempTxOutputs().size(); ++i) {
TempTxOutput tempTxOutput = genesisTx.getTempTxOutputs().get(i);
bsqOutputs.add(tempTxOutput);
tempTxOutputs.add(tempTxOutput);
}
commitTxOutputs();
commitTxOutputsForValidTx();
}
public void commitTxOutputsForValidTx() {
tempTxOutputs.forEach(output -> bsqStateService.addUnspentTxOutput(TxOutput.fromTempOutput(output)));
}
/**
* This sets all outputs to BTC_OUTPUT and doesn't add any txOutputs to the unspentTxOutput map in bsqStateService
*/
public void commitTxOutputsForInvalidTx() {
tempTxOutputs.forEach(output -> output.setTxOutputType(TxOutputType.BTC_OUTPUT));
}
boolean isOpReturnOutput(TempTxOutput txOutput) {
return txOutput.getOpReturnData() != null;
}
void processOpReturnOutput(boolean isLastOutput, TempTxOutput tempTxOutput) {
void processOpReturnOutput(TempTxOutput tempTxOutput) {
byte[] opReturnData = tempTxOutput.getOpReturnData();
if (opReturnData != null) {
handleOpReturnOutput(tempTxOutput, isLastOutput);
handleOpReturnOutput(tempTxOutput);
} else {
log.error("This should be an opReturn output");
}
@ -117,9 +113,9 @@ public class TxOutputParser {
/**
* Process a transaction output.
*
* @param isLastOutput If it is the last output
* @param tempTxOutput The TempTxOutput we are parsing
* @param index The index in the outputs
* @param isLastOutput If it is the last output
* @param tempTxOutput The TempTxOutput we are parsing
* @param index The index in the outputs
*/
void processTxOutput(boolean isLastOutput, TempTxOutput tempTxOutput, int index) {
// We do not check for pubKeyScript.scriptType.NULL_DATA because that is only set if dumpBlockchainData is true
@ -164,7 +160,7 @@ public class TxOutputParser {
availableInputValue -= optionalSpentLockupTxOutput.get().getValue();
txOutput.setTxOutputType(TxOutputType.UNLOCK_OUTPUT);
bsqOutputs.add(txOutput);
tempTxOutputs.add(txOutput);
//TODO move up to TxParser
// We should add unlockBlockHeight to TempTxOutput and remove unlockBlockHeight from tempTx
@ -198,7 +194,7 @@ public class TxOutputParser {
bsqOutput = TxOutputType.BSQ_OUTPUT;
}
txOutput.setTxOutputType(bsqOutput);
bsqOutputs.add(txOutput);
tempTxOutputs.add(txOutput);
bsqOutputFound = true;
}
@ -219,8 +215,8 @@ public class TxOutputParser {
}
}
private void handleOpReturnOutput(TempTxOutput tempTxOutput, boolean isLastOutput) {
TxOutputType txOutputType = OpReturnParser.getTxOutputType(tempTxOutput, isLastOutput);
private void handleOpReturnOutput(TempTxOutput tempTxOutput) {
TxOutputType txOutputType = OpReturnParser.getTxOutputType(tempTxOutput);
tempTxOutput.setTxOutputType(txOutputType);
optionalVerifiedOpReturnType = getMappedOpReturnType(txOutputType);

View File

@ -127,7 +127,7 @@ public class TxParser {
int lastNonOpReturnIndex = lastIndex;
if (txOutputParser.isOpReturnOutput(outputs.get(lastIndex))) {
// TODO(SQ): perhaps the check for isLastOutput could be skipped
txOutputParser.processOpReturnOutput(true, outputs.get(lastIndex));
txOutputParser.processOpReturnOutput(outputs.get(lastIndex));
lastNonOpReturnIndex -= 1;
}
@ -170,8 +170,8 @@ public class TxParser {
}
}
if (tempTx.getTxType() != TxType.INVALID){
txOutputParser.commitTxOutputs();
if (tempTx.getTxType() != TxType.INVALID) {
txOutputParser.commitTxOutputsForValidTx();
} else {
txOutputParser.commitTxOutputsForInvalidTx();
}