Skip to content

Commit

Permalink
Merge branch 'main' into 92-implement-pop-result-handler
Browse files Browse the repository at this point in the history
Signed-off-by: Jürgen Eckel <[email protected]>
  • Loading branch information
eckelj committed Nov 14, 2023
2 parents fba1c77 + 1e1138d commit eca8bc6
Show file tree
Hide file tree
Showing 110 changed files with 7,810 additions and 628 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/audit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Setup Go
uses: actions/setup-go@v4
Expand All @@ -24,6 +26,13 @@ jobs:
- name: Build
run: go build -v ./...

- name: Check generated files
run: |
curl https://get.ignite.com/cli | bash
./ignite chain init --clear-cache --yes
rm ignite
if [ "$(git diff --stat | wc -l)" -gt 0 ]; then exit 1; fi
- name: Run gofmt
run: if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi

Expand All @@ -47,7 +56,7 @@ jobs:
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest

- name: Run golangci-lint
run: golangci-lint run --timeout 5m
run: golangci-lint run

- name: Run tests
run: go test -race -vet=off ./...
24 changes: 24 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
run:
timeout: 5m
linters:
enable:
- errcheck
- forbidigo
- gosimple
- govet
- ineffassign
- revive
- staticcheck
- unused
issues:
exclude-rules:
- path: codec\.go
linters:
- nosnakecase
- path: app\/simulation_test\.go
linters:
- nosnakecase
- path: testutil\/rest\.go
linters:
- nosnakecase
65 changes: 8 additions & 57 deletions app/ante/ante.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package ante

import (
assettypes "github.com/planetmint/planetmint-go/x/asset/types"
daotypes "github.com/planetmint/planetmint-go/x/dao/types"
machinetypes "github.com/planetmint/planetmint-go/x/machine/types"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
Expand All @@ -14,57 +10,6 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)

type CheckMachineDecorator struct {
mk MachineKeeper
}

func NewCheckMachineDecorator(mk MachineKeeper) CheckMachineDecorator {
return CheckMachineDecorator{
mk: mk,
}
}

func (cm CheckMachineDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
for _, msg := range tx.GetMsgs() {
switch sdk.MsgTypeURL(msg) {
case "/planetmintgo.asset.MsgNotarizeAsset":
notarizeMsg, ok := msg.(*assettypes.MsgNotarizeAsset)
if ok {
_, found := cm.mk.GetMachineIndexByAddress(ctx, notarizeMsg.GetCreator())
if !found {
return ctx, errorsmod.Wrapf(machinetypes.ErrMachineNotFound, "error during CheckTx or ReCheckTx")
}
}
case "/planetmintgo.machine.MsgAttestMachine":
attestMsg, ok := msg.(*machinetypes.MsgAttestMachine)
if ok {
if attestMsg.GetCreator() != attestMsg.Machine.GetAddress() {
return ctx, errorsmod.Wrapf(machinetypes.ErrMachineIsNotCreator, "error during CheckTx or ReCheckTx")
}
_, activated, found := cm.mk.GetTrustAnchor(ctx, attestMsg.Machine.MachineId)
if !found {
return ctx, errorsmod.Wrapf(machinetypes.ErrTrustAnchorNotFound, "error during CheckTx or ReCheckTx")
}
if activated {
return ctx, errorsmod.Wrapf(machinetypes.ErrTrustAnchorAlreadyInUse, "error during CheckTx or ReCheckTx")
}
}
case "planetmintgo.dao.MsgReportPoPResult":
popMsg, ok := msg.(*daotypes.MsgReportPopResult)
if ok {
_, found := cm.mk.GetMachineIndexByAddress(ctx, popMsg.GetCreator())
if !found {
return ctx, errorsmod.Wrapf(machinetypes.ErrMachineNotFound, "error during CheckTx or ReCheckTx")
}
}
default:
continue
}
}

return next(ctx, tx, simulate)
}

// HandlerOptions are the options required for constructing a default SDK AnteHandler.
type HandlerOptions struct {
AccountKeeper AccountKeeper
Expand All @@ -73,8 +18,9 @@ type HandlerOptions struct {
FeegrantKeeper FeegrantKeeper
SignModeHandler authsigning.SignModeHandler
SigGasConsumer func(meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params) error
TxFeeChecker ante.TxFeeChecker
TxFeeChecker TxFeeChecker
MachineKeeper MachineKeeper
DaoKeeper DaoKeeper
}

// NewAnteHandler returns an AnteHandler that checks and increments sequence
Expand All @@ -96,6 +42,9 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.MachineKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "machine keeper is required for ante builder")
}
if options.DaoKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "dao keeper is required for ante builder")
}

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
Expand All @@ -104,8 +53,10 @@ func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
NewCheckMachineDecorator(options.MachineKeeper),
NewCheckMintAddressDecorator(options.DaoKeeper),
NewCheckReissuanceDecorator(),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.TxFeeChecker),
ante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
Expand Down
60 changes: 60 additions & 0 deletions app/ante/check_machine_decorator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package ante

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
assettypes "github.com/planetmint/planetmint-go/x/asset/types"
daotypes "github.com/planetmint/planetmint-go/x/dao/types"
machinetypes "github.com/planetmint/planetmint-go/x/machine/types"
)

type CheckMachineDecorator struct {
mk MachineKeeper
}

func NewCheckMachineDecorator(mk MachineKeeper) CheckMachineDecorator {
return CheckMachineDecorator{
mk: mk,
}
}

func (cm CheckMachineDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
for _, msg := range tx.GetMsgs() {
switch sdk.MsgTypeURL(msg) {
case "/planetmintgo.asset.MsgNotarizeAsset":
notarizeMsg, ok := msg.(*assettypes.MsgNotarizeAsset)
if ok {
_, found := cm.mk.GetMachineIndexByAddress(ctx, notarizeMsg.GetCreator())
if !found {
return ctx, errorsmod.Wrapf(machinetypes.ErrMachineNotFound, "error during CheckTx or ReCheckTx")
}
}
case "/planetmintgo.machine.MsgAttestMachine":
attestMsg, ok := msg.(*machinetypes.MsgAttestMachine)
if ok {
if attestMsg.GetCreator() != attestMsg.Machine.GetAddress() {
return ctx, errorsmod.Wrapf(machinetypes.ErrMachineIsNotCreator, "error during CheckTx or ReCheckTx")
}
_, activated, found := cm.mk.GetTrustAnchor(ctx, attestMsg.Machine.MachineId)
if !found {
return ctx, errorsmod.Wrapf(machinetypes.ErrTrustAnchorNotFound, "error during CheckTx or ReCheckTx")
}
if activated {
return ctx, errorsmod.Wrapf(machinetypes.ErrTrustAnchorAlreadyInUse, "error during CheckTx or ReCheckTx")
}
}
case "planetmintgo.dao.MsgReportPoPResult":
popMsg, ok := msg.(*daotypes.MsgReportPopResult)
if ok {
_, found := cm.mk.GetMachineIndexByAddress(ctx, popMsg.GetCreator())
if !found {
return ctx, errorsmod.Wrapf(machinetypes.ErrMachineNotFound, "error during CheckTx or ReCheckTx")
}
}
default:
continue
}
}

return next(ctx, tx, simulate)
}
38 changes: 38 additions & 0 deletions app/ante/check_mint_address_decorator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package ante

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/planetmint/planetmint-go/config"
daotypes "github.com/planetmint/planetmint-go/x/dao/types"
)

type CheckMintAddressDecorator struct {
dk DaoKeeper
}

func NewCheckMintAddressDecorator(dk DaoKeeper) CheckMintAddressDecorator {
return CheckMintAddressDecorator{
dk: dk,
}
}

func (cmad CheckMintAddressDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
for _, msg := range tx.GetMsgs() {
if sdk.MsgTypeURL(msg) == "/planetmintgo.dao.MsgMintToken" {
mintMsg, ok := msg.(*daotypes.MsgMintToken)
if ok {
cfg := config.GetConfig()
if mintMsg.Creator != cfg.MintAddress {
return ctx, errorsmod.Wrapf(daotypes.ErrInvalidMintAddress, "expected: %s; got: %s", cfg.MintAddress, mintMsg.Creator)
}
_, found := cmad.dk.GetMintRequestByHash(ctx, mintMsg.GetMintRequest().GetLiquidTxHash())
if found {
return ctx, errorsmod.Wrapf(daotypes.ErrAlreadyMinted, "liquid tx hash %s has already been minted", mintMsg.GetMintRequest().GetLiquidTxHash())
}
}
}
}

return next(ctx, tx, simulate)
}
35 changes: 35 additions & 0 deletions app/ante/check_reissuance_decorator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ante

import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/planetmint/planetmint-go/config"
"github.com/planetmint/planetmint-go/x/dao"
daotypes "github.com/planetmint/planetmint-go/x/dao/types"
)

type CheckReissuanceDecorator struct{}

func NewCheckReissuanceDecorator() CheckReissuanceDecorator {
return CheckReissuanceDecorator{}
}

func (cmad CheckReissuanceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
logger := ctx.Logger()
for _, msg := range tx.GetMsgs() {
if sdk.MsgTypeURL(msg) == "/planetmintgo.dao.MsgReissueRDDLProposal" {
MsgProposal, ok := msg.(*daotypes.MsgReissueRDDLProposal)
if ok {
logger.Debug("REISSUE: receive Proposal")
conf := config.GetConfig()
isValid := dao.IsValidReissuanceCommand(MsgProposal.GetTx(), conf.ReissuanceAsset, MsgProposal.GetBlockHeight())
if !isValid {
logger.Debug("REISSUE: Invalid Proposal")
return ctx, errorsmod.Wrapf(daotypes.ErrReissuanceProposal, "error during CheckTx or ReCheckTx")
}
}
}
}

return next(ctx, tx, simulate)
}
Loading

0 comments on commit eca8bc6

Please sign in to comment.