Skip to content

Commit

Permalink
fixup: check account balance + faucet
Browse files Browse the repository at this point in the history
  • Loading branch information
altergui committed Sep 6, 2024
1 parent 02e6e13 commit fb487cb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
14 changes: 14 additions & 0 deletions vochain/state/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,20 @@ func (v *State) GetAccount(address common.Address, committed bool) (*Account, er
return &acc, acc.Unmarshal(raw)
}

// GetAccountBalance retrieves the Account.Balance for an address.
// Returns 0 and no error if the account does not exist.
// Committed is relative to the state on which the function is executed.
func (v *State) GetAccountBalance(address common.Address, committed bool) (uint64, error) {
acc, err := v.GetAccount(address, committed)
if err != nil {
return 0, err
}
if acc == nil {
return 0, nil
}
return acc.Balance, nil
}

// CountAccounts returns the overall number of accounts the vochain has
func (v *State) CountAccounts(committed bool) (uint64, error) {
// TODO: Once statedb.TreeView.Size() works, replace this by that.
Expand Down
11 changes: 11 additions & 0 deletions vochain/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,17 @@ func (t *TransactionHandler) checkFaucetPackageAndTransfer(txFaucetPackage *mode
)
}

balance, err := t.state.GetAccountBalance(txSenderAddress, false)
if err != nil {
return false, fmt.Errorf("cannot get tx sender balance: %w", err)
}
if balance+faucetPayload.Amount < txCost {
return false, fmt.Errorf(
"account balance (%d) + faucet amount (%d) is not enough to pay the tx cost %d",
balance, faucetPayload.Amount, txCost,
)
}

// if forCommit, then the cost of the transaction is consumed from the faucet
if forCommit {
if err := t.state.ConsumeFaucetPayload(
Expand Down

0 comments on commit fb487cb

Please sign in to comment.