Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ledger: disable minimum balance check when not validating or generating #2149

Merged
merged 4 commits into from
May 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 49 additions & 33 deletions ledger/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,49 @@ func (eval *BlockEvaluator) transactionGroup(txgroup []transactions.SignedTxnWit
return nil
}

// Check the minimum balance requirement for the modified accounts in `cow`.
func (eval *BlockEvaluator) checkMinBalance(cow *roundCowState) error {
rewardlvl := cow.rewardsLevel()
for _, addr := range cow.modifiedAccounts() {
// Skip FeeSink, RewardsPool, and CompactCertSender MinBalance checks here.
// There's only a few accounts, so space isn't an issue, and we don't
// expect them to have low balances, but if they do, it may cause
// surprises.
if addr == eval.block.FeeSink || addr == eval.block.RewardsPool ||
addr == transactions.CompactCertSender {
continue
}

data, err := cow.lookup(addr)
if err != nil {
return err
}

// It's always OK to have the account move to an empty state,
// because the accounts DB can delete it. Otherwise, we will
// enforce MinBalance.
if data.IsZero() {
continue
}

dataNew := data.WithUpdatedRewards(eval.proto, rewardlvl)
effectiveMinBalance := dataNew.MinBalance(&eval.proto)
if dataNew.MicroAlgos.Raw < effectiveMinBalance.Raw {
return fmt.Errorf("account %v balance %d below min %d (%d assets)",
addr, dataNew.MicroAlgos.Raw, effectiveMinBalance.Raw, len(dataNew.Assets))
}

// Check if we have exceeded the maximum minimum balance
if eval.proto.MaximumMinimumBalance != 0 {
if effectiveMinBalance.Raw > eval.proto.MaximumMinimumBalance {
return fmt.Errorf("account %v would use too much space after this transaction. Minimum balance requirements would be %d (greater than max %d)", addr, effectiveMinBalance.Raw, eval.proto.MaximumMinimumBalance)
}
}
}

return nil
}

// transaction tentatively executes a new transaction as part of this block evaluation.
// If the transaction cannot be added to the block without violating some constraints,
// an error is returned and the block evaluator state is unchanged.
Expand Down Expand Up @@ -837,40 +880,13 @@ func (eval *BlockEvaluator) transaction(txn transactions.SignedTxn, evalParams *

// Check if any affected accounts dipped below MinBalance (unless they are
// completely zero, which means the account will be deleted.)
rewardlvl := cow.rewardsLevel()
for _, addr := range cow.modifiedAccounts() {
// Skip FeeSink, RewardsPool, and CompactCertSender MinBalance checks here.
// There's only a few accounts, so space isn't an issue, and we don't
// expect them to have low balances, but if they do, it may cause
// surprises.
if addr == spec.FeeSink || addr == spec.RewardsPool || addr == transactions.CompactCertSender {
continue
}

data, err := cow.lookup(addr)
// Only do those checks if we are validating or generating. It is useful to skip them
// if we cannot provide account data that contains enough information to
// compute the correct minimum balance (the case with indexer which does not store it).
if eval.validate || eval.generate {
err := eval.checkMinBalance(cow)
if err != nil {
return err
}

// It's always OK to have the account move to an empty state,
// because the accounts DB can delete it. Otherwise, we will
// enforce MinBalance.
if data.IsZero() {
continue
}

dataNew := data.WithUpdatedRewards(eval.proto, rewardlvl)
effectiveMinBalance := dataNew.MinBalance(&eval.proto)
if dataNew.MicroAlgos.Raw < effectiveMinBalance.Raw {
return fmt.Errorf("transaction %v: account %v balance %d below min %d (%d assets)",
txid, addr, dataNew.MicroAlgos.Raw, effectiveMinBalance.Raw, len(dataNew.Assets))
}

// Check if we have exceeded the maximum minimum balance
if eval.proto.MaximumMinimumBalance != 0 {
if effectiveMinBalance.Raw > eval.proto.MaximumMinimumBalance {
return fmt.Errorf("transaction %v: account %v would use too much space after this transaction. Minimum balance requirements would be %d (greater than max %d)", txid, addr, effectiveMinBalance.Raw, eval.proto.MaximumMinimumBalance)
}
return fmt.Errorf("transaction %v: %w", txid, err)
}
}

Expand Down