Created
August 23, 2019 21:43
-
-
Save minostro/6dcfb61e7e22c295ced731e99c1cf112 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean isValidTx(Transaction tx) { | |
boolean areInputsInUTXOPool = !tx.getInputs().stream() | |
.filter(i -> !utxoPool.contains(new UTXO(i.prevTxHash, i.outputIndex))) | |
.findFirst() | |
.isPresent(); | |
boolean areOutputValuesNonNegative = !tx.getOutputs().stream() | |
.filter(o -> o.value < 0) | |
.findFirst() | |
.isPresent(); | |
double inputsValue = tx.getInputs().stream() | |
.map(i -> utxoPool.getTxOutput(new UTXO(i.prevTxHash, i.outputIndex))) | |
.filter(Objects::nonNull) | |
.map(o -> o.value) | |
.reduce(0.0, (x, y) -> x + y); | |
double outputsValue = tx.getOutputs().stream() | |
.map(o -> o.value) | |
.reduce(0.0, (x, y) -> x + y); | |
boolean areUTXOClaimedOnlyOnce = !tx.getInputs().stream() | |
.map(i -> new UTXO(i.prevTxHash, i.outputIndex)) | |
.collect(Collectors.groupingBy(UTXO::hashCode)) | |
.entrySet() | |
.stream() | |
.filter(x -> x.getValue().size() > 1) | |
.findFirst() | |
.isPresent(); | |
boolean areAllInputSignaturesValid = !tx.getInputs().stream() | |
.filter(i -> { | |
Optional<Transaction.Output> output = Optional.ofNullable(utxoPool.getTxOutput(new UTXO(i.prevTxHash, i.outputIndex))); | |
if (output.isPresent()) | |
return !Crypto.verifySignature(output.get().address, tx.getRawDataToSign(i.outputIndex), i.signature); | |
else | |
return false; | |
}) | |
.findFirst() | |
.isPresent(); | |
if (!areInputsInUTXOPool || !areOutputValuesNonNegative || outputsValue > inputsValue || !areUTXOClaimedOnlyOnce || !areAllInputSignaturesValid) | |
return false; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment