Commit 31b788f2 authored by duanjinfei's avatar duanjinfei

fix nil point err

parent 521b6f8d
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
"receiveAddr": "0x0Fb196385c8826e3806ebA2cA2cb78B26E08fEEe", "receiveAddr": "0x0Fb196385c8826e3806ebA2cA2cb78B26E08fEEe",
"count": 100, "count": 100,
"type": 1, "type": 1,
"rpcNode": "35.78.87.116:38002", "rpcNode": "54.168.125.67:8545",
"amount": 10000000, "amount": 10000000,
"chainId": 100, "chainId": 100,
"initAccountPrv": "FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A", "initAccountPrv": "FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A",
......
...@@ -22,7 +22,7 @@ func init() { ...@@ -22,7 +22,7 @@ func init() {
startCmd.PersistentFlags().StringVar(&cpuProfile, "cpuProfile", "cpuProfile.prof", "Statistics cpu profile") startCmd.PersistentFlags().StringVar(&cpuProfile, "cpuProfile", "cpuProfile.prof", "Statistics cpu profile")
startCmd.PersistentFlags().IntVar(&startCount, "startCount", 0, "read excel start count") startCmd.PersistentFlags().IntVar(&startCount, "startCount", 0, "read excel start count")
startCmd.PersistentFlags().IntVar(&endCount, "endCount", 100, "read excel end count") startCmd.PersistentFlags().IntVar(&endCount, "endCount", 100, "read excel end count")
startCmd.PersistentFlags().BoolVar(&broadcastTxArr, "broadcastTxArr", false, "test grpc interface -> broadcastTxArr") startCmd.PersistentFlags().BoolVar(&broadcastTxArr, "broadcastTxArr", true, "test grpc interface -> broadcastTxArr")
startCmd.PersistentFlags().IntVar(&txCount, "txCount", 1000, "send tran count") startCmd.PersistentFlags().IntVar(&txCount, "txCount", 1000, "send tran count")
startCmd.PersistentFlags().IntVar(&goRoutineCount, "goRoutineCount", 10, "send tran goRoutine count") startCmd.PersistentFlags().IntVar(&goRoutineCount, "goRoutineCount", 10, "send tran goRoutine count")
startCmd.PersistentFlags().IntVar(&batchCount, "batchCount", 100, "batch send tran count") startCmd.PersistentFlags().IntVar(&batchCount, "batchCount", 100, "batch send tran count")
......
package test
import (
"context"
"crypto/ecdsa"
ringv1 "github.com/CaduceusMetaverseProtocol/MetaProtocol/gen/proto/go/ring/v1"
metatypes "github.com/CaduceusMetaverseProtocol/MetaTypes/types"
"github.com/ethereum/go-ethereum/crypto"
"google.golang.org/grpc"
"net"
"strings"
"testing"
"time"
)
func TestNonce(t *testing.T) {
local, _ := crypto.HexToECDSA("FD5CC6F5E7E2805E920AC5DC83D5AF1106F9C92F0C04F9D5E1FD4261B4B4464A")
publicKey := local.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddr := crypto.PubkeyToAddress(*publicKeyECDSA)
c := StartGRPCClient("54.168.125.67:8545")
ctx, cancel := context.WithTimeout(context.Background(), time.Second*8)
defer cancel()
nonce, err := c.Nonce(ctx, &ringv1.NonceRequest{Address: (*metatypes.Address)(fromAddr.Bytes())})
if err != nil {
t.Fatal(err)
}
t.Logf("nonce: %v \n", nonce.Nonce)
}
func StartGRPCClient(protoAddr string) ringv1.RingServiceClient {
//nolint:staticcheck // SA1019 Existing use of deprecated but supported dial option.
conn, err := grpc.Dial(protoAddr, grpc.WithInsecure(), grpc.WithContextDialer(dialerFunc))
if err != nil {
panic(err)
}
return ringv1.NewRingServiceClient(conn)
}
func dialerFunc(ctx context.Context, addr string) (net.Conn, error) {
return Connect(addr)
}
func Connect(protoAddr string) (net.Conn, error) {
proto, address := ProtocolAndAddress(protoAddr)
conn, err := net.Dial(proto, address)
return conn, err
}
func ProtocolAndAddress(listenAddr string) (string, string) {
protocol, address := "tcp", listenAddr
parts := strings.SplitN(address, "://", 2)
if len(parts) == 2 {
protocol, address = parts[0], parts[1]
}
return protocol, address
}
...@@ -81,7 +81,9 @@ func broadcastTx(client ring.RingServiceClient) error { ...@@ -81,7 +81,9 @@ func broadcastTx(client ring.RingServiceClient) error {
if beforeSendTxTime.UnixMilli() > 0 && time.Since(beforeSendTxTime).Milliseconds() < 1 { if beforeSendTxTime.UnixMilli() > 0 && time.Since(beforeSendTxTime).Milliseconds() < 1 {
time.Sleep(time.Millisecond * time.Duration(1)) time.Sleep(time.Millisecond * time.Duration(1))
} }
txs, err := client.BroadcastTxs(context.Background(), broadcastEthTxWithFromRequests, nil) ctx, cancel := context.WithTimeout(context.Background(), time.Second*9)
defer cancel()
txs, err := client.BroadcastTxs(ctx, broadcastEthTxWithFromRequests)
if err != nil { if err != nil {
log.Error("BroadcastTxs Error:", err) log.Error("BroadcastTxs Error:", err)
} }
......
...@@ -12,8 +12,9 @@ import ( ...@@ -12,8 +12,9 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"math/big" "math/big"
"net"
"strings"
"sync/atomic" "sync/atomic"
"time" "time"
) )
...@@ -61,12 +62,29 @@ func newTransactor(cfg TranConfig) (*Transactor, error) { ...@@ -61,12 +62,29 @@ func newTransactor(cfg TranConfig) (*Transactor, error) {
return &res, nil return &res, nil
} }
func dialerFunc(ctx context.Context, addr string) (net.Conn, error) {
return Connect(addr)
}
func Connect(protoAddr string) (net.Conn, error) {
proto, address := ProtocolAndAddress(protoAddr)
conn, err := net.Dial(proto, address)
return conn, err
}
func ProtocolAndAddress(listenAddr string) (string, string) {
protocol, address := "tcp", listenAddr
parts := strings.SplitN(address, "://", 2)
if len(parts) == 2 {
protocol, address = parts[0], parts[1]
}
return protocol, address
}
// SignedTxArr 获取全部签名数据 // SignedTxArr 获取全部签名数据
func SignedTxArr(sendTxAccountArr [][]string, cfg *tool.Config) []*types.Transaction { func SignedTxArr(sendTxAccountArr [][]string, cfg *tool.Config) []*types.Transaction {
for i := 0; i < cfg.GoRoutineCount; i++ { for i := 0; i < cfg.GoRoutineCount; i++ {
go signedTxFunc() go signedTxFunc()
} }
client, err := grpc.Dial(cfg.RpcNode, grpc.WithTransportCredentials(insecure.NewCredentials())) client, err := grpc.Dial(cfg.RpcNode, grpc.WithInsecure(), grpc.WithContextDialer(dialerFunc))
if err != nil { if err != nil {
log.Error("grpc dial error:", err) log.Error("grpc dial error:", err)
return nil return nil
...@@ -77,10 +95,13 @@ func SignedTxArr(sendTxAccountArr [][]string, cfg *tool.Config) []*types.Transac ...@@ -77,10 +95,13 @@ func SignedTxArr(sendTxAccountArr [][]string, cfg *tool.Config) []*types.Transac
privateKey := rows[1] privateKey := rows[1]
fromAddr := metatypes.HexToAddress(rows[0]) fromAddr := metatypes.HexToAddress(rows[0])
nonceReq := &ring.NonceRequest{ nonceReq := &ring.NonceRequest{
Address: &fromAddr, Address: (*metatypes.Address)(fromAddr.Bytes()),
} }
response, err := serviceClient.Nonce(context.Background(), nonceReq, nil) ctx, cancel := context.WithTimeout(context.Background(), time.Second*9)
defer cancel()
response, err := serviceClient.Nonce(ctx, nonceReq)
if err != nil { if err != nil {
log.Error("get account nonce error:", err)
return nil return nil
} }
nonce := new(big.Int).SetUint64(response.Nonce) nonce := new(big.Int).SetUint64(response.Nonce)
......
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