Skip to content

Commit

Permalink
Merge pull request #910 from iotaledger/fix/bic-underflow
Browse files Browse the repository at this point in the history
BIC underflow
  • Loading branch information
muXxer authored Apr 15, 2024
2 parents 7426466 + dd25892 commit bea8598
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 1 addition & 2 deletions pkg/protocol/engine/accounts/accountsledger/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,12 +531,11 @@ func (m *Manager) commitAccountTree(slot iotago.SlotIndex, accountDiffChanges ma

if diffChange.BICChange != 0 || !exists {
// decay the credits to the current slot if the account exists
if exists {
if exists && accountData.Credits.Value > 0 {
decayedPreviousCredits, err := m.apiProvider.APIForSlot(slot).ManaDecayProvider().DecayManaBySlots(iotago.Mana(accountData.Credits.Value), accountData.Credits.UpdateSlot, slot)
if err != nil {
return ierrors.Wrapf(err, "can't retrieve account, could not decay credits for account %s in slot %d", accountData.ID, slot)
}

// update the account data diff taking into account the decay, the modified diff will be stored in the calling
// ApplyDiff function to be able to properly rollback the account to a previous slot.
diffChange.BICChange -= accountData.Credits.Value - iotago.BlockIssuanceCredits(decayedPreviousCredits)
Expand Down
12 changes: 8 additions & 4 deletions pkg/tests/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,10 @@ func Test_NegativeBIC_BlockIssuerLocked(t *testing.T) {
iotago.SlotIndex(0),
testsuite.GenesisTimeWithOffsetBySlots(iotago.SlotIndex(200), testsuite.DefaultSlotDurationInSeconds),
testsuite.DefaultSlotDurationInSeconds,
testsuite.DefaultSlotsPerEpochExponent,
3,
),
iotago.WithLivenessOptions(
10, 10, 2, 4, 5,
),
),
)
Expand Down Expand Up @@ -588,6 +591,7 @@ func Test_NegativeBIC_BlockIssuerLocked(t *testing.T) {
// Allot some mana to the locked account to unlock it.
// The locked wallet 2 is preparing and signs the transaction, but it's issued by wallet 1 whose account is not locked.
{
latestParents = ts.CommitUntilSlot(block3Slot+ts.API.TimeProvider().EpochDurationSlots(), ts.BlockID("block2.1"))
allottedBIC := testPayloadCost
tx1 := wallet2.AllotManaFromInputs("TX1",
iotago.Allotments{&iotago.Allotment{
Expand All @@ -608,15 +612,15 @@ func Test_NegativeBIC_BlockIssuerLocked(t *testing.T) {

ts.AssertAccountData(&accounts.AccountData{
ID: wallet1.BlockIssuer.AccountData.ID,
Credits: accounts.NewBlockIssuanceCredits(wallet1BIC, block3Slot),
Credits: accounts.NewBlockIssuanceCredits(wallet1BIC, block31.ID().Slot()),
OutputID: wallet1OutputID,
ExpirySlot: iotago.MaxSlotIndex,
BlockIssuerKeys: wallet1.BlockIssuer.BlockIssuerKeys(),
}, ts.Nodes()...)

ts.AssertAccountData(&accounts.AccountData{
ID: wallet2.BlockIssuer.AccountData.ID,
Credits: accounts.NewBlockIssuanceCredits(wallet2BIC, block3Slot),
Credits: accounts.NewBlockIssuanceCredits(wallet2BIC, block31.ID().Slot()),
ExpirySlot: iotago.MaxSlotIndex,
OutputID: wallet2OutputID,
BlockIssuerKeys: wallet2.BlockIssuer.BlockIssuerKeys(),
Expand All @@ -639,7 +643,7 @@ func Test_NegativeBIC_BlockIssuerLocked(t *testing.T) {

ts.AssertAccountData(&accounts.AccountData{
ID: wallet1.BlockIssuer.AccountData.ID,
Credits: accounts.NewBlockIssuanceCredits(wallet1BIC, block3Slot),
Credits: accounts.NewBlockIssuanceCredits(wallet1BIC, ts.BlockID("block3.1").Slot()),
OutputID: wallet1OutputID,
ExpirySlot: iotago.MaxSlotIndex,
BlockIssuerKeys: wallet1.BlockIssuer.BlockIssuerKeys(),
Expand Down

0 comments on commit bea8598

Please sign in to comment.