Skip to content

Commit 4d2040f

Browse files
committed
Merge branch 'obj_main' into release/v0.53.x
2 parents fa2c766 + 9ee27ce commit 4d2040f

File tree

95 files changed

+1816
-1120
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1816
-1120
lines changed

.github/workflows/dependabot-update-all.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ jobs:
1414
if: ${{ github.actor == 'dependabot[bot]' }}
1515
steps:
1616
- name: Generate Token
17-
<<<<<<< HEAD
1817
uses: actions/create-github-app-token@3ff1caaa28b64c9cc276ce0a02e2ff584f3900c5 # v1
19-
=======
20-
uses: actions/create-github-app-token@df432ceedc7162793a195dd1713ff69aefc7379e # v1
21-
>>>>>>> acb39aa52 (build(deps): Bump actions/create-github-app-token from 2.0.3 to 2.0.6 (#24673))
2218
id: app-token
2319
with:
2420
app-id: "${{ secrets.APP_ID }}"

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5555
* (genutil) [#24018](https://github.com/cosmos/cosmos-sdk/pull/24018) Allow manually setting the consensus key type in genesis
5656
* (client) [#18557](https://github.com/cosmos/cosmos-sdk/pull/18557) Add `--qrcode` flag to `keys show` command to support displaying keys address QR code.
5757
* (x/auth) [#24030](https://github.com/cosmos/cosmos-sdk/pull/24030) Allow usage of ed25519 keys for transaction signing.
58+
* (baseapp) [#24159](https://github.com/cosmos/cosmos-sdk/pull/24159) Support mount object store in baseapp, add `ObjectStore` api in context.
5859
* (baseapp) [#24163](https://github.com/cosmos/cosmos-sdk/pull/24163) Add `StreamingManager` to baseapp to extend the abci listeners.
5960
* (x/protocolpool) [#23933](https://github.com/cosmos/cosmos-sdk/pull/23933) Add x/protocolpool module.
6061
* x/distribution can now utilize an externally managed community pool. NOTE: this will make the message handlers for FundCommunityPool and CommunityPoolSpend error, as well as the query handler for CommunityPool.

baseapp/abci.go

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -791,48 +791,47 @@ func (app *BaseApp) internalFinalizeBlock(ctx context.Context, req *abci.Request
791791

792792
// Reset the gas meter so that the AnteHandlers aren't required to
793793
gasMeter = app.getBlockGasMeter(app.finalizeBlockState.Context())
794-
app.finalizeBlockState.SetContext(app.finalizeBlockState.Context().WithBlockGasMeter(gasMeter))
794+
app.finalizeBlockState.SetContext(
795+
app.finalizeBlockState.Context().
796+
WithBlockGasMeter(gasMeter).
797+
WithTxCount(len(req.Txs)),
798+
)
795799

796800
// Iterate over all raw transactions in the proposal and attempt to execute
797801
// them, gathering the execution results.
798802
//
799803
// NOTE: Not all raw transactions may adhere to the sdk.Tx interface, e.g.
800804
// vote extensions, so skip those.
801-
txResults := make([]*abci.ExecTxResult, 0, len(req.Txs))
802-
for _, rawTx := range req.Txs {
803-
var response *abci.ExecTxResult
804-
805-
if _, err := app.txDecoder(rawTx); err == nil {
806-
response = app.deliverTx(rawTx)
807-
} else {
808-
// In the case where a transaction included in a block proposal is malformed,
809-
// we still want to return a default response to comet. This is because comet
810-
// expects a response for each transaction included in a block proposal.
811-
response = sdkerrors.ResponseExecTxResultWithEvents(
812-
sdkerrors.ErrTxDecode,
813-
0,
814-
0,
815-
nil,
816-
false,
817-
)
818-
}
819-
820-
// check after every tx if we should abort
821-
select {
822-
case <-ctx.Done():
823-
return nil, ctx.Err()
824-
default:
825-
// continue
826-
}
827-
828-
txResults = append(txResults, response)
805+
txResults, err := app.executeTxs(ctx, req.Txs)
806+
if err != nil {
807+
// usually due to canceled
808+
return nil, err
829809
}
830810

831811
if app.finalizeBlockState.ms.TracingEnabled() {
832812
app.finalizeBlockState.ms = app.finalizeBlockState.ms.SetTracingContext(nil).(storetypes.CacheMultiStore)
833813
}
834814

835-
endBlock, err := app.endBlock(app.finalizeBlockState.Context())
815+
var (
816+
blockGasUsed uint64
817+
blockGasWanted uint64
818+
)
819+
for _, res := range txResults {
820+
// GasUsed should not be -1 but just in case
821+
if res.GasUsed > 0 {
822+
blockGasUsed += uint64(res.GasUsed)
823+
}
824+
// GasWanted could be -1 if the tx is invalid
825+
if res.GasWanted > 0 {
826+
blockGasWanted += uint64(res.GasWanted)
827+
}
828+
}
829+
app.finalizeBlockState.SetContext(
830+
app.finalizeBlockState.Context().
831+
WithBlockGasUsed(blockGasUsed).
832+
WithBlockGasWanted(blockGasWanted),
833+
)
834+
endBlock, err := app.endBlock(ctx)
836835
if err != nil {
837836
return nil, err
838837
}
@@ -856,6 +855,44 @@ func (app *BaseApp) internalFinalizeBlock(ctx context.Context, req *abci.Request
856855
}, nil
857856
}
858857

858+
func (app *BaseApp) executeTxs(ctx context.Context, txs [][]byte) ([]*abci.ExecTxResult, error) {
859+
if app.txExecutor != nil {
860+
return app.txExecutor(ctx, txs, app.finalizeBlockState.ms, func(i int, memTx sdk.Tx, ms storetypes.MultiStore, incarnationCache map[string]any) *abci.ExecTxResult {
861+
return app.deliverTxWithMultiStore(txs[i], memTx, i, ms, incarnationCache)
862+
})
863+
}
864+
865+
txResults := make([]*abci.ExecTxResult, 0, len(txs))
866+
for i, rawTx := range txs {
867+
var response *abci.ExecTxResult
868+
869+
if memTx, err := app.txDecoder(rawTx); err == nil {
870+
response = app.deliverTx(rawTx, memTx, i)
871+
} else {
872+
// In the case where a transaction included in a block proposal is malformed,
873+
// we still want to return a default response to comet. This is because comet
874+
// expects a response for each transaction included in a block proposal.
875+
response = sdkerrors.ResponseExecTxResultWithEvents(
876+
sdkerrors.ErrTxDecode,
877+
0,
878+
0,
879+
nil,
880+
false,
881+
)
882+
}
883+
// check after every tx if we should abort
884+
select {
885+
case <-ctx.Done():
886+
return nil, ctx.Err()
887+
default:
888+
// continue
889+
}
890+
891+
txResults = append(txResults, response)
892+
}
893+
return txResults, nil
894+
}
895+
859896
// FinalizeBlock will execute the block proposal provided by RequestFinalizeBlock.
860897
// Specifically, it will execute an application's BeginBlock (if defined), followed
861898
// by the transactions in the proposal, finally followed by the application's
@@ -1213,7 +1250,7 @@ func (app *BaseApp) CreateQueryContextWithCheckHeader(height int64, prove, check
12131250
// use custom query multi-store if provided
12141251
qms := app.qms
12151252
if qms == nil {
1216-
qms = app.cms.(storetypes.MultiStore)
1253+
qms = storetypes.RootMultiStore(app.cms)
12171254
}
12181255

12191256
lastBlockHeight := qms.LatestVersion()

baseapp/abci_utils.go

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ type (
201201
// to verify a transaction.
202202
ProposalTxVerifier interface {
203203
PrepareProposalVerifyTx(tx sdk.Tx) ([]byte, error)
204-
ProcessProposalVerifyTx(txBz []byte) (sdk.Tx, error)
204+
ProcessProposalVerifyTx(txBz []byte) (sdk.Tx, uint64, error)
205205
TxDecode(txBz []byte) (sdk.Tx, error)
206206
TxEncode(tx sdk.Tx) ([]byte, error)
207207
}
@@ -213,6 +213,9 @@ type (
213213
txVerifier ProposalTxVerifier
214214
txSelector TxSelector
215215
signerExtAdapter mempool.SignerExtractionAdapter
216+
217+
// fastPrepareProposal together with NoOpMempool will bypass tx selector
218+
fastPrepareProposal bool
216219
}
217220
)
218221

@@ -225,6 +228,12 @@ func NewDefaultProposalHandler(mp mempool.Mempool, txVerifier ProposalTxVerifier
225228
}
226229
}
227230

231+
func NewDefaultProposalHandlerFast(mp mempool.Mempool, txVerifier ProposalTxVerifier) *DefaultProposalHandler {
232+
h := NewDefaultProposalHandler(mp, txVerifier)
233+
h.fastPrepareProposal = true
234+
return h
235+
}
236+
228237
// SetTxSelector sets the TxSelector function on the DefaultProposalHandler.
229238
func (h *DefaultProposalHandler) SetTxSelector(ts TxSelector) {
230239
h.txSelector = ts
@@ -265,13 +274,23 @@ func (h *DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHan
265274
// Note, we still need to ensure the transactions returned respect req.MaxTxBytes.
266275
_, isNoOp := h.mempool.(mempool.NoOpMempool)
267276
if h.mempool == nil || isNoOp {
277+
if h.fastPrepareProposal {
278+
txs := h.txSelector.SelectTxForProposalFast(ctx, req.Txs)
279+
return &abci.ResponsePrepareProposal{Txs: txs}, nil
280+
}
281+
268282
for _, txBz := range req.Txs {
269283
tx, err := h.txVerifier.TxDecode(txBz)
270284
if err != nil {
271285
return nil, err
272286
}
273287

274-
stop := h.txSelector.SelectTxForProposal(ctx, uint64(req.MaxTxBytes), maxBlockGas, tx, txBz)
288+
var txGasLimit uint64
289+
if gasTx, ok := tx.(mempool.GasTx); ok {
290+
txGasLimit = gasTx.GetGas()
291+
}
292+
293+
stop := h.txSelector.SelectTxForProposal(ctx, uint64(req.MaxTxBytes), maxBlockGas, tx, txBz, txGasLimit)
275294
if stop {
276295
break
277296
}
@@ -286,14 +305,14 @@ func (h *DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHan
286305
selectedTxsNums int
287306
invalidTxs []sdk.Tx // invalid txs to be removed out of the loop to avoid dead lock
288307
)
289-
mempool.SelectBy(ctx, h.mempool, req.Txs, func(memTx sdk.Tx) bool {
290-
unorderedTx, ok := memTx.(sdk.TxWithUnordered)
308+
mempool.SelectBy(ctx, h.mempool, req.Txs, func(memTx mempool.Tx) bool {
309+
unorderedTx, ok := memTx.Tx.(sdk.TxWithUnordered)
291310
isUnordered := ok && unorderedTx.GetUnordered()
292311
txSignersSeqs := make(map[string]uint64)
293312

294313
// if the tx is unordered, we don't need to check the sequence, we just add it
295314
if !isUnordered {
296-
signerData, err := h.signerExtAdapter.GetSigners(memTx)
315+
signerData, err := h.signerExtAdapter.GetSigners(memTx.Tx)
297316
if err != nil {
298317
// propagate the error to the caller
299318
resError = err
@@ -328,11 +347,11 @@ func (h *DefaultProposalHandler) PrepareProposalHandler() sdk.PrepareProposalHan
328347
// which calls mempool.Insert, in theory everything in the pool should be
329348
// valid. But some mempool implementations may insert invalid txs, so we
330349
// check again.
331-
txBz, err := h.txVerifier.PrepareProposalVerifyTx(memTx)
350+
txBz, err := h.txVerifier.PrepareProposalVerifyTx(memTx.Tx)
332351
if err != nil {
333-
invalidTxs = append(invalidTxs, memTx)
352+
invalidTxs = append(invalidTxs, memTx.Tx)
334353
} else {
335-
stop := h.txSelector.SelectTxForProposal(ctx, uint64(req.MaxTxBytes), maxBlockGas, memTx, txBz)
354+
stop := h.txSelector.SelectTxForProposal(ctx, uint64(req.MaxTxBytes), maxBlockGas, memTx.Tx, txBz, memTx.GasWanted)
336355
if stop {
337356
return false
338357
}
@@ -404,17 +423,13 @@ func (h *DefaultProposalHandler) ProcessProposalHandler() sdk.ProcessProposalHan
404423
}
405424

406425
for _, txBytes := range req.Txs {
407-
tx, err := h.txVerifier.ProcessProposalVerifyTx(txBytes)
426+
_, gasWanted, err := h.txVerifier.ProcessProposalVerifyTx(txBytes)
408427
if err != nil {
409428
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
410429
}
411430

412431
if maxBlockGas > 0 {
413-
gasTx, ok := tx.(GasTx)
414-
if ok {
415-
totalTxGas += gasTx.GetGas()
416-
}
417-
432+
totalTxGas += gasWanted
418433
if totalTxGas > uint64(maxBlockGas) {
419434
return &abci.ResponseProcessProposal{Status: abci.ResponseProcessProposal_REJECT}, nil
420435
}
@@ -472,7 +487,13 @@ type TxSelector interface {
472487
// a proposal based on inclusion criteria defined by the TxSelector. It must
473488
// return <true> if the caller should halt the transaction selection loop
474489
// (typically over a mempool) or <false> otherwise.
475-
SelectTxForProposal(ctx context.Context, maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte) bool
490+
SelectTxForProposal(ctx context.Context, maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte, gasWanted uint64) bool
491+
492+
// SelectTxForProposalFast is called in the case of NoOpMempool,
493+
// where cometbft already checked the block gas/size limit,
494+
// so the tx selector should simply accept them all to the proposal.
495+
// But extra validations on the tx are still possible though.
496+
SelectTxForProposalFast(ctx context.Context, txs [][]byte) [][]byte
476497
}
477498

478499
type defaultTxSelector struct {
@@ -494,26 +515,19 @@ func (ts *defaultTxSelector) SelectedTxs(_ context.Context) [][]byte {
494515
func (ts *defaultTxSelector) Clear() {
495516
ts.totalTxBytes = 0
496517
ts.totalTxGas = 0
497-
ts.selectedTxs = nil
518+
ts.selectedTxs = ts.selectedTxs[:0] // keep the allocated memory
498519
}
499520

500-
func (ts *defaultTxSelector) SelectTxForProposal(_ context.Context, maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte) bool {
521+
func (ts *defaultTxSelector) SelectTxForProposal(_ context.Context, maxTxBytes, maxBlockGas uint64, memTx sdk.Tx, txBz []byte, gasWanted uint64) bool {
501522
txSize := uint64(cmttypes.ComputeProtoSizeForTxs([]cmttypes.Tx{txBz}))
502523

503-
var txGasLimit uint64
504-
if memTx != nil {
505-
if gasTx, ok := memTx.(GasTx); ok {
506-
txGasLimit = gasTx.GetGas()
507-
}
508-
}
509-
510524
// only add the transaction to the proposal if we have enough capacity
511525
if (txSize + ts.totalTxBytes) <= maxTxBytes {
512526
// If there is a max block gas limit, add the tx only if the limit has
513527
// not been met.
514528
if maxBlockGas > 0 {
515-
if (txGasLimit + ts.totalTxGas) <= maxBlockGas {
516-
ts.totalTxGas += txGasLimit
529+
if (gasWanted + ts.totalTxGas) <= maxBlockGas {
530+
ts.totalTxGas += gasWanted
517531
ts.totalTxBytes += txSize
518532
ts.selectedTxs = append(ts.selectedTxs, txBz)
519533
}
@@ -526,3 +540,7 @@ func (ts *defaultTxSelector) SelectTxForProposal(_ context.Context, maxTxBytes,
526540
// check if we've reached capacity; if so, we cannot select any more transactions
527541
return ts.totalTxBytes >= maxTxBytes || (maxBlockGas > 0 && (ts.totalTxGas >= maxBlockGas))
528542
}
543+
544+
func (ts *defaultTxSelector) SelectTxForProposalFast(ctx context.Context, txs [][]byte) [][]byte {
545+
return txs
546+
}

0 commit comments

Comments
 (0)