Skip to content

Commit

Permalink
fix: apply further terra classic specifics
Browse files Browse the repository at this point in the history
  • Loading branch information
fragwuerdig committed Mar 8, 2024
1 parent b926886 commit 1040442
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 49 deletions.
30 changes: 22 additions & 8 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,19 @@ func (app *BaseApp) ProcessProposal(req abci.RequestProcessProposal) (resp abci.
return resp
}

func isOracleTx(msgs []sdk.Msg) bool {
for _, msg := range msgs {
if sdk.MsgTypeURL(msg) == "/terra.oracle.v1beta1.MsgAggregateExchangeRatePrevote" ||
sdk.MsgTypeURL(msg) == "/terra.oracle.v1beta1.MsgAggregateExchangeRateVote" {
continue
} else {
return false
}
}

return true
}

// CheckTx implements the ABCI interface and executes a tx in CheckTx mode. In
// CheckTx mode, messages are not executed. This means messages are only validated
// and only the AnteHandler is executed. State is persisted to the BaseApp's
Expand All @@ -371,18 +384,19 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) abci.ResponseCheckTx {
panic(fmt.Sprintf("unknown RequestCheckTx type: %s", req.Type))
}

gInfo, result, anteEvents, priority, err := app.runTx(mode, req.Tx)
gInfo, result, anteEvents, priority, tx, err := app.runTx(mode, req.Tx)
if err != nil {
return sdkerrors.ResponseCheckTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, anteEvents, app.trace)
}

return abci.ResponseCheckTx{
GasWanted: int64(gInfo.GasWanted), // TODO: Should type accept unsigned ints?
GasUsed: int64(gInfo.GasUsed), // TODO: Should type accept unsigned ints?
Log: result.Log,
Data: result.Data,
Events: sdk.MarkEventsToIndex(result.Events, app.indexEvents),
Priority: priority,
GasWanted: int64(gInfo.GasWanted), // TODO: Should type accept unsigned ints?
GasUsed: int64(gInfo.GasUsed), // TODO: Should type accept unsigned ints?
Log: result.Log,
Data: result.Data,
Events: sdk.MarkEventsToIndex(result.Events, app.indexEvents),
Priority: priority,
IsOracleTx: isOracleTx(tx.GetMsgs()),
}
}

Expand Down Expand Up @@ -410,7 +424,7 @@ func (app *BaseApp) DeliverTx(req abci.RequestDeliverTx) (res abci.ResponseDeliv
telemetry.SetGauge(float32(gInfo.GasWanted), "tx", "gas", "wanted")
}()

gInfo, result, anteEvents, _, err := app.runTx(runTxModeDeliver, req.Tx)
gInfo, result, anteEvents, _, _, err := app.runTx(runTxModeDeliver, req.Tx)
if err != nil {
resultStr = "failed"
return sdkerrors.ResponseDeliverTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, sdk.MarkEventsToIndex(anteEvents, app.indexEvents), app.trace)
Expand Down
26 changes: 13 additions & 13 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func NewBaseApp(
logger: logger,
name: name,
db: db,
cms: store.NewCommitMultiStore(db),
cms: store.NewCommitMultiStoreWithLogger(db, logger),
storeLoader: DefaultStoreLoader,
grpcQueryRouter: NewGRPCQueryRouter(),
msgServiceRouter: NewMsgServiceRouter(),
Expand Down Expand Up @@ -626,7 +626,7 @@ func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context
// Note, gas execution info is always returned. A reference to a Result is
// returned if the tx does not run out of gas and if all the messages are valid
// and execute successfully. An error is returned otherwise.
func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, result *sdk.Result, anteEvents []abci.Event, priority int64, err error) {
func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, result *sdk.Result, anteEvents []abci.Event, priority int64, tx sdk.Tx, err error) {
// NOTE: GasWanted should be returned by the AnteHandler. GasUsed is
// determined by the GasMeter. We need access to the context to get the gas
// meter, so we initialize upfront.
Expand All @@ -637,7 +637,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re

// only run the tx if there is block gas remaining
if mode == runTxModeDeliver && ctx.BlockGasMeter().IsOutOfGas() {
return gInfo, nil, nil, 0, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx")
return gInfo, nil, nil, 0, nil, sdkerrors.Wrap(sdkerrors.ErrOutOfGas, "no block gas left to run tx")
}

defer func() {
Expand Down Expand Up @@ -674,14 +674,14 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
defer consumeBlockGas()
}

tx, err := app.txDecoder(txBytes)
tx, err = app.txDecoder(txBytes)
if err != nil {
return sdk.GasInfo{}, nil, nil, 0, err
return sdk.GasInfo{}, nil, nil, 0, nil, err
}

msgs := tx.GetMsgs()
if err := validateBasicTxMsgs(msgs); err != nil {
return sdk.GasInfo{}, nil, nil, 0, err
return sdk.GasInfo{}, nil, nil, 0, nil, err
}

if app.anteHandler != nil {
Expand Down Expand Up @@ -717,7 +717,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
gasWanted = ctx.GasMeter().Limit()

if err != nil {
return gInfo, nil, nil, 0, err
return gInfo, nil, nil, 0, nil, err
}

priority = ctx.Priority()
Expand All @@ -728,12 +728,12 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
if mode == runTxModeCheck {
err = app.mempool.Insert(ctx, tx)
if err != nil {
return gInfo, nil, anteEvents, priority, err
return gInfo, nil, anteEvents, priority, nil, err
}
} else if mode == runTxModeDeliver {
err = app.mempool.Remove(tx)
if err != nil && !errors.Is(err, mempool.ErrTxNotFound) {
return gInfo, nil, anteEvents, priority,
return gInfo, nil, anteEvents, priority, nil,
fmt.Errorf("failed to remove tx from mempool: %w", err)
}
}
Expand All @@ -759,7 +759,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re

newCtx, err := app.postHandler(postCtx, tx, mode == runTxModeSimulate, err == nil)
if err != nil {
return gInfo, nil, anteEvents, priority, err
return gInfo, nil, anteEvents, priority, nil, err
}

result.Events = append(result.Events, newCtx.EventManager().ABCIEvents()...)
Expand All @@ -778,7 +778,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
}
}

return gInfo, result, anteEvents, priority, err
return gInfo, result, anteEvents, priority, tx, err
}

// runMsgs iterates through a list of messages and executes them with the provided
Expand Down Expand Up @@ -884,7 +884,7 @@ func (app *BaseApp) PrepareProposalVerifyTx(tx sdk.Tx) ([]byte, error) {
return nil, err
}

_, _, _, _, err = app.runTx(runTxPrepareProposal, bz) //nolint:dogsled
_, _, _, _, _, err = app.runTx(runTxPrepareProposal, bz) //nolint:dogsled
if err != nil {
return nil, err
}
Expand All @@ -903,7 +903,7 @@ func (app *BaseApp) ProcessProposalVerifyTx(txBz []byte) (sdk.Tx, error) {
return nil, err
}

_, _, _, _, err = app.runTx(runTxProcessProposal, txBz) //nolint:dogsled
_, _, _, _, _, err = app.runTx(runTxProcessProposal, txBz) //nolint:dogsled
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions baseapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ func (app *BaseApp) SimCheck(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo, *
if err != nil {
return sdk.GasInfo{}, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err)
}
gasInfo, result, _, _, err := app.runTx(runTxModeCheck, bz)
gasInfo, result, _, _, _, err := app.runTx(runTxModeCheck, bz)
return gasInfo, result, err
}

// Simulate executes a tx in simulate mode to get result and gas info.
func (app *BaseApp) Simulate(txBytes []byte) (sdk.GasInfo, *sdk.Result, error) {
gasInfo, result, _, _, err := app.runTx(runTxModeSimulate, txBytes)
gasInfo, result, _, _, _, err := app.runTx(runTxModeSimulate, txBytes)
return gasInfo, result, err
}

Expand All @@ -32,7 +32,7 @@ func (app *BaseApp) SimDeliver(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo,
if err != nil {
return sdk.GasInfo{}, nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "%s", err)
}
gasInfo, result, _, _, err := app.runTx(runTxModeDeliver, bz)
gasInfo, result, _, _, _, err := app.runTx(runTxModeDeliver, bz)
return gasInfo, result, err
}

Expand Down
19 changes: 18 additions & 1 deletion client/pruning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/viper"

dbm "github.com/cometbft/cometbft-db"
cfg "github.com/cometbft/cometbft/config"
bftflags "github.com/cometbft/cometbft/libs/cli/flags"
"github.com/cometbft/cometbft/libs/log"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
Expand Down Expand Up @@ -81,6 +84,11 @@ Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`,
}

logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger, err = bftflags.ParseLogLevel(vp.GetString(flags.FlagLogLevel), logger, cfg.DefaultLogLevel)
if err != nil {
return err
}

app := appCreator(logger, db, nil, vp)
cms := app.CommitMultiStore()

Expand All @@ -94,8 +102,16 @@ Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`,
return fmt.Errorf("the database has no valid heights to prune, the latest height: %v", latestHeight)
}

startHeight := 1

if str := vp.GetString(flags.FlagHeight); str != "" {
if startHeight, err = strconv.Atoi(str); err != nil {
return fmt.Errorf("invalid height flag %v", str)
}
}

var pruningHeights []int64
for height := int64(1); height < latestHeight; height++ {
for height := int64(startHeight); height < latestHeight; height++ {
if height < latestHeight-int64(pruningOptions.KeepRecent) {
pruningHeights = append(pruningHeights, height)
}
Expand All @@ -111,6 +127,7 @@ Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`,
}

cmd.Println("successfully pruned the application root multi stores")
cmd.Flags().Uint64(server.FlagHeight, 1, `the height to begin with`)
return nil
},
}
Expand Down
17 changes: 2 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
go 1.19
go 1.20

module github.com/cosmos/cosmos-sdk

Expand Down Expand Up @@ -186,6 +186,7 @@ require (
replace (
// use cosmos fork of keyring
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
github.com/cometbft/cometbft => github.com/classic-terra/cometbft v0.37.3-0.20240308104225-9f5ce8b85c1b
// dgrijalva/jwt-go is deprecated and doesn't receive security updates.
// TODO: remove it: https://github.com/cosmos/cosmos-sdk/issues/13134
github.com/dgrijalva/jwt-go => github.com/golang-jwt/jwt/v4 v4.4.2
Expand All @@ -199,17 +200,3 @@ replace (
// stick with compatible version of rapid in v0.47.x line
pgregory.net/rapid => pgregory.net/rapid v0.5.5
)

retract (
// revert fix https://github.com/cosmos/cosmos-sdk/pull/16331
v0.46.12
// subject to a bug in the group module and gov module migration
[v0.46.5, v0.46.6]
// subject to the dragonberry vulnerability
// and/or the bank coin metadata migration issue
[v0.46.0, v0.46.4]
// subject to the dragonberry vulnerability
[v0.45.0, v0.45.8]
// do not use
v0.43.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
github.com/classic-terra/cometbft v0.37.3-0.20240308104225-9f5ce8b85c1b h1:x0holP0dCb0W6GnUzVAsAaHPp6pirupf0Po/f7uJ0QE=
github.com/classic-terra/cometbft v0.37.3-0.20240308104225-9f5ce8b85c1b/go.mod h1:Cmg5Hp4sNpapm7j+x0xRyt2g0juQfmB752ous+pA0G8=
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand All @@ -310,8 +312,6 @@ github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZ
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/coinbase/rosetta-sdk-go/types v1.0.0 h1:jpVIwLcPoOeCR6o1tU+Xv7r5bMONNbHU7MuEHboiFuA=
github.com/coinbase/rosetta-sdk-go/types v1.0.0/go.mod h1:eq7W2TMRH22GTW0N0beDnN931DW0/WOI1R2sdHNHG4c=
github.com/cometbft/cometbft v0.37.4 h1:xyvvEqlyfK8MgNIIKVJaMsuIp03wxOcFmVkT26+Ikpg=
github.com/cometbft/cometbft v0.37.4/go.mod h1:Cmg5Hp4sNpapm7j+x0xRyt2g0juQfmB752ous+pA0G8=
github.com/cometbft/cometbft-db v0.7.0 h1:uBjbrBx4QzU0zOEnU8KxoDl18dMNgDh+zZRUE0ucsbo=
github.com/cometbft/cometbft-db v0.7.0/go.mod h1:yiKJIm2WKrt6x8Cyxtq9YTEcIMPcEe4XPxhgX59Fzf0=
github.com/confio/ics23/go v0.9.0 h1:cWs+wdbS2KRPZezoaaj+qBleXgUk5WOQFMP3CQFGTr4=
Expand Down
30 changes: 28 additions & 2 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -81,6 +82,31 @@ func New(clientCtx client.Context, logger log.Logger) *Server {
}
}

// blockHeightMiddleware parses height query parameter and sets GRPCBlockHeightHeader
func blockHeightMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
heightStr := r.FormValue("height")
if heightStr != "" {
height, err := strconv.ParseInt(heightStr, 10, 64)
if err != nil {
writeErrorResponse(w, http.StatusBadRequest, "syntax error")
return
}

if height < 0 {
writeErrorResponse(w, http.StatusBadRequest, "height must be equal or greater than zero")
return
}

if height > 0 {
r.Header.Set(grpctypes.GRPCBlockHeightHeader, heightStr)
}
}

next.ServeHTTP(w, r)
})
}

// Start starts the API server. Internally, the API server leverages Tendermint's
// JSON RPC server. Configuration options are provided via config.APIConfig
// and are delegated to the Tendermint JSON RPC server. The process is
Expand All @@ -102,7 +128,7 @@ func (s *Server) Start(cfg config.Config) error {

s.registerGRPCGatewayRoutes()
s.listener = listener
var h http.Handler = s.Router
var h http.Handler = blockHeightMiddleware(s.Router)

Check failure on line 131 in server/api/server.go

View workflow job for this annotation

GitHub Actions / Analyze

ST1023: should omit type http.Handler from declaration; it will be inferred from the right-hand side (stylecheck)

Check failure on line 131 in server/api/server.go

View workflow job for this annotation

GitHub Actions / golangci-lint

ST1023: should omit type http.Handler from declaration; it will be inferred from the right-hand side (stylecheck)

s.mtx.Unlock()

Expand All @@ -112,7 +138,7 @@ func (s *Server) Start(cfg config.Config) error {
}

s.logger.Info("starting API server...")
return tmrpcserver.Serve(s.listener, s.Router, s.logger, tmCfg)
return tmrpcserver.Serve(s.listener, h, s.logger, tmCfg)
}

// Close closes the API server.
Expand Down
3 changes: 2 additions & 1 deletion server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,8 @@ is performed. Note, when enabled, gRPC will also be automatically enabled.
cmd.Flags().Uint64(FlagStateSyncSnapshotInterval, 0, "State sync snapshot interval")
cmd.Flags().Uint32(FlagStateSyncSnapshotKeepRecent, 2, "State sync snapshot to keep")

cmd.Flags().Bool(FlagDisableIAVLFastNode, false, "Disable fast node for IAVL tree")
// TODO: Should we disable by default?
cmd.Flags().Bool(FlagDisableIAVLFastNode, true, "Disable fast node for IAVL tree")

cmd.Flags().Int(FlagMempoolMaxTxs, mempool.DefaultMaxTx, "Sets MaxTx value for the app-side mempool")

Expand Down
3 changes: 2 additions & 1 deletion simapp/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module cosmossdk.io/simapp

go 1.19
go 1.20

require (
cosmossdk.io/api v0.3.1
Expand Down Expand Up @@ -180,6 +180,7 @@ require (
replace (
// use cosmos fork of keyring
github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0
github.com/cometbft/cometbft => github.com/classic-terra/cometbft v0.37.3-0.20240308104225-9f5ce8b85c1b
// Simapp always use the latest version of the cosmos-sdk
github.com/cosmos/cosmos-sdk => ../.
// Fix upstream GHSA-h395-qcrw-5vmq and GHSA-3vp4-m3rf-835h vulnerabilities.
Expand Down
4 changes: 2 additions & 2 deletions simapp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag=
github.com/circonus-labs/circonusllhist v0.1.3/go.mod h1:kMXHVDlOchFAehlya5ePtbp5jckzBHf4XRpQvBOLI+I=
github.com/classic-terra/cometbft v0.37.3-0.20240308104225-9f5ce8b85c1b h1:x0holP0dCb0W6GnUzVAsAaHPp6pirupf0Po/f7uJ0QE=
github.com/classic-terra/cometbft v0.37.3-0.20240308104225-9f5ce8b85c1b/go.mod h1:Cmg5Hp4sNpapm7j+x0xRyt2g0juQfmB752ous+pA0G8=
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
Expand All @@ -309,8 +311,6 @@ github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZ
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/coinbase/rosetta-sdk-go/types v1.0.0 h1:jpVIwLcPoOeCR6o1tU+Xv7r5bMONNbHU7MuEHboiFuA=
github.com/coinbase/rosetta-sdk-go/types v1.0.0/go.mod h1:eq7W2TMRH22GTW0N0beDnN931DW0/WOI1R2sdHNHG4c=
github.com/cometbft/cometbft v0.37.4 h1:xyvvEqlyfK8MgNIIKVJaMsuIp03wxOcFmVkT26+Ikpg=
github.com/cometbft/cometbft v0.37.4/go.mod h1:Cmg5Hp4sNpapm7j+x0xRyt2g0juQfmB752ous+pA0G8=
github.com/cometbft/cometbft-db v0.8.0 h1:vUMDaH3ApkX8m0KZvOFFy9b5DZHBAjsnEuo9AKVZpjo=
github.com/cometbft/cometbft-db v0.8.0/go.mod h1:6ASCP4pfhmrCBpfk01/9E1SI29nD3HfVHrY4PG8x5c0=
github.com/confio/ics23/go v0.9.0 h1:cWs+wdbS2KRPZezoaaj+qBleXgUk5WOQFMP3CQFGTr4=
Expand Down
Loading

0 comments on commit 1040442

Please sign in to comment.