Skip to content

Commit

Permalink
avoid using grpc server
Browse files Browse the repository at this point in the history
  • Loading branch information
bharath-123 committed May 29, 2024
1 parent 7f64d5f commit 2273524
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 117 deletions.
79 changes: 21 additions & 58 deletions grpc/execution/server_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package execution

import (
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"
primitivev1 "buf.build/gen/go/astria/primitives/protocolbuffers/go/astria/primitive/v1"
sequencerblockv1alpha1 "buf.build/gen/go/astria/sequencerblock-apis/protocolbuffers/go/astria/sequencerblock/v1alpha1"
Expand All @@ -13,24 +12,17 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
"math/big"
"testing"
)

func TestExecutionService_GetGenesisInfo(t *testing.T) {
n, ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)
ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)

conn, err := grpc.Dial(GrpcEndpointWithoutPrefix(n), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.Nil(t, err, "Failed to dial gRPC: %v", err)

client := astriaGrpc.NewExecutionServiceClient(conn)

genesisInfo, err := client.GetGenesisInfo(context.Background(), &astriaPb.GetGenesisInfoRequest{})
genesisInfo, err := serviceV1Alpha1.GetGenesisInfo(context.Background(), &astriaPb.GetGenesisInfoRequest{})
require.Nil(t, err, "GetGenesisInfo failed: %v", err)

hashedRollupId := sha256.Sum256([]byte(ethservice.BlockChain().Config().AstriaRollupName))
Expand All @@ -43,14 +35,9 @@ func TestExecutionService_GetGenesisInfo(t *testing.T) {
}

func TestExecutionServiceServerV1Alpha2_GetCommitmentState(t *testing.T) {
n, ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)

conn, err := grpc.Dial(GrpcEndpointWithoutPrefix(n), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.Nil(t, err, "Failed to dial gRPC: %v", err)

client := astriaGrpc.NewExecutionServiceClient(conn)
ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)

commitmentState, err := client.GetCommitmentState(context.Background(), &astriaPb.GetCommitmentStateRequest{})
commitmentState, err := serviceV1Alpha1.GetCommitmentState(context.Background(), &astriaPb.GetCommitmentStateRequest{})
require.Nil(t, err, "GetCommitmentState failed: %v", err)

require.NotNil(t, commitmentState, "CommitmentState is nil")
Expand All @@ -73,12 +60,7 @@ func TestExecutionServiceServerV1Alpha2_GetCommitmentState(t *testing.T) {
}

func TestExecutionService_GetBlock(t *testing.T) {
n, ethservice, _ := setupExecutionService(t, 10)

conn, err := grpc.Dial(GrpcEndpointWithoutPrefix(n), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.Nil(t, err, "Failed to dial gRPC: %v", err)

client := astriaGrpc.NewExecutionServiceClient(conn)
ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)

tests := []struct {
description string
Expand Down Expand Up @@ -110,7 +92,7 @@ func TestExecutionService_GetBlock(t *testing.T) {

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
blockInfo, err := client.GetBlock(context.Background(), tt.getBlockRequst)
blockInfo, err := serviceV1Alpha1.GetBlock(context.Background(), tt.getBlockRequst)
if tt.expectedReturnCode > 0 {
require.NotNil(t, err, "GetBlock should return an error")
require.Equal(t, tt.expectedReturnCode, status.Code(err), "GetBlock failed: %v", err)
Expand All @@ -137,12 +119,7 @@ func TestExecutionService_GetBlock(t *testing.T) {
}

func TestExecutionServiceServerV1Alpha2_BatchGetBlocks(t *testing.T) {
n, ethservice, _ := setupExecutionService(t, 10)

conn, err := grpc.Dial(GrpcEndpointWithoutPrefix(n), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.Nil(t, err, "Failed to dial gRPC: %v", err)

client := astriaGrpc.NewExecutionServiceClient(conn)
ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)

tests := []struct {
description string
Expand Down Expand Up @@ -192,7 +169,7 @@ func TestExecutionServiceServerV1Alpha2_BatchGetBlocks(t *testing.T) {

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
batchBlocksRes, err := client.BatchGetBlocks(context.Background(), tt.batchGetBlockRequest)
batchBlocksRes, err := serviceV1Alpha1.BatchGetBlocks(context.Background(), tt.batchGetBlockRequest)
if tt.expectedReturnCode > 0 {
require.NotNil(t, err, "BatchGetBlocks should return an error")
require.Equal(t, tt.expectedReturnCode, status.Code(err), "BatchGetBlocks failed: %v", err)
Expand All @@ -219,12 +196,7 @@ func bigIntToProtoU128(i *big.Int) *primitivev1.Uint128 {
}

func TestExecutionServiceServerV1Alpha2_ExecuteBlock(t *testing.T) {
n, ethservice, _ := setupExecutionService(t, 10)

conn, err := grpc.Dial(GrpcEndpointWithoutPrefix(n), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.Nil(t, err, "Failed to dial gRPC: %v", err)

client := astriaGrpc.NewExecutionServiceClient(conn)
ethservice, _ := setupExecutionService(t, 10)

tests := []struct {
description string
Expand Down Expand Up @@ -276,22 +248,18 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlock(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
// reset the blockchain with each test
n, ethservice, _ = setupExecutionService(t, 10)

conn, err = grpc.Dial(GrpcEndpointWithoutPrefix(n), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.Nil(t, err, "Failed to dial gRPC: %v", err)

client = astriaGrpc.NewExecutionServiceClient(conn)
ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)

var err error // adding this to prevent shadowing of genesisInfo in the below if branch
var genesisInfo *astriaPb.GenesisInfo
var commitmentStateBeforeExecuteBlock *astriaPb.CommitmentState
if tt.callGenesisInfoAndGetCommitmentState {
// call getGenesisInfo and getCommitmentState before calling executeBlock
genesisInfo, err = client.GetGenesisInfo(context.Background(), &astriaPb.GetGenesisInfoRequest{})
genesisInfo, err = serviceV1Alpha1.GetGenesisInfo(context.Background(), &astriaPb.GetGenesisInfoRequest{})
require.Nil(t, err, "GetGenesisInfo failed: %v", err)
require.NotNil(t, genesisInfo, "GenesisInfo is nil")

commitmentStateBeforeExecuteBlock, err = client.GetCommitmentState(context.Background(), &astriaPb.GetCommitmentStateRequest{})
commitmentStateBeforeExecuteBlock, err = serviceV1Alpha1.GetCommitmentState(context.Background(), &astriaPb.GetCommitmentStateRequest{})
require.Nil(t, err, "GetCommitmentState failed: %v", err)
require.NotNil(t, commitmentStateBeforeExecuteBlock, "CommitmentState is nil")
}
Expand Down Expand Up @@ -346,7 +314,7 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlock(t *testing.T) {
Transactions: marshalledTxs,
}

executeBlockRes, err := client.ExecuteBlock(context.Background(), executeBlockReq)
executeBlockRes, err := serviceV1Alpha1.ExecuteBlock(context.Background(), executeBlockReq)
if tt.expectedReturnCode > 0 {
require.NotNil(t, err, "ExecuteBlock should return an error")
require.Equal(t, tt.expectedReturnCode, status.Code(err), "ExecuteBlock failed: %v", err)
Expand All @@ -358,7 +326,7 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlock(t *testing.T) {
require.Equal(t, 0, astriaOrdered.Len(), "AstriaOrdered should be empty")

// check if commitment state is not updated
commitmentStateAfterExecuteBlock, err := client.GetCommitmentState(context.Background(), &astriaPb.GetCommitmentStateRequest{})
commitmentStateAfterExecuteBlock, err := serviceV1Alpha1.GetCommitmentState(context.Background(), &astriaPb.GetCommitmentStateRequest{})
require.Nil(t, err, "GetCommitmentState failed: %v", err)

require.Exactly(t, commitmentStateBeforeExecuteBlock, commitmentStateAfterExecuteBlock, "Commitment state should not be updated")
Expand All @@ -369,20 +337,15 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlock(t *testing.T) {
}

func TestExecutionServiceServerV1Alpha2_ExecuteBlockAndUpdateCommitment(t *testing.T) {
n, ethservice, _ := setupExecutionService(t, 10)

conn, err := grpc.Dial(GrpcEndpointWithoutPrefix(n), grpc.WithTransportCredentials(insecure.NewCredentials()))
require.Nil(t, err, "Failed to dial gRPC: %v", err)

client := astriaGrpc.NewExecutionServiceClient(conn)
ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)

// call genesis info
genesisInfo, err := client.GetGenesisInfo(context.Background(), &astriaPb.GetGenesisInfoRequest{})
genesisInfo, err := serviceV1Alpha1.GetGenesisInfo(context.Background(), &astriaPb.GetGenesisInfoRequest{})
require.Nil(t, err, "GetGenesisInfo failed: %v", err)
require.NotNil(t, genesisInfo, "GenesisInfo is nil")

// call get commitment state
commitmentState, err := client.GetCommitmentState(context.Background(), &astriaPb.GetCommitmentStateRequest{})
commitmentState, err := serviceV1Alpha1.GetCommitmentState(context.Background(), &astriaPb.GetCommitmentStateRequest{})
require.Nil(t, err, "GetCommitmentState failed: %v", err)
require.NotNil(t, commitmentState, "CommitmentState is nil")

Expand Down Expand Up @@ -443,7 +406,7 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlockAndUpdateCommitment(t *testi
Transactions: marshalledTxs,
}

executeBlockRes, err := client.ExecuteBlock(context.Background(), executeBlockReq)
executeBlockRes, err := serviceV1Alpha1.ExecuteBlock(context.Background(), executeBlockReq)
require.Nil(t, err, "ExecuteBlock failed: %v", err)

require.NotNil(t, executeBlockRes, "ExecuteBlock response is nil")
Expand All @@ -470,7 +433,7 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlockAndUpdateCommitment(t *testi
},
}

updateCommitmentStateRes, err := client.UpdateCommitmentState(context.Background(), updateCommitmentStateReq)
updateCommitmentStateRes, err := serviceV1Alpha1.UpdateCommitmentState(context.Background(), updateCommitmentStateReq)
require.Nil(t, err, "UpdateCommitmentState failed: %v", err)
require.NotNil(t, updateCommitmentStateRes, "UpdateCommitmentState response should not be nil")

Expand All @@ -496,5 +459,5 @@ func TestExecutionServiceServerV1Alpha2_ExecuteBlockAndUpdateCommitment(t *testi
chainDestinationAddressBalanceAfter := stateDb.GetBalance(chainDestinationAddress)

balanceDiff := new(big.Int).Sub(chainDestinationAddressBalanceAfter, chainDestinationAddressBalanceBefore)
require.True(t, balanceDiff.Cmp(big.NewInt(0)) > 0, "Chain destination address balance is not correct")
require.True(t, balanceDiff.Cmp(big.NewInt(1000000000000000000)) == 0, "Chain destination address balance is not correct")
}
38 changes: 5 additions & 33 deletions grpc/execution/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package execution
import (
"crypto/ecdsa"
"crypto/sha256"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus"
beaconConsensus "github.com/ethereum/go-ethereum/consensus/beacon"
Expand All @@ -18,7 +17,6 @@ import (
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/params"
"math/big"
"net"
"testing"
"time"
)
Expand Down Expand Up @@ -104,34 +102,14 @@ func generateMergeChain(n int, merged bool) (*core.Genesis, []*types.Block, *ecd
return genesis, blocks, bridgeAddressKey, feeCollectorKey
}

func getFreePort() (int, error) {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
if err != nil {
return 0, err
}

l, err := net.ListenTCP("tcp", addr)
if err != nil {
return 0, err
}
defer l.Close()
return l.Addr().(*net.TCPAddr).Port, nil
}

// startEthService creates a full node instance for testing.
func startEthService(t *testing.T, genesis *core.Genesis) (*node.Node, *eth.Ethereum) {
freePort, err := getFreePort()
if err != nil {
t.Fatal("can't get free port:", err)
}
func startEthService(t *testing.T, genesis *core.Genesis) *eth.Ethereum {
n, err := node.New(&node.Config{
P2P: p2p.Config{
ListenAddr: "0.0.0.0:0",
NoDiscovery: true,
MaxPeers: 25,
},
GRPCHost: "127.0.0.1",
GRPCPort: freePort,
})
if err != nil {
t.Fatal("can't create node:", err)
Expand All @@ -146,13 +124,13 @@ func startEthService(t *testing.T, genesis *core.Genesis) (*node.Node, *eth.Ethe
ethservice.SetEtherbase(testAddr)
ethservice.SetSynced()

return n, ethservice
return ethservice
}

func setupExecutionService(t *testing.T, noOfBlocksToGenerate int) (*node.Node, *eth.Ethereum, *ExecutionServiceServerV1Alpha2) {
func setupExecutionService(t *testing.T, noOfBlocksToGenerate int) (*eth.Ethereum, *ExecutionServiceServerV1Alpha2) {
t.Helper()
genesis, blocks, bridgeAddressKey, feeCollectorKey := generateMergeChain(noOfBlocksToGenerate, true)
n, ethservice := startEthService(t, genesis)
ethservice := startEthService(t, genesis)

serviceV1Alpha1, err := NewExecutionServiceServerV1Alpha2(ethservice)
if err != nil {
Expand All @@ -176,17 +154,11 @@ func setupExecutionService(t *testing.T, noOfBlocksToGenerate int) (*node.Node,
t.Fatalf("bridgeAddress not set correctly")
}

utils.RegisterGRPCExecutionService(n, serviceV1Alpha1, n.Config())

if err := n.Start(); err != nil {
t.Fatal("can't start node:", err)
}
if _, err := ethservice.BlockChain().InsertChain(blocks); err != nil {
n.Close()
t.Fatal("can't import test blocks:", err)
}

return n, ethservice, serviceV1Alpha1
return ethservice, serviceV1Alpha1

}

Expand Down
37 changes: 11 additions & 26 deletions grpc/execution/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
"github.com/stretchr/testify/require"
"math/big"
"strings"
"testing"
)

Expand All @@ -33,33 +33,23 @@ func randomDepositTx() *types.Transaction {
}

func TestSequenceTxValidation(t *testing.T) {
_, ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)
ethservice, serviceV1Alpha1 := setupExecutionService(t, 10)

blobTx, err := randomBlobTx().MarshalBinary()
if err != nil {
t.Fatalf("failed to marshal random blob tx: %v", err)
}
require.Nil(t, err, "failed to marshal random blob tx: %v", err)

depositTx, err := randomDepositTx().MarshalBinary()
if err != nil {
t.Fatalf("failed to marshal random deposit tx: %v", err)
}
require.Nil(t, err, "failed to marshal random deposit tx: %v", err)

unsignedTx := types.NewTransaction(uint64(0), common.HexToAddress("0x9a9070028361F7AAbeB3f2F2Dc07F82C4a98A02a"), big.NewInt(1), params.TxGas, big.NewInt(params.InitialBaseFee*2), nil)
tx, err := types.SignTx(unsignedTx, types.LatestSigner(ethservice.BlockChain().Config()), testKey)
if err != nil {
t.Fatalf("Failed to sign tx: %v", err)
}
require.Nil(t, err, "failed to sign tx: %v", err)

validMarshalledTx, err := tx.MarshalBinary()
if err != nil {
t.Fatalf("failed to marshal valid tx: %v", err)
}
require.Nil(t, err, "failed to marshal valid tx: %v", err)

chainDestinationKey, err := crypto.GenerateKey()
if err != nil {
t.Fatalf("failed to generate chain destination key: %v", err)
}
require.Nil(t, err, "failed to generate chain destination key: %v", err)
chainDestinationAddress := crypto.PubkeyToAddress(chainDestinationKey.PublicKey)

bridgeAssetDenom := sha256.Sum256([]byte(ethservice.BlockChain().Config().AstriaBridgeAddressConfigs[0].AssetDenom))
Expand Down Expand Up @@ -164,16 +154,11 @@ func TestSequenceTxValidation(t *testing.T) {
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
_, err := validateAndUnmarshalSequencerTx(test.sequencerTx, serviceV1Alpha1.bridgeAddresses, serviceV1Alpha1.bridgeAllowedAssetIDs)
if test.wantErr != "" && err == nil {
t.Errorf("expected error, got nil")
}
if test.wantErr == "" && err != nil {
t.Errorf("unexpected error: %v", err)
}
// check if wantErr is in err.Error()
if test.wantErr != "" && !strings.Contains(err.Error(), test.wantErr) {
t.Errorf("expected error to contain %q, got %q", test.wantErr, err.Error())
if test.wantErr == "" && err == nil {
return
}
require.False(t, test.wantErr == "" && err != nil, "expected error, got nil")
require.Contains(t, err.Error(), test.wantErr)
})
}
}

0 comments on commit 2273524

Please sign in to comment.