Skip to content

Commit

Permalink
AllotAllMana does not check if we met minimum required mana
Browse files Browse the repository at this point in the history
  • Loading branch information
daria305 committed Oct 13, 2023
1 parent 11c5505 commit ad2a52b
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions builder/transaction_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,24 +179,20 @@ func (b *TransactionBuilder) AllotRequiredManaAndStoreRemainingManaInOutput(targ
return b
}

func (b *TransactionBuilder) AllotAllMana(targetSlot iotago.SlotIndex, rmc iotago.Mana, blockIssuerAccountID iotago.AccountID) *TransactionBuilder {
// AllotAllMana allots all loose mana to the provided account, even if alloted value is less than required paymnet.
func (b *TransactionBuilder) AllotAllMana(targetSlot iotago.SlotIndex, blockIssuerAccountID iotago.AccountID) *TransactionBuilder {
setBuildError := func(err error) *TransactionBuilder {
b.occurredBuildErr = err
return b
}
// calculate the minimum required mana to issue the block
minRequiredMana, err := b.MinRequiredAllotedMana(b.api.ProtocolParameters().WorkScoreStructure(), rmc, blockIssuerAccountID)
if err != nil {
return setBuildError(ierrors.Wrap(err, "failed to calculate the minimum required mana to issue the block"))
}

unboundManaInputsLeftoverBalance, err := b.calculateMaximumPossibleAllotment(targetSlot, minRequiredMana, blockIssuerAccountID)
unboundManaInputsLeftoverBalance, err := b.calculateMaximumPossibleAllotment(targetSlot, 0, blockIssuerAccountID)
if err != nil {
return setBuildError(err)
}

// allot the mana to the block issuer account (we increase the value, so we don't interfere with the already alloted value)
b.IncreaseAllotment(blockIssuerAccountID, unboundManaInputsLeftoverBalance+minRequiredMana)
b.IncreaseAllotment(blockIssuerAccountID, unboundManaInputsLeftoverBalance)

return b
}
Expand All @@ -221,7 +217,7 @@ func (b *TransactionBuilder) calculateMaximumPossibleAllotment(targetSlot iotago
// subtract the remainder from the unbound mana
unboundManaInputs, err = safemath.SafeSub(unboundManaInputs, accountBoundManaOut-accountBalance)
if err != nil {
return ierrors.Wrapf(err, "not enough unbound mana on the input side for account %s", accountID.String())
return ierrors.Wrapf(err, "not enough unbound mana on the input side for account %s while subtracting remainder", accountID.String())
}

return nil
Expand All @@ -248,14 +244,14 @@ func (b *TransactionBuilder) calculateMaximumPossibleAllotment(targetSlot iotago
case *iotago.AccountOutput:
// mana on account outputs is locked to this account
if err = updateUnboundAndAccountBoundManaBalances(output.AccountID, output.StoredMana()); err != nil {
return 0, ierrors.Wrap(err, "failed to subtract the stored mana on the outputs side")
return 0, ierrors.Wrap(err, "failed to subtract the stored mana on the outputs side for account output")
}

default:
// check if the output locked mana to a certain account
if accountID, isManaLocked := b.hasManalockCondition(output); isManaLocked {
if err = updateUnboundAndAccountBoundManaBalances(accountID, output.StoredMana()); err != nil {
return 0, ierrors.Wrap(err, "failed to subtract the stored mana on the outputs side")
return 0, ierrors.Wrap(err, "failed to subtract the stored mana on the outputs side because of lock")
}
} else {
unboundManaInputs, err = safemath.SafeSub(unboundManaInputs, output.StoredMana())
Expand Down

0 comments on commit ad2a52b

Please sign in to comment.