Convert to Account.modifyAssetBalance()

Change code of the form (assetId aspect not shown):

account.setConfirmedBalance( account.getConfirmedBalance(), amount )

to:

account.modifyAssetBalance( amount )

Also tidied "0 - value" to use unary negate: "- value"
This commit is contained in:
catbref 2020-05-04 08:45:31 +01:00
parent 800103225b
commit 71e80bd02f
11 changed files with 49 additions and 69 deletions

View File

@ -85,6 +85,17 @@ public class Account {
LOGGER.trace(() -> String.format("%s balance now %s [assetId %s]", this.address, prettyAmount(balance), assetId));
}
// Convenience method
public void modifyAssetBalance(long assetId, long deltaBalance) throws DataException {
this.repository.getAccountRepository().modifyAssetBalance(this.getAddress(), assetId, deltaBalance);
LOGGER.trace(() -> String.format("%s balance %s by %s [assetId %s]",
this.address,
(deltaBalance >= 0 ? "increased" : "decreased"),
prettyAmount(Math.abs(deltaBalance)),
assetId));
}
public void deleteBalance(long assetId) throws DataException {
this.repository.getAccountRepository().delete(this.address, assetId);
}

View File

@ -256,7 +256,7 @@ public class Order {
public void process() throws DataException {
// Subtract have-asset from creator
Account creator = new PublicKeyAccount(this.repository, this.orderData.getCreatorPublicKey());
creator.setConfirmedBalance(haveAssetId, creator.getConfirmedBalance(haveAssetId) - this.calcHaveAssetCommittment());
creator.modifyAssetBalance(haveAssetId, - this.calcHaveAssetCommittment());
// Save this order into repository so it's available for matching, possibly by itself
this.repository.getAssetRepository().save(this.orderData);
@ -418,7 +418,7 @@ public class Order {
// Return asset to creator
Account creator = new PublicKeyAccount(this.repository, this.orderData.getCreatorPublicKey());
creator.setConfirmedBalance(haveAssetId, creator.getConfirmedBalance(haveAssetId) + this.calcHaveAssetCommittment());
creator.modifyAssetBalance(haveAssetId, this.calcHaveAssetCommittment());
}
// This is called by CancelOrderTransaction so that an Order can no longer trade
@ -428,14 +428,14 @@ public class Order {
// Update creator's balance with unfulfilled amount
Account creator = new PublicKeyAccount(this.repository, this.orderData.getCreatorPublicKey());
creator.setConfirmedBalance(haveAssetId, creator.getConfirmedBalance(haveAssetId) + calcHaveAssetRefund());
creator.modifyAssetBalance(haveAssetId, calcHaveAssetRefund());
}
// Opposite of cancel() above for use during orphaning
public void reopen() throws DataException {
// Update creator's balance with unfulfilled amount
Account creator = new PublicKeyAccount(this.repository, this.orderData.getCreatorPublicKey());
creator.setConfirmedBalance(haveAssetId, creator.getConfirmedBalance(haveAssetId) - calcHaveAssetRefund());
creator.modifyAssetBalance(haveAssetId, - calcHaveAssetRefund());
this.orderData.setIsClosed(false);
this.repository.getAssetRepository().save(this.orderData);

View File

@ -67,15 +67,15 @@ public class Trade {
// Actually transfer asset balances
Account initiatingCreator = new PublicKeyAccount(this.repository, initiatingOrder.getCreatorPublicKey());
initiatingCreator.setConfirmedBalance(initiatingOrder.getWantAssetId(), initiatingCreator.getConfirmedBalance(initiatingOrder.getWantAssetId()) + tradeData.getTargetAmount());
initiatingCreator.modifyAssetBalance(initiatingOrder.getWantAssetId(), tradeData.getTargetAmount());
Account targetCreator = new PublicKeyAccount(this.repository, targetOrder.getCreatorPublicKey());
targetCreator.setConfirmedBalance(targetOrder.getWantAssetId(), targetCreator.getConfirmedBalance(targetOrder.getWantAssetId()) + tradeData.getInitiatorAmount());
targetCreator.modifyAssetBalance(targetOrder.getWantAssetId(), tradeData.getInitiatorAmount());
// Possible partial saving to refund to initiator
long initiatorSaving = this.tradeData.getInitiatorSaving();
if (initiatorSaving > 0)
initiatingCreator.setConfirmedBalance(initiatingOrder.getHaveAssetId(), initiatingCreator.getConfirmedBalance(initiatingOrder.getHaveAssetId()) + initiatorSaving);
initiatingCreator.modifyAssetBalance(initiatingOrder.getHaveAssetId(), initiatorSaving);
}
public void orphan() throws DataException {
@ -99,15 +99,15 @@ public class Trade {
// Reverse asset transfers
Account initiatingCreator = new PublicKeyAccount(this.repository, initiatingOrder.getCreatorPublicKey());
initiatingCreator.setConfirmedBalance(initiatingOrder.getWantAssetId(), initiatingCreator.getConfirmedBalance(initiatingOrder.getWantAssetId()) - tradeData.getTargetAmount());
initiatingCreator.modifyAssetBalance(initiatingOrder.getWantAssetId(), - tradeData.getTargetAmount());
Account targetCreator = new PublicKeyAccount(this.repository, targetOrder.getCreatorPublicKey());
targetCreator.setConfirmedBalance(targetOrder.getWantAssetId(), targetCreator.getConfirmedBalance(targetOrder.getWantAssetId()) - tradeData.getInitiatorAmount());
targetCreator.modifyAssetBalance(targetOrder.getWantAssetId(), - tradeData.getInitiatorAmount());
// Possible partial saving to claw back from initiator
long initiatorSaving = this.tradeData.getInitiatorSaving();
if (initiatorSaving > 0)
initiatingCreator.setConfirmedBalance(initiatingOrder.getHaveAssetId(), initiatingCreator.getConfirmedBalance(initiatingOrder.getHaveAssetId()) - initiatorSaving);
initiatingCreator.modifyAssetBalance(initiatingOrder.getHaveAssetId(), - initiatorSaving);
// Remove trade from repository
assetRepository.delete(tradeData);

View File

@ -193,7 +193,7 @@ public class Block {
// minter & recipient the same - simpler case
LOGGER.trace(() -> String.format("Minter/recipient account %s share: %s", this.mintingAccount.getAddress(), Amounts.prettyAmount(accountAmount)));
if (accountAmount != 0)
this.repository.getAccountRepository().modifyAssetBalance(this.mintingAccount.getAddress(), Asset.QORT, accountAmount);
this.mintingAccount.modifyAssetBalance(Asset.QORT, accountAmount);
} else {
// minter & recipient different - extra work needed
long recipientAmount = (accountAmount * this.sharePercent) / 100L / 100L; // because scaled by 2dp and 'percent' means "per 100"
@ -201,11 +201,11 @@ public class Block {
LOGGER.trace(() -> String.format("Minter account %s share: %s", this.mintingAccount.getAddress(), Amounts.prettyAmount(minterAmount)));
if (minterAmount != 0)
this.repository.getAccountRepository().modifyAssetBalance(this.mintingAccount.getAddress(), Asset.QORT, minterAmount);
this.mintingAccount.modifyAssetBalance(Asset.QORT, minterAmount);
LOGGER.trace(() -> String.format("Recipient account %s share: %s", this.recipientAccount.getAddress(), Amounts.prettyAmount(recipientAmount)));
if (recipientAmount != 0)
this.repository.getAccountRepository().modifyAssetBalance(this.recipientAccount.getAddress(), Asset.QORT, recipientAmount);
this.recipientAccount.modifyAssetBalance(Asset.QORT, recipientAmount);
}
}
}
@ -1420,7 +1420,7 @@ public class Block {
Account atAccount = new Account(this.repository, atStateData.getATAddress());
// Subtract AT-generated fees from AT accounts
atAccount.setConfirmedBalance(Asset.QORT, atAccount.getConfirmedBalance(Asset.QORT) - atStateData.getFees());
atAccount.modifyAssetBalance(Asset.QORT, - atStateData.getFees());
// Update AT info with latest state
ATData atData = atRepository.fromATAddress(atStateData.getATAddress());
@ -1566,7 +1566,7 @@ public class Block {
if (reward == 0)
return;
distributeBlockReward(0 - reward);
distributeBlockReward(- reward);
}
protected void deductTransactionFees() throws DataException {
@ -1576,7 +1576,7 @@ public class Block {
if (blockFees <= 0)
return;
distributeBlockReward(0 - blockFees);
distributeBlockReward(- blockFees);
}
protected void orphanAtFeesAndStates() throws DataException {
@ -1585,7 +1585,7 @@ public class Block {
Account atAccount = new Account(this.repository, atStateData.getATAddress());
// Return AT-generated fees to AT accounts
atAccount.setConfirmedBalance(Asset.QORT, atAccount.getConfirmedBalance(Asset.QORT) + atStateData.getFees());
atAccount.modifyAssetBalance(Asset.QORT, atStateData.getFees());
// Revert AT info to prior values
ATData atData = atRepository.fromATAddress(atStateData.getATAddress());
@ -1766,7 +1766,7 @@ public class Block {
}
}
this.repository.getAccountRepository().modifyAssetBalance(qoraHolder.getAddress(), Asset.QORT, holderReward);
qoraHolderAccount.modifyAssetBalance(Asset.QORT, holderReward);
if (newQortFromQoraBalance > 0)
qoraHolderAccount.setConfirmedBalance(Asset.QORT_FROM_QORA, newQortFromQoraBalance);
@ -1804,7 +1804,7 @@ public class Block {
if (founderExpandedAccounts.isEmpty()) {
// Simple case: no founder-as-minter reward-shares online so founder gets whole amount.
this.repository.getAccountRepository().modifyAssetBalance(founderAccount.getAddress(), Asset.QORT, perFounderAmount);
founderAccount.modifyAssetBalance(Asset.QORT, perFounderAmount);
} else {
// Distribute over reward-shares
long perFounderRewardShareAmount = perFounderAmount / founderExpandedAccounts.size();

View File

@ -158,13 +158,13 @@ public class Name {
// Update seller's balance
Account seller = new Account(this.repository, this.nameData.getOwner());
seller.setConfirmedBalance(Asset.QORT, seller.getConfirmedBalance(Asset.QORT) + buyNameTransactionData.getAmount());
seller.modifyAssetBalance(Asset.QORT, buyNameTransactionData.getAmount());
// Set new owner
Account buyer = new PublicKeyAccount(this.repository, buyNameTransactionData.getBuyerPublicKey());
this.nameData.setOwner(buyer.getAddress());
// Update buyer's balance
buyer.setConfirmedBalance(Asset.QORT, buyer.getConfirmedBalance(Asset.QORT) - buyNameTransactionData.getAmount());
buyer.modifyAssetBalance(Asset.QORT, - buyNameTransactionData.getAmount());
// Update reference in transaction data
buyNameTransactionData.setNameReference(this.nameData.getReference());
@ -189,14 +189,14 @@ public class Name {
// Revert buyer's balance
Account buyer = new PublicKeyAccount(this.repository, buyNameTransactionData.getBuyerPublicKey());
buyer.setConfirmedBalance(Asset.QORT, buyer.getConfirmedBalance(Asset.QORT) + buyNameTransactionData.getAmount());
buyer.modifyAssetBalance(Asset.QORT, buyNameTransactionData.getAmount());
// Previous Name's owner and/or data taken from referenced transaction
this.revert();
// Revert seller's balance
Account seller = new Account(this.repository, this.nameData.getOwner());
seller.setConfirmedBalance(Asset.QORT, seller.getConfirmedBalance(Asset.QORT) - buyNameTransactionData.getAmount());
seller.modifyAssetBalance(Asset.QORT, - buyNameTransactionData.getAmount());
// Save reverted name data
this.repository.getNameRepository().save(this.nameData);

View File

@ -161,10 +161,10 @@ public class Payment {
long amount = paymentData.getAmount();
// Update sender's balance due to amount
sender.setConfirmedBalance(assetId, sender.getConfirmedBalance(assetId) - amount);
sender.modifyAssetBalance(assetId, - amount);
// Update recipient's balance
recipient.setConfirmedBalance(assetId, recipient.getConfirmedBalance(assetId) + amount);
recipient.modifyAssetBalance(assetId, amount);
}
}
@ -181,7 +181,7 @@ public class Payment {
Account sender = new PublicKeyAccount(this.repository, senderPublicKey);
// Update sender's balance due to fee
sender.setConfirmedBalance(Asset.QORT, sender.getConfirmedBalance(Asset.QORT) - fee);
sender.modifyAssetBalance(Asset.QORT, - fee);
// Update sender's reference
sender.setLastReference(signature);
@ -216,10 +216,10 @@ public class Payment {
long amount = paymentData.getAmount();
// Update sender's balance due to amount
sender.setConfirmedBalance(assetId, sender.getConfirmedBalance(assetId) + amount);
sender.modifyAssetBalance(assetId, amount);
// Update recipient's balance
recipient.setConfirmedBalance(assetId, recipient.getConfirmedBalance(assetId) - amount);
recipient.modifyAssetBalance(assetId, - amount);
}
}
@ -234,7 +234,7 @@ public class Payment {
Account sender = new PublicKeyAccount(this.repository, senderPublicKey);
// Update sender's balance due to fee
sender.setConfirmedBalance(Asset.QORT, sender.getConfirmedBalance(Asset.QORT) + fee);
sender.modifyAssetBalance(Asset.QORT, fee);
// Update sender's reference
sender.setLastReference(reference);

View File

@ -143,37 +143,6 @@ public class HSQLDBAccountRepository implements AccountRepository {
@Override
public void ensureAccount(AccountData accountData) throws DataException {
/*
* Why do we need to check/set the public_key?
* Is there something that sets an account's balance which also needs to set the public key?
byte[] publicKey = accountData.getPublicKey();
String sql = "SELECT public_key FROM Accounts WHERE account = ?";
try (ResultSet resultSet = this.repository.checkedExecute(sql, accountData.getAddress())) {
if (resultSet != null) {
// We know account record exists at this point.
// If accountData has no public key then we're done.
// If accountData's public key matches repository's public key then we're done.
if (publicKey == null || Arrays.equals(resultSet.getBytes(1), publicKey))
return;
}
// No record exists, or we have a public key to set
HSQLDBSaver saveHelper = new HSQLDBSaver("Accounts");
saveHelper.bind("account", accountData.getAddress());
if (publicKey != null)
saveHelper.bind("public_key", publicKey);
saveHelper.execute(this.repository);
} catch (SQLException e) {
throw new DataException("Unable to ensure minimal account in repository", e);
}
*/
String sql = "INSERT IGNORE INTO Accounts (account) VALUES (?)"; // MySQL syntax
try {
this.repository.checkedExecuteUpdateCount(sql, accountData.getAddress());

View File

@ -136,10 +136,10 @@ public class AtTransaction extends Transaction {
long assetId = this.atTransactionData.getAssetId();
// Update sender's balance due to amount
sender.setConfirmedBalance(assetId, sender.getConfirmedBalance(assetId) - amount);
sender.modifyAssetBalance(assetId, - amount);
// Update recipient's balance
recipient.setConfirmedBalance(assetId, recipient.getConfirmedBalance(assetId) + amount);
recipient.modifyAssetBalance(assetId, amount);
}
}
@ -170,10 +170,10 @@ public class AtTransaction extends Transaction {
long assetId = this.atTransactionData.getAssetId();
// Update sender's balance due to amount
sender.setConfirmedBalance(assetId, sender.getConfirmedBalance(assetId) + amount);
sender.modifyAssetBalance(assetId, amount);
// Update recipient's balance
recipient.setConfirmedBalance(assetId, recipient.getConfirmedBalance(assetId) - amount);
recipient.modifyAssetBalance(assetId, - amount);
}
// As AT_TRANSACTIONs are really part of a block, the caller (Block) will probably delete this transaction after orphaning

View File

@ -206,7 +206,7 @@ public class DeployAtTransaction extends Transaction {
// Update creator's balance regarding initial payment to AT
Account creator = getCreator();
creator.setConfirmedBalance(assetId, creator.getConfirmedBalance(assetId) - this.deployATTransactionData.getAmount());
creator.modifyAssetBalance(assetId, - this.deployATTransactionData.getAmount());
// Update AT's reference, which also creates AT account
Account atAccount = this.getATAccount();
@ -226,7 +226,7 @@ public class DeployAtTransaction extends Transaction {
// Update creator's balance regarding initial payment to AT
Account creator = getCreator();
creator.setConfirmedBalance(assetId, creator.getConfirmedBalance(assetId) + this.deployATTransactionData.getAmount());
creator.modifyAssetBalance(assetId, this.deployATTransactionData.getAmount());
// Delete AT's account (and hence its balance)
this.repository.getAccountRepository().delete(this.deployATTransactionData.getAtAddress());

View File

@ -905,7 +905,7 @@ public abstract class Transaction {
Account creator = getCreator();
// Update transaction creator's balance
creator.setConfirmedBalance(Asset.QORT, creator.getConfirmedBalance(Asset.QORT) - transactionData.getFee());
creator.modifyAssetBalance(Asset.QORT, - transactionData.getFee());
// Update transaction creator's reference (and possibly public key)
creator.setLastReference(transactionData.getSignature());
@ -929,7 +929,7 @@ public abstract class Transaction {
Account creator = getCreator();
// Update transaction creator's balance
creator.setConfirmedBalance(Asset.QORT, creator.getConfirmedBalance(Asset.QORT) + transactionData.getFee());
creator.modifyAssetBalance(Asset.QORT, transactionData.getFee());
// Update transaction creator's reference (and possibly public key)
creator.setLastReference(transactionData.getReference());

View File

@ -37,7 +37,7 @@ public class BIP39 {
bitShift = BITS_PER_WORD - bitShift;
} else {
// Leftover spread over next two bytes
bitShift = 0 - bitShift;
bitShift = - bitShift;
entropy[byteIndex++] |= (byte) (wordListIndex >> bitShift);
entropy[byteIndex] |= (byte) (wordListIndex << (8 - bitShift));