Skip to content

Commit

Permalink
Merge branch 'main' into reece/add-authority-to-keeper-admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Jul 23, 2024
2 parents 850903a + 54217d0 commit 5d3244d
Show file tree
Hide file tree
Showing 32 changed files with 1,453 additions and 2,294 deletions.
1 change: 0 additions & 1 deletion .coverageignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
*.pb.go
*.pb.gw.go
*.pulsar.go
*_simulation.go
30 changes: 30 additions & 0 deletions .github/workflows/simulator.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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
- name: Simulation import/export (fixed seed)
run: make sim-import-export
- name: Simulate application state determinism (fixed seed)
run: make sim-app-determinism
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
# Depinject debug file
debug_container.dot

build/\
build/

go.work.sum
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
118 changes: 106 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,43 @@ ictest-val-remove:

.PHONY: test ictest-poa ictest-jail ictest-val-add ictest-val-remove

COV_ROOT="/tmp/poa-coverage"
COV_UNIT_E2E="${COV_ROOT}/unit-e2e"
COV_SIMULATION="${COV_ROOT}/simulation"
COV_PKG="github.com/strangelove-ventures/poa/..."
COV_SIM_CMD=${COV_SIMULATION}/simulation.test
COV_SIM_COMMON=-Enabled=True -NumBlocks=100 -Commit=true -Period=5 -Params=$(shell pwd)/simulation/sim_params.json -Verbose=false -test.v -test.gocoverdir=${COV_SIMULATION}

coverage: ## Run coverage report
@echo "--> Running coverage"
@go test -race -cpu=$$(nproc) -covermode=atomic -coverprofile=coverage.out $$(go list ./...) ./e2e/... ./simapp/... -coverpkg=github.com/strangelove-ventures/poa/... > /dev/null 2>&1
@echo "--> Running coverage filter"
@./scripts/filter-coverage.sh
@echo "--> Running coverage report"
@go tool cover -func=coverage-filtered.out
@echo "--> Running coverage html"
@go tool cover -html=coverage-filtered.out -o coverage.html
@echo "--> Creating GOCOVERDIR"
@mkdir -p ${COV_UNIT_E2E} ${COV_SIMULATION}
@echo "--> Cleaning up coverage files, if any"
@rm -rf ${COV_UNIT_E2E}/* ${COV_SIMULATION}/*
@echo "--> Building instrumented simulation test binary"
@go test -c ./simapp -mod=readonly -covermode=atomic -coverpkg=${COV_PKG} -cover -o ${COV_SIM_CMD}
@echo " --> Running Full App Simulation"
@${COV_SIM_CMD} -test.run TestFullAppSimulation ${COV_SIM_COMMON} > /dev/null 2>&1
@echo " --> Running App Simulation After Import"
@${COV_SIM_CMD} -test.run TestAppSimulationAfterImport ${COV_SIM_COMMON} > /dev/null 2>&1
@echo " --> Running App Import/Export Simulation"
@${COV_SIM_CMD} -test.run TestAppImportExport ${COV_SIM_COMMON} > /dev/null 2>&1
@echo " --> Running App State Determinism Simulation"
@${COV_SIM_CMD} -test.run TestAppStateDeterminism ${COV_SIM_COMMON} > /dev/null 2>&1
@echo "--> Running unit & e2e tests coverage"
@go test -race -covermode=atomic -v -cpu=$$(nproc) -cover $$(go list ./...) ./e2e/... ./simapp/... -coverpkg=${COV_PKG} -args -test.gocoverdir="${COV_UNIT_E2E}" > /dev/null 2>&1
@echo "--> Merging coverage reports"
@go tool covdata merge -i=${COV_UNIT_E2E},${COV_SIMULATION} -o ${COV_ROOT}
@echo "--> Converting binary coverage report to text format"
@go tool covdata textfmt -i=${COV_ROOT} -o ${COV_ROOT}/coverage-merged.out
@echo "--> Filtering coverage reports"
@./scripts/filter-coverage.sh ${COV_ROOT}/coverage-merged.out ${COV_ROOT}/coverage-merged-filtered.out
@echo "--> Generating coverage report"
@go tool cover -func=${COV_ROOT}/coverage-merged-filtered.out
@echo "--> Generating HTML coverage report"
@go tool cover -html=${COV_ROOT}/coverage-merged-filtered.out -o coverage.html
@echo "--> Coverage report available at coverage.html"
@echo "--> Cleaning up coverage files"
@rm coverage.out
@rm -rf ${COV_UNIT_E2E}/* ${COV_SIMULATION}/*
@echo "--> Running coverage complete"

.PHONY: coverage
Expand All @@ -64,7 +89,7 @@ local-image:
### Protobuf ####
###################

protoVer=0.13.2
protoVer=0.14.0
protoImageName=ghcr.io/cosmos/proto-builder:$(protoVer)
protoImage=$(DOCKER) run --rm -v $(CURDIR):/workspace --workdir /workspace $(protoImageName)

Expand All @@ -88,7 +113,7 @@ proto-lint:
##################

golangci_lint_cmd=golangci-lint
golangci_version=v1.55.2
golangci_version=v1.59.1

lint:
@echo "--> Running linter"
Expand All @@ -100,4 +125,73 @@ 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

##################
### Security ###
##################
govulncheck_version=latest

govulncheck-install:
@echo "--> Installing govulncheck $(govulncheck_version)"
@go install golang.org/x/vuln/cmd/govulncheck@$(govulncheck_version)
@echo "--> Installing govulncheck $(govulncheck_version) complete"

govulncheck: ## Run govulncheck
@echo "--> Running govulncheck"
$(MAKE) govulncheck-install
@govulncheck ./... ./e2e/... ./simapp/...

.PHONY: govulncheck govulncheck-install

vet: ## Run go vet
@echo "--> Running go vet"
@go vet ./...

.PHONY: vet

##################
### 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
51 changes: 51 additions & 0 deletions ante/disable_withdraw_delegator_rewards.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package poaante

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
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 {
// authz nested message check (recursive)
if execMsg, ok := msg.(*authz.MsgExec); ok {
msgs, err := execMsg.GetMessages()
if err != nil {
return true
}

if mdwr.hasWithdrawDelegatorRewardsMsg(msgs) {
return true
}
}

if _, ok := msg.(*distrtypes.MsgWithdrawDelegatorReward); ok {
return true
}
}
return false
}
17 changes: 8 additions & 9 deletions e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ replace (
cosmossdk.io/core => cosmossdk.io/core v0.11.0
github.com/ChainSafe/go-schnorrkel => github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d
github.com/ChainSafe/go-schnorrkel/1 => github.com/ChainSafe/go-schnorrkel v1.0.0
github.com/btcsuite/btcd => github.com/btcsuite/btcd v0.22.2 //indirect

github.com/cosmos/cosmos-sdk => github.com/rollchains/cosmos-sdk v0.50.8
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

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

github.com/strangelove-ventures/poa => ../.

github.com/vedhavyas/go-subkey => github.com/strangelove-ventures/go-subkey v1.0.7
)

require (
cosmossdk.io/math v1.3.0
github.com/cosmos/cosmos-sdk v0.50.5
github.com/cosmos/cosmos-sdk v0.50.8
github.com/rs/zerolog v1.32.0
github.com/strangelove-ventures/interchaintest/v8 v8.2.1-0.20240405204947-1c05790eb2d8
github.com/strangelove-ventures/poa v0.0.0-00010101000000-000000000000
Expand All @@ -33,15 +32,15 @@ require (
cloud.google.com/go/compute/metadata v0.3.0 // indirect
cloud.google.com/go/iam v1.1.6 // indirect
cloud.google.com/go/storage v1.36.0 // indirect
cosmossdk.io/api v0.7.4 // indirect
cosmossdk.io/api v0.7.5 // indirect
cosmossdk.io/collections v0.4.0 // indirect
cosmossdk.io/core v0.12.0 // indirect
cosmossdk.io/depinject v1.0.0-alpha.4 // indirect
cosmossdk.io/errors v1.0.1 // indirect
cosmossdk.io/log v1.3.1 // indirect
cosmossdk.io/store v1.1.0 // indirect
cosmossdk.io/x/feegrant v0.1.0 // indirect
cosmossdk.io/x/tx v0.13.2 // indirect
cosmossdk.io/x/tx v0.13.3 // indirect
cosmossdk.io/x/upgrade v0.1.1 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
Expand Down Expand Up @@ -72,14 +71,14 @@ require (
github.com/cockroachdb/pebble v1.1.0 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft v0.38.9 // indirect
github.com/cometbft/cometbft v0.38.10 // indirect
github.com/cometbft/cometbft-db v0.9.1 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v1.0.2 // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.5 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/gogoproto v1.4.12 // indirect
github.com/cosmos/gogoproto v1.5.0 // indirect
github.com/cosmos/iavl v1.1.2 // indirect
github.com/cosmos/ibc-go/modules/capability v1.0.0 // indirect
github.com/cosmos/ibc-go/v8 v8.1.0 // indirect
Expand Down Expand Up @@ -205,7 +204,7 @@ require (
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/rs/cors v1.11.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
Expand Down
Loading

0 comments on commit 5d3244d

Please sign in to comment.