Skip to content

Commit

Permalink
add state for conductor commitments + stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
mycodecrafting committed Feb 9, 2024
1 parent a248579 commit 2a61400
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 173 deletions.
13 changes: 8 additions & 5 deletions astria/execution/handler.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package execution

import (
log "log/slog"
"net"
"sync"

astriaGrpc "buf.build/gen/go/astria/execution-apis/grpc/go/astria/execution/v1alpha2/executionv1alpha2grpc"
"github.com/cometbft/cometbft/libs/log"
"google.golang.org/grpc"
)

Expand All @@ -17,20 +17,22 @@ type GRPCServerHandler struct {
endpoint string
server *grpc.Server
executionServiceServerV1a2 *astriaGrpc.ExecutionServiceServer
logger log.Logger
}

// NewServer creates a new gRPC server.
// It registers the execution service server.
// It registers the gRPC server with the node so it can be stopped on shutdown.
func NewGRPCServerHandler(execServ astriaGrpc.ExecutionServiceServer, endpoint string) *GRPCServerHandler {
func NewGRPCServerHandler(execServ astriaGrpc.ExecutionServiceServer, endpoint string, logger log.Logger) *GRPCServerHandler {
server := grpc.NewServer()

log.Info("gRPC server enabled", "endpoint", endpoint)
logger.Info("gRPC server enabled", "endpoint", endpoint)

handler := &GRPCServerHandler{
endpoint: endpoint,
server: server,
executionServiceServerV1a2: &execServ,
logger: logger,
}

astriaGrpc.RegisterExecutionServiceServer(server, execServ)
Expand All @@ -49,10 +51,11 @@ func (handler *GRPCServerHandler) Start() error {
// Start the gRPC server
lis, err := net.Listen("tcp", handler.endpoint)
if err != nil {
handler.logger.Error("gRPC server could not be started", "err", err)
return err
}
go handler.server.Serve(lis)
log.Info("gRPC server started", "endpoint", handler.endpoint)
handler.logger.Info("gRPC server started", "endpoint", handler.endpoint)
return nil
}

Expand All @@ -62,6 +65,6 @@ func (handler *GRPCServerHandler) Stop() error {
defer handler.mu.Unlock()

handler.server.GracefulStop()
log.Info("gRPC server stopped", "endpoint", handler.endpoint)
handler.logger.Info("gRPC server stopped", "endpoint", handler.endpoint)
return nil
}
57 changes: 17 additions & 40 deletions astria/execution/v1alpha2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package execution
import (
"context"
"sync"
"time"

astriaGrpc "buf.build/gen/go/astria/execution-apis/grpc/go/astria/execution/v1alpha2/executionv1alpha2grpc"
astriaPb "buf.build/gen/go/astria/execution-apis/protocolbuffers/go/astria/execution/v1alpha2"
"github.com/astriaorg/rollkit/astria/state/commitment"
"github.com/astriaorg/rollkit/block"
"github.com/astriaorg/rollkit/store"
"github.com/astriaorg/rollkit/types"
Expand All @@ -23,6 +23,7 @@ type ExecutionServiceServerV1Alpha2 struct {
// UnimplementedExecutionServiceServer for forward compatibility
astriaGrpc.UnimplementedExecutionServiceServer

commitmentStore *commitment.CommitmentState
store store.Store
blockManager *block.SSManager
genesis GenesisInfo
Expand All @@ -37,12 +38,13 @@ type GenesisInfo struct {
CelestiaBlockVariance uint64
}

func NewExecutionServiceServerV1Alpha2(blockManager *block.SSManager, genesis GenesisInfo, store store.Store, logger log.Logger) *ExecutionServiceServerV1Alpha2 {
func NewExecutionServiceServerV1Alpha2(blockManager *block.SSManager, genesis GenesisInfo, commitmentStore *commitment.CommitmentState, store store.Store, logger log.Logger) *ExecutionServiceServerV1Alpha2 {
return &ExecutionServiceServerV1Alpha2{
blockManager: blockManager,
genesis: genesis,
store: store,
logger: logger,
blockManager: blockManager,
genesis: genesis,
commitmentStore: commitmentStore,
store: store,
logger: logger,
}
}

Expand Down Expand Up @@ -148,40 +150,10 @@ func (s *ExecutionServiceServerV1Alpha2) GetCommitmentState(ctx context.Context,
reqJson, _ := protojson.Marshal(req)
s.logger.Info("GetCommitmentState called", "request", reqJson)

var res *astriaPb.CommitmentState

height := s.blockManager.GetStoreHeight()

if height == 0 {
genHash := [32]byte{0x0}
pbGenBlock := &astriaPb.Block{
Number: uint32(0),
Hash: genHash[:],
ParentBlockHash: genHash[:],
Timestamp: timestamppb.New(time.Now()),
}
res = &astriaPb.CommitmentState{
Soft: pbGenBlock,
Firm: pbGenBlock,
}
} else {
block, err := s.store.GetBlock(ctx, height)
if err != nil {
s.logger.Error("failed finding block with height", "height", height, "error", err)
return nil, err
}

pbBlock := &astriaPb.Block{
Number: uint32(block.Height()),
Hash: cmbytes.HexBytes(block.Hash()),
ParentBlockHash: cmbytes.HexBytes(block.LastHeader()),
Timestamp: timestamppb.New(block.Time()),
}

res = &astriaPb.CommitmentState{
Soft: pbBlock,
Firm: pbBlock,
}
res, err := s.commitmentStore.GetCommitmentState()
if err != nil {
s.logger.Error("GetCommitmentState failed", "err", err)
return &astriaPb.CommitmentState{}, err
}

resJson, _ := protojson.Marshal(res)
Expand All @@ -193,6 +165,11 @@ func (s *ExecutionServiceServerV1Alpha2) GetCommitmentState(ctx context.Context,
func (s *ExecutionServiceServerV1Alpha2) UpdateCommitmentState(ctx context.Context, req *astriaPb.UpdateCommitmentStateRequest) (*astriaPb.CommitmentState, error) {
reqJson, _ := protojson.Marshal(req)
s.logger.Info("UpdateCommitmentState called", "request", reqJson)

if err := s.commitmentStore.UpdateCommitmentState(req.CommitmentState); err != nil {
s.logger.Error("UpdateCommitmentState failed", "err", err)
}

return req.CommitmentState, nil
}

Expand Down
2 changes: 1 addition & 1 deletion astria/mempool/reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (mr *MempoolReaper) Reap() {
if err != nil {
panic(fmt.Sprintf("error sending message: %s\n", err))
}
println(res.Log)
mr.logger.Debug("tx response", "log", res.Log)

// wait for next tx
tx0 = tx0.NextWait()
Expand Down
7 changes: 5 additions & 2 deletions astria/sequencer/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

astriaPb "buf.build/gen/go/astria/astria/protocolbuffers/go/astria/sequencer/v1alpha1"
"github.com/astriaorg/go-sequencer-client/client"
"github.com/cometbft/cometbft/libs/log"
tendermintPb "github.com/cometbft/cometbft/rpc/core/types"
"google.golang.org/protobuf/encoding/protojson"
)
Expand All @@ -17,9 +18,10 @@ type Client struct {
Signer *client.Signer
rollupId [32]byte
nonce uint32
logger log.Logger
}

func NewClient(sequencerAddr string, private ed25519.PrivateKey, rollupId [32]byte) *Client {
func NewClient(sequencerAddr string, private ed25519.PrivateKey, rollupId [32]byte, logger log.Logger) *Client {
c, err := client.NewClient(sequencerAddr)
if err != nil {
panic(err)
Expand All @@ -29,6 +31,7 @@ func NewClient(sequencerAddr string, private ed25519.PrivateKey, rollupId [32]by
Client: c,
Signer: client.NewSigner(private),
rollupId: rollupId,
logger: logger,
}
}

Expand All @@ -53,7 +56,7 @@ func (c *Client) BroadcastTx(tx []byte) (*tendermintPb.ResultBroadcastTx, error)
}

signedJson, _ := protojson.Marshal(signed)
fmt.Printf("submitting tx to sequencer: %s\n", signedJson)
c.logger.Info("Submitting tx to sequencer", "signedTx", signedJson)

resp, err := c.Client.BroadcastTxSync(context.Background(), signed)
if err != nil {
Expand Down
74 changes: 74 additions & 0 deletions astria/state/commitment/kv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package commitment

import (
"context"
"fmt"
"time"

astriaPb "buf.build/gen/go/astria/execution-apis/protocolbuffers/go/astria/execution/v1alpha2"
ds "github.com/ipfs/go-datastore"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)

const CommitmentKey = "commitment"

type CommitmentState struct {
store ds.TxnDatastore
ctx context.Context
}

func NewCommitmentState(ctx context.Context, store ds.TxnDatastore) *CommitmentState {
return &CommitmentState{
store: store,
ctx: ctx,
}
}

func (cs *CommitmentState) UpdateCommitmentState(commitment *astriaPb.CommitmentState) error {
txn, err := cs.store.NewTransaction(cs.ctx, false)
if err != nil {
return fmt.Errorf("failed to create a new batch for transaction: %w", err)
}
defer txn.Discard(cs.ctx)

key := ds.NewKey(CommitmentKey)
val, err := proto.Marshal(commitment)
if err != nil {
return fmt.Errorf("failed to marshal the commitment: %w", err)
}

if err := txn.Put(cs.ctx, key, val); err != nil {
return err
}

return txn.Commit(cs.ctx)
}

func (cs *CommitmentState) GetCommitmentState() (*astriaPb.CommitmentState, error) {
val, err := cs.store.Get(cs.ctx, ds.NewKey(CommitmentKey))
if err != nil {
if err == ds.ErrNotFound {
genHash := [32]byte{0x0}
pbGenBlock := &astriaPb.Block{
Number: uint32(0),
Hash: genHash[:],
ParentBlockHash: genHash[:],
Timestamp: timestamppb.New(time.Now()),
}
return &astriaPb.CommitmentState{
Soft: pbGenBlock,
Firm: pbGenBlock,
}, nil
} else {
return nil, fmt.Errorf("failed to get the commitment: %w", err)
}
}

commitment := &astriaPb.CommitmentState{}
if err := proto.Unmarshal(val, commitment); err != nil {
return nil, fmt.Errorf("failed to unmarshal the commitment: %w", err)
}

return commitment, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
)

require (
buf.build/gen/go/astria/astria/protocolbuffers/go v1.32.0-20240208041217-cec081a8099b.1
buf.build/gen/go/astria/astria/protocolbuffers/go v1.32.0-20240208213846-0e715ec578d3.1
buf.build/gen/go/astria/execution-apis/grpc/go v1.3.0-20240207231045-2f6384a93a8d.2
buf.build/gen/go/astria/execution-apis/protocolbuffers/go v1.32.0-20240207231045-2f6384a93a8d.1
github.com/astriaorg/go-sequencer-client v0.0.0-20231201013457-0df599de8e74
Expand Down
12 changes: 2 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
4d63.com/gochecknoglobals v0.1.0/go.mod h1:wfdC5ZjKSPr7CybKEcgJhUOgeAQW1+7WcyK8OvUilfo=
bitbucket.org/creachadair/shell v0.0.6/go.mod h1:8Qqi/cYk7vPnsOePHroKXDJYmb5x7ENhtiFtfZq8K+M=
buf.build/gen/go/astria/astria/protocolbuffers/go v1.32.0-20240208041217-cec081a8099b.1 h1:fnyvaSUmCcM+Ono8mu4KuR7Al0rcgsrswHRUB/osTaI=
buf.build/gen/go/astria/astria/protocolbuffers/go v1.32.0-20240208041217-cec081a8099b.1/go.mod h1:HTIae3hIWhV69v7f8BJQzr2tP6yd0/gxwLrMiG306RY=
buf.build/gen/go/astria/astria/protocolbuffers/go v1.32.0-20240208213846-0e715ec578d3.1 h1:1NLTTOQkht+eaKjttaFHfN83DKh4wduI+mkelmfuIr4=
buf.build/gen/go/astria/astria/protocolbuffers/go v1.32.0-20240208213846-0e715ec578d3.1/go.mod h1:Ma4TsfKTQby0r4gCkdb6D2ThBxTNu0mSVNZF1udF2ko=
buf.build/gen/go/astria/execution-apis/grpc/go v1.3.0-20240207231045-2f6384a93a8d.2 h1:AjjfhXMKvUteizjKbLvE0wINSs7Zp/BhhdTBJTdXEJc=
buf.build/gen/go/astria/execution-apis/grpc/go v1.3.0-20240207231045-2f6384a93a8d.2/go.mod h1:LK1fGXWZLwItgvIjWjPyJkEoFvpQ6evGC/nyXlzwZw0=
buf.build/gen/go/astria/execution-apis/protocolbuffers/go v1.28.1-20240207231045-2f6384a93a8d.4/go.mod h1:5wxRDkWimPnuhDUA4pFBaHMtrViNJAHguLU1Wq8T6x8=
buf.build/gen/go/astria/execution-apis/protocolbuffers/go v1.32.0-20240207231045-2f6384a93a8d.1 h1:480dXLg2BTRFoXGVkCxUyOoS1v3dWn+LM77kRP30ZIU=
buf.build/gen/go/astria/execution-apis/protocolbuffers/go v1.32.0-20240207231045-2f6384a93a8d.1/go.mod h1:m409hJcO0kExqrFoQS8fQ7yXHuuM8JRTEYB+09WWVy0=
buf.build/gen/go/cosmos/cosmos-proto/protocolbuffers/go v1.32.0-20211202220400-1935555c206d.1/go.mod h1:GpU2rx3tDDSvCER8/rvvgu6s6LeMU73TKjfBZ89OZKg=
buf.build/gen/go/cosmos/cosmos-sdk/protocolbuffers/go v1.32.0-20230522115704-e7a85cef453e.1/go.mod h1:J8VpbpzO6pccEOWdLbYylDP8lRPifk2EA8tmJNkdZZo=
buf.build/gen/go/cosmos/cosmos-sdk/protocolbuffers/go v1.32.0-20230719110346-aa25660f4ff7.1/go.mod h1:Tl9HTTTqDT4kfcsEwfEyUeFdJmbcgSOXL2q50rG5XBw=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.32.0-20221020125208-34d970b699f8.1/go.mod h1:5GqIYthcy/ASmnKcaT26APpxMhZirnIHXHKki69zjWI=
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.32.0-20230509103710-5e5b9fdd0180.1/go.mod h1:5GqIYthcy/ASmnKcaT26APpxMhZirnIHXHKki69zjWI=
buf.build/gen/go/cosmos/ibc/protocolbuffers/go v1.32.0-20230913112312-7ab44ae956a0.1/go.mod h1:IPgm3IicGPCXFRfFMLOgHFp3kexNEULuIzEJshSNPcY=
buf.build/gen/go/cosmos/ics23/protocolbuffers/go v1.32.0-20221207100654-55085f7c710a.1/go.mod h1:pjXPxEgmuc0apOM+/10DC/vWa3di1I0Z+CxwKqRXWYQ=
buf.build/gen/go/penumbra-zone/penumbra/protocolbuffers/go v1.32.0-20231120132728-bc443669626d.1/go.mod h1:HOI8VDE0aOk+R3EtXPICCUm0pO8D0PSnS7k2fFq2soE=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.31.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
Expand Down
Loading

0 comments on commit 2a61400

Please sign in to comment.