Skip to content

Commit

Permalink
Merge pull request #11361 from vegaprotocol/11360-nullchain-prepare
Browse files Browse the repository at this point in the history
feat: nullchain now calls prepare proposal to acknowledge tx delays
  • Loading branch information
wwestgarth authored Jun 13, 2024
2 parents 0661372 + 8e27a77 commit 0ef13b7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
15 changes: 15 additions & 0 deletions core/blockchain/nullchain/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 18 additions & 4 deletions core/blockchain/nullchain/nullchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type TimeService interface {

type ApplicationService interface {
InitChain(context.Context, *abci.RequestInitChain) (*abci.ResponseInitChain, error)
PrepareProposal(_ context.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error)
FinalizeBlock(context.Context, *abci.RequestFinalizeBlock) (*abci.ResponseFinalizeBlock, error)
Commit(context.Context, *abci.RequestCommit) (*abci.ResponseCommit, error)
Info(context.Context, *abci.RequestInfo) (*abci.ResponseInfo, error)
Expand Down Expand Up @@ -187,22 +188,35 @@ func (n *NullBlockchain) processBlock() {
n.log.Debugf("processing block %d with %d transactions", n.blockHeight, len(n.pending))
}

// prepare it first
ctx := context.Background()
proposal, err := n.app.PrepareProposal(ctx,
&abci.RequestPrepareProposal{
Height: n.blockHeight,
Time: n.now,
Txs: n.pending,
})
if err != nil {
// core always returns nil so we are safe really
panic("nullchain cannot handle failure to prepare a proposal")
}

resp := &abci.ResponseFinalizeBlock{}
if n.replayer != nil && n.cfg.Replay.Record {
n.replayer.startBlock(n.blockHeight, n.now.UnixNano(), n.pending)
n.replayer.startBlock(n.blockHeight, n.now.UnixNano(), proposal.Txs)
defer func() {
n.replayer.saveBlock(resp.AppHash)
}()
}

resp, _ = n.app.FinalizeBlock(context.Background(), &abci.RequestFinalizeBlock{
resp, _ = n.app.FinalizeBlock(ctx, &abci.RequestFinalizeBlock{
Height: n.blockHeight,
Time: n.now,
Hash: vgcrypto.Hash([]byte(strconv.FormatInt(n.blockHeight+n.now.UnixNano(), 10))),
Txs: n.pending,
Txs: proposal.Txs,
})
n.pending = n.pending[:0]
n.app.Commit(context.Background(), &abci.RequestCommit{})
n.app.Commit(ctx, &abci.RequestCommit{})

// Increment time, blockheight, ready to start a new block
n.blockHeight++
Expand Down
9 changes: 9 additions & 0 deletions core/blockchain/nullchain/nullchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,15 @@ func getTestUnstartedNullChain(t *testing.T, txnPerBlock uint64, d time.Duration
n.SetABCIApp(app)
require.NotNil(t, n)

app.EXPECT().PrepareProposal(gomock.Any(), gomock.Any()).DoAndReturn(
func(_ context.Context, req *abci.RequestPrepareProposal) (*abci.ResponsePrepareProposal, error) {
ret := &abci.ResponsePrepareProposal{
Txs: req.Txs,
}
return ret, nil
},
).AnyTimes()

return &testNullBlockChain{
chain: n,
ctrl: ctrl,
Expand Down
1 change: 1 addition & 0 deletions core/txcache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func NewTxCache(commander *nodewallets.Commander) *TxCache {
return &TxCache{
commander: commander,
marketToDelayRequired: map[string]bool{},
heightToTxs: map[uint64][][]byte{},
}
}

Expand Down

0 comments on commit 0ef13b7

Please sign in to comment.