3
0
mirror of https://github.com/Qortal/altcoinj.git synced 2025-01-31 15:22:16 +00:00

TransactionOutput: tighter checks on values when constructing (don't allow negative values, etc).

This commit is contained in:
Mike Hearn 2013-10-07 17:57:53 +02:00
parent b09c4cbe09
commit 9953bbe5cb
3 changed files with 7 additions and 1 deletions

View File

@ -1014,7 +1014,7 @@ public class Transaction extends ChildMessage implements Serializable {
// that position are "nulled out". Unintuitively, the value in a "null" transaction is set to -1.
this.outputs = new ArrayList<TransactionOutput>(this.outputs.subList(0, inputIndex + 1));
for (int i = 0; i < inputIndex; i++)
this.outputs.set(i, new TransactionOutput(params, this, BigInteger.valueOf(-1), new byte[] {}));
this.outputs.set(i, new TransactionOutput(params, this, NEGATIVE_ONE, new byte[] {}));
// The signature isn't broken by new versions of the transaction issued by other parties.
for (int i = 0; i < inputs.size(); i++)
if (i != inputIndex)

View File

@ -28,6 +28,7 @@ import java.io.OutputStream;
import java.io.Serializable;
import java.math.BigInteger;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
@ -107,6 +108,10 @@ public class TransactionOutput extends ChildMessage implements Serializable {
public TransactionOutput(NetworkParameters params, Transaction parent, BigInteger value, byte[] scriptBytes) {
super(params);
// Negative values obviously make no sense, except for -1 which is used as a sentinel value when calculating
// SIGHASH_SINGLE signatures, so unfortunately we have to allow that here.
checkArgument(value.compareTo(BigInteger.ZERO) >= 0 || value.equals(Utils.NEGATIVE_ONE), "Negative values not allowed");
checkArgument(value.compareTo(NetworkParameters.MAX_MONEY) < 0, "Values larger than MAX_MONEY not allowed");
this.value = value;
this.scriptBytes = scriptBytes;
parentTransaction = parent;

View File

@ -38,6 +38,7 @@ import static com.google.common.base.Preconditions.checkArgument;
* To enable debug logging from the library, run with -Dbitcoinj.logging=true on your command line.
*/
public class Utils {
public static final BigInteger NEGATIVE_ONE = BigInteger.valueOf(-1);
private static final MessageDigest digest;
static {
try {