Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yarom Swisa authored and Yarom Swisa committed Sep 23, 2024
1 parent 450b257 commit 4eaba69
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 6 deletions.
3 changes: 2 additions & 1 deletion x/dualstaking/keeper/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,9 @@ func TestUndelegateProvider(t *testing.T) {
err = ts.StakeProvider(providerAcct.GetVaultAddr(), providerAcct.Addr.String(), ts.spec, sdk.NewIntFromUint64(1000).Int64())
require.NoError(t, err)

_, found = ts.Keepers.Epochstorage.GetStakeEntryCurrent(ts.Ctx, ts.spec.Index, provider)
entry, found := ts.Keepers.Epochstorage.GetStakeEntryCurrent(ts.Ctx, ts.spec.Index, provider)
require.True(t, found)
require.Equal(t, int64(9999), entry.DelegateTotal.Amount.Int64())

// delegator1 should be able to redelegate back to the empty provider
_, err = ts.TxDualstakingRedelegate(delegatorAcc1.Addr.String(),
Expand Down
18 changes: 13 additions & 5 deletions x/pairing/keeper/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
pairingtypes "github.com/lavanet/lava/v3/x/pairing/types"
planstypes "github.com/lavanet/lava/v3/x/plans/types"
rewardstypes "github.com/lavanet/lava/v3/x/rewards/types"
"github.com/lavanet/lava/v3/x/spec/types"
spectypes "github.com/lavanet/lava/v3/x/spec/types"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -73,24 +74,30 @@ func (ts *tester) addValidators(count int) {
// addProvider: with default endpoints, geolocation, moniker
func (ts *tester) addProvider(count int) error {
d := common.MockDescription()
return ts.addProviderExtra(count, nil, 0, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details) // default: endpoints, geolocation, moniker
return ts.addProviderExtra(count, nil, 0, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details, ts.spec) // default: endpoints, geolocation, moniker
}

// addProvider: with default endpoints, geolocation, moniker
func (ts *tester) addProviderSpec(count int, spec string) error {
d := common.MockDescription()
return ts.addProviderExtra(count, nil, 0, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details, ts.Spec(spec))
}

// addProviderGelocation: with geolocation, and default endpoints, moniker
func (ts *tester) addProviderGeolocation(count int, geolocation int32) error {
d := common.MockDescription()
return ts.addProviderExtra(count, nil, geolocation, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details)
return ts.addProviderExtra(count, nil, geolocation, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details, ts.spec)
}

// addProviderEndpoints: with endpoints, and default geolocation, moniker
func (ts *tester) addProviderEndpoints(count int, endpoints []epochstoragetypes.Endpoint) error {
d := common.MockDescription()
return ts.addProviderExtra(count, endpoints, 0, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details)
return ts.addProviderExtra(count, endpoints, 0, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details, ts.spec)
}

// addProviderDescription: with description, and default endpoints, geolocation
func (ts *tester) addProviderDescription(count int, moniker string, identity string, website string, securityContact string, descriptionDetails string) error {
return ts.addProviderExtra(count, nil, 0, moniker, identity, website, securityContact, descriptionDetails)
return ts.addProviderExtra(count, nil, 0, moniker, identity, website, securityContact, descriptionDetails, ts.spec)
}

// addProviderExtra: with mock endpoints, and preset geolocation, description details
Expand All @@ -103,11 +110,12 @@ func (ts *tester) addProviderExtra(
website string,
securityContact string,
descriptionDetails string,
spec types.Spec,
) error {
start := len(ts.Accounts(common.PROVIDER))
for i := 0; i < count; i++ {
acc, addr := ts.AddAccount(common.PROVIDER, start+i, testBalance)
err := ts.StakeProviderExtra(acc.GetVaultAddr(), addr, ts.spec, testStake, endpoints, geoloc, moniker, identity, website, securityContact, descriptionDetails)
err := ts.StakeProviderExtra(acc.GetVaultAddr(), addr, spec, testStake, endpoints, geoloc, moniker, identity, website, securityContact, descriptionDetails)
if err != nil {
return err
}
Expand Down
164 changes: 164 additions & 0 deletions x/pairing/keeper/single_provider_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package keeper_test

import (
"strconv"
"testing"

"github.com/cosmos/cosmos-sdk/types"
"github.com/lavanet/lava/v3/testutil/common"
"github.com/stretchr/testify/require"
)

func SpecName(num int) string {
return "spec" + strconv.Itoa(num)
}

func SetupForSingleProviderTests(ts *tester, providers, specs, clientsCount int) {
for i := 0; i < providers; i++ {
ts.AddAccount(common.PROVIDER, i, testBalance)
}

for i := 0; i < specs; i++ {
spec := ts.spec
spec.Index = SpecName(i)
ts.AddSpec(spec.Index, spec)
for i := 0; i < providers; i++ {
acc, addr := ts.GetAccount(common.PROVIDER, i)
d := common.MockDescription()
err := ts.StakeProviderExtra(acc.GetVaultAddr(), addr, spec, testStake, nil, 0, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details)
require.NoError(ts.T, err)
}
}

ts.addClient(clientsCount)
ts.AdvanceEpoch()
}

// * unstake to see the delegations distributions
// * unstake from the rest of the chains
// * stake to get all delegations back
func TestUnstakeStake(t *testing.T) {
ts := newTester(t)
SetupForSingleProviderTests(ts, 1, 5, 0)

delegator, _ := ts.AddAccount("del", 1, 1000000000)
provider0, _ := ts.GetAccount(common.PROVIDER, 0)

_, err := ts.TxDualstakingDelegate(delegator.Addr.String(), provider0.Addr.String(), types.NewInt64Coin(ts.TokenDenom(), 5000))
require.NoError(t, err)

for i := 0; i < 5; i++ {
res, err := ts.QueryPairingProvider(provider0.Addr.String(), SpecName(i))
require.NoError(t, err)
require.Equal(t, int64(1000), res.StakeEntries[0].DelegateTotal.Amount.Int64())
}

_, err = ts.TxPairingUnstakeProvider(provider0.GetVaultAddr(), SpecName(0))
require.NoError(t, err)

for i := 1; i < 5; i++ {
res, err := ts.QueryPairingProvider(provider0.Addr.String(), SpecName(i))
require.NoError(t, err)
require.Equal(t, int64(1250), res.StakeEntries[0].DelegateTotal.Amount.Int64())
}

for i := 1; i < 5; i++ {
_, err = ts.TxPairingUnstakeProvider(provider0.GetVaultAddr(), SpecName(i))
require.NoError(t, err)
}

res, err := ts.QueryDualstakingDelegatorProviders(delegator.Addr.String())
require.NoError(t, err)
require.Len(t, res.Delegations, 1)
require.Equal(t, provider0.Addr.String(), res.Delegations[0].Provider)
require.Equal(t, int64(5000), res.Delegations[0].Amount.Amount.Int64())

d := common.MockDescription()
err = ts.StakeProviderExtra(provider0.GetVaultAddr(), provider0.Addr.String(), ts.Spec(SpecName(0)), testStake, nil, 0, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details)
require.NoError(ts.T, err)

res1, err := ts.QueryPairingProvider(provider0.Addr.String(), SpecName(0))
require.NoError(t, err)
require.Equal(t, int64(5000), res1.StakeEntries[0].DelegateTotal.Amount.Int64())

err = ts.StakeProviderExtra(provider0.GetVaultAddr(), provider0.Addr.String(), ts.Spec(SpecName(1)), testStake, nil, 0, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details)
require.NoError(ts.T, err)

res1, err = ts.QueryPairingProvider(provider0.Addr.String(), SpecName(0))
require.NoError(t, err)
require.Equal(t, int64(2500), res1.StakeEntries[0].DelegateTotal.Amount.Int64())

res1, err = ts.QueryPairingProvider(provider0.Addr.String(), SpecName(1))
require.NoError(t, err)
require.Equal(t, int64(2500), res1.StakeEntries[0].DelegateTotal.Amount.Int64())
}

func TestDelegations(t *testing.T) {
ts := newTester(t)
SetupForSingleProviderTests(ts, 1, 5, 0)

delegator, _ := ts.AddAccount("del", 1, 1000000000)
provider, _ := ts.GetAccount(common.PROVIDER, 0)

// delegate amount that does not divide by 5
_, err := ts.TxDualstakingDelegate(delegator.Addr.String(), provider.Addr.String(), types.NewInt64Coin(ts.TokenDenom(), 4999))
require.NoError(t, err)

for i := 0; i < 5; i++ {
res, err := ts.QueryPairingProvider(provider.Addr.String(), SpecName(i))
require.NoError(t, err)
require.Equal(t, int64(999), res.StakeEntries[0].DelegateTotal.Amount.Int64())
}

// unbond 4 tokens, now amount divides by 5
_, err = ts.TxDualstakingUnbond(delegator.Addr.String(), provider.Addr.String(), types.NewInt64Coin(ts.TokenDenom(), 4))
require.NoError(t, err)

for i := 0; i < 5; i++ {
res, err := ts.QueryPairingProvider(provider.Addr.String(), SpecName(i))
require.NoError(t, err)
require.Equal(t, int64(999), res.StakeEntries[0].DelegateTotal.Amount.Int64())
}

// add stake to spec0
d := common.MockDescription()
err = ts.StakeProviderExtra(provider.GetVaultAddr(), provider.Addr.String(), ts.Spec(SpecName(0)), 2*testStake, nil, 0, d.Moniker, d.Identity, d.Website, d.SecurityContact, d.Details)
require.NoError(ts.T, err)

// check delegate total is twice than others in spec0
res, err := ts.QueryPairingProvider(provider.Addr.String(), SpecName(0))
require.NoError(t, err)
require.Equal(t, int64(1665), res.StakeEntries[0].DelegateTotal.Amount.Int64())

for i := 1; i < 5; i++ {
res, err := ts.QueryPairingProvider(provider.Addr.String(), SpecName(i))
require.NoError(t, err)
require.Equal(t, int64(832), res.StakeEntries[0].DelegateTotal.Amount.Int64())
}

// unbond with vault, should be uniformali
_, err = ts.TxDualstakingUnbond(provider.GetVaultAddr(), provider.Addr.String(), types.NewInt64Coin(ts.TokenDenom(), testStake))
require.NoError(t, err)

res, err = ts.QueryPairingProvider(provider.Addr.String(), SpecName(0))
require.NoError(t, err)
require.Equal(t, testStake+testStake*4/5, res.StakeEntries[0].Stake.Amount.Int64())
require.Equal(t, 4995*res.StakeEntries[0].Stake.Amount.Int64()/(5*testStake), res.StakeEntries[0].DelegateTotal.Amount.Int64())

for i := 1; i < 5; i++ {
res, err := ts.QueryPairingProvider(provider.Addr.String(), SpecName(i))
require.NoError(t, err)
require.Equal(t, testStake*4/5, res.StakeEntries[0].Stake.Amount.Int64())
require.Equal(t, 4995*res.StakeEntries[0].Stake.Amount.Int64()/(5*testStake), res.StakeEntries[0].DelegateTotal.Amount.Int64())
}

// unbond all delegator
_, err = ts.TxDualstakingUnbond(delegator.Addr.String(), provider.Addr.String(), types.NewInt64Coin(ts.TokenDenom(), 4995))
require.NoError(t, err)

for i := 0; i < 5; i++ {
res, err := ts.QueryPairingProvider(provider.Addr.String(), SpecName(i))
require.NoError(t, err)
require.Equal(t, int64(0), res.StakeEntries[0].DelegateTotal.Amount.Int64())
}
}

0 comments on commit 4eaba69

Please sign in to comment.