mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-01-19 13:44:11 +01:00
Merge pull request #11 from schildbach/refactor-signum
Use BigInteger.signum() rather than .compareTo(ZERO)
This commit is contained in:
commit
52df132a9d
@ -627,7 +627,7 @@ public class Block extends Message {
|
||||
public BigInteger getDifficultyTargetAsInteger() throws VerificationException {
|
||||
maybeParseHeader();
|
||||
BigInteger target = Utils.decodeCompactBits(difficultyTarget);
|
||||
if (target.compareTo(BigInteger.ZERO) <= 0 || target.compareTo(params.proofOfWorkLimit) > 0)
|
||||
if (target.signum() <= 0 || target.compareTo(params.proofOfWorkLimit) > 0)
|
||||
throw new VerificationException("Difficulty target is bad: " + target.toString());
|
||||
return target;
|
||||
}
|
||||
|
@ -707,8 +707,8 @@ public class ECKey implements Serializable {
|
||||
@Nullable
|
||||
public static ECKey recoverFromSignature(int recId, ECDSASignature sig, Sha256Hash message, boolean compressed) {
|
||||
Preconditions.checkArgument(recId >= 0, "recId must be positive");
|
||||
Preconditions.checkArgument(sig.r.compareTo(BigInteger.ZERO) >= 0, "r must be positive");
|
||||
Preconditions.checkArgument(sig.s.compareTo(BigInteger.ZERO) >= 0, "s must be positive");
|
||||
Preconditions.checkArgument(sig.r.signum() >= 0, "r must be positive");
|
||||
Preconditions.checkArgument(sig.s.signum() >= 0, "s must be positive");
|
||||
Preconditions.checkNotNull(message);
|
||||
// 1.0 For j from 0 to h (h == recId here and the loop is outside this function)
|
||||
// 1.1 Let x = r + jn
|
||||
|
@ -224,7 +224,7 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
|
||||
}
|
||||
// All values were already checked for being non-negative (as it is verified in Transaction.verify())
|
||||
// but we check again here just for defence in depth. Transactions with zero output value are OK.
|
||||
if (valueOut.compareTo(BigInteger.ZERO) < 0 || valueOut.compareTo(params.MAX_MONEY) > 0)
|
||||
if (valueOut.signum() < 0 || valueOut.compareTo(params.MAX_MONEY) > 0)
|
||||
throw new VerificationException("Transaction output value out of rage");
|
||||
if (isCoinBase) {
|
||||
coinbaseValue = valueOut;
|
||||
@ -346,7 +346,7 @@ public class FullPrunedBlockChain extends AbstractBlockChain {
|
||||
}
|
||||
// All values were already checked for being non-negative (as it is verified in Transaction.verify())
|
||||
// but we check again here just for defence in depth. Transactions with zero output value are OK.
|
||||
if (valueOut.compareTo(BigInteger.ZERO) < 0 || valueOut.compareTo(params.MAX_MONEY) > 0)
|
||||
if (valueOut.signum() < 0 || valueOut.compareTo(params.MAX_MONEY) > 0)
|
||||
throw new VerificationException("Transaction output value out of rage");
|
||||
if (isCoinBase) {
|
||||
coinbaseValue = valueOut;
|
||||
|
@ -1184,7 +1184,7 @@ public class Transaction extends ChildMessage implements Serializable {
|
||||
|
||||
BigInteger valueOut = BigInteger.ZERO;
|
||||
for (TransactionOutput output : outputs) {
|
||||
if (output.getValue().compareTo(BigInteger.ZERO) < 0)
|
||||
if (output.getValue().signum() < 0)
|
||||
throw new VerificationException("Transaction output negative");
|
||||
valueOut = valueOut.add(output.getValue());
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ public class TransactionOutput extends ChildMessage implements Serializable {
|
||||
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.signum() >= 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;
|
||||
|
@ -120,7 +120,7 @@ public class Utils {
|
||||
*/
|
||||
public static BigInteger toNanoCoins(String coins) {
|
||||
BigInteger bigint = new BigDecimal(coins).movePointRight(8).toBigIntegerExact();
|
||||
if (bigint.compareTo(BigInteger.ZERO) < 0)
|
||||
if (bigint.signum() < 0)
|
||||
throw new ArithmeticException("Negative coins specified");
|
||||
if (bigint.compareTo(NetworkParameters.MAX_MONEY) > 0)
|
||||
throw new ArithmeticException("Amount larger than the total quantity of Bitcoins possible specified.");
|
||||
@ -331,7 +331,7 @@ public class Utils {
|
||||
*/
|
||||
public static String bitcoinValueToFriendlyString(BigInteger value) {
|
||||
// TODO: This API is crap. This method should go away when we encapsulate money values.
|
||||
boolean negative = value.compareTo(BigInteger.ZERO) < 0;
|
||||
boolean negative = value.signum() < 0;
|
||||
if (negative)
|
||||
value = value.negate();
|
||||
BigDecimal bd = new BigDecimal(value, 8);
|
||||
@ -404,7 +404,7 @@ public class Utils {
|
||||
else
|
||||
return new byte[] {0x00, 0x00, 0x00, 0x00};
|
||||
}
|
||||
boolean isNegative = value.compareTo(BigInteger.ZERO) < 0;
|
||||
boolean isNegative = value.signum() < 0;
|
||||
if (isNegative)
|
||||
value = value.negate();
|
||||
byte[] array = value.toByteArray();
|
||||
|
@ -732,8 +732,8 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
|
||||
public boolean isTransactionRelevant(Transaction tx) throws ScriptException {
|
||||
lock.lock();
|
||||
try {
|
||||
return tx.getValueSentFromMe(this).compareTo(BigInteger.ZERO) > 0 ||
|
||||
tx.getValueSentToMe(this).compareTo(BigInteger.ZERO) > 0 ||
|
||||
return tx.getValueSentFromMe(this).signum() > 0 ||
|
||||
tx.getValueSentToMe(this).signum() > 0 ||
|
||||
checkForDoubleSpendAgainstPending(tx, false);
|
||||
} finally {
|
||||
lock.unlock();
|
||||
@ -901,7 +901,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
|
||||
BigInteger newBalance = getBalance(); // This is slow.
|
||||
log.info("Balance is now: " + bitcoinValueToFriendlyString(newBalance));
|
||||
if (!wasPending) {
|
||||
int diff = valueDifference.compareTo(BigInteger.ZERO);
|
||||
int diff = valueDifference.signum();
|
||||
// We pick one callback based on the value difference, though a tx can of course both send and receive
|
||||
// coins from the wallet.
|
||||
if (diff > 0) {
|
||||
@ -1000,7 +1000,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
|
||||
// Now make sure it ends up in the right pool. Also, handle the case where this TX is double-spending
|
||||
// against our pending transactions. Note that a tx may double spend our pending transactions and also send
|
||||
// us money/spend our money.
|
||||
boolean hasOutputsToMe = tx.getValueSentToMe(this, true).compareTo(BigInteger.ZERO) > 0;
|
||||
boolean hasOutputsToMe = tx.getValueSentToMe(this, true).signum() > 0;
|
||||
if (hasOutputsToMe) {
|
||||
// Needs to go into either unspent or spent (if the outputs were already spent by a pending tx).
|
||||
if (tx.isEveryOwnedOutputSpent(this)) {
|
||||
@ -1010,7 +1010,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
|
||||
log.info(" tx {} ->unspent", tx.getHashAsString());
|
||||
addWalletTransaction(Pool.UNSPENT, tx);
|
||||
}
|
||||
} else if (tx.getValueSentFromMe(this).compareTo(BigInteger.ZERO) > 0) {
|
||||
} else if (tx.getValueSentFromMe(this).signum() > 0) {
|
||||
// Didn't send us any money, but did spend some. Keep it around for record keeping purposes.
|
||||
log.info(" tx {} ->spent", tx.getHashAsString());
|
||||
addWalletTransaction(Pool.SPENT, tx);
|
||||
@ -1224,11 +1224,11 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
|
||||
BigInteger valueSentFromMe = tx.getValueSentFromMe(this);
|
||||
BigInteger valueSentToMe = tx.getValueSentToMe(this);
|
||||
BigInteger newBalance = balance.add(valueSentToMe).subtract(valueSentFromMe);
|
||||
if (valueSentToMe.compareTo(BigInteger.ZERO) > 0) {
|
||||
if (valueSentToMe.signum() > 0) {
|
||||
checkBalanceFuturesLocked(null);
|
||||
queueOnCoinsReceived(tx, balance, newBalance);
|
||||
}
|
||||
if (valueSentFromMe.compareTo(BigInteger.ZERO) > 0)
|
||||
if (valueSentFromMe.signum() > 0)
|
||||
queueOnCoinsSent(tx, balance, newBalance);
|
||||
|
||||
maybeQueueOnWalletChanged();
|
||||
@ -1876,7 +1876,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
|
||||
log.info(" with {} coins change", bitcoinValueToFriendlyString(bestChangeOutput.getValue()));
|
||||
}
|
||||
final BigInteger calculatedFee = totalInput.subtract(totalOutput);
|
||||
if (calculatedFee.compareTo(BigInteger.ZERO) > 0) {
|
||||
if (calculatedFee.signum() > 0) {
|
||||
log.info(" with a fee of {}", bitcoinValueToFriendlyString(calculatedFee));
|
||||
}
|
||||
|
||||
@ -3411,7 +3411,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
|
||||
|
||||
int size = 0;
|
||||
TransactionOutput changeOutput = null;
|
||||
if (change.compareTo(BigInteger.ZERO) > 0) {
|
||||
if (change.signum() > 0) {
|
||||
// The value of the inputs is greater than what we want to send. Just like in real life then,
|
||||
// we need to take back some coins ... this is called "change". Add another output that sends the change
|
||||
// back to us. The address comes either from the request or getChangeAddress() as a default.
|
||||
@ -3450,7 +3450,7 @@ public class Wallet implements Serializable, BlockChainListener, PeerFilterProvi
|
||||
// include things we haven't added yet like input signatures/scripts or the change output.
|
||||
size += req.tx.bitcoinSerialize().length;
|
||||
size += estimateBytesForSigning(selection);
|
||||
if (size/1000 > lastCalculatedSize/1000 && req.feePerKb.compareTo(BigInteger.ZERO) > 0) {
|
||||
if (size/1000 > lastCalculatedSize/1000 && req.feePerKb.signum() > 0) {
|
||||
lastCalculatedSize = size;
|
||||
// We need more fees anyway, just try again with the same additional value
|
||||
additionalValueForNextCategory = additionalValueSelected;
|
||||
|
@ -157,7 +157,7 @@ public class PaymentChannelClientState {
|
||||
*/
|
||||
public PaymentChannelClientState(Wallet wallet, ECKey myKey, ECKey serverMultisigKey,
|
||||
BigInteger value, long expiryTimeInSeconds) throws VerificationException {
|
||||
checkArgument(value.compareTo(BigInteger.ZERO) > 0);
|
||||
checkArgument(value.signum() > 0);
|
||||
this.wallet = checkNotNull(wallet);
|
||||
initWalletListeners();
|
||||
this.serverMultisigKey = checkNotNull(serverMultisigKey);
|
||||
@ -396,15 +396,15 @@ public class PaymentChannelClientState {
|
||||
checkState(state == State.READY);
|
||||
checkNotExpired();
|
||||
checkNotNull(size); // Validity of size will be checked by makeUnsignedChannelContract.
|
||||
if (size.compareTo(BigInteger.ZERO) < 0)
|
||||
if (size.signum() < 0)
|
||||
throw new ValueOutOfRangeException("Tried to decrement payment");
|
||||
BigInteger newValueToMe = valueToMe.subtract(size);
|
||||
if (newValueToMe.compareTo(Transaction.MIN_NONDUST_OUTPUT) < 0 && newValueToMe.compareTo(BigInteger.ZERO) > 0) {
|
||||
if (newValueToMe.compareTo(Transaction.MIN_NONDUST_OUTPUT) < 0 && newValueToMe.signum() > 0) {
|
||||
log.info("New value being sent back as change was smaller than minimum nondust output, sending all");
|
||||
size = valueToMe;
|
||||
newValueToMe = BigInteger.ZERO;
|
||||
}
|
||||
if (newValueToMe.compareTo(BigInteger.ZERO) < 0)
|
||||
if (newValueToMe.signum() < 0)
|
||||
throw new ValueOutOfRangeException("Channel has too little money to pay " + size + " satoshis");
|
||||
Transaction tx = makeUnsignedChannelContract(newValueToMe);
|
||||
log.info("Signing new payment tx {}", tx);
|
||||
|
@ -315,7 +315,7 @@ public class PaymentChannelServer {
|
||||
boolean stillUsable = state.incrementPayment(refundSize, msg.getSignature().toByteArray());
|
||||
BigInteger bestPaymentChange = state.getBestValueToMe().subtract(lastBestPayment);
|
||||
|
||||
if (bestPaymentChange.compareTo(BigInteger.ZERO) > 0)
|
||||
if (bestPaymentChange.signum() > 0)
|
||||
conn.paymentIncrease(bestPaymentChange, state.getBestValueToMe());
|
||||
|
||||
if (sendAck) {
|
||||
|
@ -227,7 +227,7 @@ public class PaymentChannelServerState {
|
||||
throw new VerificationException("Multisig contract's first output was not a standard 2-of-2 multisig to client and server in that order.");
|
||||
|
||||
this.totalValue = multisigContract.getOutput(0).getValue();
|
||||
if (this.totalValue.compareTo(BigInteger.ZERO) <= 0)
|
||||
if (this.totalValue.signum() <= 0)
|
||||
throw new VerificationException("Not accepting an attempt to open a contract with zero value.");
|
||||
} catch (VerificationException e) {
|
||||
// We couldn't parse the multisig transaction or its output.
|
||||
@ -294,7 +294,7 @@ public class PaymentChannelServerState {
|
||||
if (refundSize.compareTo(clientOutput.getMinNonDustValue()) < 0 && !fullyUsedUp)
|
||||
throw new ValueOutOfRangeException("Attempt to refund negative value or value too small to be accepted by the network");
|
||||
BigInteger newValueToMe = totalValue.subtract(refundSize);
|
||||
if (newValueToMe.compareTo(BigInteger.ZERO) < 0)
|
||||
if (newValueToMe.signum() < 0)
|
||||
throw new ValueOutOfRangeException("Attempt to refund more than the contract allows.");
|
||||
if (newValueToMe.compareTo(bestValueToMe) < 0)
|
||||
throw new ValueOutOfRangeException("Attempt to roll back payment on the channel.");
|
||||
|
@ -220,8 +220,8 @@ public class StoredPaymentChannelClientStates implements WalletExtension {
|
||||
ClientState.StoredClientPaymentChannels.Builder builder = ClientState.StoredClientPaymentChannels.newBuilder();
|
||||
for (StoredClientChannel channel : mapChannels.values()) {
|
||||
// First a few asserts to make sure things won't break
|
||||
checkState(channel.valueToMe.compareTo(BigInteger.ZERO) >= 0 && channel.valueToMe.compareTo(NetworkParameters.MAX_MONEY) < 0);
|
||||
checkState(channel.refundFees.compareTo(BigInteger.ZERO) >= 0 && channel.refundFees.compareTo(NetworkParameters.MAX_MONEY) < 0);
|
||||
checkState(channel.valueToMe.signum() >= 0 && channel.valueToMe.compareTo(NetworkParameters.MAX_MONEY) < 0);
|
||||
checkState(channel.refundFees.signum() >= 0 && channel.refundFees.compareTo(NetworkParameters.MAX_MONEY) < 0);
|
||||
checkNotNull(channel.myKey.getPrivKeyBytes());
|
||||
checkState(channel.refund.getConfidence().getSource() == TransactionConfidence.Source.SELF);
|
||||
final ClientState.StoredClientPaymentChannel.Builder value = ClientState.StoredClientPaymentChannel.newBuilder()
|
||||
|
@ -150,7 +150,7 @@ public class StoredPaymentChannelServerStates implements WalletExtension {
|
||||
ServerState.StoredServerPaymentChannels.Builder builder = ServerState.StoredServerPaymentChannels.newBuilder();
|
||||
for (StoredServerChannel channel : mapChannels.values()) {
|
||||
// First a few asserts to make sure things won't break
|
||||
checkState(channel.bestValueToMe.compareTo(BigInteger.ZERO) >= 0 && channel.bestValueToMe.compareTo(NetworkParameters.MAX_MONEY) < 0);
|
||||
checkState(channel.bestValueToMe.signum() >= 0 && channel.bestValueToMe.compareTo(NetworkParameters.MAX_MONEY) < 0);
|
||||
checkState(channel.refundTransactionUnlockTimeSecs > 0);
|
||||
checkNotNull(channel.myKey.getPrivKeyBytes());
|
||||
ServerState.StoredServerPaymentChannel.Builder channelBuilder = ServerState.StoredServerPaymentChannel.newBuilder()
|
||||
|
@ -883,7 +883,7 @@ public class Script {
|
||||
numericOPnum = numericOPnum.negate();
|
||||
break;
|
||||
case OP_ABS:
|
||||
if (numericOPnum.compareTo(BigInteger.ZERO) < 0)
|
||||
if (numericOPnum.signum() < 0)
|
||||
numericOPnum = numericOPnum.negate();
|
||||
break;
|
||||
case OP_NOT:
|
||||
|
@ -326,7 +326,7 @@ public class BitcoinURI {
|
||||
public static String convertToBitcoinURI(String address, @Nullable BigInteger amount, @Nullable String label,
|
||||
@Nullable String message) {
|
||||
checkNotNull(address);
|
||||
if (amount != null && amount.compareTo(BigInteger.ZERO) < 0) {
|
||||
if (amount != null && amount.signum() < 0) {
|
||||
throw new IllegalArgumentException("Amount must be positive");
|
||||
}
|
||||
|
||||
|
@ -127,7 +127,7 @@ public class BlockChainTest {
|
||||
wallet.getKeys().get(0).toAddress(unitTestParams));
|
||||
Block b1 = createFakeBlock(blockStore, tx1).block;
|
||||
chain.add(b1);
|
||||
assertTrue(wallet.getBalance().compareTo(BigInteger.ZERO) > 0);
|
||||
assertTrue(wallet.getBalance().signum() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -181,11 +181,11 @@ public class WalletProtobufSerializerTest {
|
||||
// Start by building two blocks on top of the genesis block.
|
||||
Block b1 = params.getGenesisBlock().createNextBlock(myAddress);
|
||||
BigInteger work1 = b1.getWork();
|
||||
assertTrue(work1.compareTo(BigInteger.ZERO) > 0);
|
||||
assertTrue(work1.signum() > 0);
|
||||
|
||||
Block b2 = b1.createNextBlock(myAddress);
|
||||
BigInteger work2 = b2.getWork();
|
||||
assertTrue(work2.compareTo(BigInteger.ZERO) > 0);
|
||||
assertTrue(work2.signum() > 0);
|
||||
|
||||
assertTrue(chain.add(b1));
|
||||
assertTrue(chain.add(b2));
|
||||
|
Loading…
Reference in New Issue
Block a user