Commit 86fce5f4 authored by Ubuntu's avatar Ubuntu

add empty grpc

parent 30c64eea
......@@ -6,8 +6,8 @@ import "google/protobuf/timestamp.proto";
import "google/protobuf/any.proto";
import "google/protobuf/descriptor.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
// import "google/protobuf/descriptor.proto";
// import "github.com/gogo/protobuf/gogoproto/gogo.proto";
// import "base/v1/options.proto";
......@@ -92,7 +92,7 @@ message TxProof{
message Transaction{
TxProof tx_proof=1;
int64 timeout_block_num =2;
google.protobuf.Any tx =3 [(gogoproto.customtype) = "InterfaceType"]; // EthTx StdTx
google.protobuf.Any tx =3; // EthTx StdTx
}
//2. eth std: proto3 eth tx --> grpc --->proto3 eth tx
......
This diff is collapsed.
......@@ -12,6 +12,7 @@ import (
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
emptypb "google.golang.org/protobuf/types/known/emptypb"
)
// This is a compile-time assertion to ensure that this generated file
......@@ -28,6 +29,8 @@ type RingServiceClient interface {
SendTxAsStd(ctx context.Context, in *v1.TransactionStd, opts ...grpc.CallOption) (*SendRawTransactionResponse, error)
SendTxAsAny(ctx context.Context, in *v1.Transaction, opts ...grpc.CallOption) (*SendRawTransactionResponse, error)
SendTxAsBytes(ctx context.Context, in *v1.TransactionBytes, opts ...grpc.CallOption) (*SendRawTransactionResponse, error)
SendEmpty(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error)
SendEmptyMsg(ctx context.Context, in *EmptyRequest, opts ...grpc.CallOption) (*EmptyResponse, error)
// rpc SendRawTransaction(base.v1.EthTx) returns (SendRawTransactionResponse) {};
Nonce(ctx context.Context, in *NonceRequest, opts ...grpc.CallOption) (*NonceResponse, error)
}
......@@ -76,6 +79,24 @@ func (c *ringServiceClient) SendTxAsBytes(ctx context.Context, in *v1.Transactio
return out, nil
}
func (c *ringServiceClient) SendEmpty(ctx context.Context, in *emptypb.Empty, opts ...grpc.CallOption) (*emptypb.Empty, error) {
out := new(emptypb.Empty)
err := c.cc.Invoke(ctx, "/ring.v1.RingService/SendEmpty", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *ringServiceClient) SendEmptyMsg(ctx context.Context, in *EmptyRequest, opts ...grpc.CallOption) (*EmptyResponse, error) {
out := new(EmptyResponse)
err := c.cc.Invoke(ctx, "/ring.v1.RingService/SendEmptyMsg", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *ringServiceClient) Nonce(ctx context.Context, in *NonceRequest, opts ...grpc.CallOption) (*NonceResponse, error) {
out := new(NonceResponse)
err := c.cc.Invoke(ctx, "/ring.v1.RingService/Nonce", in, out, opts...)
......@@ -94,6 +115,8 @@ type RingServiceServer interface {
SendTxAsStd(context.Context, *v1.TransactionStd) (*SendRawTransactionResponse, error)
SendTxAsAny(context.Context, *v1.Transaction) (*SendRawTransactionResponse, error)
SendTxAsBytes(context.Context, *v1.TransactionBytes) (*SendRawTransactionResponse, error)
SendEmpty(context.Context, *emptypb.Empty) (*emptypb.Empty, error)
SendEmptyMsg(context.Context, *EmptyRequest) (*EmptyResponse, error)
// rpc SendRawTransaction(base.v1.EthTx) returns (SendRawTransactionResponse) {};
Nonce(context.Context, *NonceRequest) (*NonceResponse, error)
mustEmbedUnimplementedRingServiceServer()
......@@ -115,6 +138,12 @@ func (UnimplementedRingServiceServer) SendTxAsAny(context.Context, *v1.Transacti
func (UnimplementedRingServiceServer) SendTxAsBytes(context.Context, *v1.TransactionBytes) (*SendRawTransactionResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method SendTxAsBytes not implemented")
}
func (UnimplementedRingServiceServer) SendEmpty(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
return nil, status.Errorf(codes.Unimplemented, "method SendEmpty not implemented")
}
func (UnimplementedRingServiceServer) SendEmptyMsg(context.Context, *EmptyRequest) (*EmptyResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method SendEmptyMsg not implemented")
}
func (UnimplementedRingServiceServer) Nonce(context.Context, *NonceRequest) (*NonceResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method Nonce not implemented")
}
......@@ -203,6 +232,42 @@ func _RingService_SendTxAsBytes_Handler(srv interface{}, ctx context.Context, de
return interceptor(ctx, in, info, handler)
}
func _RingService_SendEmpty_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(emptypb.Empty)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(RingServiceServer).SendEmpty(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/ring.v1.RingService/SendEmpty",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(RingServiceServer).SendEmpty(ctx, req.(*emptypb.Empty))
}
return interceptor(ctx, in, info, handler)
}
func _RingService_SendEmptyMsg_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(EmptyRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(RingServiceServer).SendEmptyMsg(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/ring.v1.RingService/SendEmptyMsg",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(RingServiceServer).SendEmptyMsg(ctx, req.(*EmptyRequest))
}
return interceptor(ctx, in, info, handler)
}
func _RingService_Nonce_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(NonceRequest)
if err := dec(in); err != nil {
......@@ -244,6 +309,14 @@ var RingService_ServiceDesc = grpc.ServiceDesc{
MethodName: "SendTxAsBytes",
Handler: _RingService_SendTxAsBytes_Handler,
},
{
MethodName: "SendEmpty",
Handler: _RingService_SendEmpty_Handler,
},
{
MethodName: "SendEmptyMsg",
Handler: _RingService_SendEmptyMsg_Handler,
},
{
MethodName: "Nonce",
Handler: _RingService_Nonce_Handler,
......
......@@ -17,12 +17,15 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/emptypb"
base "github.com/CaduceusMetaverseProtocol/metaprotocol/gen/proto/go/base/v1"
ring "github.com/CaduceusMetaverseProtocol/metaprotocol/gen/proto/go/ring/v1"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"unsafe"
)
var countParallelAsEth int64
......@@ -34,6 +37,14 @@ type RingServer struct {
ring.UnimplementedRingServiceServer
}
func (*RingServer) SendEmptyMsg(context.Context, *ring.EmptyRequest) (*ring.EmptyResponse, error) {
return &ring.EmptyResponse{}, nil
}
func (*RingServer) SendEmpty(context.Context, *emptypb.Empty) (*emptypb.Empty, error) {
return &emptypb.Empty{}, nil
}
func (*RingServer) SendTxAsEth(ctx context.Context, in *base.TransactionEth) (*ring.SendRawTransactionResponse, error) {
addr := common.Address{}
......@@ -178,10 +189,26 @@ func pricedTransaction(to common.Address, nonce uint64, gaslimit uint64, gaspric
}
// go test -v -run EthTx -bench=. -benchtime=3s
// go test -v -run BenchmarkAnyTxEth -bench BenchmarkAnyTxEth -benchtime=3s -benchmem
// go test -v -run BenchmarkAnyTxStd -bench BenchmarkAnyTxStd -benchtime=3s -benchmem
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=3s -benchmem
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=3s -benchmem
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=3s -benchmem
//// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=3s -benchmem
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=3s -benchmem
// go test -v -run BenchmarkEmpty -bench BenchmarkEmpty -benchtime=3s -benchmem
// go test -v -run BenchmarkEmptyMsg -bench BenchmarkEmptyMsg -benchtime=3s -benchmem
// go test -v -run BenchmarkAnyTxEth -bench BenchmarkAnyTxEth -benchtime=20000x -benchmem
// go test -v -run BenchmarkAnyTxStd -bench BenchmarkAnyTxStd -benchtime=20000x -benchmem
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=20000x -benchmem
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=20000x -benchmem
//// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=20000x -benchmem
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=20000x -benchmem
// go test -v -run BenchmarkEmpty -bench BenchmarkEmpty -benchtime=20000x -benchmem
// go test -v -run BenchmarkEmptyMsg -bench BenchmarkEmptyMsg -benchtime=20000x -benchmem
//BenchmarkEmptyMsg
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=1s
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=1s
......@@ -216,6 +243,10 @@ var fromAddr common.Address
// var local, remote *ecdsa.PrivateKey
func TestTxSizeOf(t *testing.T) {
fmt.Println(unsafe.Sizeof(*tx)) //88
}
func int() {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
......@@ -233,6 +264,64 @@ var countParallel int64
var onceHash sync.Once
func BenchmarkEmpty(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Fatal(err)
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
res, err := c.SendEmpty(ctx, &emptypb.Empty{})
if err != nil {
b.Fatal(err)
}
_ = res
}
})
}
func BenchmarkEmptyMsg(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Fatal(err)
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
c.SendEmptyMsg(ctx, &ring.EmptyRequest{})
res, err := c.SendEmpty(ctx, &emptypb.Empty{})
if err != nil {
b.Fatal(err)
}
_ = res
}
})
}
func BenchmarkEthTx(b *testing.B) {
b.Logf("b.N: %d\n", b.N)
......@@ -377,7 +466,7 @@ func BenchmarkStdTx(b *testing.B) {
})
}
func BenchmarkAnyTx(b *testing.B) {
func BenchmarkAnyTxEth(b *testing.B) {
onceFunc := func() {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
......@@ -452,6 +541,81 @@ func BenchmarkAnyTx(b *testing.B) {
})
}
func BenchmarkAnyTxStd(b *testing.B) {
onceFunc := func() {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA)
remote, _ := crypto.GenerateKey()
tx = pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
}
once.Do(onceFunc)
b.ResetTimer()
// The loop body is executed b.N times total across all goroutines.
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Fatal(err)
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
inner := base.StdTxData{
AccountNonce: tx.Nonce(),
Price: tx.GasPrice().Bytes(),
GasLimit: tx.Gas(),
Payload: tx.Data(),
}
v, r, sigs := tx.RawSignatureValues()
inner.V = v.Bytes()
inner.R = r.Bytes()
inner.S = sigs.Bytes()
inner.Amount = tx.Value().Bytes()
addr := base.Address{Address: tx.To().Bytes()}
inner.Recipient = &addr
stdTx := base.StdTx{Inner: &inner}
stdTxAsAny, err := anypb.New(&stdTx)
res, err := c.SendTxAsAny(ctx, &base.Transaction{Tx: stdTxAsAny})
if err != nil {
b.Fatal(err)
}
_ = res
if bytes.Compare(tx.Hash().Bytes(), res.TxHash) != 0 {
b.Fatal(err)
}
onceHash.Do(func() {
b.Logf("response: %x local: %x \n", res.TxHash, tx.Hash().Bytes())
})
}
})
}
func BenchmarkBytesEth(b *testing.B) {
onceFunc := func() {
......
......@@ -7,6 +7,8 @@ import "ring/v1/request_response.proto";
import "ring/v1/account_req_res.proto";
import "base/v1/resource.proto";
// import "google/protobuf/empty.proto";
service RingService{
// account info service
......@@ -18,7 +20,16 @@ service RingService{
rpc SendTxAsBytes(base.v1.TransactionBytes) returns (SendRawTransactionResponse) {};
rpc SendEmpty(google.protobuf.Empty) returns (google.protobuf.Empty) {};
rpc SendEmptyMsg(EmptyRequest) returns (EmptyResponse) {};
//rpc SendRawTransaction(base.v1.EthTx) returns (SendRawTransactionResponse) {};
rpc Nonce(NonceRequest) returns (NonceResponse) {};
}
message EmptyRequest {
}
message EmptyResponse {
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment