Commit c5023e6c authored by Ubuntu's avatar Ubuntu

fix any type

parent f2a034dd
......@@ -4,6 +4,12 @@ package base.v1;
import "google/protobuf/timestamp.proto";
import "google/protobuf/any.proto";
import "google/protobuf/descriptor.proto";
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
// import "base/v1/options.proto";
message Bytes32 {
......@@ -86,7 +92,7 @@ message TxProof{
message Transaction{
TxProof tx_proof=1;
int64 timeout_block_num =2;
google.protobuf.Any tx =3; // EthTx StdTx
google.protobuf.Any tx =3 [(gogoproto.customtype) = "InterfaceType"]; // EthTx StdTx
}
//2. eth std: proto3 eth tx --> grpc --->proto3 eth tx
......
version: v1
deps:
- buf.build/googleapis/googleapis
- buf.build/acme/paymentapis
breaking:
use:
- FILE
......
......@@ -14,7 +14,6 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
base "github.com/CaduceusMetaverseProtocol/metaprotocol/gen/proto/go/base/v1"
......@@ -34,17 +33,100 @@ type RingServer struct {
ring.UnimplementedRingServiceServer
}
func (*RingServer) SendTxAsEth(ctx context.Context, req *base.TransactionEth) (*ring.SendRawTransactionResponse, error) {
func (*RingServer) SendTxAsEth(ctx context.Context, in *base.TransactionEth) (*ring.SendRawTransactionResponse, error) {
return &ring.SendRawTransactionResponse{}, nil
//fmt.Println(in.Tx.Inner.Recipient)
addr := common.Address{}
copy(addr[:], in.Tx.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: in.Tx.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(in.Tx.Inner.Amount),
Gas: in.Tx.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(in.Tx.Inner.Price),
Data: in.Tx.Inner.Payload,
V: new(big.Int).SetBytes(in.Tx.Inner.V),
R: new(big.Int).SetBytes(in.Tx.Inner.R),
S: new(big.Int).SetBytes(in.Tx.Inner.S),
})
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
}
func (*RingServer) SendTxAsStd(ctx context.Context, req *base.TransactionStd) (*ring.SendRawTransactionResponse, error) {
func (*RingServer) SendTxAsStd(ctx context.Context, in *base.TransactionStd) (*ring.SendRawTransactionResponse, error) {
return &ring.SendRawTransactionResponse{}, nil
addr := common.Address{}
copy(addr[:], in.Tx.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: in.Tx.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(in.Tx.Inner.Amount),
Gas: in.Tx.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(in.Tx.Inner.Price),
Data: in.Tx.Inner.Payload,
V: new(big.Int).SetBytes(in.Tx.Inner.V),
R: new(big.Int).SetBytes(in.Tx.Inner.R),
S: new(big.Int).SetBytes(in.Tx.Inner.S),
})
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
}
func (*RingServer) SendTxAsAny(ctx context.Context, req *base.Transaction) (*ring.SendRawTransactionResponse, error) {
func (*RingServer) SendTxAsAny(ctx context.Context, in *base.Transaction) (*ring.SendRawTransactionResponse, error) {
msg, err := in.Tx.UnmarshalNew()
if err != nil {
return nil, err
}
switch m := msg.(type) {
case *base.EthTx:
addr := common.Address{}
copy(addr[:], m.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: m.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(m.Inner.Amount),
Gas: m.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(m.Inner.Price),
Data: m.Inner.Payload,
V: new(big.Int).SetBytes(m.Inner.V),
R: new(big.Int).SetBytes(m.Inner.R),
S: new(big.Int).SetBytes(m.Inner.S),
})
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
case *base.StdTx:
addr := common.Address{}
copy(addr[:], m.Inner.Recipient.Address[:common.AddressLength])
ethTx := types.NewTx(&types.LegacyTx{
Nonce: m.Inner.AccountNonce,
To: &addr,
Value: new(big.Int).SetBytes(m.Inner.Amount),
Gas: m.Inner.GasLimit,
GasPrice: new(big.Int).SetBytes(m.Inner.Price),
Data: m.Inner.Payload,
V: new(big.Int).SetBytes(m.Inner.V),
R: new(big.Int).SetBytes(m.Inner.R),
S: new(big.Int).SetBytes(m.Inner.S),
})
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
}
return &ring.SendRawTransactionResponse{}, nil
}
......@@ -77,6 +159,11 @@ func pricedTransaction(to common.Address, nonce uint64, gaslimit uint64, gaspric
}
// go test -v -run EthTx -bench=. -benchtime=3s
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=3s
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=3s
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=3s
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=3s
// go test -v -run TestGrpcServer -timeout 0
//BenchmarkAny
......@@ -96,22 +183,46 @@ func int() {
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr = crypto.PubkeyToAddress(*publicKeyECDSA)
//b.Log(fromAddress)
remote, _ := crypto.GenerateKey()
tx = pricedTransaction(crypto.PubkeyToAddress(remote.PublicKey), 0, 100000, big.NewInt(1), local)
}
var count int64
var countParallel int64
func BenchmarkEthTx(b *testing.B) {
// count++
// defer fmt.Println("defer countParallel", countParallel)
// defer fmt.Println("defer count", count)
// defer fmt.Println("defer b.N", b.N)
onceFunc := func() {
//fmt.Println("once b.N", b.N)
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.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
//countParallel++
for pb.Next() {
//for i := 0; i < b.N; i++ { //串行
conn, err := grpc.Dial("127.0.0.1:9006", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
b.Fatal(err)
......@@ -150,12 +261,28 @@ func BenchmarkEthTx(b *testing.B) {
_ = res
//fmt.Printf("%x \n", res.TxHash)
}
})
//}
}
func BenchmarkStdTx(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.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
......@@ -199,6 +326,7 @@ func BenchmarkStdTx(b *testing.B) {
}
_ = res
//fmt.Printf("%x \n", res.TxHash)
}
})
......@@ -206,6 +334,18 @@ func BenchmarkStdTx(b *testing.B) {
func BenchmarkAnyTx(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.ReportAllocs()
// The loop body is executed b.N times total across all goroutines.
......@@ -245,7 +385,7 @@ func BenchmarkAnyTx(b *testing.B) {
ethTx := base.EthTx{Inner: &inner}
ethTxAsAny, err := pbany(ethTx)
ethTxAsAny, err := anypb.New(&ethTx)
res, err := c.SendTxAsAny(ctx, &base.Transaction{Tx: ethTxAsAny})
......@@ -259,16 +399,141 @@ func BenchmarkAnyTx(b *testing.B) {
})
}
func pbany(v interface{}) (*anypb.Any, error) {
pv, ok := v.(proto.Message)
if !ok {
return &anypb.Any{}, fmt.Errorf("%v is not proto.Message", pv)
func TestType(t *testing.T) {
ethTx := base.EthTx{}
fmt.Println(ethTx.ProtoReflect().Descriptor().FullName())
}
func TestAnyTx(t *testing.T) {
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)
}
return anypb.New(pv)
once.Do(onceFunc)
// b.ReportAllocs()
// // 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 {
t.Fatal(err)
}
defer conn.Close()
c := ring.NewRingServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, _ = c, ctx
inner := base.EthTxData{
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
ethTx := base.EthTx{Inner: &inner}
ethTxAsAny, err := anypb.New(&ethTx)
if err != nil {
t.Fatal(err)
}
//fmt.Println("ethTxAsAny.ProtoReflect().Descriptor().FullName()", ethTx.ProtoReflect().Descriptor().FullName())
res, err := c.SendTxAsAny(ctx, &base.Transaction{Tx: ethTxAsAny})
_ = res
if err != nil {
t.Fatal(err)
}
stdInner := base.StdTxData{
AccountNonce: tx.Nonce(),
Price: tx.GasPrice().Bytes(),
GasLimit: tx.Gas(),
Payload: tx.Data(),
}
// v, r, sigs := tx.RawSignatureValues()
stdInner.V = v.Bytes()
stdInner.R = r.Bytes()
stdInner.S = sigs.Bytes()
stdInner.Amount = tx.Value().Bytes()
//addr := base.Address{Address: tx.To().Bytes()}
stdInner.Recipient = &addr
stdTx := base.StdTx{Inner: &stdInner}
_ = stdTx
//stdTxAsAny, err := pbany(stdTx.ProtoReflect())
stdTxAsAny, err := anypb.New(&stdTx)
if err != nil {
t.Fatal(err)
}
_ = stdTxAsAny
res, err = c.SendTxAsAny(ctx, &base.Transaction{Tx: stdTxAsAny})
if err != nil {
t.Fatal(err)
}
_ = res
// }
// })
}
func BenchmarkBytesEth(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.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
......
......@@ -100,5 +100,6 @@ func pbany(v interface{}) (*anypb.Any, error) {
if !ok {
return &anypb.Any{}, fmt.Errorf("%v is not proto.Message", pv)
}
return anypb.New(pv)
}
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