Skip to content

Commit

Permalink
Merge pull request #33 from sei-protocol/tony-chen-update-deps
Browse files Browse the repository at this point in the history
Replace cosmos/tendermint/iavl/tmdb/ibc dependencies with forked versions
  • Loading branch information
codchen authored Jun 26, 2023
2 parents 0e88845 + b20d6a7 commit f6a5c6e
Show file tree
Hide file tree
Showing 22 changed files with 496 additions and 668 deletions.
47 changes: 24 additions & 23 deletions app/ante.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

wasmkeeper "github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmTypes "github.com/CosmWasm/wasmd/x/wasm/types"
paramskeeper "github.com/cosmos/cosmos-sdk/x/params/keeper"
)

// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
Expand All @@ -21,47 +22,47 @@ type HandlerOptions struct {
TXCounterStoreKey sdk.StoreKey
}

func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, sdk.AnteDepGenerator, error) {
if options.AccountKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler")
return nil, nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler")
}
if options.BankKeeper == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
return nil, nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
}
if options.SignModeHandler == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
return nil, nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}
if options.WasmConfig == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "wasm config is required for ante builder")
return nil, nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "wasm config is required for ante builder")
}
if options.TXCounterStoreKey == nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "tx counter key is required for ante builder")
return nil, nil, sdkerrors.Wrap(sdkerrors.ErrLogic, "tx counter key is required for ante builder")
}

sigGasConsumer := options.SigGasConsumer
if sigGasConsumer == nil {
sigGasConsumer = ante.DefaultSigVerificationGasConsumer
}

anteDecorators := []sdk.AnteDecorator{
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
wasmkeeper.NewLimitSimulationGasDecorator(options.WasmConfig.SimulationGasLimit), // after setup context to enforce limits early
wasmkeeper.NewCountTXDecorator(options.TXCounterStoreKey),
ante.NewRejectExtensionOptionsDecorator(),
ante.NewMempoolFeeDecorator(),
ante.NewValidateBasicDecorator(),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
anteDecorators := []sdk.AnteFullDecorator{
sdk.DefaultWrappedAnteDecorator(ante.NewDefaultSetUpContextDecorator()), // outermost AnteDecorator. SetUpContext must be called first
sdk.DefaultWrappedAnteDecorator(wasmkeeper.NewLimitSimulationGasDecorator(options.WasmConfig.SimulationGasLimit)), // after setup context to enforce limits early
sdk.DefaultWrappedAnteDecorator(wasmkeeper.NewCountTXDecorator(options.TXCounterStoreKey)),
sdk.DefaultWrappedAnteDecorator(ante.NewRejectExtensionOptionsDecorator()),
sdk.DefaultWrappedAnteDecorator(ante.NewValidateBasicDecorator()),
sdk.DefaultWrappedAnteDecorator(ante.NewTxTimeoutHeightDecorator()),
sdk.DefaultWrappedAnteDecorator(ante.NewValidateMemoDecorator(options.AccountKeeper)),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper),
// SetPubKeyDecorator must be called before all signature verification decorators
ante.NewSetPubKeyDecorator(options.AccountKeeper),
ante.NewValidateSigCountDecorator(options.AccountKeeper),
ante.NewSigGasConsumeDecorator(options.AccountKeeper, sigGasConsumer),
ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
ante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper, options.ParamsKeeper.(paramskeeper.Keeper), options.TxFeeChecker),
sdk.DefaultWrappedAnteDecorator(ante.NewSetPubKeyDecorator(options.AccountKeeper)), // SetPubKeyDecorator must be called before all signature verification decorators
sdk.DefaultWrappedAnteDecorator(ante.NewValidateSigCountDecorator(options.AccountKeeper)),
sdk.DefaultWrappedAnteDecorator(ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer)),
sdk.DefaultWrappedAnteDecorator(ante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler)),
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
ibcante.NewAnteDecorator(options.IBCKeeper),
sdk.DefaultWrappedAnteDecorator(ibcante.NewAnteDecorator(options.IBCKeeper)),
}

return sdk.ChainAnteDecorators(anteDecorators...), nil
anteHandler, anteDepGenerator := sdk.ChainAnteDecorators(anteDecorators...)

return anteHandler, anteDepGenerator, nil
}
124 changes: 122 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"context"
"fmt"
"io"
"net/http"
Expand All @@ -20,6 +21,7 @@ import (
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/utils"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authrest "github.com/cosmos/cosmos-sdk/x/auth/client/rest"
Expand Down Expand Up @@ -92,6 +94,7 @@ import (
porttypes "github.com/cosmos/ibc-go/v3/modules/core/05-port/types"
ibchost "github.com/cosmos/ibc-go/v3/modules/core/24-host"
ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper"
tmcfg "github.com/tendermint/tendermint/config"

// Note: please do your research before using this in production app, this is a demo and not an officially
// supported IBC team implementation. It has no known issues, but do your own research before using it.
Expand Down Expand Up @@ -279,6 +282,8 @@ type WasmApp struct {

// module configurator
configurator module.Configurator

txDecoder sdk.TxDecoder
}

// NewWasmApp returns a reference to an initialized WasmApp.
Expand All @@ -290,6 +295,7 @@ func NewWasmApp(
skipUpgradeHeights map[int64]bool,
homePath string,
invCheckPeriod uint,
tmConfig *tmcfg.Config,
encodingConfig wasmappparams.EncodingConfig,
enabledProposals []wasm.ProposalType,
appOpts servertypes.AppOptions,
Expand All @@ -299,7 +305,7 @@ func NewWasmApp(
appCodec, legacyAmino := encodingConfig.Marshaler, encodingConfig.Amino
interfaceRegistry := encodingConfig.InterfaceRegistry

bApp := baseapp.NewBaseApp(appName, logger, db, encodingConfig.TxConfig.TxDecoder(), baseAppOptions...)
bApp := baseapp.NewBaseApp(appName, logger, db, encodingConfig.TxConfig.TxDecoder(), tmConfig, appOpts, baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
bApp.SetInterfaceRegistry(interfaceRegistry)

Expand All @@ -322,6 +328,7 @@ func NewWasmApp(
keys: keys,
tkeys: tkeys,
memKeys: memKeys,
txDecoder: encodingConfig.TxConfig.TxDecoder(),
}

app.paramsKeeper = initParamsKeeper(
Expand Down Expand Up @@ -714,7 +721,7 @@ func NewWasmApp(
app.MountTransientStores(tkeys)
app.MountMemoryStores(memKeys)

anteHandler, err := NewAnteHandler(
anteHandler, anteDepGenerator, err := NewAnteHandler(
HandlerOptions{
HandlerOptions: ante.HandlerOptions{
AccountKeeper: app.accountKeeper,
Expand All @@ -733,9 +740,13 @@ func NewWasmApp(
}

app.SetAnteHandler(anteHandler)
app.SetAnteDepGenerator(anteDepGenerator)
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.SetPrepareProposalHandler(app.PrepareProposalHandler)
app.SetProcessProposalHandler(app.ProcessProposalHandler)
app.SetFinalizeBlocker(app.FinalizeBlocker)

// must be before Loading version
// requires the snapshot store to be created and registered as a BaseAppOption
Expand Down Expand Up @@ -774,6 +785,115 @@ func NewWasmApp(
// Name returns the name of the App
func (app *WasmApp) Name() string { return app.BaseApp.Name() }

func (app *WasmApp) PrepareProposalHandler(ctx sdk.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) {
return &abci.ResponsePrepareProposal{
TxRecords: utils.Map(req.Txs, func(tx []byte) *abci.TxRecord {
return &abci.TxRecord{Action: abci.TxRecord_UNMODIFIED, Tx: tx}
}),
}, nil
}

func (app *WasmApp) ProcessProposalHandler(ctx sdk.Context, req *abci.RequestProcessProposal) (*abci.ResponseProcessProposal, error) {
return &abci.ResponseProcessProposal{
Status: abci.ResponseProcessProposal_ACCEPT,
}, nil
}

func (app *WasmApp) FinalizeBlocker(ctx sdk.Context, req *abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error) {
events := []abci.Event{}
beginBlockResp := app.BeginBlock(ctx, abci.RequestBeginBlock{
Hash: req.Hash,
ByzantineValidators: utils.Map(req.ByzantineValidators, func(mis abci.Misbehavior) abci.Evidence {
return abci.Evidence{
Type: abci.MisbehaviorType(mis.Type),
Validator: abci.Validator(mis.Validator),
Height: mis.Height,
Time: mis.Time,
TotalVotingPower: mis.TotalVotingPower,
}
}),
LastCommitInfo: abci.LastCommitInfo{
Round: req.DecidedLastCommit.Round,
Votes: utils.Map(req.DecidedLastCommit.Votes, func(vote abci.VoteInfo) abci.VoteInfo {
return abci.VoteInfo{
Validator: abci.Validator(vote.Validator),
SignedLastBlock: vote.SignedLastBlock,
}
}),
},
Header: tmproto.Header{
ChainID: app.ChainID,
Height: req.Height,
Time: req.Time,
ProposerAddress: ctx.BlockHeader().ProposerAddress,
},
})
events = append(events, beginBlockResp.Events...)

typedTxs := []sdk.Tx{}
for _, tx := range req.Txs {
typedTx, err := app.txDecoder(tx)
if err != nil {
typedTxs = append(typedTxs, nil)
} else {
typedTxs = append(typedTxs, typedTx)
}
}

txResults := []*abci.ExecTxResult{}
for i, tx := range req.Txs {
ctx = ctx.WithContext(context.WithValue(ctx.Context(), ante.ContextKeyTxIndexKey, i))
deliverTxResp := app.DeliverTx(ctx, abci.RequestDeliverTx{
Tx: tx,
})
txResults = append(txResults, &abci.ExecTxResult{
Code: deliverTxResp.Code,
Data: deliverTxResp.Data,
Log: deliverTxResp.Log,
Info: deliverTxResp.Info,
GasWanted: deliverTxResp.GasWanted,
GasUsed: deliverTxResp.GasUsed,
Events: deliverTxResp.Events,
Codespace: deliverTxResp.Codespace,
})
}
endBlockResp := app.EndBlock(ctx, abci.RequestEndBlock{
Height: req.Height,
})
events = append(events, endBlockResp.Events...)

app.SetDeliverStateToCommit()
appHash := app.WriteStateToCommitAndGetWorkingHash()
return &abci.ResponseFinalizeBlock{
Events: events,
TxResults: txResults,
ValidatorUpdates: utils.Map(endBlockResp.ValidatorUpdates, func(v abci.ValidatorUpdate) abci.ValidatorUpdate {
return abci.ValidatorUpdate{
PubKey: v.PubKey,
Power: v.Power,
}
}),
ConsensusParamUpdates: &tmproto.ConsensusParams{
Block: &tmproto.BlockParams{
MaxBytes: endBlockResp.ConsensusParamUpdates.Block.MaxBytes,
MaxGas: endBlockResp.ConsensusParamUpdates.Block.MaxGas,
},
Evidence: &tmproto.EvidenceParams{
MaxAgeNumBlocks: endBlockResp.ConsensusParamUpdates.Evidence.MaxAgeNumBlocks,
MaxAgeDuration: endBlockResp.ConsensusParamUpdates.Evidence.MaxAgeDuration,
MaxBytes: endBlockResp.ConsensusParamUpdates.Block.MaxBytes,
},
Validator: &tmproto.ValidatorParams{
PubKeyTypes: endBlockResp.ConsensusParamUpdates.Validator.PubKeyTypes,
},
Version: &tmproto.VersionParams{
AppVersion: endBlockResp.ConsensusParamUpdates.Version.AppVersion,
},
},
AppHash: appHash,
}, nil
}

// application updates every begin block
func (app *WasmApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
return app.mm.BeginBlock(ctx, req)
Expand Down
19 changes: 12 additions & 7 deletions app/app_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"context"
"encoding/json"
"os"
"testing"
Expand All @@ -19,31 +20,33 @@ var emptyWasmOpts []wasm.Option = nil

func TestWasmdExport(t *testing.T) {
db := db.NewMemDB()
gapp := NewWasmApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, MakeEncodingConfig(), wasm.EnableAllProposals, EmptyBaseAppOptions{}, emptyWasmOpts)
gapp := NewWasmApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, nil, MakeEncodingConfig(), wasm.EnableAllProposals, EmptyBaseAppOptions{}, emptyWasmOpts)

genesisState := NewDefaultGenesisState()
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
require.NoError(t, err)

// Initialize the chain
gapp.InitChain(
abci.RequestInitChain{
context.Background(),
&abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
},
)
gapp.Commit()
gapp.SetDeliverStateToCommit()
gapp.Commit(context.Background())

// Making a new app object with the db, so that initchain hasn't been called
newGapp := NewWasmApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, MakeEncodingConfig(), wasm.EnableAllProposals, EmptyBaseAppOptions{}, emptyWasmOpts)
newGapp := NewWasmApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, nil, MakeEncodingConfig(), wasm.EnableAllProposals, EmptyBaseAppOptions{}, emptyWasmOpts)
_, err = newGapp.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}

// ensure that blocked addresses are properly set in bank keeper
func TestBlockedAddrs(t *testing.T) {
db := db.NewMemDB()
gapp := NewWasmApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, MakeEncodingConfig(), wasm.EnableAllProposals, EmptyBaseAppOptions{}, emptyWasmOpts)
gapp := NewWasmApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, nil, MakeEncodingConfig(), wasm.EnableAllProposals, EmptyBaseAppOptions{}, emptyWasmOpts)

for acc := range maccPerms {
t.Run(acc, func(t *testing.T) {
Expand Down Expand Up @@ -99,12 +102,14 @@ func setGenesis(gapp *WasmApp) error {

// Initialize the chain
gapp.InitChain(
abci.RequestInitChain{
context.Background(),
&abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
},
)

gapp.Commit()
gapp.SetDeliverStateToCommit()
gapp.Commit(context.Background())
return nil
}
6 changes: 3 additions & 3 deletions app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestAppImportExport(t *testing.T) {
}()

encConf := MakeEncodingConfig()
app := NewWasmApp(logger, db, nil, true, map[int64]bool{}, dir, simapp.FlagPeriodValue, encConf, wasm.EnableAllProposals, EmptyBaseAppOptions{}, nil, fauxMerkleModeOpt)
app := NewWasmApp(logger, db, nil, true, map[int64]bool{}, dir, simapp.FlagPeriodValue, nil, encConf, wasm.EnableAllProposals, EmptyBaseAppOptions{}, nil, fauxMerkleModeOpt)
require.Equal(t, appName, app.Name())

// Run randomized simulation
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestAppImportExport(t *testing.T) {
newDB.Close()
require.NoError(t, os.RemoveAll(newDir))
}()
newApp := NewWasmApp(logger, newDB, nil, true, map[int64]bool{}, newDir, simapp.FlagPeriodValue, encConf, wasm.EnableAllProposals, EmptyBaseAppOptions{}, nil, fauxMerkleModeOpt)
newApp := NewWasmApp(logger, newDB, nil, true, map[int64]bool{}, newDir, simapp.FlagPeriodValue, nil, encConf, wasm.EnableAllProposals, EmptyBaseAppOptions{}, nil, fauxMerkleModeOpt)
require.Equal(t, appName, newApp.Name())

var genesisState GenesisState
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestFullAppSimulation(t *testing.T) {
}()
encConf := MakeEncodingConfig()
app := NewWasmApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, simapp.FlagPeriodValue,
encConf, wasm.EnableAllProposals, simapp.EmptyAppOptions{}, nil, fauxMerkleModeOpt)
nil, encConf, wasm.EnableAllProposals, simapp.EmptyAppOptions{}, nil, fauxMerkleModeOpt)
require.Equal(t, "WasmApp", app.Name())

// run randomized simulation
Expand Down
Loading

0 comments on commit f6a5c6e

Please sign in to comment.