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

fix: add static checks to grpc requests #40

Merged
merged 1 commit into from
Aug 27, 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
20 changes: 19 additions & 1 deletion grpc/execution/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ func (s *ExecutionServiceServerV1Alpha2) GetGenesisInfo(ctx context.Context, req

// GetBlock will return a block given an identifier.
func (s *ExecutionServiceServerV1Alpha2) GetBlock(ctx context.Context, req *astriaPb.GetBlockRequest) (*astriaPb.Block, error) {
if req.GetIdentifier() == nil {
return nil, status.Error(codes.InvalidArgument, "identifier cannot be empty")
}

log.Debug("GetBlock called", "request", req)
getBlockRequestCount.Inc(1)

Expand All @@ -191,8 +195,13 @@ func (s *ExecutionServiceServerV1Alpha2) GetBlock(ctx context.Context, req *astr
// BatchGetBlocks will return an array of Blocks given an array of block
// identifiers.
func (s *ExecutionServiceServerV1Alpha2) BatchGetBlocks(ctx context.Context, req *astriaPb.BatchGetBlocksRequest) (*astriaPb.BatchGetBlocksResponse, error) {
log.Debug("BatchGetBlocks called", "first block", req.Identifiers[0], "last block", req.Identifiers[len(req.Identifiers)-1], "total blocks", len(req.Identifiers))
if req.Identifiers == nil || len(req.Identifiers) == 0 {
return nil, status.Error(codes.InvalidArgument, "identifiers cannot be empty")
}

batchGetBlockRequestCount.Inc(1)
log.Debug("BatchGetBlocks called", "num blocks requested", len(req.Identifiers))

var blocks []*astriaPb.Block

ids := req.GetIdentifiers()
Expand Down Expand Up @@ -225,6 +234,10 @@ func protoU128ToBigInt(u128 *primitivev1.Uint128) *big.Int {
// ExecuteBlock drives deterministic derivation of a rollup block from sequencer
// block data
func (s *ExecutionServiceServerV1Alpha2) ExecuteBlock(ctx context.Context, req *astriaPb.ExecuteBlockRequest) (*astriaPb.Block, error) {
if err := validateStaticExecuteBlockRequest(req); err != nil {
log.Error("ExecuteBlock called with invalid ExecuteBlockRequest", "err", err)
return nil, status.Error(codes.InvalidArgument, "ExecuteBlockRequest is invalid")
}
log.Debug("ExecuteBlock called", "prevBlockHash", common.BytesToHash(req.PrevBlockHash), "tx_count", len(req.Transactions), "timestamp", req.Timestamp)
executeBlockRequestCount.Inc(1)

Expand Down Expand Up @@ -343,6 +356,11 @@ func (s *ExecutionServiceServerV1Alpha2) GetCommitmentState(ctx context.Context,
// UpdateCommitmentState replaces the whole CommitmentState with a new
// CommitmentState.
func (s *ExecutionServiceServerV1Alpha2) UpdateCommitmentState(ctx context.Context, req *astriaPb.UpdateCommitmentStateRequest) (*astriaPb.CommitmentState, error) {
if err := validateStaticCommitmentState(req.CommitmentState); err != nil {
log.Error("UpdateCommitmentState called with invalid CommitmentState", "err", err)
return nil, status.Error(codes.InvalidArgument, "CommitmentState is invalid")
}

log.Debug("UpdateCommitmentState called", "request_soft_height", req.CommitmentState.Soft.Number, "request_firm_height", req.CommitmentState.Firm.Number)
updateCommitmentStateRequestCount.Inc(1)
commitmentUpdateStart := time.Now()
Expand Down
58 changes: 58 additions & 0 deletions grpc/execution/validation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package execution

import (
astriaPb "buf.build/gen/go/astria/execution-apis/protocolbuffers/go/astria/execution/v1alpha2"
sequencerblockv1alpha1 "buf.build/gen/go/astria/sequencerblock-apis/protocolbuffers/go/astria/sequencerblock/v1alpha1"
"crypto/sha256"
"fmt"
Expand Down Expand Up @@ -101,3 +102,60 @@ func validateAndUnmarshalSequencerTx(
return ethTx, nil
}
}

// `validateStaticExecuteBlockRequest` validates the given execute block request without regard
// to the current state of the system. This is useful for validating the request before any
// state changes or reads are made as a basic guard.
func validateStaticExecuteBlockRequest(req *astriaPb.ExecuteBlockRequest) error {
if req.PrevBlockHash == nil {
return fmt.Errorf("PrevBlockHash cannot be nil")
}
if req.Timestamp == nil {
return fmt.Errorf("Timestamp cannot be nil")
}

return nil
}

// `validateStaticCommitment` validates the given commitment without regard to the current state of the system.
func validateStaticCommitmentState(commitmentState *astriaPb.CommitmentState) error {
if commitmentState == nil {
return fmt.Errorf("commitment state is nil")
}
if commitmentState.Soft == nil {
return fmt.Errorf("soft block is nil")
}
if commitmentState.Firm == nil {
return fmt.Errorf("firm block is nil")
}
if commitmentState.BaseCelestiaHeight == 0 {
return fmt.Errorf("base celestia height of 0 is not valid")
}

if err := validateStaticBlock(commitmentState.Soft); err != nil {
return fmt.Errorf("soft block invalid: %w", err)
}
if err := validateStaticBlock(commitmentState.Firm); err != nil {
return fmt.Errorf("firm block invalid: %w", err)
}

return nil
}

// `validateStaticBlock` validates the given block as a without regard to the current state of the system.
func validateStaticBlock(block *astriaPb.Block) error {
if block.ParentBlockHash == nil {
return fmt.Errorf("parent block hash is nil")
}
if block.Hash == nil {
return fmt.Errorf("block hash is nil")
}
if block.Timestamp == nil {
return fmt.Errorf("timestamp is 0")
}
if block.Number == 0 {
return fmt.Errorf("block number is 0")
}

return nil
}
Loading