Skip to content

Commit 79b7899

Browse files
authored
Made block processing abci request contain a tendermint block header (#3083)
For some reason abci requests were flattening the block header, only to reconstruct it back at the request handler. By passing the header as is, the Application API has been simplified.
1 parent 8cd6609 commit 79b7899

45 files changed

Lines changed: 408 additions & 1160 deletions

File tree

Some content is hidden

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

app/app.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,7 @@ func (app *App) ProcessProposalHandler(ctx sdk.Context, req *abci.RequestProcess
12241224
// by recording the decoding results and avoid decoding again later on.
12251225

12261226
if !app.checkTotalBlockGas(ctx, req.Txs) {
1227-
metrics.IncrFailedTotalGasWantedCheck(string(req.GetProposerAddress()))
1227+
metrics.IncrFailedTotalGasWantedCheck(string(req.Header.ProposerAddress))
12281228
return &abci.ResponseProcessProposal{
12291229
Status: abci.ResponseProcessProposal_REJECT,
12301230
}, nil
@@ -1235,7 +1235,7 @@ func (app *App) ProcessProposalHandler(ctx sdk.Context, req *abci.RequestProcess
12351235
if shouldStartOptimisticProcessing {
12361236
completionSignal := make(chan struct{}, 1)
12371237
app.optimisticProcessingInfo = OptimisticProcessingInfo{
1238-
Height: req.Height,
1238+
Height: req.Header.Height,
12391239
Hash: req.Hash,
12401240
Completion: completionSignal,
12411241
}
@@ -1255,7 +1255,13 @@ func (app *App) ProcessProposalHandler(ctx sdk.Context, req *abci.RequestProcess
12551255
go func() {
12561256
// ProcessBlock has panic recovery and returns error for any processing failures
12571257
// All panics (including GetSigners) are handled in ProcessBlock, not affecting proposal acceptance
1258-
events, txResults, endBlockResp, processErr := app.ProcessBlock(ctx, req.Txs, req, req.ProposedLastCommit, false)
1258+
bpreq := &BlockProcessRequest{
1259+
Hash: req.Hash,
1260+
ByzantineValidators: req.ByzantineValidators,
1261+
Height: req.Header.Height,
1262+
Time: req.Header.Time,
1263+
}
1264+
events, txResults, endBlockResp, processErr := app.ProcessBlock(ctx, req.Txs, bpreq, req.ProposedLastCommit, false)
12591265

12601266
app.optimisticProcessingInfoMutex.Lock()
12611267
if processErr != nil {
@@ -1333,7 +1339,13 @@ func (app *App) FinalizeBlocker(ctx sdk.Context, req *abci.RequestFinalizeBlock)
13331339
}
13341340
metrics.IncrementOptimisticProcessingCounter(false)
13351341
logger.Info("optimistic processing ineligible")
1336-
events, txResults, endBlockResp, processErr := app.ProcessBlock(ctx, req.Txs, req, req.DecidedLastCommit, false)
1342+
bpreq := &BlockProcessRequest{
1343+
Hash: req.Hash,
1344+
ByzantineValidators: req.ByzantineValidators,
1345+
Height: req.Header.Height,
1346+
Time: req.Header.Time,
1347+
}
1348+
events, txResults, endBlockResp, processErr := app.ProcessBlock(ctx, req.Txs, bpreq, req.DecidedLastCommit, false)
13371349
if processErr != nil {
13381350
logger.Error("ProcessBlock failed in FinalizeBlocker", "err", processErr)
13391351
return nil, processErr
@@ -1716,7 +1728,7 @@ func (app *App) ProcessTXsWithOCCGiga(ctx sdk.Context, txs [][]byte, typedTxs []
17161728
return execResults, ctx
17171729
}
17181730

1719-
func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req BlockProcessRequest, lastCommit abci.CommitInfo, simulate bool) (events []abci.Event, txResults []*abci.ExecTxResult, endBlockResp abci.ResponseEndBlock, err error) {
1731+
func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req *BlockProcessRequest, lastCommit abci.CommitInfo, simulate bool) (events []abci.Event, txResults []*abci.ExecTxResult, endBlockResp abci.ResponseEndBlock, err error) {
17201732
defer func() {
17211733
if r := recover(); r != nil {
17221734
panicMsg := fmt.Sprintf("%v", r)
@@ -1750,10 +1762,10 @@ func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req BlockProcessRequ
17501762

17511763
blockSpanCtx, blockSpan := app.GetBaseApp().TracingInfo.Start("Block")
17521764
defer blockSpan.End()
1753-
blockSpan.SetAttributes(attribute.Int64("height", req.GetHeight()))
1765+
blockSpan.SetAttributes(attribute.Int64("height", req.Height))
17541766
ctx = ctx.WithTraceSpanContext(blockSpanCtx)
17551767

1756-
beginBlockResp := app.BeginBlock(ctx, req.GetHeight(), lastCommit.Votes, req.GetByzantineValidators(), true)
1768+
beginBlockResp := app.BeginBlock(ctx, req.Height, lastCommit.Votes, req.ByzantineValidators, true)
17571769
events = append(events, beginBlockResp.Events...)
17581770

17591771
evmTxs := make([]*evmtypes.MsgEVMTransaction, len(txs)) // nil for non-EVM txs
@@ -1766,7 +1778,7 @@ func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req BlockProcessRequ
17661778
// Execute all transactions
17671779
txResults, ctx = app.ExecuteTxsConcurrently(ctx, txs, typedTxs)
17681780

1769-
midBlockEvents := app.MidBlock(ctx, req.GetHeight())
1781+
midBlockEvents := app.MidBlock(ctx, req.Height)
17701782
events = append(events, midBlockEvents...)
17711783

17721784
// Flush giga stores so WriteDeferredBalances (which uses the standard BankKeeper)
@@ -1790,7 +1802,7 @@ func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req BlockProcessRequ
17901802
}
17911803
}
17921804

1793-
endBlockResp = app.EndBlock(ctx, req.GetHeight(), evmTotalGasUsed)
1805+
endBlockResp = app.EndBlock(ctx, req.Height, evmTotalGasUsed)
17941806

17951807
events = append(events, endBlockResp.Events...)
17961808
return events, txResults, endBlockResp, nil

app/app_test.go

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func TestEmptyBlockIdempotency(t *testing.T) {
4141

4242
for i := 1; i <= 10; i++ {
4343
testWrapper := app.NewTestWrapper(t, tm, valPub, false)
44-
res, _ := testWrapper.App.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{Height: 1})
44+
res, _ := testWrapper.App.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{Header: &types.Header{ChainID: "sei-test", Height: 1}})
4545
testWrapper.App.Commit(context.Background())
4646
data := res.AppHash
4747
commitData = append(commitData, data)
@@ -53,6 +53,17 @@ func TestEmptyBlockIdempotency(t *testing.T) {
5353
}
5454
}
5555

56+
func TestFinalizeBlockRequiresChainID(t *testing.T) {
57+
tm := time.Now().UTC()
58+
valPub := secp256k1.GenPrivKey().PubKey()
59+
60+
testWrapper := app.NewTestWrapper(t, tm, valPub, false)
61+
_, err := testWrapper.App.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{
62+
Header: &types.Header{Height: 1},
63+
})
64+
require.Error(t, err)
65+
}
66+
5667
func TestProcessOracleAndOtherTxsSuccess(t *testing.T) {
5768
tm := time.Now().UTC()
5869
valPub := secp256k1.GenPrivKey().PubKey()
@@ -100,14 +111,14 @@ func TestProcessOracleAndOtherTxsSuccess(t *testing.T) {
100111
}
101112

102113
req := &abci.RequestFinalizeBlock{
103-
Height: 1,
114+
Header: &types.Header{ChainID: "sei-test", Height: 1},
104115
}
105116
_, txResults, _, _ := testWrapper.App.ProcessBlock(
106117
testWrapper.Ctx.WithBlockHeight(
107118
1,
108119
),
109120
txs,
110-
req,
121+
finalizeToBlockProcessReq(req),
111122
req.DecidedLastCommit,
112123
false,
113124
)
@@ -123,14 +134,14 @@ func TestProcessOracleAndOtherTxsSuccess(t *testing.T) {
123134
}
124135

125136
req = &abci.RequestFinalizeBlock{
126-
Height: 1,
137+
Header: &types.Header{ChainID: "sei-test", Height: 1},
127138
}
128139
_, txResults2, _, _ := testWrapper.App.ProcessBlock(
129140
testWrapper.Ctx.WithBlockHeight(
130141
1,
131142
),
132143
diffOrderTxs,
133-
req,
144+
finalizeToBlockProcessReq(req),
134145
req.DecidedLastCommit,
135146
false,
136147
)
@@ -158,7 +169,7 @@ func TestInvalidProposalWithExcessiveGasWanted(t *testing.T) {
158169

159170
badProposal := abci.RequestProcessProposal{
160171
Txs: [][]byte{emptyTx, emptyTx},
161-
Height: 1,
172+
Header: &types.Header{ChainID: "sei-test", Height: 1},
162173
}
163174
res, err := ap.ProcessProposalHandler(ctx, &badProposal)
164175
require.Nil(t, err)
@@ -319,7 +330,7 @@ func TestInvalidProposalWithExcessiveGasEstimates(t *testing.T) {
319330

320331
proposal := abci.RequestProcessProposal{
321332
Txs: txs,
322-
Height: 1,
333+
Header: &types.Header{ChainID: "sei-test", Height: 1},
323334
}
324335
res, err := ap.ProcessProposalHandler(ctx, &proposal)
325336
require.Nil(t, err)
@@ -348,7 +359,7 @@ func TestOverflowGas(t *testing.T) {
348359

349360
proposal := abci.RequestProcessProposal{
350361
Txs: [][]byte{emptyTx, secondTx},
351-
Height: 1,
362+
Header: &types.Header{ChainID: "sei-test", Height: 1},
352363
}
353364
res, err := ap.ProcessProposalHandler(ctx, &proposal)
354365
require.Nil(t, err)
@@ -571,7 +582,7 @@ func TestProcessProposalHandlerPanicRecovery(t *testing.T) {
571582
}
572583

573584
req := &abci.RequestProcessProposal{
574-
Height: ctx.BlockHeight(),
585+
Header: &types.Header{ChainID: "sei-test", Height: ctx.BlockHeight()},
575586
Hash: []byte("panic-test"),
576587
Txs: [][]byte{maliciousTx}, // Include the malicious transaction
577588
}
@@ -727,3 +738,18 @@ func TestDeliverTxWithNilTypedTxDoesNotPanic(t *testing.T) {
727738
require.NotNil(t, result)
728739
})
729740
}
741+
742+
func finalizeToBlockProcessReq(req *abci.RequestFinalizeBlock) *app.BlockProcessRequest {
743+
var height int64
744+
var blockTime time.Time
745+
if req.Header != nil {
746+
height = req.Header.Height
747+
blockTime = req.Header.Time
748+
}
749+
return &app.BlockProcessRequest{
750+
Hash: req.Hash,
751+
ByzantineValidators: req.ByzantineValidators,
752+
Height: height,
753+
Time: blockTime,
754+
}
755+
}

app/eth_replay.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/sei-protocol/sei-chain/sei-cosmos/client"
2020
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
2121
abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
22+
tmproto "github.com/sei-protocol/sei-chain/sei-tendermint/proto/tendermint/types"
2223
tmtypes "github.com/sei-protocol/sei-chain/sei-tendermint/types"
2324
"github.com/sei-protocol/sei-chain/utils"
2425
"github.com/sei-protocol/sei-chain/x/evm/state"
@@ -74,9 +75,12 @@ func Replay(a *App) {
7475
_, err = a.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{
7576
Txs: utils.Map(b.Txs, func(tx *ethtypes.Transaction) []byte { return encodeTx(tx, a.GetTxConfig()) }),
7677
DecidedLastCommit: abci.CommitInfo{Votes: []abci.VoteInfo{}},
77-
Height: h,
7878
Hash: hash,
79-
Time: time.Now(),
79+
Header: &tmproto.Header{
80+
ChainID: a.ChainID,
81+
Height: h,
82+
Time: time.Now(),
83+
},
8084
})
8185
if err != nil {
8286
panic(err)
@@ -173,12 +177,15 @@ func BlockTest(a *App, bt *ethtests.BlockTest) {
173177
txs := utils.Map(b.Txs, func(tx *ethtypes.Transaction) []byte { return encodeTx(tx, a.GetTxConfig()) })
174178
_, err = a.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{
175179
Txs: txs,
176-
ProposerAddress: a.EvmKeeper.GetSeiAddressOrDefault(a.GetCheckCtx(), b.Coinbase()),
177-
DecidedLastCommit: abci.CommitInfo{Votes: []abci.VoteInfo{}},
178-
Height: h,
179180
Hash: blockHash[:],
180-
LastBlockHash: parentHash[:],
181-
Time: time.Now(),
181+
DecidedLastCommit: abci.CommitInfo{Votes: []abci.VoteInfo{}},
182+
Header: &tmproto.Header{
183+
ChainID: gendoc.ChainID,
184+
ProposerAddress: a.EvmKeeper.GetSeiAddressOrDefault(a.GetCheckCtx(), b.Coinbase()),
185+
LastBlockId: tmproto.BlockID{Hash: parentHash[:]},
186+
Height: h,
187+
Time: time.Now(),
188+
},
182189
})
183190
if err != nil {
184191
panic(err)

app/optimistic_processing_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type OptimisticProcessingTestSuite struct {
2020

2121
func (suite *OptimisticProcessingTestSuite) SetupTest() {
2222
suite.app = Setup(suite.T(), false, false, false)
23-
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{Height: 1})
23+
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{Height: 1, ChainID: "sei-test"})
2424
}
2525

2626
func TestOptimisticProcessingTestSuite(t *testing.T) {
@@ -111,9 +111,9 @@ func (suite *OptimisticProcessingTestSuite) TestProcessProposalHandler_NewOptimi
111111
require := suite.Require()
112112

113113
req := &abci.RequestProcessProposal{
114-
Height: suite.ctx.BlockHeight(),
115114
Hash: []byte("test-hash"),
116115
Txs: [][]byte{},
116+
Header: &tmproto.Header{Height: suite.ctx.BlockHeight(), ChainID: "sei-test"},
117117
}
118118

119119
// Ensure no existing optimistic processing
@@ -125,7 +125,7 @@ func (suite *OptimisticProcessingTestSuite) TestProcessProposalHandler_NewOptimi
125125

126126
// Verify optimistic processing info was set
127127
info := suite.app.GetOptimisticProcessingInfo()
128-
require.Equal(req.Height, info.Height)
128+
require.Equal(req.Header.Height, info.Height)
129129
require.Equal(req.Hash, info.Hash)
130130
require.NotNil(info.Completion)
131131
require.False(info.Aborted)
@@ -146,9 +146,9 @@ func (suite *OptimisticProcessingTestSuite) TestProcessProposalHandler_UpgradePl
146146
nextCtx := suite.ctx.WithBlockHeight(suite.ctx.BlockHeight() + 1)
147147

148148
req := &abci.RequestProcessProposal{
149-
Height: nextCtx.BlockHeight(),
150149
Hash: []byte("test-hash"),
151150
Txs: [][]byte{},
151+
Header: &tmproto.Header{Height: nextCtx.BlockHeight(), ChainID: "sei-test"},
152152
}
153153

154154
// Ensure no existing optimistic processing
@@ -189,9 +189,9 @@ func (suite *OptimisticProcessingTestSuite) TestProcessProposalHandler_HashMisma
189189
suite.app.optimisticProcessingInfoMutex.Unlock()
190190

191191
req := &abci.RequestProcessProposal{
192-
Height: suite.ctx.BlockHeight(),
193192
Hash: []byte("different-hash"), // Different hash
194193
Txs: [][]byte{},
194+
Header: &tmproto.Header{Height: suite.ctx.BlockHeight(), ChainID: "sei-test"},
195195
}
196196

197197
resp, err := suite.app.ProcessProposalHandler(suite.ctx, req)
@@ -220,9 +220,9 @@ func (suite *OptimisticProcessingTestSuite) TestProcessProposalHandler_SameHashC
220220
suite.app.optimisticProcessingInfoMutex.Unlock()
221221

222222
req := &abci.RequestProcessProposal{
223-
Height: suite.ctx.BlockHeight(),
224223
Hash: hash, // Same hash
225224
Txs: [][]byte{},
225+
Header: &tmproto.Header{Height: suite.ctx.BlockHeight(), ChainID: "sei-test"},
226226
}
227227

228228
resp, err := suite.app.ProcessProposalHandler(suite.ctx, req)
@@ -380,9 +380,9 @@ func (suite *OptimisticProcessingTestSuite) TestFinalizeBlocker_SuccessfulOptimi
380380
}()
381381

382382
req := &abci.RequestFinalizeBlock{
383-
Height: suite.ctx.BlockHeight(),
384383
Hash: hash,
385384
Txs: [][]byte{},
385+
Header: &tmproto.Header{Height: suite.ctx.BlockHeight(), ChainID: "sei-test"},
386386
}
387387

388388
resp, err := suite.app.FinalizeBlocker(suite.ctx, req)
@@ -418,9 +418,9 @@ func (suite *OptimisticProcessingTestSuite) TestFinalizeBlocker_AbortedOptimisti
418418
completion <- struct{}{}
419419

420420
req := &abci.RequestFinalizeBlock{
421-
Height: suite.ctx.BlockHeight(),
422421
Hash: hash,
423422
Txs: [][]byte{},
423+
Header: &tmproto.Header{Height: suite.ctx.BlockHeight(), ChainID: "sei-test"},
424424
}
425425

426426
resp, err := suite.app.FinalizeBlocker(suite.ctx, req)
@@ -452,9 +452,9 @@ func (suite *OptimisticProcessingTestSuite) TestFinalizeBlocker_HashMismatch() {
452452
completion <- struct{}{}
453453

454454
req := &abci.RequestFinalizeBlock{
455-
Height: suite.ctx.BlockHeight(),
456455
Hash: []byte("request-hash"), // Different hash
457456
Txs: [][]byte{},
457+
Header: &tmproto.Header{Height: suite.ctx.BlockHeight(), ChainID: "sei-test"},
458458
}
459459

460460
resp, err := suite.app.FinalizeBlocker(suite.ctx, req)
@@ -472,9 +472,9 @@ func (suite *OptimisticProcessingTestSuite) TestFinalizeBlocker_NoOptimisticProc
472472
suite.app.ClearOptimisticProcessingInfo()
473473

474474
req := &abci.RequestFinalizeBlock{
475-
Height: suite.ctx.BlockHeight(),
476475
Hash: []byte("test-hash"),
477476
Txs: [][]byte{},
477+
Header: &tmproto.Header{Height: suite.ctx.BlockHeight(), ChainID: "sei-test"},
478478
}
479479

480480
resp, err := suite.app.FinalizeBlocker(suite.ctx, req)

0 commit comments

Comments
 (0)