Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarom Swisa authored and Yarom Swisa committed Apr 14, 2024
1 parent 72233db commit bad6dac
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 55 deletions.
81 changes: 43 additions & 38 deletions x/dualstaking/keeper/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ func (k Keeper) increaseDelegation(ctx sdk.Context, delegator, provider, chainID
index = types.DelegatorKey(delegator)
_ = k.delegatorFS.FindEntry(ctx, index, nextEpoch, &delegatorEntry)

delegatorEntry.AddProvider(provider)

err = k.delegatorFS.AppendEntry(ctx, index, nextEpoch, &delegatorEntry)
if err != nil {
// append should never fail here
return utils.LavaFormatError("critical: append delegator entry", err,
utils.Attribute{Key: "delegator", Value: delegator},
utils.Attribute{Key: "provider", Value: provider},
utils.Attribute{Key: "chainID", Value: chainID},
)
contains := delegatorEntry.AddProvider(provider)
if !contains {
err = k.delegatorFS.AppendEntry(ctx, index, nextEpoch, &delegatorEntry)
if err != nil {
// append should never fail here
return utils.LavaFormatError("critical: append delegator entry", err,
utils.Attribute{Key: "delegator", Value: delegator},
utils.Attribute{Key: "provider", Value: provider},
utils.Attribute{Key: "chainID", Value: chainID},
)
}
}

if provider != types.EMPTY_PROVIDER {
Expand Down Expand Up @@ -140,27 +141,29 @@ func (k Keeper) decreaseDelegation(ctx sdk.Context, delegator, provider, chainID
// entry; and if the delegator entry becomes entry then remove it altogether.
// otherwise just append the new version (for next epoch).
if delegationEntry.Amount.IsZero() {
delegatorEntry.DelProvider(provider)
}
if delegatorEntry.IsEmpty() {
err := k.delegatorFS.DelEntry(ctx, index, nextEpoch)
if err != nil {
// delete should never fail here
return utils.LavaFormatError("critical: delete delegator entry", err,
utils.Attribute{Key: "delegator", Value: delegator},
utils.Attribute{Key: "provider", Value: provider},
utils.Attribute{Key: "chainID", Value: chainID},
)
}
} else {
err := k.delegatorFS.AppendEntry(ctx, index, nextEpoch, &delegatorEntry)
if err != nil {
// append should never fail here
return utils.LavaFormatError("failed to update delegator entry", err,
utils.Attribute{Key: "delegator", Value: delegator},
utils.Attribute{Key: "provider", Value: provider},
utils.Attribute{Key: "chainID", Value: chainID},
)
if len(k.GetAllProviderDelegatorDelegations(ctx, delegator, provider, nextEpoch)) == 0 {
delegatorEntry.DelProvider(provider)
if delegatorEntry.IsEmpty() {
err := k.delegatorFS.DelEntry(ctx, index, nextEpoch)
if err != nil {
// delete should never fail here
return utils.LavaFormatError("critical: delete delegator entry", err,
utils.Attribute{Key: "delegator", Value: delegator},
utils.Attribute{Key: "provider", Value: provider},
utils.Attribute{Key: "chainID", Value: chainID},
)
}
} else {
err := k.delegatorFS.AppendEntry(ctx, index, nextEpoch, &delegatorEntry)
if err != nil {
// append should never fail here
return utils.LavaFormatError("failed to update delegator entry", err,
utils.Attribute{Key: "delegator", Value: delegator},
utils.Attribute{Key: "provider", Value: provider},
utils.Attribute{Key: "chainID", Value: chainID},
)
}
}
}
}

Expand Down Expand Up @@ -468,14 +471,16 @@ func (k Keeper) GetAllProviderDelegatorDelegations(ctx sdk.Context, delegator, p
var delegations []types.Delegation
for _, ind := range indices {
var delegation types.Delegation
found := k.delegationFS.FindEntry(ctx, ind, epoch, &delegation)
_, deleted, _, found := k.delegationFS.FindEntryDetailed(ctx, ind, epoch, &delegation)
if !found {
provider, delegator, chainID := types.DelegationKeyDecode(ind)
utils.LavaFormatError("delegationFS entry index has no entry", fmt.Errorf("provider delegation not found"),
utils.Attribute{Key: "delegator", Value: delegator},
utils.Attribute{Key: "provider", Value: provider},
utils.Attribute{Key: "chainID", Value: chainID},
)
if !deleted {
provider, delegator, chainID := types.DelegationKeyDecode(ind)
utils.LavaFormatError("delegationFS entry index has no entry", fmt.Errorf("provider delegation not found"),
utils.Attribute{Key: "delegator", Value: delegator},
utils.Attribute{Key: "provider", Value: provider},
utils.Attribute{Key: "chainID", Value: chainID},
)
}
continue
}
delegations = append(delegations, delegation)
Expand Down
29 changes: 27 additions & 2 deletions x/dualstaking/keeper/delegate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,15 +377,25 @@ func TestUnbond(t *testing.T) {

// 1 delegator, 2 provider staked, 0 provider unstaked, 0 provider unstaking
ts.setupForDelegation(1, 2, 0, 0)
provider1Acct, provider1Addr := ts.GetAccount(common.PROVIDER, 0)

spec2 := ts.spec
spec2.Index = "mock2"
spec2.Name = spec2.Index
ts.AddSpec(spec2.Index, spec2)
err := ts.StakeProvider(provider1Addr, spec2, testStake)
require.NoError(t, err)

_, client1Addr := ts.GetAccount(common.CONSUMER, 0)
provider1Acct, provider1Addr := ts.GetAccount(common.PROVIDER, 0)

delegated := zeroCoin

// delegate once
amount := sdk.NewCoin(commontypes.TokenDenom, sdk.NewInt(10000))
_, err := ts.TxDualstakingDelegate(client1Addr, provider1Addr, ts.spec.Name, amount)
_, err = ts.TxDualstakingDelegate(client1Addr, provider1Addr, ts.spec.Name, amount)
require.NoError(t, err)

_, err = ts.TxDualstakingDelegate(client1Addr, provider1Addr, spec2.Index, amount)
require.NoError(t, err)

// advance epoch to digest the delegate
Expand All @@ -395,6 +405,10 @@ func TestUnbond(t *testing.T) {
stakeEntry := ts.getStakeEntry(provider1Acct.Addr, ts.spec.Name)
require.True(t, delegated.IsEqual(stakeEntry.DelegateTotal))

res, err := ts.QueryDualstakingDelegatorProviders(client1Addr, true)
require.NoError(t, err)
require.Len(t, res.Delegations, 2)

// unbond once
amount = sdk.NewCoin(commontypes.TokenDenom, sdk.NewInt(1000))
_, err = ts.TxDualstakingUnbond(client1Addr, provider1Addr, ts.spec.Name, amount)
Expand Down Expand Up @@ -437,6 +451,12 @@ func TestUnbond(t *testing.T) {
require.NoError(t, err)
stakeEntry = ts.getStakeEntry(provider1Acct.Addr, ts.spec.Name)
require.True(t, delegated.IsEqual(stakeEntry.DelegateTotal))
delegated = delegated.Sub(amount)

// unbond from unstaking provider everything
_, err = ts.TxDualstakingUnbond(client1Addr, provider1Addr, ts.spec.Name, delegated)
require.NoError(t, err)
stakeEntry = ts.getStakeEntry(provider1Acct.Addr, ts.spec.Name)

// advance half the blocks for the unbond hold-period (for sure > epoch)
ts.AdvanceBlocks(105)
Expand All @@ -448,6 +468,11 @@ func TestUnbond(t *testing.T) {
// not-blablacoins will have been returned to their delegator by now

ts.verifyDelegatorsBalance()

res, err = ts.QueryDualstakingDelegatorProviders(client1Addr, true)
require.NoError(t, err)
require.Len(t, res.Delegations, 1)
require.Equal(t, provider1Addr, res.Delegations[0].Provider)
}

func TestBondUnbondBond(t *testing.T) {
Expand Down
6 changes: 4 additions & 2 deletions x/dualstaking/types/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ func NewDelegator(delegator, provider string) Delegator {
}
}

func (delegator *Delegator) AddProvider(provider string) {
if !lavaslices.Contains(delegator.Providers, provider) {
func (delegator *Delegator) AddProvider(provider string) bool {
contains := lavaslices.Contains(delegator.Providers, provider)
if !contains {
delegator.Providers = append(delegator.Providers, provider)
}
return contains
}

func (delegator *Delegator) DelProvider(provider string) {
Expand Down
2 changes: 1 addition & 1 deletion x/fixationstore/types/fixationstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ func (fs *FixationStore) FindEntryDetailed(ctx sdk.Context, index string, block
// true in this case.

if !found || entry.IsDeletedBy(block) {
return 0, false, false, false
return 0, entry.IsDeletedBy(block), false, false
}

fs.cdc.MustUnmarshal(entry.GetData(), entryData)
Expand Down
12 changes: 0 additions & 12 deletions x/pairing/keeper/unstaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,6 @@ func (k Keeper) UnstakeEntry(ctx sdk.Context, validator, chainID, creator, unsta
)
}

// index might have changed in the unbond
existingEntry, found, indexInStakeStorage := k.epochStorageKeeper.GetStakeEntryByAddressCurrent(ctx, chainID, senderAddr)
if found {
err = k.epochStorageKeeper.RemoveStakeEntryCurrent(ctx, chainID, indexInStakeStorage)
if err != nil {
return utils.LavaFormatWarning("can't remove stake Entry, stake entry not found in index", err,
utils.Attribute{Key: "index", Value: indexInStakeStorage},
utils.Attribute{Key: "spec", Value: chainID},
)
}
}

details := map[string]string{
"address": existingEntry.GetAddress(),
"chainID": existingEntry.GetChain(),
Expand Down

0 comments on commit bad6dac

Please sign in to comment.