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

refactor: add context parameter to all executor methods #31

Merged
merged 2 commits into from
Nov 8, 2024
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
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
Loading