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

feat!: simulator support #186

Merged
merged 24 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
d898472
feat(simulator): start simulator wiring
fmorency Jun 13, 2024
5558615
feat(simulator): progress
fmorency Jun 14, 2024
080d0ad
feat(simulator): set power
fmorency Jun 17, 2024
e605186
fix(simulator): lint
fmorency Jun 17, 2024
84f7039
fix: error desc
fmorency Jun 18, 2024
66965c4
feat(simulator): update params, remove validator & fix set power
fmorency Jun 18, 2024
42ff42c
fix: determinism and refactor
fmorency Jun 19, 2024
f2b3a4a
fix: lint
fmorency Jun 19, 2024
d4eea29
fix(simulator): account keeper only used for testing
fmorency Jun 26, 2024
e8fcecf
fix(simulator): set power logic
fmorency Jul 3, 2024
2bebf6a
fix(simulator): remove x/distribution module
fmorency Jul 4, 2024
99307bf
fix(simulator): don't remove last validator
fmorency Jul 4, 2024
5bea544
fix(simulator): balance must be gte two
fmorency Jul 9, 2024
246b3bc
Revert "fix(simulator): remove x/distribution module"
fmorency Jul 17, 2024
d5fd045
feat(simulator): antehandler disabling msg withdraw delegator rewards
fmorency Jul 19, 2024
269b688
feat(make): simulation support
fmorency Jul 19, 2024
58ca31d
chore(simulator): parameter file
fmorency Jul 19, 2024
1efeec3
doc(simulator): integration
fmorency Jul 19, 2024
dfca838
fix(simulator): lint
fmorency Jul 19, 2024
eb4aa4b
ci(simulator): simulator support
fmorency Jul 19, 2024
83cc739
fix: ante check
fmorency Jul 19, 2024
d86bb53
deps: sdk v0.50.8
Reecepbcups Jul 19, 2024
aa4f188
rm go.work.sum
Reecepbcups Jul 19, 2024
7ff9388
Merge branch 'main' into simulation
Reecepbcups Jul 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/simulator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Simulator tests
on:
push:
branches:
- main
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
tests:
runs-on: ubuntu-latest
steps:
- name: Check out source
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.21"
check-latest: true
- name: Full application simulation (fixed seed)
run: make sim-full-app
- name: Simulation after state import (fixed seed)
run: make sim-after-import
# # Requires wiring v2
# # https://github.com/strangelove-ventures/poa/issues/199
# - name: Simulation export/import (fixed seed)
# run: make sim-export-import
- name: Simulate application state determinism (fixed seed)
run: make sim-app-determinism
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
# Depinject debug file
debug_container.dot

build/
build/

go.work.sum
25 changes: 25 additions & 0 deletions INTEGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,31 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
}
```

### [Disable Withdraw Delegation Rewards](./ante/disable_withdraw_delegator_rewards.go)

This decorator blocks the `MsgWithdrawDelegatorReward` message from the CosmosSDK `x/distribution` module. The decorator acts as a preventive measure against a crash caused by an interaction between the POA module and the CosmosSDK `x/distribution` module.

While the crash has a low probability of occurring in the wild, it is a critical issue that can cause the chain to halt.

More information about this issue can be found in https://github.com/strangelove-ventures/poa/issues/170

```go
import (
...
poaante "github.com/strangelove-ventures/poa/ante"
)

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
...
anteDecorators := []sdk.AnteDecorator{
...
poaante.NewPOADisableWithdrawDelegatorRewardsDecorator(),
...
}
...
}
```

### [Commission Limits](./ante/commission_limit.go)
Depending on the chain use case, it may be desired to limit the commission rate range for min, max, or set value.

Expand Down
48 changes: 47 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,50 @@ lint-fix:
@go install github.com/golangci/golangci-lint/cmd/golangci-lint@$(golangci_version)
@$(golangci_lint_cmd) run ./... --fix --timeout 15m

.PHONY: lint lint-fix
.PHONY: lint lint-fix

##################
### Simulation ###
##################

SIM_PARAMS ?= $(shell pwd)/simulation/sim_params.json
SIM_NUM_BLOCKS ?= 100
SIM_PERIOD ?= 5
SIM_COMMIT ?= true
SIM_ENABLED ?= true
SIM_VERBOSE ?= false
SIM_TIMEOUT ?= 24h
SIM_SEED ?= 42
SIM_COMMON_ARGS = -NumBlocks=${SIM_NUM_BLOCKS} -Enabled=${SIM_ENABLED} -Commit=${SIM_COMMIT} -Period=${SIM_PERIOD} -Params=${SIM_PARAMS} -Verbose=${SIM_VERBOSE} -Seed=${SIM_SEED} -v -timeout ${SIM_TIMEOUT}

sim-full-app:
@echo "--> Running full app simulation (blocks: ${SIM_NUM_BLOCKS}, commit: ${SIM_COMMIT}, period: ${SIM_PERIOD}, seed: ${SIM_SEED}, params: ${SIM_PARAMS}"
@go test ./simapp -run TestFullAppSimulation ${SIM_COMMON_ARGS}

sim-full-app-random:
$(MAKE) sim-full-app SIM_SEED=$$RANDOM

# Note: known to fail when using app wiring v1
sim-import-export:
@echo "--> Running app import/export simulation (blocks: ${SIM_NUM_BLOCKS}, commit: ${SIM_COMMIT}, period: ${SIM_PERIOD}, seed: ${SIM_SEED}, params: ${SIM_PARAMS}"
@go test ./simapp -run TestAppImportExport ${SIM_COMMON_ARGS}

# Note: known to fail when using app wiring v1
sim-import-export-random:
$(MAKE) sim-import-export SIM_SEED=$$RANDOM

sim-after-import:
@echo "--> Running app after import simulation (blocks: ${SIM_NUM_BLOCKS}, commit: ${SIM_COMMIT}, period: ${SIM_PERIOD}, seed: ${SIM_SEED}, params: ${SIM_PARAMS}"
@go test ./simapp -run TestAppSimulationAfterImport ${SIM_COMMON_ARGS}

sim-after-import-random:
$(MAKE) sim-after-import SIM_SEED=$$RANDOM

sim-app-determinism:
@echo "--> Running app determinism simulation (blocks: ${SIM_NUM_BLOCKS}, commit: ${SIM_COMMIT}, period: ${SIM_PERIOD}, seed: ${SIM_SEED}, params: ${SIM_PARAMS}"
@go test ./simapp -run TestAppStateDeterminism ${SIM_COMMON_ARGS}

sim-app-determinism-random:
$(MAKE) sim-app-determinism SIM_SEED=$$RANDOM

.PHONY: sim-full-app sim-full-app-random sim-import-export sim-after-import sim-app-determinism sim-import-export-random sim-after-import-random sim-app-determinism-random
32 changes: 32 additions & 0 deletions ante/ante_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
protov2 "google.golang.org/protobuf/proto"

sdk "github.com/cosmos/cosmos-sdk/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"cosmossdk.io/math"
Expand Down Expand Up @@ -149,6 +150,37 @@ func TestAnteStakingFilter(t *testing.T) {
}
}

func TestAnteDisableWithdrawRewards(t *testing.T) {
ctx := sdk.Context{}
dwr := NewPOADisableWithdrawDelegatorRewards()

blockedMsgs := map[string]sdk.Msg{
"WithdrawDelegatorReward": &distrtypes.MsgWithdrawDelegatorReward{},
}

for k, msg := range blockedMsgs {
tx := MockTx{
msgs: []sdk.Msg{
msg,
},
}

t.Run(fmt.Sprintf("allow GenTx to pass (%s)", k), func(t *testing.T) {
ctx = setBlockHeader(ctx, 1)
_, err := dwr.AnteHandle(ctx, tx, false, EmptyAnte)
require.NoError(t, err)
})

t.Run(fmt.Sprintf("fail: withdraw rewards not allowed after gentx (%s)", k), func(t *testing.T) {
for h := uint64(2); h < 10; h++ {
ctx = setBlockHeader(ctx, h)
_, err := dwr.AnteHandle(ctx, tx, false, EmptyAnte)
require.Error(t, err)
}
})
}
}

func setBlockHeader(ctx sdk.Context, height uint64) sdk.Context {
h := ctx.BlockHeader()
h.Height = int64(height)
Expand Down
38 changes: 38 additions & 0 deletions ante/disable_withdraw_delegator_rewards.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package poaante

import (
sdk "github.com/cosmos/cosmos-sdk/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

"github.com/strangelove-ventures/poa"
)

type MsgDisableWithdrawDelegatorRewards struct {
}

func NewPOADisableWithdrawDelegatorRewards() MsgDisableWithdrawDelegatorRewards {
return MsgDisableWithdrawDelegatorRewards{}
}

func (mdwr MsgDisableWithdrawDelegatorRewards) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
currHeight := ctx.BlockHeight()
if currHeight <= 1 {
// allow GenTx to pass
return next(ctx, tx, simulate)
}

if mdwr.hasWithdrawDelegatorRewardsMsg(tx.GetMsgs()) {
return ctx, poa.ErrWithdrawDelegatorRewardsNotAllowed
}

return next(ctx, tx, simulate)
}

func (mdwr MsgDisableWithdrawDelegatorRewards) hasWithdrawDelegatorRewardsMsg(msgs []sdk.Msg) bool {
for _, msg := range msgs {
if _, ok := msg.(*distrtypes.MsgWithdrawDelegatorReward); ok {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically there is a security issue here with authz nested bypasses. but since it is a trusted val set for POA, if someone really does that kinda obvious who did

return true
}
}
return false
}
13 changes: 7 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
)

var (
ErrStakingActionNotAllowed = sdkerrors.Register(ModuleName, 1, "staking actions are now allowed on this chain")
ErrPowerBelowMinimum = sdkerrors.Register(ModuleName, 2, "power must be above 1_000_000")
ErrNotAnAuthority = sdkerrors.Register(ModuleName, 3, "not an authority")
ErrUnsafePower = sdkerrors.Register(ModuleName, 4, "unsafe: msg.Power is >30%% of total power, set unsafe=true to override")
ErrMustProvideAtLeastOneAddress = sdkerrors.Register(ModuleName, 5, "must provide at least one address")
ErrValidatorSelfRemoval = sdkerrors.Register(ModuleName, 6, "validator is not allowed to remove themselves")
ErrStakingActionNotAllowed = sdkerrors.Register(ModuleName, 1, "staking actions are now allowed on this chain")
ErrPowerBelowMinimum = sdkerrors.Register(ModuleName, 2, "power must be above 1_000_000")
ErrNotAnAuthority = sdkerrors.Register(ModuleName, 3, "not an authority")
ErrUnsafePower = sdkerrors.Register(ModuleName, 4, "unsafe: msg.Power is >=30% of total power, set unsafe=true to override")
ErrMustProvideAtLeastOneAddress = sdkerrors.Register(ModuleName, 5, "must provide at least one address")
ErrValidatorSelfRemoval = sdkerrors.Register(ModuleName, 6, "validator is not allowed to remove themselves")
ErrWithdrawDelegatorRewardsNotAllowed = sdkerrors.Register(ModuleName, 7, "withdraw delegator rewards is not allowed on this chain")
)
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ replace (

// Fixes POA retreaving jailed validators.
// - https://github.com/cosmos/cosmos-sdk/pull/20059
github.com/cosmos/cosmos-sdk => github.com/rollchains/cosmos-sdk v0.50.6
github.com/cosmos/cosmos-sdk => github.com/rollchains/cosmos-sdk v0.50.8

// breaks SDK app.toml parsing in ictest w/ SDK
github.com/spf13/viper => github.com/spf13/viper v1.17.0
)

require (
cosmossdk.io/api v0.7.4
cosmossdk.io/api v0.7.5
cosmossdk.io/collections v0.4.0
cosmossdk.io/core v0.12.0
cosmossdk.io/depinject v1.0.0-alpha.4
Expand All @@ -26,9 +26,10 @@ require (
github.com/cometbft/cometbft v0.38.9
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/cosmos-sdk v0.50.5
github.com/cosmos/gogoproto v1.4.12
github.com/cosmos/gogoproto v1.5.0
github.com/golang/protobuf v1.5.4
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
Expand All @@ -38,7 +39,7 @@ require (
)

require (
cosmossdk.io/x/tx v0.13.2 // indirect
cosmossdk.io/x/tx v0.13.3 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down Expand Up @@ -126,7 +127,6 @@ require (
github.com/oklog/run v1.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
Expand Down
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,8 @@ cloud.google.com/go/workflows v1.8.0/go.mod h1:ysGhmEajwZxGn1OhGOGKsTXc5PyxOc0vf
cloud.google.com/go/workflows v1.9.0/go.mod h1:ZGkj1aFIOd9c8Gerkjjq7OW7I5+l6cSvT3ujaO/WwSA=
cloud.google.com/go/workflows v1.10.0/go.mod h1:fZ8LmRmZQWacon9UCX1r/g/DfAXx5VcPALq2CxzdePw=
cloud.google.com/go/workflows v1.11.1/go.mod h1:Z+t10G1wF7h8LgdY/EmRcQY8ptBD/nvofaL6FqlET6g=
cosmossdk.io/api v0.7.4 h1:sPo8wKwCty1lht8kgL3J7YL1voJywP3YWuA5JKkBz30=
cosmossdk.io/api v0.7.4/go.mod h1:IcxpYS5fMemZGqyYtErK7OqvdM0C8kdW3dq8Q/XIG38=
cosmossdk.io/api v0.7.5 h1:eMPTReoNmGUm8DeiQL9DyM8sYDjEhWzL1+nLbI9DqtQ=
cosmossdk.io/api v0.7.5/go.mod h1:IcxpYS5fMemZGqyYtErK7OqvdM0C8kdW3dq8Q/XIG38=
cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s=
cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0=
cosmossdk.io/core v0.11.0 h1:vtIafqUi+1ZNAE/oxLOQQ7Oek2n4S48SWLG8h/+wdbo=
Expand All @@ -770,8 +770,8 @@ cosmossdk.io/math v1.3.0 h1:RC+jryuKeytIiictDslBP9i1fhkVm6ZDmZEoNP316zE=
cosmossdk.io/math v1.3.0/go.mod h1:vnRTxewy+M7BtXBNFybkuhSH4WfedVAAnERHgVFhp3k=
cosmossdk.io/store v1.1.0 h1:LnKwgYMc9BInn9PhpTFEQVbL9UK475G2H911CGGnWHk=
cosmossdk.io/store v1.1.0/go.mod h1:oZfW/4Fc/zYqu3JmQcQdUJ3fqu5vnYTn3LZFFy8P8ng=
cosmossdk.io/x/tx v0.13.2 h1:Kh90UH30bhnnUdJH+CmWLyaH8IKdY6BBGY3EkdOk82o=
cosmossdk.io/x/tx v0.13.2/go.mod h1:yhPokDCfXVIuAtyp49IFlWB5YAXUgD7Zek+ZHwsHzvU=
cosmossdk.io/x/tx v0.13.3 h1:Ha4mNaHmxBc6RMun9aKuqul8yHiL78EKJQ8g23Zf73g=
cosmossdk.io/x/tx v0.13.3/go.mod h1:I8xaHv0rhUdIvIdptKIqzYy27+n2+zBVaxO6fscFhys=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/edwards25519 v1.0.0 h1:0wAIcmJUqRdI8IJ/3eGi5/HwXZWPujYXXlkrQogz0Ek=
filippo.io/edwards25519 v1.0.0/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns=
Expand Down Expand Up @@ -928,8 +928,8 @@ github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4x
github.com/cosmos/gogogateway v1.2.0 h1:Ae/OivNhp8DqBi/sh2A8a1D0y638GpL3tkmLQAiKxTE=
github.com/cosmos/gogogateway v1.2.0/go.mod h1:iQpLkGWxYcnCdz5iAdLcRBSw3h7NXeOkZ4GUkT+tbFI=
github.com/cosmos/gogoproto v1.4.2/go.mod h1:cLxOsn1ljAHSV527CHOtaIP91kK6cCrZETRBrkzItWU=
github.com/cosmos/gogoproto v1.4.12 h1:vB6Lbe/rtnYGjQuFxkPiPYiCybqFT8QvLipDZP8JpFE=
github.com/cosmos/gogoproto v1.4.12/go.mod h1:LnZob1bXRdUoqMMtwYlcR3wjiElmlC+FkjaZRv1/eLY=
github.com/cosmos/gogoproto v1.5.0 h1:SDVwzEqZDDBoslaeZg+dGE55hdzHfgUA40pEanMh52o=
github.com/cosmos/gogoproto v1.5.0/go.mod h1:iUM31aofn3ymidYG6bUR5ZFrk+Om8p5s754eMUcyp8I=
github.com/cosmos/iavl v1.1.2 h1:zL9FK7C4L/P4IF1Dm5fIwz0WXCnn7Bp1M2FxH0ayM7Y=
github.com/cosmos/iavl v1.1.2/go.mod h1:jLeUvm6bGT1YutCaL2fIar/8vGUE8cPZvh/gXEWDaDM=
github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM=
Expand Down Expand Up @@ -1566,8 +1566,8 @@ github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/f
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/rollchains/cosmos-sdk v0.50.6 h1:7Ks3uJdn84r21POXp5XRT6PIpRxviBRBBzcPUk4GyY8=
github.com/rollchains/cosmos-sdk v0.50.6/go.mod h1:lVkRY6cdMJ0fG3gp8y4hFrsKZqF4z7y0M2UXFb9Yt40=
github.com/rollchains/cosmos-sdk v0.50.8 h1:56BmM9k6QxOGz+PgZ4vSrVwEaMy36WoZlGfwiJLdNZo=
github.com/rollchains/cosmos-sdk v0.50.8/go.mod h1:Zb+DgHtiByNwgj71IlJBXwOq6dLhtyAq3AgqpXm/jHo=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.8.3 h1:O+qNyWn7Z+F9M0ILBHgMVPuB1xTOucVd5gtaYyXBpRo=
github.com/rs/cors v1.8.3/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
Expand Down
Loading
Loading