Skip to content

Commit

Permalink
refactor(x/rewards): move state to use collections (#475)
Browse files Browse the repository at this point in the history
* state works in a backwards compatible manner

* migrate contract metadata to use collections

* move params in a backwards compatible way

* move params to use collections

* flat fees moved in backwards compatible way

* move flat fees fully to use collections

* flat fee keys cleanup

* move min cons fee in a non state breaking way

* move min cons fee to use collections fully

* cleanup keys.go

* move blockrewards to collections in a state compatible way

* move genesis to use collections

* move more code to use collections

* remove more code

* move DeleteBLockRewardsCascade from State to Keeper

* move tx rewards to collections in a safe way

* more of TxRewards state moved to collections

* fully migrate TxRewards to collections

* checkpoint

* remove GetRewardsByRewardAddr in state_rewards_record.go

* remove CreateRewardsRecord in state_rewards_record.go

* remove more from state_rewards_record.go

* tmp commit

* remove all unneeded things

* finalise fast removal

* fix prefix and cleanup

* lint fully + CHANGELOG.md

* address nits
  • Loading branch information
fdymylja authored Nov 7, 2023
1 parent 3b382eb commit 7bc526a
Show file tree
Hide file tree
Showing 45 changed files with 776 additions and 1,219 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Contains all the PRs that improved the code without changing the behaviours.

- [#481](https://github.com/archway-network/archway/pull/481) - Add Release Checklist issue type

### Improvements

- [#478](https://github.com/archway-network/archway/pull/475) – moves x/rewards state to use collections for state management.

### Changed

### Deprecated
Expand Down
16 changes: 8 additions & 8 deletions contracts/go/voter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,33 @@ require (
github.com/CosmWasm/cosmwasm-go v0.5.1-0.20220822092235-974247a04ac7
github.com/CosmWasm/tinyjson v0.9.0
github.com/CosmWasm/wasmvm v1.0.0-rc.0
github.com/stretchr/testify v1.7.1
github.com/stretchr/testify v1.8.1
)

require (
filippo.io/edwards25519 v1.0.0-rc.1 // indirect
github.com/DataDog/zstd v1.4.5 // indirect
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cometbft/cometbft-db v0.7.0 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgraph-io/badger/v2 v2.2007.2 // indirect
github.com/dgraph-io/badger/v2 v2.2007.4 // indirect
github.com/dgraph-io/ristretto v0.0.3 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/hdevalence/ed25519consensus v0.0.0-20220222234857-c00d1f31bab3 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca // indirect
github.com/cometbft/cometbft-db v0.7.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sys v0.3.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 12 additions & 0 deletions contracts/go/voter/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cometbft/cometbft-db v0.7.0/go.mod h1:yiKJIm2WKrt6x8Cyxtq9YTEcIMPcEe4XPxhgX59Fzf0=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
Expand All @@ -42,6 +43,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/decred/dcrd/lru v1.0.0/go.mod h1:mxKOwFd7lFjN2GZYsiz/ecgqR6kkYAl+0pz0tEMk218=
github.com/dgraph-io/badger/v2 v2.2007.2 h1:EjjK0KqwaFMlPin1ajhP943VPENHJdEz1KLIegjaI3k=
github.com/dgraph-io/badger/v2 v2.2007.2/go.mod h1:26P/7fbL4kUZVEVKLAKXkBXKOydDmM2p1e+NhhnBCAE=
github.com/dgraph-io/badger/v2 v2.2007.4/go.mod h1:vSw/ax2qojzbN6eXHIx6KPKtCSHJN/Uz0X0VPruTIhk=
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
github.com/dgraph-io/ristretto v0.0.3 h1:jh22xisGBjrEVnRZ1DVTpBVQm0Xndu8sMl0CWDzSIBI=
github.com/dgraph-io/ristretto v0.0.3/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
Expand Down Expand Up @@ -72,6 +74,7 @@ github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA=
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.1.2/go.mod h1:qOPhT0dTNdNzV6Z/lhRX0YXUafgPLFUh+gZMl761Gm4=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand All @@ -90,6 +93,7 @@ github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8Hm
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jrick/logrotate v1.0.0/go.mod h1:LNinyqDIJnpAur+b8yyulnQw/wDuN1+BYKlTRt3OuAQ=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/klauspost/compress v1.12.3/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down Expand Up @@ -127,10 +131,14 @@ github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb6
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca h1:Ld/zXl5t4+D69SiV4JoN7kkfvJdOWlPpfxrzxpLMoUk=
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca/go.mod h1:u2MKkTVTVJWe5D1rCvame8WqhBd88EuIwODJZ1VHCPM=
github.com/tendermint/tm-db v0.6.7 h1:fE00Cbl0jayAoqlExN6oyQJ7fR/ZtoVOmvPJ//+shu8=
Expand All @@ -153,6 +161,7 @@ golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd h1:O7DYs+zxREGLKzKoMQrtrEacpb0ZVXA5rIwylE2Xchk=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.4.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -168,6 +177,7 @@ golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9 h1:XfKQ4OlFl8okEOr5UvAqFRVj8pY/4yfcXrddB8qAbU0=
golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand All @@ -186,6 +196,7 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b h1:QRR6H1YWRnHb4Y/HeNFCTJLFVxaq6wH4YuVdsUOr75U=
Expand All @@ -200,3 +211,4 @@ gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 4 additions & 5 deletions e2e/gastracking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ func (s *E2ETestSuite) TestGasTrackingAndRewardsDistribution() {
// Check x/rewards Tx record
s.Run("Check tx fee rebate rewards records", func() {
ctx := chain.GetContext()
txRewardsState := rewardsKeeper.GetState().TxRewardsState(ctx)

txRewards := txRewardsState.GetTxRewardsByBlock(ctx.BlockHeight() - 1)
txRewards, err := keepers.RewardsKeeper.GetTxRewardsByBlock(ctx, uint64(ctx.BlockHeight()-1))
s.Require().NoError(err)
s.Require().Len(txRewards, 1)
s.Assert().Equal(txID, txRewards[0].TxId)
s.Assert().Equal(ctx.BlockHeight()-1, txRewards[0].Height)
Expand All @@ -223,10 +223,9 @@ func (s *E2ETestSuite) TestGasTrackingAndRewardsDistribution() {
// Check x/rewards Block record
s.Run("Check block rewards record", func() {
ctx := chain.GetContext()
blockRewardsState := rewardsKeeper.GetState().BlockRewardsState(ctx)

blockRewards, found := blockRewardsState.GetBlockRewards(ctx.BlockHeight() - 1)
s.Require().True(found)
blockRewards, err := rewardsKeeper.BlockRewards.Get(ctx, uint64(ctx.BlockHeight()-1))
s.Require().NoError(err)
s.Assert().Equal(ctx.BlockHeight()-1, blockRewards.Height)
s.Assert().Equal(blockInflationRewardsExpected.String(), blockRewards.InflationRewards.String())
s.Assert().EqualValues(blockGasLimit, blockRewards.MaxGas)
Expand Down
45 changes: 25 additions & 20 deletions e2e/rewards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ func (s *E2ETestSuite) TestRewardsWithdrawProfitAndFees() {
trackingOp := trackingTx.ContractOperations[0]
s.Require().Equal(trackingOp.ContractAddress, contractAddr.String())

rewardsBlock, found := rewardsKeeper.GetState().BlockRewardsState(ctx).GetBlockRewards(record.CalculatedHeight)
s.Require().True(found)
rewardsBlock, err := rewardsKeeper.BlockRewards.Get(ctx, uint64(record.CalculatedHeight))
s.Require().NoError(err)

rewardsTxs := rewardsKeeper.GetState().TxRewardsState(ctx).GetTxRewardsByBlock(record.CalculatedHeight)
rewardsTxs, err := rewardsKeeper.GetTxRewardsByBlock(ctx, uint64(record.CalculatedHeight))
s.Require().NoError(err)
s.Require().Len(rewardsTxs, 1)
rewardsTx := rewardsTxs[0]
s.Require().EqualValues(trackingTx.Info.Id, rewardsTx.TxId)
Expand All @@ -162,12 +163,11 @@ func (s *E2ETestSuite) TestRewardsWithdrawProfitAndFees() {
// Create a bunch of mock reward records
{
ctx := chain.GetContext()
recordsState := rewardsKeeper.GetState().RewardsRecord(ctx)

// Create records
coinsToMint := sdk.NewCoins()
for i := 1; i < recordsLen; i++ {
record := recordsState.CreateRewardsRecord(senderAcc.Address, recordRewards, ctx.BlockHeight(), ctx.BlockTime())
record, err := rewardsKeeper.CreateRewardsRecord(ctx, senderAcc.Address, recordRewards, ctx.BlockHeight(), ctx.BlockTime())
s.Require().NoError(err)
s.Require().EqualValues(i+1, record.Id)
coinsToMint = coinsToMint.Add(recordRewards...)
}
Expand Down Expand Up @@ -240,7 +240,7 @@ func (s *E2ETestSuite) TestRewardsParamMaxWithdrawRecordsLimit() {
rewardsTypes.MaxWithdrawRecordsParamLimit = uint64(29500) // an actual value is (thisValue - 1), refer to the query below

chain := e2eTesting.NewTestChain(s.T(), 1,
e2eTesting.WithBlockGasLimit(100_000_000),
e2eTesting.WithBlockGasLimit(100_000_001),
e2eTesting.WithMaxWithdrawRecords(rewardsTypes.MaxWithdrawRecordsParamLimit),
)
keepers := chain.GetApp().Keepers
Expand All @@ -260,16 +260,16 @@ func (s *E2ETestSuite) TestRewardsParamMaxWithdrawRecordsLimit() {
recordIDs := make([]uint64, 0, rewardsTypes.MaxWithdrawRecordsParamLimit)
{
ctx := chain.GetContext()
recordsState := rewardsKeeper.GetState().RewardsRecord(ctx)

recordRewards := sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())
for i := uint64(0); i < rewardsTypes.MaxWithdrawRecordsParamLimit; i++ {
record := recordsState.CreateRewardsRecord(
record, err := rewardsKeeper.CreateRewardsRecord(
ctx,
contractAddr,
sdk.Coins{recordRewards},
ctx.BlockHeight(),
ctx.BlockTime(),
)
s.Require().NoError(err)

recordIDs = append(recordIDs, record.Id)
}
Expand Down Expand Up @@ -332,17 +332,17 @@ func (s *E2ETestSuite) TestRewardsRecordsQueryLimit() {
var recordsExpected []rewardsTypes.RewardsRecord
{
ctx := chain.GetContext()
recordsState := rewardsKeeper.GetState().RewardsRecord(ctx)

records := make([]rewardsTypes.RewardsRecord, 0, rewardsTypes.MaxRecordsQueryLimit)
recordRewards := sdk.NewCoin(sdk.DefaultBondDenom, sdk.OneInt())
for i := uint64(0); i < rewardsTypes.MaxRecordsQueryLimit; i++ {
record := recordsState.CreateRewardsRecord(
record, err := rewardsKeeper.CreateRewardsRecord(
ctx,
contractAddr,
sdk.Coins{recordRewards},
ctx.BlockHeight(),
ctx.BlockTime(),
)
s.Require().NoError(err)

records = append(records, record)
}
Expand Down Expand Up @@ -458,7 +458,8 @@ func (s *E2ETestSuite) TestTXFailsAfterAnteHandler() {
chain.NextBlock(1 * time.Second)

// only rewards record for contract premiums. no rewards record for feerebaes/inflation because because the TX failed.
rewards := rewardsKeeper.GetState().RewardsRecord(chain.GetContext()).GetRewardsRecordByRewardsAddress(contractAddr)
rewards, err := rewardsKeeper.GetRewardsRecordsByWithdrawAddress(chain.GetContext(), contractAddr)
require.NoError(s.T(), err)
require.Len(s.T(), rewards, 1)
require.Equal(s.T(), flatFees, rewards[0].Rewards[0])
}
Expand Down Expand Up @@ -533,7 +534,8 @@ func (s *E2ETestSuite) TestRewardsFlatFees() {
// should find two rewards records
// 1. Flatfee rewards record
// 2. InflationaryRewards + FeeRewards rewards record
rewards := rewardsKeeper.GetState().RewardsRecord(chain.GetContext()).GetRewardsRecordByRewardsAddress(contractAddr)
rewards, err := rewardsKeeper.GetRewardsRecordsByWithdrawAddress(chain.GetContext(), contractAddr)
require.NoError(s.T(), err)
require.Len(s.T(), rewards, 2) // there are two rewards records. first for flat fees and the second for tx&inflation fees
require.Equal(s.T(), flatFees, rewards[0].Rewards[0]) // the first rewards record matches our set flat fees

Expand Down Expand Up @@ -599,10 +601,12 @@ func (s *E2ETestSuite) TestRewardsFlatFees() {
require.NoError(s.T(), err)
chain.NextBlock(1 * time.Second)
}
rewards = rewardsKeeper.GetState().RewardsRecord(chain.GetContext()).GetRewardsRecordByRewardsAddress(contractAddr)
rewards, err = rewardsKeeper.GetRewardsRecordsByWithdrawAddress(chain.GetContext(), contractAddr)
require.NoError(s.T(), err)
require.Len(s.T(), rewards, 52) // why 52? cuz we already had 2 rewards record. we made 10 loops with 2 txs for this contract. And second txs contains 2 msgs. so 2 + (10 * (2 + 3)) = 52

rewards = rewardsKeeper.GetState().RewardsRecord(chain.GetContext()).GetRewardsRecordByRewardsAddress(contract2Addr)
rewards, err = rewardsKeeper.GetRewardsRecordsByWithdrawAddress(chain.GetContext(), contract2Addr)
require.NoError(s.T(), err)
require.Len(s.T(), rewards, 40) // why 40? cuz we made 10 loops with 2 txs for this contract. and each msg creates two records. so 10 * 2 * 2 = 40
}

Expand Down Expand Up @@ -679,7 +683,6 @@ func (s *E2ETestSuite) TestSubMsgRevert() {

return
}
rk := chain.GetApp().Keepers.RewardsKeeper

// send a message that passes the ante handler but not the wasm execution step
sendMsg(&wasmdTypes.MsgExecuteContract{
Expand All @@ -692,9 +695,11 @@ func (s *E2ETestSuite) TestSubMsgRevert() {
chain.NextBlock(1 * time.Second)

// has rewards because of reply on error
rewards := rk.GetState().RewardsRecord(chain.GetContext()).GetRewardsRecordByRewardsAddress(contractAddr)
rewards, err := rewardsKeeper.GetRewardsRecordsByWithdrawAddress(chain.GetContext(), contractAddr)
require.NoError(s.T(), err)
require.NotEmpty(s.T(), rewards)
// does not have rewards because it failed
rewards = rk.GetState().RewardsRecord(chain.GetContext()).GetRewardsRecordByRewardsAddress(calledContractAddr)
rewards, err = rewardsKeeper.GetRewardsRecordsByWithdrawAddress(chain.GetContext(), calledContractAddr)
require.NoError(s.T(), err)
require.Empty(s.T(), rewards)
}
8 changes: 3 additions & 5 deletions e2e/testing/chain_ops_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package e2eTesting

import (
"encoding/json"
"io/ioutil" //nolint: staticcheck
"io/ioutil" // nolint: staticcheck

rewardsTypes "github.com/archway-network/archway/x/rewards/types"

Expand Down Expand Up @@ -105,10 +105,8 @@ func (chain *TestChain) GetContractInfo(contractAddr sdk.AccAddress) wasmdTypes.
func (chain *TestChain) GetContractMetadata(contractAddr sdk.AccAddress) rewardsTypes.ContractMetadata {
t := chain.t

state := chain.app.Keepers.RewardsKeeper.GetState().ContractMetadataState(chain.GetContext())

metadata, found := state.GetContractMetadata(contractAddr)
require.True(t, found)
metadata, err := chain.app.Keepers.RewardsKeeper.ContractMetadata.Get(chain.GetContext(), contractAddr)
require.NoError(t, err)

return metadata
}
Expand Down
6 changes: 2 additions & 4 deletions e2e/txfees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,8 @@ func (s *E2ETestSuite) TestTxFees() {
var blockRewards sdk.Coin
{
ctx := chain.GetContext()
blockRewardsState := keepers.RewardsKeeper.GetState().BlockRewardsState(ctx)

blockRewardsInfo, found := blockRewardsState.GetBlockRewards(ctx.BlockHeight() - 1)
s.Require().True(found)
blockRewardsInfo, err := keepers.RewardsKeeper.BlockRewards.Get(ctx, uint64(ctx.BlockHeight()-1))
s.Require().NoError(err)

blockRewards = blockRewardsInfo.InflationRewards
}
Expand Down
Loading

0 comments on commit 7bc526a

Please sign in to comment.