Skip to content

Commit

Permalink
add grpc client
Browse files Browse the repository at this point in the history
  • Loading branch information
gupadhyaya committed Jul 19, 2024
1 parent 06dad9e commit d674367
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
module github.com/rollkit/go-sequencing

go 1.21.0

require (
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 h1:Zy9XzmMEflZ/MAaA7vNcoebnRAld7FsPW1EeBB7V0m8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
18 changes: 18 additions & 0 deletions proxy/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package proxy

Check failure on line 1 in proxy/client.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

: # github.com/rollkit/go-sequencing/proxy

import (
"net/url"

"github.com/rollkit/go-sequencing"
)

func NewClient(uri string) (sequencing.Sequencer, error) {
addr, err := url.Parse(uri)
if err != nil {
return nil, err
}
switch addr.Scheme {
case "grpc":
default:
return nil, fmt.Errorf("unknown url scheme '%s'", addr.Scheme)
}

Check failure on line 18 in proxy/client.go

View workflow job for this annotation

GitHub Actions / lint / golangci-lint

expected '}', found 'EOF' (typecheck)
72 changes: 72 additions & 0 deletions proxy/grpc/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package grpc

import (
"context"

pbseq "github.com/rollkit/go-sequencing/types/pb/sequencing"
"google.golang.org/grpc"
)

// Client is a gRPC proxy client for DA interface.
type Client struct {
conn *grpc.ClientConn

pbseq.SequencerInputClient
pbseq.SequencerOutputClient
pbseq.BatchVerifierClient
}

// NewClient returns new Client instance.
func NewClient() *Client {
return &Client{}
}

// Start connects Client to target, with given options.
func (c *Client) Start(target string, opts ...grpc.DialOption) (err error) {
c.conn, err = grpc.NewClient(target, opts...)
if err != nil {
return err
}

c.SequencerInputClient = pbseq.NewSequencerInputClient(c.conn)
c.SequencerOutputClient = pbseq.NewSequencerOutputClient(c.conn)
c.BatchVerifierClient = pbseq.NewBatchVerifierClient(c.conn)

return nil
}

// Stop gently closes Client connection.
func (c *Client) Stop() error {
return c.conn.Close()
}

// SubmitRollupTransaction submits a transaction from rollup to sequencer.
func (c *Client) SubmitRollupTransaction(ctx context.Context, rollupId []byte, tx []byte) error {
_, err := c.SequencerInputClient.SubmitRollupTransaction(ctx, &pbseq.SubmitRollupTransactionRequest{
RollupId: rollupId,
Data: tx,
})
return err
}

// GetNextBatch returns the next batch of transactions from sequencer to rollup.
func (c *Client) GetNextBatch(ctx context.Context, lastBatch [][]byte) ([][]byte, error) {
resp, err := c.SequencerOutputClient.GetNextBatch(ctx, &pbseq.BatchRequest{
Transactions: lastBatch,
})
if err != nil {
return nil, err
}
return resp.Transactions, nil
}

// VerifyBatch verifies a batch of transactions received from the sequencer.
func (c *Client) VerifyBatch(ctx context.Context, batch [][]byte) (bool, error) {
resp, err := c.BatchVerifierClient.VerifyBatch(ctx, &pbseq.BatchRequest{
Transactions: batch,
})
if err != nil {
return false, err
}
return resp.Success, nil
}

0 comments on commit d674367

Please sign in to comment.