diff --git a/tests/e2e/e2e_test.go b/tests/e2e/e2e_test.go index 2cfc3c7ef..da7a4ef86 100644 --- a/tests/e2e/e2e_test.go +++ b/tests/e2e/e2e_test.go @@ -1,6 +1,8 @@ package e2e_test import ( + "encoding/hex" + "fmt" "math/big" "math/rand" "strconv" @@ -12,15 +14,22 @@ import ( simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" + govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" + + collections "cosmossdk.io/collections" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/tellor-io/layer/utils" disputekeeper "github.com/tellor-io/layer/x/dispute/keeper" disputetypes "github.com/tellor-io/layer/x/dispute/types" minttypes "github.com/tellor-io/layer/x/mint/types" oraclekeeper "github.com/tellor-io/layer/x/oracle/keeper" - "github.com/tellor-io/layer/x/oracle/types" oracletypes "github.com/tellor-io/layer/x/oracle/types" oracleutils "github.com/tellor-io/layer/x/oracle/utils" + registrykeeper "github.com/tellor-io/layer/x/registry/keeper" + registrytypes "github.com/tellor-io/layer/x/registry/types" reporterkeeper "github.com/tellor-io/layer/x/reporter/keeper" reportertypes "github.com/tellor-io/layer/x/reporter/types" ) @@ -96,7 +105,9 @@ func (s *E2ETestSuite) TestTransferAfterMint() { func (s *E2ETestSuite) TestValidateCycleList() { require := s.Require() - // height 0 + //--------------------------------------------------------------------------- + // Height 0 - get initial cycle list query + //--------------------------------------------------------------------------- _, err := s.app.BeginBlocker(s.ctx) require.NoError(err) firstInCycle, err := s.oraclekeeper.GetCurrentQueryInCycleList(s.ctx) @@ -108,7 +119,9 @@ func (s *E2ETestSuite) TestValidateCycleList() { _, err = s.app.EndBlocker(s.ctx) require.NoError(err) - // height 1 + //--------------------------------------------------------------------------- + // Height 1 - get second cycle list query + //--------------------------------------------------------------------------- s.ctx = s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1) _, err = s.app.BeginBlocker(s.ctx) require.NoError(err) @@ -121,7 +134,9 @@ func (s *E2ETestSuite) TestValidateCycleList() { _, err = s.app.EndBlocker(s.ctx) require.NoError(err) - // height 2 + //--------------------------------------------------------------------------- + // Height 2 - get third cycle list query + //--------------------------------------------------------------------------- s.ctx = s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1) _, err = s.app.BeginBlocker(s.ctx) require.NoError(err) @@ -134,7 +149,7 @@ func (s *E2ETestSuite) TestValidateCycleList() { _, err = s.app.EndBlocker(s.ctx) require.NoError(err) - // loop through more times + // loop through 20 more blocks list, err := s.oraclekeeper.GetCyclelist(s.ctx) require.NoError(err) for i := 0; i < 20; i++ { @@ -824,7 +839,7 @@ func (s *E2ETestSuite) TestDisputes() { // mint 5000*1e6 tokens for reporter s.NoError(s.bankKeeper.MintCoins(s.ctx, authtypes.Minter, sdk.NewCoins(initCoins))) s.NoError(s.bankKeeper.SendCoinsFromModuleToAccount(s.ctx, authtypes.Minter, reporterAccount, sdk.NewCoins(initCoins))) - // delegate to validator so reporter can delegate to themselves + // delegate 5k trb to validator so reporter can delegate to themselves reporterDelToVal := Delegator{delegatorAddress: reporterAccount, validator: validator, tokenAmount: math.NewInt(5000 * 1e6)} _, err = s.stakingKeeper.Delegate(s.ctx, reporterDelToVal.delegatorAddress, reporterDelToVal.tokenAmount, stakingtypes.Unbonded, reporterDelToVal.validator, true) require.NoError(err) @@ -836,7 +851,7 @@ func (s *E2ETestSuite) TestDisputes() { err = s.distrKeeper.Hooks().AfterDelegationModified(s.ctx, reporterAccount, valAccountValAddrs[0]) require.NoError(err) - // define createReporterMsg params + // self delegate in reporter module with 4k trb var createReporterMsg reportertypes.MsgCreateReporter reporterAddress := reporterDelToVal.delegatorAddress.String() amount := math.NewInt(4000 * 1e6) @@ -912,9 +927,6 @@ func (s *E2ETestSuite) TestDisputes() { require.Equal(int64(4000000000), result.Report.ReporterPower) require.Equal(int64(1), result.Report.Height) - // votingPower := reporter.TotalTokens.Quo(layertypes.PowerReduction).Int64() - // fmt.Println("voting power: ", votingPower) - //--------------------------------------------------------------------------- // Height 2 - create a dispute //--------------------------------------------------------------------------- @@ -934,7 +946,7 @@ func (s *E2ETestSuite) TestDisputes() { // todo: is there a getter for this ? // get microreport for dispute - report := types.MicroReport{ + report := oracletypes.MicroReport{ Reporter: reporter.Reporter, Power: reporter.TotalTokens.Int64(), QueryId: queryId, @@ -986,10 +998,6 @@ func (s *E2ETestSuite) TestDisputes() { // reporter lost 1% of their free floating tokens freeFloatingBalanceAfter := s.bankKeeper.GetBalance(s.ctx, reporterAccount, s.denom) require.Equal(freeFloatingBalanceAfter, freeFloatingBalanceBefore.Sub(disputeFee)) - // disputeModAcct := s.accountKeeper.GetModuleAddress(disputetypes.ModuleName) - // disputeModAcctBal := s.bankKeeper.GetBalance(s.ctx, disputeModAcct, s.denom) - // fmt.Println("dispute Module Acct Bal: ", disputeModAcctBal) - // require.Equal(disputeModAcctBal.Amount, disputeFee.Amount) // create msgUnJailReporter msgUnjailReporter := reportertypes.MsgUnjailReporter{ @@ -1068,7 +1076,7 @@ func (s *E2ETestSuite) TestDisputes() { fivePercent := balBeforeDispute.Mul(math.NewInt(5)).Quo(math.NewInt(100)) disputeFee = sdk.NewCoin(s.denom, fivePercent) - report = types.MicroReport{ + report = oracletypes.MicroReport{ Reporter: reporter.Reporter, Power: reporter.TotalTokens.Int64(), QueryId: queryId, @@ -1174,7 +1182,6 @@ func (s *E2ETestSuite) TestDisputes() { disputes, err = s.disputekeeper.OpenDisputes.Get(s.ctx) require.NoError(err) require.NotNil(disputes) - // fmt.Println("disputes: ", disputes) // get new cycle list query data cycleListQuery, err = s.oraclekeeper.GetCurrentQueryInCycleList(s.ctx) @@ -1229,7 +1236,7 @@ func (s *E2ETestSuite) TestDisputes() { oneHundredPercent := reporter.TotalTokens disputeFee = sdk.NewCoin(s.denom, oneHundredPercent) - report = types.MicroReport{ + report = oracletypes.MicroReport{ Reporter: reporter.Reporter, Power: reporter.TotalTokens.Int64(), QueryId: queryId, @@ -1279,13 +1286,6 @@ func (s *E2ETestSuite) TestDisputes() { // todo: handle reporter removal reporter, err = s.reporterkeeper.Reporters.Get(s.ctx, reporterAccount) require.NoError(err) - // fmt.Println(reporter) - // reporter, err = s.reporterkeeper.Reporters.Get(s.ctx, reporterAccount) - // require.NoError(err) - // fmt.Println("reporter.TotalTokens during dispute: ", reporter.TotalTokens) - // require.NoError(err) - // freeFloatingBalanceBefore = s.bankKeeper.GetBalance(s.ctx, reporterAccount, s.denom) - // fmt.Println("freeFloatingBalance during dispute: ", freeFloatingBalanceBefore) // create vote tx msg msgVote = disputetypes.MsgVote{ @@ -1327,5 +1327,751 @@ func (s *E2ETestSuite) TestDisputes() { } func (s *E2ETestSuite) TestUnstaking() { + require := s.Require() + + // create 5 validators with 5_000 TRB + accaddr, valaddr, _ := s.CreateValidators(5) + s.stakingKeeper.EndBlocker(s.ctx) + // all validators are bonded + validators, err := s.stakingKeeper.GetAllValidators(s.ctx) + require.NoError(err) + require.NotNil(validators) + for _, val := range validators { + require.Equal(val.Status.String(), stakingtypes.BondStatusBonded) + } + + // begin unbonding validator 0 + del, err := s.stakingKeeper.GetDelegation(s.ctx, accaddr[1], valaddr[1]) + require.NoError(err) + // undelegate all shares except for 1 to avoid getting the validator deleted + timeToUnbond, _, err := s.stakingKeeper.Undelegate(s.ctx, accaddr[1], valaddr[1], del.Shares.Sub(math.LegacyNewDec(1))) + require.NoError(err) + + // unbonding time is 21 days after calling BeginUnbondingValidator + unbondingStartTime := s.ctx.BlockTime() + twentyOneDays := time.Hour * 24 * 21 + require.Equal(unbondingStartTime.Add(twentyOneDays), timeToUnbond) + + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + val1, err := s.stakingKeeper.GetValidator(s.ctx, valaddr[1]) + require.Equal(val1.UnbondingTime, unbondingStartTime.Add(twentyOneDays)) + require.NoError(err) + require.Equal(val1.IsUnbonding(), true) + require.Equal(val1.IsBonded(), false) + require.Equal(val1.IsUnbonded(), false) + // new block + s.ctx = s.ctx.WithBlockHeight(val1.UnbondingHeight + 1) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(twentyOneDays)) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + // validator 0 is now unbonded + val1, err = s.stakingKeeper.GetValidator(s.ctx, valaddr[1]) + require.NoError(err) + require.Equal(val1.IsUnbonded(), true) +} + +func (s *E2ETestSuite) TestGovernanceChangesCycleList() { + require := s.Require() + + govMsgServer := govkeeper.NewMsgServerImpl(s.govKeeper) + require.NotNil(govMsgServer) + + //--------------------------------------------------------------------------- + // Height 0 - create bonded validators and reporters + //--------------------------------------------------------------------------- + _, err := s.app.BeginBlocker(s.ctx) + require.NoError(err) + + valAccAddrs, valValAddrs, vals := s.CreateValidators(5) + repAccAddrs := s.CreateReporters(5, valValAddrs, vals) + proposer := repAccAddrs[0] + initCoins := sdk.NewCoin(s.denom, math.NewInt(500*1e6)) + for _, rep := range repAccAddrs { + s.NoError(s.bankKeeper.MintCoins(s.ctx, authtypes.Minter, sdk.NewCoins(initCoins))) + s.NoError(s.bankKeeper.SendCoinsFromModuleToAccount(s.ctx, authtypes.Minter, rep, sdk.NewCoins(initCoins))) + } + + govParams, err := s.govKeeper.Params.Get(s.ctx) + require.NoError(err) + + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + //--------------------------------------------------------------------------- + // Height 1 - submit proposal + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + + matic, _ := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000953706F745072696365000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000C00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000056D6174696300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000037573640000000000000000000000000000000000000000000000000000000000") + msgUpdateCycleList := oracletypes.MsgUpdateCyclelist{ + Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(), + Cyclelist: [][]byte{matic}, + } + anyMsg, err := codectypes.NewAnyWithValue(&msgUpdateCycleList) + proposalMsg := []*codectypes.Any{anyMsg} + require.NoError(err) + msgSubmitProposal := v1.MsgSubmitProposal{ + Messages: proposalMsg, + InitialDeposit: govParams.MinDeposit, + Proposer: proposer.String(), + Metadata: "test metadata", + Title: "test title", + Summary: "test summary", + Expedited: false, + } + + proposal, err := govMsgServer.SubmitProposal(s.ctx, &msgSubmitProposal) + fmt.Println("propRepsonse: ", proposal) + require.NoError(err) + require.Equal(proposal.ProposalId, uint64(1)) + + proposal1, err := s.govKeeper.Proposals.Get(s.ctx, proposal.ProposalId) + require.NoError(err) + require.Equal(proposal1.Status, v1.StatusVotingPeriod) + require.Equal(proposal1.Proposer, proposer.String()) + require.Equal(proposal1.TotalDeposit, govParams.MinDeposit) + require.Equal(proposal1.Messages, proposalMsg) + require.Equal(proposal1.Metadata, "test metadata") + require.Equal(proposal1.Title, "test title") + require.Equal(proposal1.Summary, "test summary") + require.Equal(proposal1.Expedited, false) + + _, err = s.app.EndBlocker(s.ctx) // end blocker should emit active proposal event + require.NoError(err) + + //--------------------------------------------------------------------------- + // Height 2 - vote on proposal + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + + // vote from each validator + for _, val := range valAccAddrs { + voteResponse, err := govMsgServer.Vote(s.ctx, &v1.MsgVote{ + ProposalId: proposal.ProposalId, + Voter: val.String(), + Option: v1.VoteOption(1), + Metadata: "vote metadata from validator", + }) + require.NoError(err) + require.NotNil(voteResponse) + } + + // check on vote in collections + vote, err := s.govKeeper.Votes.Get(s.ctx, collections.Join(proposal.ProposalId, valAccAddrs[0])) + require.NoError(err) + require.Equal(vote.ProposalId, proposal.ProposalId) + require.Equal(vote.Voter, valAccAddrs[0].String()) + require.Equal(vote.Metadata, "vote metadata from validator") + + for _, val := range valAccAddrs { + voteResponse, err := govMsgServer.Vote(s.ctx, &v1.MsgVote{ + ProposalId: proposal.ProposalId, + Voter: val.String(), + Option: v1.VoteOption(1), + Metadata: "vote metadata from validator", + }) + require.NoError(err) + require.NotNil(voteResponse) + } + + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(48 * time.Hour))) + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + //--------------------------------------------------------------------------- + // Height 3 - advance time to expire vote + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + + // proposal passed + proposal1, err = s.govKeeper.Proposals.Get(s.ctx, proposal.ProposalId) + require.NoError(err) + require.Equal(proposal1.Status, v1.StatusPassed) + require.Equal(proposal1.Proposer, proposer.String()) + require.Equal(proposal1.TotalDeposit, govParams.MinDeposit) + require.Equal(proposal1.Messages, proposalMsg) + require.Equal(proposal1.Metadata, "test metadata") + require.Equal(proposal1.Title, "test title") + require.Equal(proposal1.Summary, "test summary") + require.Equal(proposal1.Expedited, false) + + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + //--------------------------------------------------------------------------- + // Height 4 - check cycle list + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(s.ctx.BlockHeight() + 1) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + + cycleList, err := s.oraclekeeper.GetCyclelist(s.ctx) + require.NoError(err) + require.Equal(cycleList, [][]byte{matic}) + +} + +func (s *E2ETestSuite) TestEditingSpec() { + require := s.Require() + + registryMsgServer := registrykeeper.NewMsgServerImpl(s.registrykeeper) + require.NotNil(registryMsgServer) + +} + +func (s *E2ETestSuite) TestDisputes2() { + require := s.Require() + msgServerOracle := oraclekeeper.NewMsgServerImpl(s.oraclekeeper) + require.NotNil(msgServerOracle) + msgServerReporter := reporterkeeper.NewMsgServerImpl(s.reporterkeeper) + require.NotNil(msgServerReporter) + msgServerDispute := disputekeeper.NewMsgServerImpl(s.disputekeeper) + require.NotNil(msgServerDispute) + + //--------------------------------------------------------------------------- + // Height 0 - create 3 validators and 3 reporters + //--------------------------------------------------------------------------- + _, err := s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + valsAcctAddrs, valsValAddrs, vals := s.CreateValidators(3) + require.NotNil(valsAcctAddrs) + repsAccs := s.CreateReporters(3, valsValAddrs, vals) + badReporter := repsAccs[0] + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + // mapping to track reporter delegation balance + reporterToBalanceMap := make(map[string]math.Int) + for _, acc := range repsAccs { + rkDelegation, err := s.reporterkeeper.Delegators.Get(s.ctx, acc) + require.NoError(err) + reporterToBalanceMap[acc.String()] = rkDelegation.Amount + } + + //--------------------------------------------------------------------------- + // Height 1 - delegate 500 trb to validator 0 and bad reporter + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(1) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + pk := secp256k1.GenPrivKey() + delAcc := s.convertToAccAddress([]secp256k1.PrivKey{*pk}) + delAccAddr := sdk.AccAddress(delAcc[0]) + initCoins := sdk.NewCoin(s.denom, math.NewInt(500*1e6)) + s.NoError(s.bankKeeper.MintCoins(s.ctx, authtypes.Minter, sdk.NewCoins(initCoins))) + s.NoError(s.bankKeeper.SendCoinsFromModuleToAccount(s.ctx, authtypes.Minter, delAccAddr, sdk.NewCoins(initCoins))) + + // delegate to validator 0 + val, err := s.stakingKeeper.GetValidator(s.ctx, valsValAddrs[0]) + require.NoError(err) + _, err = s.stakingKeeper.Delegate(s.ctx, delAccAddr, math.NewInt(500*1e6), stakingtypes.Unbonded, val, false) + require.NoError(err) + + // delegate to bad reporter + source := reportertypes.TokenOrigin{ValidatorAddress: val.OperatorAddress, Amount: math.NewInt(500 * 1e6)} + msgDelegate := reportertypes.NewMsgDelegateReporter(delAccAddr.String(), badReporter.String(), math.NewInt(500*1e6), []*reportertypes.TokenOrigin{&source}) + _, err = msgServerReporter.DelegateReporter(s.ctx, msgDelegate) + require.NoError(err) + + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + val, err = s.stakingKeeper.GetValidator(s.ctx, valsValAddrs[0]) + require.NoError(err) + require.Equal(val.Tokens, math.NewInt(1500*1e6)) + rep, err := s.reporterkeeper.Reporters.Get(s.ctx, badReporter) + require.NoError(err) + require.Equal(rep.TotalTokens, math.NewInt(1500*1e6)) + + //--------------------------------------------------------------------------- + // Height 2 - direct reveal for cycle list + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(2) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + + disputedRep, err := s.reporterkeeper.Reporters.Get(s.ctx, repsAccs[0]) + require.NoError(err) + + // get new cycle list query data + cycleListQuery, err := s.oraclekeeper.GetCurrentQueryInCycleList(s.ctx) + require.NoError(err) + queryId := utils.QueryIDFromData(cycleListQuery) + // create reveal message + value := encodeValue(10_000) + require.NoError(err) + reveal := oracletypes.MsgSubmitValue{ + Creator: disputedRep.Reporter, + QueryData: cycleListQuery, + Value: value, + } + // send reveal message + revealResponse, err := msgServerOracle.SubmitValue(s.ctx, &reveal) + require.NoError(err) + require.NotNil(revealResponse) + revealTime := s.ctx.BlockTime() + // advance time and block height to expire the query and aggregate report + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(7 * time.Second)) + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + //--------------------------------------------------------------------------- + // Height 3 - open warning, pay from bond from reporter 1 + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(3) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + disputer, err := s.reporterkeeper.Reporters.Get(s.ctx, repsAccs[1]) + require.NoError(err) + + // disputerBal := disputer.TotalTokens + disputedBal := disputedRep.TotalTokens + onePercent := disputedBal.Mul(math.NewInt(1)).Quo(math.NewInt(100)) + disputeFee := sdk.NewCoin(s.denom, onePercent) // warning should be 1% of bonded tokens + + // todo: is there a getter for this ? + // get microreport for dispute + report := oracletypes.MicroReport{ + Reporter: disputedRep.Reporter, + Power: disputedRep.TotalTokens.Quo(sdk.DefaultPowerReduction).Int64(), + QueryId: queryId, + Value: value, + Timestamp: revealTime, + } + + // create msg for propose dispute tx + msgProposeDispute := disputetypes.MsgProposeDispute{ + Creator: disputer.Reporter, + Report: &report, + DisputeCategory: disputetypes.Warning, + Fee: disputeFee, + PayFromBond: true, + } + + // send propose dispute tx + _, err = msgServerDispute.ProposeDispute(s.ctx, &msgProposeDispute) + require.NoError(err) + + burnAmount := disputeFee.Amount.MulRaw(1).QuoRaw(20) + disputes, err := s.disputekeeper.OpenDisputes.Get(s.ctx) + require.NoError(err) + require.NotNil(disputes) + // dispute is created correctly + dispute, err := s.disputekeeper.Disputes.Get(s.ctx, 1) + require.NoError(err) + require.Equal(dispute.DisputeId, uint64(1)) + require.Equal(dispute.DisputeStatus, disputetypes.Voting) + require.Equal(dispute.DisputeCategory, disputetypes.Warning) + require.Equal(dispute.DisputeFee, disputeFee.Amount.Sub(burnAmount)) + require.Equal(dispute.FeePayers, []disputetypes.PayerInfo{{PayerAddress: disputer.Reporter, Amount: disputeFee.Amount, FromBond: true, BlockNumber: 3}}) + + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + //--------------------------------------------------------------------------- + // Height 4 - disputed reporter reports after calling unjail + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(4) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + + disputedRep, err = s.reporterkeeper.Reporters.Get(s.ctx, repsAccs[0]) + require.NoError(err) + require.Equal(disputedRep.Jailed, true) + + // disputed reporter cant report yet + cycleListQuery, err = s.oraclekeeper.GetCurrentQueryInCycleList(s.ctx) + require.NoError(err) + value = encodeValue(10_000) + require.NoError(err) + reveal = oracletypes.MsgSubmitValue{ + Creator: disputedRep.Reporter, + QueryData: cycleListQuery, + Value: value, + } + _, err = msgServerOracle.SubmitValue(s.ctx, &reveal) + require.Error(err) + + // disputed reporter can report after calling unjail function + msgUnjail := reportertypes.MsgUnjailReporter{ + ReporterAddress: disputedRep.Reporter, + } + _, err = msgServerReporter.UnjailReporter(s.ctx, &msgUnjail) + require.NoError(err) + disputedRep, err = s.reporterkeeper.Reporters.Get(s.ctx, repsAccs[0]) + require.NoError(err) + require.Equal(disputedRep.Jailed, false) + // send reveal message + revealResponse, err = msgServerOracle.SubmitValue(s.ctx, &reveal) + require.NoError(err) + require.NotNil(revealResponse) + revealTime = s.ctx.BlockTime() + + // give disputer tokens to pay for next disputes not from bond + initCoins = sdk.NewCoin(s.denom, math.NewInt(10_000*1e6)) + 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, sdk.MustAccAddressFromBech32(disputer.Reporter), sdk.NewCoins(initCoins))) + require.Equal(initCoins, s.bankKeeper.GetBalance(s.ctx, sdk.MustAccAddressFromBech32(disputer.Reporter), s.denom)) + + // advance time and block height to expire the query and aggregate report + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(7 * time.Second)) + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + disputer, err = s.reporterkeeper.Reporters.Get(s.ctx, repsAccs[1]) + require.NoError(err) + disputedRep, err = s.reporterkeeper.Reporters.Get(s.ctx, repsAccs[0]) + require.NoError(err) + + //--------------------------------------------------------------------------- + // Height 5 - open warning, pay from not bond from reporter 1 + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(5) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + // disputerBal := disputer.TotalTokens + disputedBal = disputedRep.TotalTokens + onePercent = disputedBal.Mul(math.NewInt(1)).Quo(math.NewInt(100)) + disputeFee = sdk.NewCoin(s.denom, onePercent) // warning should be 1% of bonded tokens + + // get microreport for dispute + report = oracletypes.MicroReport{ + Reporter: disputedRep.Reporter, + Power: disputedRep.TotalTokens.Quo(sdk.DefaultPowerReduction).Int64(), + QueryId: queryId, + Value: value, + Timestamp: revealTime, + } + + // create msg for propose dispute tx + msgProposeDispute = disputetypes.MsgProposeDispute{ + Creator: disputer.Reporter, + Report: &report, + DisputeCategory: disputetypes.Warning, + Fee: disputeFee, + PayFromBond: false, + } + + // send propose dispute tx + _, err = msgServerDispute.ProposeDispute(s.ctx, &msgProposeDispute) + require.NoError(err) + + burnAmount = disputeFee.Amount.MulRaw(1).QuoRaw(20) + disputes, err = s.disputekeeper.OpenDisputes.Get(s.ctx) + require.NoError(err) + require.NotNil(disputes) + // dispute is created correctly + dispute, err = s.disputekeeper.Disputes.Get(s.ctx, 2) + require.NoError(err) + require.Equal(dispute.DisputeId, uint64(2)) + require.Equal(dispute.DisputeStatus, disputetypes.Voting) + require.Equal(dispute.DisputeCategory, disputetypes.Warning) + require.Equal(dispute.DisputeFee, disputeFee.Amount.Sub(burnAmount)) + require.Equal(dispute.FeePayers, []disputetypes.PayerInfo{{PayerAddress: disputer.Reporter, Amount: disputeFee.Amount, FromBond: false, BlockNumber: 5}}) + + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + //--------------------------------------------------------------------------- + // Height 6 - dispute is resolved, direct reveal again + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(6) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + disputedRep, err = s.reporterkeeper.Reporters.Get(s.ctx, repsAccs[0]) + require.NoError(err) + require.Equal(disputedRep.Jailed, true) + + // disputed reporter cant report yet + cycleListQuery, err = s.oraclekeeper.GetCurrentQueryInCycleList(s.ctx) + require.NoError(err) + value = encodeValue(10_000) + require.NoError(err) + queryId = utils.QueryIDFromData(cycleListQuery) + reveal = oracletypes.MsgSubmitValue{ + Creator: disputedRep.Reporter, + QueryData: cycleListQuery, + Value: value, + } + _, err = msgServerOracle.SubmitValue(s.ctx, &reveal) + require.Error(err) + + // disputed reporter can report after calling unjail function + msgUnjail = reportertypes.MsgUnjailReporter{ + ReporterAddress: disputedRep.Reporter, + } + _, err = msgServerReporter.UnjailReporter(s.ctx, &msgUnjail) + require.NoError(err) + disputedRep, err = s.reporterkeeper.Reporters.Get(s.ctx, repsAccs[0]) + require.NoError(err) + require.Equal(disputedRep.Jailed, false) + // send reveal message + revealResponse, err = msgServerOracle.SubmitValue(s.ctx, &reveal) + require.NoError(err) + require.NotNil(revealResponse) + revealTime = s.ctx.BlockTime() + + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + //--------------------------------------------------------------------------- + // Height 7 - open minor dispute, pay from bond from reporter 1 + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(7) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + balBeforeDispute := disputedRep.TotalTokens + fivePercent := balBeforeDispute.Mul(math.NewInt(5)).Quo(math.NewInt(100)) + disputeFee = sdk.NewCoin(s.denom, fivePercent) + + report = oracletypes.MicroReport{ + Reporter: disputedRep.Reporter, + Power: disputedRep.TotalTokens.Quo(sdk.DefaultPowerReduction).Int64(), + QueryId: queryId, + Value: value, + Timestamp: revealTime, + } + + // create msg for propose dispute tx + msgProposeDispute = disputetypes.MsgProposeDispute{ + Creator: disputer.Reporter, + Report: &report, + DisputeCategory: disputetypes.Minor, + Fee: disputeFee, + PayFromBond: true, + } + + // send propose dispute tx + _, err = msgServerDispute.ProposeDispute(s.ctx, &msgProposeDispute) + require.NoError(err) + _ = s.ctx.BlockTime() + + _, err = s.app.EndBlocker(s.ctx) + require.NoError(err) + + //--------------------------------------------------------------------------- + // Height 8 - vote on minor dispute -- reaches quorum + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(8) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + // vote from disputer + msgVote := disputetypes.MsgVote{ + Voter: disputer.Reporter, + Id: dispute.DisputeId, + Vote: disputetypes.VoteEnum_VOTE_SUPPORT, + } + voteResponse, err := msgServerDispute.Vote(s.ctx, &msgVote) + require.NoError(err) + require.NotNil(voteResponse) + + // vote from disputed reporter + msgVote = disputetypes.MsgVote{ + Voter: disputedRep.Reporter, + Id: dispute.DisputeId, + Vote: disputetypes.VoteEnum_VOTE_SUPPORT, + } + voteResponse, err = msgServerDispute.Vote(s.ctx, &msgVote) + require.NoError(err) + require.NotNil(voteResponse) + + // vote from third reporter + thirdReporter, err := s.reporterkeeper.Reporters.Get(s.ctx, repsAccs[2]) + require.NoError(err) + msgVote = disputetypes.MsgVote{ + Voter: thirdReporter.Reporter, + Id: dispute.DisputeId, + Vote: disputetypes.VoteEnum_VOTE_SUPPORT, + } + voteResponse, err = msgServerDispute.Vote(s.ctx, &msgVote) + require.NoError(err) + require.NotNil(voteResponse) + + // vote from team + // fmt.Println(disputetypes.TeamAddress) + // msgVote = disputetypes.MsgVote{ + // Voter: sdk.MustAccAddressFromBech32(disputetypes.TeamAddress).String(), + // Id: dispute.DisputeId, + // Vote: disputetypes.VoteEnum_VOTE_SUPPORT, + // } + // voteResponse, err = msgServerDispute.Vote(s.ctx, &msgVote) + // require.NoError(err) + // require.NotNil(voteResponse) + + totalTips, err := s.disputekeeper.GetTotalTips(s.ctx) + require.NoError(err) + fmt.Println("totalTips: ", totalTips) + + totalReporterPower, err := s.reporterkeeper.TotalReporterPower(s.ctx) + require.NoError(err) + fmt.Println("total reporter power: ", totalReporterPower.Quo(sdk.DefaultPowerReduction)) + reporter1Power, err := s.disputekeeper.GetReporterPower(s.ctx, disputedRep.Reporter) + require.NoError(err) + fmt.Println("reporter1 Power: ", reporter1Power) + reporter2Power, err := s.disputekeeper.GetReporterPower(s.ctx, disputer.Reporter) + require.NoError(err) + fmt.Println("reporter2 Power: ", reporter2Power) + reporter3Power, err := s.disputekeeper.GetReporterPower(s.ctx, thirdReporter.Reporter) + require.NoError(err) + fmt.Println("reporter3 Power: ", reporter3Power) + + totalFreeFloatingTokens := s.disputekeeper.GetTotalSupply(s.ctx) + fmt.Println("total Free Floating Tokens: ", totalFreeFloatingTokens) + owners, err := s.bankKeeper.DenomOwners(s.ctx, &banktypes.QueryDenomOwnersRequest{Denom: s.denom}) + require.NoError(err) + sumFromDenomOwners := math.ZeroInt() + for _, owner := range owners.DenomOwners { + fmt.Println("owner: ", owner) + sumFromDenomOwners = sumFromDenomOwners.Add(owner.Balance.Amount) + } + fmt.Println("sumFromDenomOwners: ", sumFromDenomOwners) + + // print all reporter sdk.AccAddr + for _, rep := range repsAccs { + fmt.Println("rep: ", rep.String()) + } + for _, val := range valsAcctAddrs { + fmt.Println("val: ", val.String()) + } + fmt.Println("delegator acc addr: ", delAccAddr.String()) + + // print tbr module account address + tbrModuleAccount := s.accountKeeper.GetModuleAddress(minttypes.TimeBasedRewards) // yes + fmt.Println("tbr module account: ", tbrModuleAccount.String()) + + teamAccount := s.accountKeeper.GetModuleAddress(minttypes.MintToTeam) // yes + fmt.Println("team account: ", teamAccount.String()) + + disputeModuleAccount := s.accountKeeper.GetModuleAddress(disputetypes.ModuleName) // yes + fmt.Println("dispute module account: ", disputeModuleAccount.String()) + + authModuleAccount := s.accountKeeper.GetModuleAddress(authtypes.ModuleName) // + fmt.Println("auth module account: ", authModuleAccount.String()) + + reporterModuleAccount := s.accountKeeper.GetModuleAddress(reportertypes.ModuleName) // yes + fmt.Println("reporter module account: ", reporterModuleAccount.String()) + + registryModuleAccount := s.accountKeeper.GetModuleAddress(registrytypes.ModuleName) // no + fmt.Println("registry module account: ", registryModuleAccount.String()) + + reporterTipsEscrowAccount := s.accountKeeper.GetModuleAddress(reportertypes.TipsEscrowPool) // no + fmt.Println("reporter tips escrow account: ", reporterTipsEscrowAccount.String()) + + oracleModuleAccount := s.accountKeeper.GetModuleAddress(oracletypes.ModuleName) // no + fmt.Println("oracle module account: ", oracleModuleAccount.String()) + + stakingModuleAccount := s.accountKeeper.GetModuleAddress(stakingtypes.ModuleName) // + fmt.Println("staking module account: ", stakingModuleAccount.String()) + + //--------------------------------------------------------------------------- + // Height 9 - resolve dispute, direct reveal again + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(9) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + //--------------------------------------------------------------------------- + // Height 10 - open minor dispute, pay from not bond from reporter 1 + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(10) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + //--------------------------------------------------------------------------- + // Height 11 - vote on minor dispute + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(11) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + //--------------------------------------------------------------------------- + // Height 12 - resolve dispute, direct reveal again + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(12) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + //--------------------------------------------------------------------------- + // Height 13 - open major dispute, pay from bond from reporter 1 + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(13) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + //--------------------------------------------------------------------------- + // Height 14 - vote on major dispute + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(14) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + //--------------------------------------------------------------------------- + // Height 15 - resolve dispute, direct reveal again + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(15) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + //--------------------------------------------------------------------------- + // Height 16 - open major dispute, pay from not bond from reporter 1 + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(16) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + //--------------------------------------------------------------------------- + // Height 17 - vote on major dispute + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(17) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) + + //--------------------------------------------------------------------------- + // Height 18 - resolve dispute, direct reveal again + //--------------------------------------------------------------------------- + s.ctx = s.ctx.WithBlockHeight(18) + _, err = s.app.BeginBlocker(s.ctx) + require.NoError(err) + s.ctx = s.ctx.WithBlockTime(s.ctx.BlockTime().Add(time.Duration(1 * time.Second))) } diff --git a/tests/e2e/keeper_test.go b/tests/e2e/keeper_test.go index 15fe1fa49..3bd5f54ad 100644 --- a/tests/e2e/keeper_test.go +++ b/tests/e2e/keeper_test.go @@ -12,6 +12,7 @@ import ( "cosmossdk.io/math" authmodulev1 "cosmossdk.io/api/cosmos/auth/module/v1" + // govmodulev1 "cosmossdk.io/api/cosmos/gov/module/v1" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -107,6 +108,7 @@ type E2ETestSuite struct { ctx sdk.Context appCodec codec.Codec authConfig *authmodulev1.Module + // govConfig *govmodulev1.Module queryHelper *baseapp.QueryServiceTestHelper interfaceRegistry codectypes.InterfaceRegistry @@ -146,6 +148,20 @@ func (suite *E2ETestSuite) initKeepersWithmAccPerms(blockedAddrs map[string]bool log.NewNopLogger(), ) + // govConfig := suite.govConfig + + // suite.govKeeper = govkeeper.NewKeeper( + // appCodec, + // runtime.NewKVStoreService(suite.fetchStoreKey(govtypes.StoreKey).(*storetypes.KVStoreKey)), + // suite.accountKeeper, + // suite.bankKeeper, + // suite.stakingKeeper, + // suite.distrKeeper, + // baseapp.NewMsgServiceRouter(), + // govConfig, + // authtypes.NewModuleAddress(govtypes.ModuleName).String(), + // ) + suite.stakingKeeper = stakingkeeper.NewKeeper( appCodec, runtime.NewKVStoreService(suite.fetchStoreKey(stakingtypes.StoreKey).(*storetypes.KVStoreKey)), @@ -227,18 +243,21 @@ func (s *E2ETestSuite) SetupTest() { s.app = app } -func (s *E2ETestSuite) CreateValidators(numValidators int, numTrb int64) ([]sdk.AccAddress, []sdk.ValAddress, []stakingtypes.Validator) { +func (s *E2ETestSuite) CreateValidators(numValidators int) ([]sdk.AccAddress, []sdk.ValAddress, []stakingtypes.Validator) { require := s.Require() + // create account that will become a validator accountsAddrs := simtestutil.CreateIncrementalAccounts(numValidators) // mint numTrb for each validator - initCoins := sdk.NewCoin(s.denom, math.NewInt(numTrb*1e6)) + initCoins := sdk.NewCoin(s.denom, math.NewInt(5000*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 @@ -254,7 +273,7 @@ func (s *E2ETestSuite) CreateValidators(numValidators int, numTrb int64) ([]sdk. s.stakingKeeper.SetValidatorByConsAddr(s.ctx, validator) s.stakingKeeper.SetNewValidatorByPowerIndex(s.ctx, validator) - _, err = s.stakingKeeper.Delegate(s.ctx, accountsAddrs[i], math.NewInt(numTrb*1e6), stakingtypes.Unbonded, validator, true) + _, err = s.stakingKeeper.Delegate(s.ctx, accountsAddrs[i], math.NewInt(5000*1e6), stakingtypes.Unbonded, validator, true) require.NoError(err) // call hooks for distribution init valBz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(validator.GetOperator()) @@ -272,11 +291,16 @@ func (s *E2ETestSuite) CreateValidators(numValidators int, numTrb int64) ([]sdk. return accountsAddrs, validatorsAddrs, validators } -func (s *E2ETestSuite) CreateReporters(numReporters int, numTrb int64, valAddrs []sdk.ValAddress) []sdk.AccAddress { +func (s *E2ETestSuite) CreateReporters(numReporters int, valAddrs []sdk.ValAddress, vals []stakingtypes.Validator) []sdk.AccAddress { require := s.Require() + type Delegator struct { + delegatorAddress sdk.AccAddress + validator stakingtypes.Validator + tokenAmount math.Int + } - if numReporters < len(valAddrs) { - panic("numReporters must be equal to the the number of validators (not sure how else to implement yet)") + if numReporters != len(valAddrs) { + panic("numReporters must be equal to the the number of validators (make other reporters manually)") } msgServerReporter := reporterkeeper.NewMsgServerImpl(s.reporterkeeper) @@ -286,48 +310,59 @@ func (s *E2ETestSuite) CreateReporters(numReporters int, numTrb int64, valAddrs privKeys := CreateRandomPrivateKeys(numReporters) accs := s.convertToAccAddress(privKeys) // sdk.AccountAddresses - // mint tokens for each account - initCoins := sdk.NewCoin(s.denom, math.NewInt(numTrb*1e6)) + // mint 1k trb to each account + initCoins := sdk.NewCoin(s.denom, math.NewInt(1000*1e6)) for _, acc := range accs { s.NoError(s.bankKeeper.MintCoins(s.ctx, authtypes.Minter, sdk.NewCoins(initCoins))) s.NoError(s.bankKeeper.SendCoinsFromModuleToAccount(s.ctx, authtypes.Minter, acc, sdk.NewCoins(initCoins))) } - - // prep for CreateReporter tx - commission := stakingtypes.NewCommissionWithTime(math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDecWithPrec(3, 1), - math.LegacyNewDecWithPrec(1, 1), s.ctx.BlockTime()) // rate = 10%, maxRate = 30%, maxChangeRate = 10% - + // delegate 1k trb to validators + for i, acc := range accs { + valBz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(vals[i].GetOperator()) + require.NoError(err) + reporterDelToVal := Delegator{delegatorAddress: acc, validator: vals[i], tokenAmount: math.NewInt(1000 * 1e6)} + _, err = s.stakingKeeper.Delegate(s.ctx, reporterDelToVal.delegatorAddress, reporterDelToVal.tokenAmount, stakingtypes.Unbonded, reporterDelToVal.validator, true) + require.NoError(err) + // call dist module hooks + err = s.distrKeeper.Hooks().AfterValidatorCreated(s.ctx, valBz) + require.NoError(err) + err = s.distrKeeper.Hooks().BeforeDelegationCreated(s.ctx, acc, valBz) + require.NoError(err) + err = s.distrKeeper.Hooks().AfterDelegationModified(s.ctx, acc, valBz) + require.NoError(err) + } + // self delegate in reporter module with 1k trb // CreateReporter tx for i, acc := range accs { - // make CreateReporterMsg + valBz, err := s.stakingKeeper.ValidatorAddressCodec().StringToBytes(vals[i].GetOperator()) + require.NoError(err) + commission := stakingtypes.NewCommissionWithTime(math.LegacyNewDecWithPrec(1, 1), math.LegacyNewDecWithPrec(3, 1), + math.LegacyNewDecWithPrec(1, 1), s.ctx.BlockTime()) // rate = 10%, maxRate = 30%, maxChangeRate = 10% + var createReporterMsg reportertypes.MsgCreateReporter createReporterMsg.Reporter = acc.String() - createReporterMsg.Amount = initCoins.Amount + createReporterMsg.Amount = math.NewInt(1000 * 1e6) createReporterMsg.Commission = &commission createReporterMsg.TokenOrigins = []*reportertypes.TokenOrigin{ { - ValidatorAddress: valAddrs[i].String(), - Amount: initCoins.Amount, + ValidatorAddress: vals[i].GetOperator(), + Amount: math.NewInt(1000 * 1e6), }, } // send CreateReporter Tx - _, err := msgServerReporter.CreateReporter(s.ctx, &createReporterMsg) + _, err = msgServerReporter.CreateReporter(s.ctx, &createReporterMsg) s.NoError(err) - } - // Self delegate every reporter - for i, acc := range accs { - // define delegation source - source := reportertypes.TokenOrigin{ValidatorAddress: valAddrs[i].String(), Amount: math.NewInt((numTrb * 1e6))} - delegationMsg := reportertypes.NewMsgDelegateReporter( - acc.String(), - acc.String(), - math.NewInt(numTrb*1e6), - []*reportertypes.TokenOrigin{&source}, - ) - // send delegate reporter tx - _, err := msgServerReporter.DelegateReporter(s.ctx, delegationMsg) - s.NoError(err) + // verify in collections + rkDelegation, err := s.reporterkeeper.Delegators.Get(s.ctx, acc) + require.NoError(err) + require.Equal(rkDelegation.Reporter, acc.String()) + require.Equal(rkDelegation.Amount, math.NewInt(1000*1e6)) + // check on reporter/validator delegation + skDelegation, err := s.stakingKeeper.Delegation(s.ctx, acc, valBz) + require.NoError(err) + require.Equal(skDelegation.GetDelegatorAddr(), acc.String()) + require.Equal(skDelegation.GetValidatorAddr(), vals[i].GetOperator()) } return accs diff --git a/x/oracle/keeper/query_get_data_before_test.go b/x/oracle/keeper/query_get_data_before_test.go index ffbaa3b84..1b8482ea8 100644 --- a/x/oracle/keeper/query_get_data_before_test.go +++ b/x/oracle/keeper/query_get_data_before_test.go @@ -1,6 +1,6 @@ package keeper_test -func (s *KeeperTestSuite) TestGetDataBefore() { +func (s *KeeperTestSuite) TestQueryGetDataBefore() { // require := s.Require() // s.TestSubmitValue()