Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tested jail.go completely and tested distribution.go/DivvyingTips #172

Merged
merged 12 commits into from
Jun 6, 2024
48 changes: 15 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,21 @@ crypto-economically incentivized to honestly report requested data.

For more in-depth information, checkout the [Tellor Layer tech paper](https://github.com/tellor-io/layer/blob/main/TellorLayer%20-%20tech.pdf) and our [ADRs](https://github.com/tellor-io/layer/tree/main/adr).

## Starting the Chain (Without Ignite):

1) Remove old test chains (if present):
`rm -rf ~/.layer`
2) Go build layerd:
`go build ./cmd/layerd`
3) Initialize the chain:
`./layerd init layer --chain-id layer-test-1`
4) Add a validator account:
`./layerd keys add alice`
5) Create a tx to Give the alice loyas to stake:
`./layerd genesis add-genesis-account alice 10000000000000loya`
6) Create a tx to Stake some loyas for alice:
`./layerd genesis gentx alice 1000000000000loya --chain-id layer-test-1`
7) Add the transactions to the genesis block:
`./layerd genesis collect-gentxs`
8) Start the chain:
`./layerd start`

## Starting the Chain With Ignite CLI:

To start the chain locally with Ignite CLI:
`ignite chain serve`

To create a transaction, in another terminal:
`layerd tx [command]`

To see all available commands:
`layerd`
## Starting a New Chain:

1) Select the start script that works for you
- `start_one_node.sh` is for those who want to run a chain with a single validator in a mac environment
- `start_one_node_aws.sh` is for those who want a chain with a single validator and the option to import a faucet account from a seed phrase to be used in a linux environment
- `start_two_chains.sh` (mac environment) sets up two nodes/validators and starts one of them from this script. Then to start the other validator you would run the `start_bill.sh` script

2) Run the selected script from the base layer folder:
`sh ./start_scripts/{selected_script}`

## Joining a Running Chain:

To find more information please go to the layer_scripts folder.

Here you will find a detailed breakdown for how to join a chain as a node and how to create a new validator for the chain

## Tests

Expand Down Expand Up @@ -77,7 +63,3 @@ Check out our issues log here on Github or feel free to reach out anytime [info@
## Copyright<a name="copyright"> </a>

Tellor Inc. 2024

### Tools<a name="tools"> </a>

1. Install [golangci-lint](https://golangci-lint.run/usage/install/) 1.55.2
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
Loading