Skip to content

Commit

Permalink
Merge pull request #172 from tellor-io/caleb-reporter-testing
Browse files Browse the repository at this point in the history
Tested jail.go completely and tested distribution.go/DivvyingTips
  • Loading branch information
tkernell authored Jun 6, 2024
2 parents 3152ec3 + 991d882 commit a8de1c6
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 2 deletions.
102 changes: 102 additions & 0 deletions x/reporter/keeper/distribution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package keeper_test

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/require"
keepertest "github.com/tellor-io/layer/testutil/keeper"
"github.com/tellor-io/layer/testutil/sample"
"github.com/tellor-io/layer/x/reporter/types"

"cosmossdk.io/collections"
"cosmossdk.io/math"

simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func TestDivvyingTips(t *testing.T) {
k, _, _, ctx := keepertest.ReporterKeeper(t)
height := int64(10)
val1Address := sample.AccAddressBytes()
vals := simtestutil.ConvertAddrsToValAddrs([]sdk.AccAddress{val1Address})
val1 := vals[0]
addr := sample.AccAddressBytes()
addr2 := sample.AccAddressBytes()
updatedAt := time.Now().UTC()
commission := types.NewCommissionWithTime(types.DefaultMinCommissionRate, types.DefaultMinCommissionRate.MulInt(math.NewInt(2)), types.DefaultMinCommissionRate, updatedAt)
reporter1 := types.NewOracleReporter(addr.String(), math.NewInt(2000*1e6), &commission, 1)
ctx = ctx.WithBlockHeight(height)

err := k.Reporters.Set(ctx, addr, reporter1)
require.NoError(t, err)

tokenOrigin1 := &types.TokenOriginInfo{
DelegatorAddress: addr.Bytes(),
ValidatorAddress: val1.Bytes(),
Amount: math.NewInt(1000 * 1e6),
}

tokenOrigin2 := &types.TokenOriginInfo{
DelegatorAddress: addr2.Bytes(),
ValidatorAddress: val1.Bytes(),
Amount: math.NewInt(1000 * 1e6),
}
tokenOrigins := []*types.TokenOriginInfo{tokenOrigin1, tokenOrigin2}
total := tokenOrigin1.Amount.Add(tokenOrigin2.Amount)

delegationAmounts := types.DelegationsAmounts{TokenOrigins: tokenOrigins, Total: total}

err = k.Report.Set(ctx, collections.Join(addr.Bytes(), height), delegationAmounts)
require.NoError(t, err)

ctx = ctx.WithBlockHeight(12)
err = k.DivvyingTips(ctx, addr, math.NewInt(10*1e6), 10)
require.NoError(t, err)

ctx = ctx.WithBlockHeight(13)
del1, err := k.DelegatorTips.Get(ctx, addr.Bytes())
require.NoError(t, err)
del2, err := k.DelegatorTips.Get(ctx, addr2.Bytes())

fmt.Printf("delegator1: %v, delegator2: %v\r", del1, del2)
require.Equal(t, math.NewInt(5*1e6), del1)

require.NoError(t, err)
require.Equal(t, math.NewInt(5*1e6), del2)
}

// func TestReturnSlashedTokens(t *testing.T) {
// k, sk, bk, ctx := keepertest.ReporterKeeper(t)

// val1Address := sample.AccAddressBytes()
// vals := simtestutil.ConvertAddrsToValAddrs([]sdk.AccAddress{val1Address})
// val1 := vals[0]
// addr := sample.AccAddressBytes()
// addr2 := sample.AccAddressBytes()
// updatedAt := time.Now().UTC()
// commission := types.NewCommissionWithTime(types.DefaultMinCommissionRate, types.DefaultMinCommissionRate.MulInt(math.NewInt(2)), types.DefaultMinCommissionRate, updatedAt)
// reporter1 := types.NewOracleReporter(addr.String(), math.NewInt(2000*1e6), &commission)
// //reporter2 := types.NewOracleReporter(addr2.String(), math.NewInt(1000*1e6), &commission)

// err := k.Reporters.Set(ctx, addr, reporter1)
// require.NoError(t, err)

// tokenOrigin1 := &types.TokenOriginInfo{
// DelegatorAddress: addr.Bytes(),
// ValidatorAddress: val1.Bytes(),
// Amount: math.NewInt(1000 * 1e6),
// }

// tokenOrigin2 := &types.TokenOriginInfo{
// DelegatorAddress: addr2.Bytes(),
// ValidatorAddress: val1.Bytes(),
// Amount: math.NewInt(1000 * 1e6),
// }
// tokenOrigins := []*types.TokenOriginInfo{tokenOrigin1, tokenOrigin2}
// total := tokenOrigin1.Amount.Add(tokenOrigin2.Amount)

// delegationAmounts := types.DelegationsAmounts{TokenOrigins: tokenOrigins, Total: total}
// }
2 changes: 1 addition & 1 deletion x/reporter/keeper/jail.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (k Keeper) JailReporter(ctx context.Context, reporterAddr sdk.AccAddress, j
}

// remove a reporter from jail
func (k Keeper) unjailReporter(ctx context.Context, reporterAddr sdk.AccAddress, reporter types.OracleReporter) error {
func (k Keeper) UnjailReporter(ctx context.Context, reporterAddr sdk.AccAddress, reporter types.OracleReporter) error {
if !reporter.Jailed {
return types.ErrReporterNotJailed.Wrapf("cannot unjail already unjailed reporter, %v", reporter)
}
Expand Down
63 changes: 63 additions & 0 deletions x/reporter/keeper/jail_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package keeper_test

import (
"testing"
"time"

"github.com/stretchr/testify/require"
keepertest "github.com/tellor-io/layer/testutil/keeper"
"github.com/tellor-io/layer/testutil/sample"
"github.com/tellor-io/layer/x/reporter/types"

"cosmossdk.io/math"
)

func TestJailReporter(t *testing.T) {
k, _, _, ctx := keepertest.ReporterKeeper(t)
addr := sample.AccAddressBytes()
updatedAt := time.Now().UTC()
commission := types.NewCommissionWithTime(types.DefaultMinCommissionRate, types.DefaultMinCommissionRate.MulInt(math.NewInt(2)), types.DefaultMinCommissionRate, updatedAt)
reporter := types.NewOracleReporter(addr.String(), math.NewInt(1000*1e6), &commission, 1)

err := k.Reporters.Set(ctx, addr, reporter)
require.NoError(t, err)

ctx = ctx.WithBlockTime(updatedAt.Add(time.Second * 10))
jailedDuration := int64(100)

err = k.JailReporter(ctx, addr, jailedDuration)
require.NoError(t, err)

ctx = ctx.WithBlockTime(updatedAt.Add(time.Second * 15))
updatedReporter, err := k.Reporters.Get(ctx, addr)
require.NoError(t, err)
require.Equal(t, true, updatedReporter.Jailed)
require.Equal(t, updatedAt.Add(time.Second*110), updatedReporter.JailedUntil)
}

func TestUnJailReporter(t *testing.T) {
k, _, _, ctx := keepertest.ReporterKeeper(t)
addr := sample.AccAddressBytes()
jailedAt := time.Now().UTC()
commission := types.NewCommissionWithTime(types.DefaultMinCommissionRate, types.DefaultMinCommissionRate.MulInt(math.NewInt(2)), types.DefaultMinCommissionRate, jailedAt)
reporter := types.NewOracleReporter(addr.String(), math.NewInt(1000*1e6), &commission, 1)
reporter.Jailed = true
reporter.JailedUntil = jailedAt.Add(time.Second * 100)
ctx = ctx.WithBlockTime(jailedAt.Add(time.Second * 50))

// test unjailing reporter before the JailedUntil time
err := k.UnjailReporter(ctx, addr, reporter)
require.Error(t, err)

// test unjailing after enough time has passed
ctx = ctx.WithBlockTime(jailedAt.Add(time.Second * 505))
err = k.UnjailReporter(ctx, addr, reporter)
require.NoError(t, err)

updatedReporter, err := k.Reporters.Get(ctx, addr)
require.NoError(t, err)
require.Equal(t, false, updatedReporter.Jailed)

err = k.UnjailReporter(ctx, addr, updatedReporter)
require.Error(t, err)
}
2 changes: 1 addition & 1 deletion x/reporter/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (k msgServer) UnjailReporter(goCtx context.Context, msg *types.MsgUnjailRep
return nil, err
}

if err := k.Keeper.unjailReporter(ctx, reporterAddr, reporter); err != nil {
if err := k.Keeper.UnjailReporter(ctx, reporterAddr, reporter); err != nil {
return nil, err
}

Expand Down

0 comments on commit a8de1c6

Please sign in to comment.