Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(abci): Pass the context parameter #2576

Merged
merged 2 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions consensus/cometbft/service/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *Service) InitChain(
return &cmtabci.InitChainResponse{}, s.ctx.Err()
}
//nolint:contextcheck // see s.ctx comment for more details
return s.initChain(req) // internally this uses s.ctx
return s.initChain(s.ctx, req)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this PR, I could easily interchange this s.ctx with the ctx that is provided in the function parameters.

}

// PrepareProposal implements the PrepareProposal ABCI method and returns a
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *Service) ProcessProposal(
return nil, s.ctx.Err()
}
//nolint:contextcheck // see s.ctx comment for more details
return s.processProposal(req) // internally this uses s.ctx
return s.processProposal(s.ctx, req)
}

func (s *Service) FinalizeBlock(
Expand All @@ -115,7 +115,8 @@ func (s *Service) FinalizeBlock(
// We expect this to happen and do not want to finalize any incomplete or invalid state.
return nil, s.ctx.Err()
}
return s.finalizeBlock(req) // internally this uses s.ctx
//nolint:contextcheck // see s.ctx comment for more details
return s.finalizeBlock(s.ctx, req)
}

// Commit implements the ABCI interface. It will commit all state that exists in
Expand Down
9 changes: 6 additions & 3 deletions consensus/cometbft/service/finalize_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@
package cometbft

import (
"context"
"fmt"

cmtabci "github.com/cometbft/cometbft/abci/types"
"github.com/sourcegraph/conc/iter"
)

func (s *Service) finalizeBlock(
ctx context.Context,
req *cmtabci.FinalizeBlockRequest,
) (*cmtabci.FinalizeBlockResponse, error) {
res, err := s.finalizeBlockInternal(req)
res, err := s.finalizeBlockInternal(ctx, req)
if res != nil {
res.AppHash = s.workingHash()
}
Expand All @@ -39,6 +41,7 @@ func (s *Service) finalizeBlock(
}

func (s *Service) finalizeBlockInternal(
ctx context.Context,
req *cmtabci.FinalizeBlockRequest,
) (*cmtabci.FinalizeBlockResponse, error) {
if err := s.validateFinalizeBlockHeight(req); err != nil {
Expand All @@ -50,10 +53,10 @@ func (s *Service) finalizeBlockInternal(
// here given that during block replay ProcessProposal is not executed by
// CometBFT.
if s.finalizeBlockState == nil {
s.finalizeBlockState = s.resetState(s.ctx)
s.finalizeBlockState = s.resetState(ctx)
} else {
// Preserve the CosmosSDK context while using the correct base ctx.
s.finalizeBlockState.SetContext(s.finalizeBlockState.Context().WithContext(s.ctx))
s.finalizeBlockState.SetContext(s.finalizeBlockState.Context().WithContext(ctx))
}

// Iterate over all raw transactions in the proposal and attempt to execute
Expand Down
5 changes: 4 additions & 1 deletion consensus/cometbft/service/init_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package cometbft

import (
"context"
"fmt"

"github.com/berachain/beacon-kit/primitives/encoding/json"
Expand All @@ -30,6 +31,7 @@ import (
)

func (s *Service) initChain(
ctx context.Context,
req *cmtabci.InitChainRequest,
) (*cmtabci.InitChainResponse, error) {
if req.ChainId != s.chainID {
Expand Down Expand Up @@ -81,8 +83,9 @@ func (s *Service) initChain(
}
}

s.finalizeBlockState = s.resetState(s.ctx)
s.finalizeBlockState = s.resetState(ctx)

//nolint:contextcheck // ctx already passed via resetState
resValidators, err := s.initChainer(
s.finalizeBlockState.Context(),
req.AppStateBytes,
Expand Down
7 changes: 5 additions & 2 deletions consensus/cometbft/service/process_proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
package cometbft

import (
"context"
"fmt"
"time"

cmtabci "github.com/cometbft/cometbft/abci/types"
)

func (s *Service) processProposal(
ctx context.Context,
req *cmtabci.ProcessProposalRequest,
) (*cmtabci.ProcessProposalResponse, error) {
startTime := time.Now()
Expand All @@ -50,11 +52,12 @@ func (s *Service) processProposal(
// processed the first block, as we want to avoid overwriting the
// finalizeState
// after state changes during InitChain.
s.processProposalState = s.resetState(s.ctx)
s.processProposalState = s.resetState(ctx)
if req.Height > s.initialHeight {
s.finalizeBlockState = s.resetState(s.ctx)
s.finalizeBlockState = s.resetState(ctx)
}

//nolint:contextcheck // ctx already passed via resetState
s.processProposalState.SetContext(
s.getContextForProposal(
s.processProposalState.Context(),
Expand Down
2 changes: 1 addition & 1 deletion consensus/cometbft/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (s *Service) Start(
return err
}

s.ctx = ctx
s.ResetAppCtx(ctx)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also just a nit. Might as well use the setter function.

s.node, err = node.NewNode(
ctx,
cfg,
Expand Down
Loading