Skip to content

Commit

Permalink
refactor: add context parameter to all executor methods (#31)
Browse files Browse the repository at this point in the history
* refactor: add context parameter to all executor methods

This commit adds context.Context as the first parameter to all executor methods and updates relevant function calls and mocks accordingly. This change enhances context propagation and allows for better control over request lifecycles and timeouts.

Resolves #25.

* refactor: replace `context.TODO()` with `ctx` arg
  • Loading branch information
tzdybal authored Nov 8, 2024
1 parent d1712b8 commit 291f759
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 145 deletions.
28 changes: 5 additions & 23 deletions execution.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package execution

import (
"context"
"time"

"github.com/rollkit/go-execution/types"
Expand All @@ -9,33 +10,14 @@ import (
// Executor defines a common interface for interacting with the execution client.
type Executor interface {
// InitChain initializes the blockchain with genesis information.
InitChain(
genesisTime time.Time,
initialHeight uint64,
chainID string,
) (
stateRoot types.Hash,
maxBytes uint64,
err error,
)
InitChain(ctx context.Context, genesisTime time.Time, initialHeight uint64, chainID string) (stateRoot types.Hash, maxBytes uint64, err error)

// GetTxs retrieves all available transactions from the execution client's mempool.
GetTxs() ([]types.Tx, error)
GetTxs(ctx context.Context) ([]types.Tx, error)

// ExecuteTxs executes a set of transactions to produce a new block header.
ExecuteTxs(
txs []types.Tx,
blockHeight uint64,
timestamp time.Time,
prevStateRoot types.Hash,
) (
updatedStateRoot types.Hash,
maxBytes uint64,
err error,
)
ExecuteTxs(ctx context.Context, txs []types.Tx, blockHeight uint64, timestamp time.Time, prevStateRoot types.Hash) (updatedStateRoot types.Hash, maxBytes uint64, err error)

// SetFinal marks a block at the given height as final.
SetFinal(
blockHeight uint64,
) error
SetFinal(ctx context.Context, blockHeight uint64) error
}
119 changes: 63 additions & 56 deletions mocks/mock_Executor.go

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

20 changes: 4 additions & 16 deletions proxy/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ func (c *Client) Stop() error {
}

// InitChain initializes the blockchain with genesis information.
func (c *Client) InitChain(genesisTime time.Time, initialHeight uint64, chainID string) (types.Hash, uint64, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.config.DefaultTimeout)
defer cancel()

func (c *Client) InitChain(ctx context.Context, genesisTime time.Time, initialHeight uint64, chainID string) (types.Hash, uint64, error) {
resp, err := c.client.InitChain(ctx, &pb.InitChainRequest{
GenesisTime: genesisTime.Unix(),
InitialHeight: initialHeight,
Expand All @@ -71,10 +68,7 @@ func (c *Client) InitChain(genesisTime time.Time, initialHeight uint64, chainID
}

// GetTxs retrieves all available transactions from the execution client's mempool.
func (c *Client) GetTxs() ([]types.Tx, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.config.DefaultTimeout)
defer cancel()

func (c *Client) GetTxs(ctx context.Context) ([]types.Tx, error) {
resp, err := c.client.GetTxs(ctx, &pb.GetTxsRequest{})
if err != nil {
return nil, err
Expand All @@ -89,10 +83,7 @@ func (c *Client) GetTxs() ([]types.Tx, error) {
}

// ExecuteTxs executes a set of transactions to produce a new block header.
func (c *Client) ExecuteTxs(txs []types.Tx, blockHeight uint64, timestamp time.Time, prevStateRoot types.Hash) (types.Hash, uint64, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.config.DefaultTimeout)
defer cancel()

func (c *Client) ExecuteTxs(ctx context.Context, txs []types.Tx, blockHeight uint64, timestamp time.Time, prevStateRoot types.Hash) (types.Hash, uint64, error) {
req := &pb.ExecuteTxsRequest{
Txs: make([][]byte, len(txs)),
BlockHeight: blockHeight,
Expand All @@ -115,10 +106,7 @@ func (c *Client) ExecuteTxs(txs []types.Tx, blockHeight uint64, timestamp time.T
}

// SetFinal marks a block at the given height as final.
func (c *Client) SetFinal(blockHeight uint64) error {
ctx, cancel := context.WithTimeout(context.Background(), c.config.DefaultTimeout)
defer cancel()

func (c *Client) SetFinal(ctx context.Context, blockHeight uint64) error {
_, err := c.client.SetFinal(ctx, &pb.SetFinalRequest{
BlockHeight: blockHeight,
})
Expand Down
Loading

0 comments on commit 291f759

Please sign in to comment.