Skip to content

Commit

Permalink
Fix app, cmd and compilation of binary and docker reproducible issues. (
Browse files Browse the repository at this point in the history
  • Loading branch information
arhamchordia authored Aug 7, 2024
1 parent 948caac commit 28f7e4a
Show file tree
Hide file tree
Showing 23 changed files with 1,439 additions and 193 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
go mod download

# Cosmwasm - Download correct libwasmvm version
RUN ARCH=$(uname -m) && WASMVM_VERSION=$(go list -m github.com/CosmWasm/wasmvm/v2 | sed 's/.* //') && \
wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm_muslc.$ARCH.a \
-O /lib/libwasmvm_muslc.a && \
RUN WASMVM_VERSION=$(go list -m github.com/CosmWasm/wasmvm/v2 | cut -d ' ' -f 2) && \
wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm_muslc.$(uname -m).a \
-O /lib/libwasmvm_muslc.$(uname -m).a && \
# verify checksum
wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/checksums.txt -O /tmp/checksums.txt && \
sha256sum /lib/libwasmvm_muslc.a | grep $(cat /tmp/checksums.txt | grep libwasmvm_muslc.$ARCH | cut -d ' ' -f 1)
sha256sum /lib/libwasmvm_muslc.$(uname -m).a | grep $(cat /tmp/checksums.txt | grep libwasmvm_muslc.$(uname -m) | cut -d ' ' -f 1)

# Copy the remaining files
COPY . .
Expand Down
43 changes: 32 additions & 11 deletions ante/ante.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
package ante

import (
ibcante "github.com/cosmos/ibc-go/v8/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"

corestoretypes "cosmossdk.io/core/store"
errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
ibcante "github.com/cosmos/ibc-go/v8/modules/core/ante"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

quasarerrors "github.com/quasar-finance/quasar/types/errors"
)

// UseFeeMarketDecorator to make the integration testing easier: we can switch off its ante and post decorators with this flag
var UseFeeMarketDecorator = true

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
// channel keeper.
type HandlerOptions struct {
ante.HandlerOptions
Codec codec.BinaryCodec
IBCkeeper *ibckeeper.Keeper
Codec codec.BinaryCodec
IBCkeeper *ibckeeper.Keeper
StakingKeeper *stakingkeeper.Keeper
TXCounterStoreService corestoretypes.KVStoreService
WasmConfig *wasmtypes.WasmConfig
}

func NewAnteHandler(opts HandlerOptions) (sdk.AnteHandler, error) {
if opts.AccountKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler")
return nil, errorsmod.Wrap(quasarerrors.ErrLogic, "account keeper is required for AnteHandler")
}
if opts.BankKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
return nil, errorsmod.Wrap(quasarerrors.ErrLogic, "bank keeper is required for AnteHandler")
}
if opts.SignModeHandler == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for AnteHandler")
return nil, errorsmod.Wrap(quasarerrors.ErrLogic, "sign mode handler is required for AnteHandler")
}
if opts.IBCkeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "IBC keeper is required for AnteHandler")
return nil, errorsmod.Wrap(quasarerrors.ErrLogic, "IBC keeper is required for AnteHandler")
}

if opts.StakingKeeper == nil {
return nil, errorsmod.Wrap(quasarerrors.ErrNotFound, "staking param store is required for AnteHandler")
}

sigGasConsumer := opts.SigGasConsumer
Expand All @@ -38,13 +56,16 @@ func NewAnteHandler(opts HandlerOptions) (sdk.AnteHandler, error) {
}

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
wasmkeeper.NewLimitSimulationGasDecorator(opts.WasmConfig.SimulationGasLimit), // after setup context to enforce limits early
wasmkeeper.NewCountTXDecorator(opts.TXCounterStoreService),
ante.NewExtensionOptionsDecorator(opts.ExtensionOptionChecker),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(opts.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(opts.AccountKeeper),
ante.NewDeductFeeDecorator(opts.AccountKeeper, opts.BankKeeper, opts.FeegrantKeeper, opts.TxFeeChecker),
NewGovVoteDecorator(opts.Codec, opts.StakingKeeper),
NewGovExpeditedProposalsDecorator(opts.Codec),
ante.NewSetPubKeyDecorator(opts.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
ante.NewValidateSigCountDecorator(opts.AccountKeeper),
ante.NewSigGasConsumeDecorator(opts.AccountKeeper, sigGasConsumer),
Expand Down
79 changes: 79 additions & 0 deletions ante/gov_expedited_ante.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ante

import (
errorsmod "cosmossdk.io/errors"
quasarerrors "github.com/quasar-finance/quasar/types/errors"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)

var expeditedPropDecoratorEnabled = true

// SetExpeditedProposalsEnabled toggles the expedited proposals decorator on/off.
// Should only be used in testing - this is to allow simtests to pass.
func SetExpeditedProposalsEnabled(val bool) {
expeditedPropDecoratorEnabled = val
}

var expeditedPropsWhitelist = map[string]struct{}{
"/cosmos.upgrade.v1beta1.MsgSoftwareUpgrade": {},
"/cosmos.upgrade.v1beta1.MsgCancelUpgrade": {},
}

// Check if the proposal is whitelisted for expedited voting.
type GovExpeditedProposalsDecorator struct {
cdc codec.BinaryCodec
}

func NewGovExpeditedProposalsDecorator(cdc codec.BinaryCodec) GovExpeditedProposalsDecorator {
return GovExpeditedProposalsDecorator{
cdc: cdc,
}
}

// AnteHandle checks if the proposal is whitelisted for expedited voting.
// Only proposals submitted using "quasard tx gov submit-proposal" can be expedited.
// Legacy proposals submitted using "quasard tx gov submit-legacy-proposal" cannot be marked as expedited.
func (g GovExpeditedProposalsDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if expeditedPropDecoratorEnabled {
for _, msg := range tx.GetMsgs() {
prop, ok := msg.(*govv1.MsgSubmitProposal)
if !ok {
continue
}
if prop.Expedited {
if err := g.validateExpeditedGovProp(prop); err != nil {
return ctx, err
}
}
}
}
return next(ctx, tx, simulate)
}

func (g GovExpeditedProposalsDecorator) isWhitelisted(msgType string) bool {
_, ok := expeditedPropsWhitelist[msgType]
return ok
}

func (g GovExpeditedProposalsDecorator) validateExpeditedGovProp(prop *govv1.MsgSubmitProposal) error {
msgs := prop.GetMessages()
if len(msgs) == 0 {
return quasarerrors.ErrInvalidExpeditedProposal
}
for _, message := range msgs {
// in case of legacy content submitted using govv1.MsgSubmitProposal
if sdkMsg, isLegacy := message.GetCachedValue().(*govv1.MsgExecLegacyContent); isLegacy {
if !g.isWhitelisted(sdkMsg.Content.TypeUrl) {
return errorsmod.Wrapf(quasarerrors.ErrInvalidExpeditedProposal, "invalid Msg type: %s", sdkMsg.Content.TypeUrl)
}
continue
}
if !g.isWhitelisted(message.TypeUrl) {
return errorsmod.Wrapf(quasarerrors.ErrInvalidExpeditedProposal, "invalid Msg type: %s", message.TypeUrl)
}
}
return nil
}
215 changes: 215 additions & 0 deletions ante/gov_expedited_ante_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package ante_test

import (
"testing"

"cosmossdk.io/math"
upgradetypes "cosmossdk.io/x/upgrade/types"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/quasar-finance/quasar/ante"
"github.com/quasar-finance/quasar/app/helpers"
"github.com/stretchr/testify/require"
)

func TestGovExpeditedProposalsDecorator(t *testing.T) {
quasarApp := helpers.Setup(t)

testCases := []struct {
name string
ctx sdk.Context
msgs []sdk.Msg
expectErr bool
}{
// these cases should pass
{
name: "expedited - govv1.MsgSubmitProposal - MsgSoftwareUpgrade",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newGovProp([]sdk.Msg{&upgradetypes.MsgSoftwareUpgrade{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Plan: upgradetypes.Plan{
Name: "upgrade plan-plan",
Info: "some text here",
Height: 123456789,
},
}}, true),
},
expectErr: false,
},
{
name: "expedited - govv1.MsgSubmitProposal - MsgCancelUpgrade",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newGovProp([]sdk.Msg{&upgradetypes.MsgCancelUpgrade{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
}}, true),
},
expectErr: false,
},
{
name: "normal - govv1.MsgSubmitProposal - TextProposal",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newLegacyTextProp(false), // normal
},
expectErr: false,
},
{
name: "normal - govv1.MsgSubmitProposal - MsgCommunityPoolSpend",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newGovProp([]sdk.Msg{&distrtypes.MsgCommunityPoolSpend{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Recipient: sdk.AccAddress{}.String(),
Amount: sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(100))),
}}, false), // normal
},
expectErr: false,
},
{
name: "normal - govv1.MsgSubmitProposal - MsgTransfer",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newGovProp([]sdk.Msg{&banktypes.MsgSend{
FromAddress: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
ToAddress: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Amount: sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(100))),
}}, false), // normal
},
expectErr: false,
},
{
name: "normal - govv1.MsgSubmitProposal - MsgUpdateParams",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newGovProp([]sdk.Msg{&banktypes.MsgUpdateParams{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
}}, false),
},
expectErr: false,
},
// legacy proposals - antehandler should not affect them
// submitted using "quasard tx gov submit-legacy-proposal"
{
name: "normal - govv1beta.MsgSubmitProposal - LegacySoftwareUpgrade",
ctx: sdk.Context{},
msgs: []sdk.Msg{newGovV1BETA1LegacyUpgradeProp()},
expectErr: false,
},
{
name: "normal - govv1beta.MsgSubmitProposal - LegacyCancelSoftwareUpgrade",
ctx: sdk.Context{},
msgs: []sdk.Msg{newGovV1BETA1LegacyCancelUpgradeProp()},
expectErr: false,
},
// these cases should fail
// these are normal proposals, not whitelisted for expedited voting
{
name: "fail - expedited - govv1.MsgSubmitProposal - Empty",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newGovProp([]sdk.Msg{}, true),
},
expectErr: true,
},
{
name: "fail - expedited - govv1.MsgSubmitProposal - TextProposal",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newLegacyTextProp(true), // expedite
},
expectErr: true,
},
{
name: "fail - expedited - govv1.MsgSubmitProposal - MsgCommunityPoolSpend",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newGovProp([]sdk.Msg{&distrtypes.MsgCommunityPoolSpend{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Recipient: sdk.AccAddress{}.String(),
Amount: sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(100))),
}}, true),
},
expectErr: true,
},
{
name: "fail - expedited - govv1.MsgSubmitProposal - MsgTransfer",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newGovProp([]sdk.Msg{&banktypes.MsgSend{
FromAddress: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
ToAddress: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Amount: sdk.NewCoins(sdk.NewCoin("stake", math.NewInt(100))),
}}, true),
},
expectErr: true,
},
{
name: "fail - expedited - govv1.MsgSubmitProposal - MsgUpdateParams",
ctx: sdk.Context{},
msgs: []sdk.Msg{
newGovProp([]sdk.Msg{&banktypes.MsgUpdateParams{
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
}}, true),
},
expectErr: true,
},
}

for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
txCfg := quasarApp.GetTxConfig()
decorator := ante.NewGovExpeditedProposalsDecorator(quasarApp.AppCodec())

txBuilder := txCfg.NewTxBuilder()
require.NoError(t, txBuilder.SetMsgs(tc.msgs...))

_, err := decorator.AnteHandle(tc.ctx, txBuilder.GetTx(), false,
func(ctx sdk.Context, _ sdk.Tx, _ bool) (sdk.Context, error) { return ctx, nil })
if tc.expectErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}

func newLegacyTextProp(expedite bool) *govv1.MsgSubmitProposal {
testProposal := govv1beta1.NewTextProposal("Proposal", "Test as normal proposal")
msgContent, err := govv1.NewLegacyContent(testProposal, "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn")
if err != nil {
return nil
}
return newGovProp([]sdk.Msg{msgContent}, expedite)
}

func newGovV1BETA1LegacyUpgradeProp() *govv1beta1.MsgSubmitProposal {
legacyContent := upgradetypes.NewSoftwareUpgradeProposal("test legacy upgrade", "test legacy upgrade", upgradetypes.Plan{
Name: "upgrade plan-plan",
Info: "some text here",
Height: 123456789,
})

msg, _ := govv1beta1.NewMsgSubmitProposal(legacyContent, sdk.NewCoins(), sdk.AccAddress{})
return msg
}

func newGovV1BETA1LegacyCancelUpgradeProp() *govv1beta1.MsgSubmitProposal {
legacyContent := upgradetypes.NewCancelSoftwareUpgradeProposal("test legacy upgrade", "test legacy upgrade")

msg, _ := govv1beta1.NewMsgSubmitProposal(legacyContent, sdk.NewCoins(), sdk.AccAddress{})
return msg
}

func newGovProp(msgs []sdk.Msg, expedite bool) *govv1.MsgSubmitProposal {
msg, _ := govv1.NewMsgSubmitProposal(msgs, sdk.NewCoins(), sdk.AccAddress{}.String(), "", "expedite", "expedite", expedite)
// fmt.Println("### msg ###", msg, "err", err)
return msg
}
Loading

0 comments on commit 28f7e4a

Please sign in to comment.