Skip to content

Commit

Permalink
feat: simple binary to serve exec-api middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
tzdybal committed Nov 18, 2024
1 parent fff67cf commit 50c3a92
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 24 deletions.
69 changes: 69 additions & 0 deletions cmd/evm-middleware/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"errors"
"github.com/ethereum/go-ethereum/common"
"google.golang.org/grpc"
"log"
"net"
"sync"
"time"

"github.com/ethereum/go-ethereum/core"

grpcproxy "github.com/rollkit/go-execution/proxy/grpc"
pb "github.com/rollkit/go-execution/types/pb/execution"

evm "github.com/rollkit/go-execution-evm"
)

const bufSize = 1024 * 1024

func main() {
config := &grpcproxy.Config{
DefaultTimeout: 5 * time.Second,
MaxRequestSize: bufSize,
}

listener, err := net.Listen("tcp4", "0.0.0.0:40041")
if err != nil {
log.Fatalf("error while creating listener: %v\n", err)
}
defer func() {
_ = listener.Close()
}()

// TODO(tzdybal): initialize from genesis file
var genesisHash common.Hash
var feeRecipient common.Address

genesis := core.DefaultGenesisBlock()
genesisHash = genesis.ToBlock().Hash()

evmClient, err := evm.NewEngineAPIExecutionClient("http://:8545", "http://8551", "7fc46b41499ebc2ce07cb22e8771436de42040990d9982162620ec2eafbd2d78", genesisHash, feeRecipient)
if err != nil {
log.Fatalf("failed to create Engine API client middleware: %v", err)
}
_ = evmClient.Start()
defer evmClient.Stop()

log.Println("Starting server...")
server := grpcproxy.NewServer(evmClient, config)
s := grpc.NewServer()
pb.RegisterExecutionServiceServer(s, server)

wg := sync.WaitGroup{}
wg.Add(1)

go func() {
log.Println("Serving...")
if err := s.Serve(listener); err != nil && !errors.Is(err, grpc.ErrServerStopped) {
log.Fatalf("Server exited with error: %v\n", err)
}
wg.Done()
}()
defer s.Stop()

wg.Wait()
log.Println("Server stopped")
}
24 changes: 12 additions & 12 deletions execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log"
"math/big"
"net/http"
"strings"
Expand All @@ -16,8 +17,7 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
"github.com/golang-jwt/jwt/v5"
execution "github.com/rollkit/go-execution"
proxy_json_rpc "github.com/rollkit/go-execution/proxy/jsonrpc"
"github.com/rollkit/go-execution"
execution_types "github.com/rollkit/go-execution/types"
)

Expand All @@ -31,7 +31,6 @@ var _ execution.Executor = (*EngineAPIExecutionClient)(nil)

// EngineAPIExecutionClient implements the execution.Execute interface
type EngineAPIExecutionClient struct {
proxyClient *proxy_json_rpc.Client
engineClient *rpc.Client // engine api
ethClient *ethclient.Client
genesisHash common.Hash
Expand All @@ -40,16 +39,12 @@ type EngineAPIExecutionClient struct {

// NewEngineAPIExecutionClient creates a new instance of EngineAPIExecutionClient
func NewEngineAPIExecutionClient(
proxyConfig *proxy_json_rpc.Config,
ethURL,
engineURL string,
jwtSecret string,
genesisHash common.Hash,
feeRecipient common.Address,
) (*EngineAPIExecutionClient, error) {
proxyClient := proxy_json_rpc.NewClient()
proxyClient.SetConfig(proxyConfig)

ethClient, err := ethclient.Dial(ethURL)
if err != nil {
return nil, err
Expand Down Expand Up @@ -86,7 +81,6 @@ func NewEngineAPIExecutionClient(
}

return &EngineAPIExecutionClient{
proxyClient: proxyClient,
engineClient: engineClient,
ethClient: ethClient,
genesisHash: genesisHash,
Expand All @@ -95,14 +89,12 @@ func NewEngineAPIExecutionClient(
}

// Start starts the execution client
func (c *EngineAPIExecutionClient) Start(url string) error {
return c.proxyClient.Start(url)
func (c *EngineAPIExecutionClient) Start() error {
return nil
}

// Stop stops the execution client and closes all connections
func (c *EngineAPIExecutionClient) Stop() {
c.proxyClient.Stop()

if c.engineClient != nil {
c.engineClient.Close()
}
Expand All @@ -114,7 +106,9 @@ func (c *EngineAPIExecutionClient) Stop() {

// InitChain initializes the blockchain with genesis information
func (c *EngineAPIExecutionClient) InitChain(ctx context.Context, genesisTime time.Time, initialHeight uint64, chainID string) (execution_types.Hash, uint64, error) {
log.Println("InitCHain")
var forkchoiceResult ForkchoiceUpdatedResponse
log.Println("calling engine_forkchoiceUpdatedV3... 1")
err := c.engineClient.CallContext(ctx, &forkchoiceResult, "engine_forkchoiceUpdatedV3",
ForkchoiceState{
HeadBlockHash: c.genesisHash,
Expand All @@ -128,6 +122,7 @@ func (c *EngineAPIExecutionClient) InitChain(ctx context.Context, genesisTime ti
ParentBeaconBlockRoot: common.Hash{},
},
)
log.Println("done 1")
if err != nil {
return execution_types.Hash{}, 0, fmt.Errorf("engine_forkchoiceUpdatedV3 failed: %w", err)
}
Expand All @@ -137,7 +132,9 @@ func (c *EngineAPIExecutionClient) InitChain(ctx context.Context, genesisTime ti
}

var payloadResult PayloadResponse
log.Println("calling engine_getPayloadV3... 2")
err = c.engineClient.CallContext(ctx, &payloadResult, "engine_getPayloadV3", *forkchoiceResult.PayloadID)
log.Println("done 2")
if err != nil {
return execution_types.Hash{}, 0, fmt.Errorf("engine_getPayloadV3 failed: %w", err)
}
Expand All @@ -153,6 +150,7 @@ func (c *EngineAPIExecutionClient) InitChain(ctx context.Context, genesisTime ti

// GetTxs retrieves transactions from the transaction pool
func (c *EngineAPIExecutionClient) GetTxs(ctx context.Context) ([]execution_types.Tx, error) {
log.Println("GeteTxs")
var result struct {
Pending map[string]map[string]*types.Transaction `json:"pending"`
Queued map[string]map[string]*types.Transaction `json:"queued"`
Expand Down Expand Up @@ -190,6 +188,7 @@ func (c *EngineAPIExecutionClient) GetTxs(ctx context.Context) ([]execution_type

// ExecuteTxs executes the given transactions and returns the new state root and gas used
func (c *EngineAPIExecutionClient) ExecuteTxs(ctx context.Context, txs []execution_types.Tx, height uint64, timestamp time.Time, prevStateRoot execution_types.Hash) (execution_types.Hash, uint64, error) {
log.Println("ExecuteTxs")
ethTxs := make([][]byte, len(txs))
for i, tx := range txs {
ethTxs[i] = tx
Expand Down Expand Up @@ -256,6 +255,7 @@ func (c *EngineAPIExecutionClient) ExecuteTxs(ctx context.Context, txs []executi

// SetFinal marks a block at the given height as final
func (c *EngineAPIExecutionClient) SetFinal(ctx context.Context, height uint64) error {
log.Println("SetFinal")
block, err := c.ethClient.BlockByNumber(ctx, big.NewInt(int64(height)))
if err != nil {
return fmt.Errorf("failed to get block at height %d: %w", height, err)
Expand Down
2 changes: 1 addition & 1 deletion execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/rollkit/go-execution-evm/mocks"
proxy_json_rpc "github.com/rollkit/go-execution/proxy/jsonrpc"
proxy_json_rpc "github.com/rollkit/go-execution/proxy/grpc"
execution_types "github.com/rollkit/go-execution/types"
"github.com/stretchr/testify/require"
)
Expand Down
40 changes: 34 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@ module github.com/rollkit/go-execution-evm

go 1.22.8

replace github.com/rollkit/go-execution => github.com/lastdotnet/go-execution v0.0.0-20241108025553-291f75953069

require (
github.com/ethereum/go-ethereum v1.14.11
github.com/rollkit/go-execution v0.0.0-00010101000000-000000000000
github.com/rollkit/go-execution v0.2.1-0.20241118103724-f65906014a51
)

require (
github.com/DataDog/zstd v1.4.5 // indirect
github.com/Microsoft/go-winio v0.6.2 // indirect
github.com/StackExchange/wmi v1.2.1 // indirect
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 // indirect
github.com/celestiaorg/go-header v0.6.3 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cockroachdb/errors v1.11.3 // indirect
github.com/cockroachdb/fifo v0.0.0-20240606204812-0bbfbd93a7ce // indirect
github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect
github.com/cockroachdb/pebble v1.1.2 // indirect
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/cosmos/gogoproto v1.7.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
Expand All @@ -27,15 +36,22 @@ require (
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/ethereum/go-verkle v0.1.1-0.20240829091221-dffa7562dbe9 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/getsentry/sentry-go v0.27.0 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/gofrs/flock v0.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.3.1 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/klauspost/compress v1.17.8 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/libp2p/go-libp2p v0.35.0 // indirect
github.com/libp2p/go-libp2p-pubsub v0.11.0 // indirect
Expand All @@ -55,20 +71,31 @@ require (
github.com/multiformats/go-multihash v0.2.3 // indirect
github.com/multiformats/go-multistream v0.5.0 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/rs/cors v1.11.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/supranational/blst v0.3.13 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/text v0.17.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240814211410-ddb44dafa142 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/blake3 v1.2.1 // indirect
Expand All @@ -78,4 +105,5 @@ require (
require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/stretchr/testify v1.9.0
google.golang.org/grpc v1.67.1
)
Loading

0 comments on commit 50c3a92

Please sign in to comment.