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

test: cleanup, add random number dispute test #414

Merged
merged 10 commits into from
Oct 31, 2024
Merged
644 changes: 0 additions & 644 deletions tests/e2e/basic_dispute_test.go

This file was deleted.

1,652 changes: 1,652 additions & 0 deletions tests/e2e/dispute_test.go

Large diffs are not rendered by default.

620 changes: 0 additions & 620 deletions tests/e2e/e2e_test.go

Large diffs are not rendered by default.

File renamed without changes.
178 changes: 178 additions & 0 deletions tests/setup.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package setup

import (
"fmt"
"strconv"
"testing"
"time"
Expand All @@ -21,14 +22,19 @@ import (
bridgekeeper "github.com/tellor-io/layer/x/bridge/keeper"
_ "github.com/tellor-io/layer/x/dispute"
disputekeeper "github.com/tellor-io/layer/x/dispute/keeper"
disputetypes "github.com/tellor-io/layer/x/dispute/types"
_ "github.com/tellor-io/layer/x/mint"
mintkeeper "github.com/tellor-io/layer/x/mint/keeper"
_ "github.com/tellor-io/layer/x/oracle"
oraclekeeper "github.com/tellor-io/layer/x/oracle/keeper"
oracletypes "github.com/tellor-io/layer/x/oracle/types"
registrykeeper "github.com/tellor-io/layer/x/registry/keeper"
_ "github.com/tellor-io/layer/x/registry/module"
registrytypes "github.com/tellor-io/layer/x/registry/types"
reporterkeeper "github.com/tellor-io/layer/x/reporter/keeper"
_ "github.com/tellor-io/layer/x/reporter/module"
reportertypes "github.com/tellor-io/layer/x/reporter/types"
"golang.org/x/exp/rand"

appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
authmodulev1 "cosmossdk.io/api/cosmos/auth/module/v1"
Expand Down Expand Up @@ -309,6 +315,65 @@ func (s *SharedSetup) CreateValidators(numValidators int) ([]sdk.AccAddress, []s
return accountsAddrs, validatorsAddrs, validators
}

func (s *SharedSetup) CreateValidatorsRandomStake(numValidators int) ([]sdk.AccAddress, []sdk.ValAddress, []stakingtypes.Validator, []int64) {
require := s.require

// create account that will become a validator
accountsAddrs := simtestutil.CreateIncrementalAccounts(numValidators)
// mint 250k trb for each validator
maxTrb := int64(250_000)
initCoins := sdk.NewCoin(s.Denom, math.NewInt(maxTrb*1e6))
for _, acc := range accountsAddrs {
// mint to module
require.NoError(s.Bankkeeper.MintCoins(s.Ctx, authtypes.Minter, sdk.NewCoins(initCoins)))
// send from module to account
require.NoError(s.Bankkeeper.SendCoinsFromModuleToAccount(s.Ctx, authtypes.Minter, acc, sdk.NewCoins(initCoins)))
require.Equal(initCoins, s.Bankkeeper.GetBalance(s.Ctx, acc, s.Denom))
}

// get val address for each account
validatorsAddrs := simtestutil.ConvertAddrsToValAddrs(accountsAddrs)
// create pub keys for validators
pubKeys := simtestutil.CreateTestPubKeys(numValidators)
validators := make([]stakingtypes.Validator, numValidators)
stakingServer := stakingkeeper.NewMsgServerImpl(s.Stakingkeeper)
// set each account with proper keepers
stakes := make([]int64, numValidators)
for i, pubKey := range pubKeys {
s.Accountkeeper.NewAccountWithAddress(s.Ctx, accountsAddrs[i])
// pick random amount of trb between 1 and 200,000 to stake
rand := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
randAmt := (rand.Int63n(200_000 * 1e6))
stakes[i] = randAmt
randCoins := sdk.NewCoin(s.Denom, math.NewInt(randAmt))
// create msg for validator creation
valMsg, err := stakingtypes.NewMsgCreateValidator(
validatorsAddrs[i].String(),
pubKey,
randCoins,
stakingtypes.Description{Moniker: strconv.Itoa(i)},
stakingtypes.CommissionRates{
Rate: math.LegacyNewDecWithPrec(5, 1),
MaxRate: math.LegacyNewDecWithPrec(5, 1),
MaxChangeRate: math.LegacyNewDec(0),
},
math.OneInt())
require.NoError(err)
// create validator
_, err = stakingServer.CreateValidator(s.Ctx, valMsg)
require.NoError(err)
}
_, err := s.Stakingkeeper.EndBlocker(s.Ctx)
require.NoError(err)

for _, val := range validatorsAddrs {
err := s.Bridgekeeper.SetEVMAddressByOperator(s.Ctx, val.String(), []byte("evmAddr"))
require.NoError(err)
}

return accountsAddrs, validatorsAddrs, validators, stakes
}

func (s *SharedSetup) MintTokens(addr sdk.AccAddress, amount math.Int) {
require := s.require
Ctx := s.Ctx
Expand All @@ -324,3 +389,116 @@ func (s *SharedSetup) ConvertToAccAddress(priv []ed25519.PrivKey) []sdk.AccAddre
}
return testAddrs
}

func (s *SharedSetup) CreateReporter(ctx sdk.Context, accAddr sdk.AccAddress, commissionRate math.Uint, minTokensRequired math.Int) (reportertypes.OracleReporter, error) {
msgCreateReporter := reportertypes.MsgCreateReporter{
ReporterAddress: accAddr.String(),
CommissionRate: commissionRate,
MinTokensRequired: minTokensRequired,
}
msgServerReporter := reporterkeeper.NewMsgServerImpl(s.Reporterkeeper)
_, err := msgServerReporter.CreateReporter(ctx, &msgCreateReporter)
if err != nil {
fmt.Println("create reporter fail")
panic(err)
}

reporter, err := s.Reporterkeeper.Reporter(ctx, accAddr)
if err != nil {
fmt.Println("get reporter fail")
panic(err)
}
return reporter, nil
}

func (s *SharedSetup) DelegateAndSelect(msgServerStaking stakingtypes.MsgServer,
msgServerReporter reportertypes.MsgServer,
numLoya math.Int,
delegatorAccAddr sdk.AccAddress,
valAddr sdk.ValAddress,
reporterAccAddr sdk.AccAddress,
) {
msgDelegate := stakingtypes.MsgDelegate{
DelegatorAddress: delegatorAccAddr.String(),
ValidatorAddress: valAddr.String(),
Amount: sdk.NewCoin(s.Denom, numLoya),
}
_, err := msgServerStaking.Delegate(s.Ctx, &msgDelegate)
if err != nil {
fmt.Println("delegate fail")
panic(err)
}

msgSelectReporter := reportertypes.MsgSelectReporter{
SelectorAddress: delegatorAccAddr.String(),
ReporterAddress: reporterAccAddr.String(),
}
_, err = msgServerReporter.SelectReporter(s.Ctx, &msgSelectReporter)
if err != nil {
fmt.Println("select reporter fail")
panic(err)
}
}

func (s *SharedSetup) CreateFundedAccount(numTrb int64) (sdk.AccAddress, error) {
priv := secp256k1.GenPrivKey()
addr := sdk.AccAddress(priv.PubKey().Address())
s.MintTokens(addr, math.NewInt(numTrb*1e6))
return addr, nil
}

func (s *SharedSetup) CreateSpotPriceTip(ctx sdk.Context, tipperAccAddr sdk.AccAddress, parameters string, amountLoya math.Int) []byte {
req := &registrytypes.QueryGenerateQuerydataRequest{
Querytype: "SpotPrice",
Parameters: parameters,
}
res, err := s.Registrykeeper.GenerateQuerydata(ctx, req)
if err != nil {
panic(err)
}

msgTip := oracletypes.MsgTip{
Tipper: tipperAccAddr.String(),
QueryData: res.QueryData,
Amount: sdk.NewCoin(s.Denom, amountLoya),
}
oracleMsgServer := oraclekeeper.NewMsgServerImpl(s.Oraclekeeper)
_, err = oracleMsgServer.Tip(ctx, &msgTip)
if err != nil {
panic(err)
}

return res.QueryData
}

func (s *SharedSetup) Report(ctx sdk.Context, reporterAccAddr sdk.AccAddress, queryData []byte, reportValue string) {
msgSubmitValue := oracletypes.MsgSubmitValue{
Creator: reporterAccAddr.String(),
QueryData: queryData,
Value: reportValue,
}

oracleMsgServer := oraclekeeper.NewMsgServerImpl(s.Oraclekeeper)
_, err := oracleMsgServer.SubmitValue(ctx, &msgSubmitValue)
if err != nil {
fmt.Println("submit value fail")
panic(err)
}
}

func (s *SharedSetup) OpenDispute(ctx sdk.Context, disputerAccAddr sdk.AccAddress, report oracletypes.MicroReport, category disputetypes.DisputeCategory, fee math.Int, payFromBond bool) {
msgProposeDispute := disputetypes.MsgProposeDispute{
Creator: disputerAccAddr.String(),
Report: &report,
DisputeCategory: disputetypes.Warning,
Fee: sdk.NewCoin(s.Denom, fee),
PayFromBond: payFromBond,
}

msgServerDispute := disputekeeper.NewMsgServerImpl(s.Disputekeeper)
_, err := msgServerDispute.ProposeDispute(ctx, &msgProposeDispute)
if err != nil {
fmt.Println("propose dispute fail")
panic(err)
}
}
2 changes: 1 addition & 1 deletion x/dispute/keeper/dispute.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func (k Keeper) SetNewDispute(ctx sdk.Context, sender sdk.AccAddress, msg types.
if err != nil {
return err
}

if msg.Fee.Amount.GT(disputeFee) {
msg.Fee.Amount = disputeFee
}
Expand Down Expand Up @@ -112,6 +111,7 @@ func (k Keeper) SetNewDispute(ctx sdk.Context, sender sdk.AccAddress, msg types.
if err := k.PayDisputeFee(ctx, sender, msg.Fee, msg.PayFromBond, dispute.HashId); err != nil {
return err
}

// if the paid fee is equal to the slash amount, then slash validator and jail
if dispute.FeeTotal.Equal(dispute.SlashAmount) {
if err := k.SlashAndJailReporter(ctx, dispute.InitialEvidence, dispute.DisputeCategory, dispute.HashId); err != nil {
Expand Down
21 changes: 9 additions & 12 deletions x/oracle/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,20 @@ package oracle

import (
"context"
"fmt"

"github.com/tellor-io/layer/utils"
// "github.com/tellor-io/layer/utils"
"github.com/tellor-io/layer/x/oracle/keeper"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func EndBlocker(ctx context.Context, k keeper.Keeper) error {
currentHeight := sdk.UnwrapSDKContext(ctx).BlockHeight()
currentCycleListQuery, err := k.GetCurrentQueryInCycleList(ctx)
if err != nil {
return err
}
queryId := utils.QueryIDFromData(currentCycleListQuery)
fmt.Println("\ncurrentHeight:", currentHeight)
fmt.Println("queryId:", queryId)
// currentHeight := sdk.UnwrapSDKContext(ctx).BlockHeight()
// currentCycleListQuery, err := k.GetCurrentQueryInCycleList(ctx)
// if err != nil {
// return err
// }
// queryId := utils.QueryIDFromData(currentCycleListQuery)
// fmt.Println("\ncurrentHeight:", currentHeight)
// fmt.Println("queryId:", queryId)
// Rotate through the cycle list and set the current query index
if err := k.SetAggregatedReport(ctx); err != nil {
return err
Expand Down
1 change: 0 additions & 1 deletion x/oracle/keeper/msg_server_tip.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,5 @@ func (k msgServer) Tip(goCtx context.Context, msg *types.MsgTip) (*types.MsgTipR
sdk.NewAttribute("amount", tip.Amount.String()),
),
})
fmt.Println("tip added")
return &types.MsgTipResponse{}, nil
}
Loading