Commit 83e05cc4 authored by Ubuntu's avatar Ubuntu

compare tx hash

parent c5023e6c
package main_test package main_test
import ( import (
"bytes"
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"errors"
"fmt" "fmt"
"sync" "sync"
...@@ -126,14 +128,21 @@ func (*RingServer) SendTxAsAny(ctx context.Context, in *base.Transaction) (*ring ...@@ -126,14 +128,21 @@ func (*RingServer) SendTxAsAny(ctx context.Context, in *base.Transaction) (*ring
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
default:
return nil, errors.New(fmt.Sprintf("server expected tx type: %T and %T, but actually is %T", base.EthTx{}, base.StdTx{}, msg))
} }
return &ring.SendRawTransactionResponse{}, nil
} }
func (*RingServer) SendTxAsBytes(ctx context.Context, req *base.TransactionBytes) (*ring.SendRawTransactionResponse, error) { func (*RingServer) SendTxAsBytes(ctx context.Context, in *base.TransactionBytes) (*ring.SendRawTransactionResponse, error) {
return &ring.SendRawTransactionResponse{}, nil ethTx := types.Transaction{}
if err := ethTx.UnmarshalBinary(in.Tx); err != nil {
return nil, err
}
return &ring.SendRawTransactionResponse{TxHash: ethTx.Hash().Bytes()}, nil
} }
func TestGrpcServer(t *testing.T) { func TestGrpcServer(t *testing.T) {
...@@ -164,6 +173,11 @@ func pricedTransaction(to common.Address, nonce uint64, gaslimit uint64, gaspric ...@@ -164,6 +173,11 @@ func pricedTransaction(to common.Address, nonce uint64, gaslimit uint64, gaspric
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=3s // go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=3s
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=3s // go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=3s
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=1s
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=1s
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=1s
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=1s
// go test -v -run TestGrpcServer -timeout 0 // go test -v -run TestGrpcServer -timeout 0
//BenchmarkAny //BenchmarkAny
...@@ -192,6 +206,8 @@ func int() { ...@@ -192,6 +206,8 @@ func int() {
var count int64 var count int64
var countParallel int64 var countParallel int64
var onceHash sync.Once
func BenchmarkEthTx(b *testing.B) { func BenchmarkEthTx(b *testing.B) {
// count++ // count++
...@@ -261,6 +277,14 @@ func BenchmarkEthTx(b *testing.B) { ...@@ -261,6 +277,14 @@ func BenchmarkEthTx(b *testing.B) {
_ = res _ = 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())
})
//fmt.Printf("%x \n", res.TxHash) //fmt.Printf("%x \n", res.TxHash)
} }
...@@ -327,6 +351,13 @@ func BenchmarkStdTx(b *testing.B) { ...@@ -327,6 +351,13 @@ func BenchmarkStdTx(b *testing.B) {
_ = res _ = res
//fmt.Printf("%x \n", res.TxHash) //fmt.Printf("%x \n", res.TxHash)
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())
})
} }
}) })
...@@ -395,6 +426,93 @@ func BenchmarkAnyTx(b *testing.B) { ...@@ -395,6 +426,93 @@ func BenchmarkAnyTx(b *testing.B) {
_ = res _ = 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() {
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) {
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()
txAsBytes, err := tx.MarshalBinary()
// 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
// inner.From = fromAddr.Bytes()
// txAsRes := &base.TransactionEth{Tx: &base.EthTx{Inner: &inner}}
// txAsRes.
// if err != nil {
// b.Fatal(err)
// }
res, err := c.SendTxAsBytes(ctx, &base.TransactionBytes{Tx: txAsBytes, From: fromAddr[:]})
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())
})
} }
}) })
} }
...@@ -519,52 +637,3 @@ func TestAnyTx(t *testing.T) { ...@@ -519,52 +637,3 @@ func TestAnyTx(t *testing.T) {
// } // }
// }) // })
} }
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) {
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()
txAsBytes, err := tx.MarshalBinary()
if err != nil {
b.Fatal(err)
}
res, err := c.SendTxAsBytes(ctx, &base.TransactionBytes{Tx: txAsBytes, From: fromAddr[:]})
if err != nil {
b.Fatal(err)
}
_ = res
}
})
}
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