Cache the online accounts validation result, to speed up block minting.

This commit is contained in:
CalDescent 2023-08-12 10:24:55 +01:00
parent 9574100a08
commit d9147b3af3
2 changed files with 20 additions and 0 deletions

View File

@ -130,6 +130,9 @@ public class Block {
/** Locally-generated AT fees */ /** Locally-generated AT fees */
protected long ourAtFees; // Generated locally protected long ourAtFees; // Generated locally
/** Cached online accounts validation decision, to avoid revalidating when true */
private boolean onlineAccountsAlreadyValid = false;
@FunctionalInterface @FunctionalInterface
private interface BlockRewardDistributor { private interface BlockRewardDistributor {
long distribute(long amount, Map<String, Long> balanceChanges) throws DataException; long distribute(long amount, Map<String, Long> balanceChanges) throws DataException;
@ -563,6 +566,13 @@ public class Block {
} }
/**
* Force online accounts to be revalidated, e.g. at final stage of block minting.
*/
public void clearOnlineAccountsValidationCache() {
this.onlineAccountsAlreadyValid = false;
}
// More information // More information
/** /**
@ -1043,6 +1053,10 @@ public class Block {
if (this.blockData.getHeight() != null && this.blockData.getHeight() == 1) if (this.blockData.getHeight() != null && this.blockData.getHeight() == 1)
return ValidationResult.OK; return ValidationResult.OK;
// Don't bother revalidating if accounts have already been validated in this block
if (this.onlineAccountsAlreadyValid)
return ValidationResult.OK;
// Expand block's online accounts indexes into actual accounts // Expand block's online accounts indexes into actual accounts
ConciseSet accountIndexes = BlockTransformer.decodeOnlineAccounts(this.blockData.getEncodedOnlineAccounts()); ConciseSet accountIndexes = BlockTransformer.decodeOnlineAccounts(this.blockData.getEncodedOnlineAccounts());
// We use count of online accounts to validate decoded account indexes // We use count of online accounts to validate decoded account indexes
@ -1130,6 +1144,9 @@ public class Block {
// All online accounts valid, so save our list of online accounts for potential later use // All online accounts valid, so save our list of online accounts for potential later use
this.cachedOnlineRewardShares = onlineRewardShares; this.cachedOnlineRewardShares = onlineRewardShares;
// Remember that the accounts are valid, to speed up subsequent checks
this.onlineAccountsAlreadyValid = true;
return ValidationResult.OK; return ValidationResult.OK;
} }

View File

@ -562,6 +562,9 @@ public class BlockMinter extends Thread {
// Sign to create block's signature // Sign to create block's signature
newBlock.sign(); newBlock.sign();
// Ensure online accounts are fully re-validated in this final check
newBlock.clearOnlineAccountsValidationCache();
// Is newBlock still valid? // Is newBlock still valid?
ValidationResult validationResult = newBlock.isValid(); ValidationResult validationResult = newBlock.isValid();
if (validationResult != ValidationResult.OK) if (validationResult != ValidationResult.OK)