Commit 05d36acb authored by duanjinfei's avatar duanjinfei

batch get acc nonce

parent bca8d77d
...@@ -26,7 +26,12 @@ func startTest() { ...@@ -26,7 +26,12 @@ func startTest() {
cfg.GoRoutineCount = goRoutineCount cfg.GoRoutineCount = goRoutineCount
cfg.SignCount = txCount / 100 cfg.SignCount = txCount / 100
cfg.BatchCount = batchCount cfg.BatchCount = batchCount
arr := transaction.SignedTxArr(SendTxAccountArr, cfg) err, syncMap := transaction.InitAccNonce(SendTxAccountArr, cfg)
if err != nil {
log.Errorf("Bath Send Tran error: %s", err)
return
}
arr := transaction.SignedTxArr(syncMap, SendTxAccountArr, cfg)
if broadcastTxArr { if broadcastTxArr {
if err := transaction.BroadcastTxArr(arr, cfg); err != nil { if err := transaction.BroadcastTxArr(arr, cfg); err != nil {
log.Errorf("Bath Send Tran error: %s", err) log.Errorf("Bath Send Tran error: %s", err)
......
...@@ -45,7 +45,7 @@ var ( ...@@ -45,7 +45,7 @@ var (
signTranArr = &SignTranArr{ signTranArr = &SignTranArr{
TranArr: make([]*types.Transaction, 0), TranArr: make([]*types.Transaction, 0),
} }
batchSignCount int32 batchSignCount, handleNonceCount int32
) )
func newTransactor(cfg TranConfig) (*Transactor, error) { func newTransactor(cfg TranConfig) (*Transactor, error) {
...@@ -87,32 +87,63 @@ func ProtocolAndAddress(listenAddr string) (string, string) { ...@@ -87,32 +87,63 @@ func ProtocolAndAddress(listenAddr string) (string, string) {
return protocol, address return protocol, address
} }
// SignedTxArr 获取全部签名数据 func InitAccNonce(sendTxAccountArr [][]string, cfg *tool.Config) (error, sync.Map) {
func SignedTxArr(sendTxAccountArr [][]string, cfg *tool.Config) []*types.Transaction { var accountsNonceMap sync.Map
for i := 0; i < cfg.GoRoutineCount; i++ { rowsCh := make(chan []string, 1000000)
go signedTxFunc()
}
client, err := grpc.Dial(cfg.RpcNode, grpc.WithInsecure(), grpc.WithContextDialer(dialerFunc)) 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 err, sync.Map{}
} }
defer client.Close() defer client.Close()
serviceClient := ring.NewRingServiceClient(client) serviceClient := ring.NewRingServiceClient(client)
for i := 0; i < cfg.GoRoutineCount; i++ {
go func() {
for {
select {
case sendTxAccount := <-rowsCh:
addressRow := sendTxAccount[0]
privateKey := sendTxAccount[1]
fromAddr := metatypes.HexToAddress(addressRow)
nonceReq := &ring.NonceRequest{
Address: (*metatypes.Address)(fromAddr.Bytes()),
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
defer cancel()
response, err := serviceClient.Nonce(ctx, nonceReq)
if err != nil {
log.Error("get account nonce error:", err)
}
accountsNonceMap.Store(privateKey, response.Nonce)
atomic.AddInt32(&handleNonceCount, 1)
}
}
}()
}
for _, rows := range sendTxAccountArr { for _, rows := range sendTxAccountArr {
privateKey := rows[1] rowsCh <- rows
fromAddr := metatypes.HexToAddress(rows[0]) }
nonceReq := &ring.NonceRequest{ for {
Address: (*metatypes.Address)(fromAddr.Bytes()), if handleNonceCount == int32(len(sendTxAccountArr)) {
log.Info("Wait get all acc nonce successful")
return nil, accountsNonceMap
} }
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1) }
defer cancel() }
response, err := serviceClient.Nonce(ctx, nonceReq)
if err != nil { // SignedTxArr 获取全部签名数据
log.Error("get account nonce error:", err) func SignedTxArr(syncMap sync.Map, sendTxAccountArr [][]string, cfg *tool.Config) []*types.Transaction {
return nil for i := 0; i < cfg.GoRoutineCount; i++ {
go signedTxFunc()
}
for _, rows := range sendTxAccountArr {
privateKey := rows[1]
value, ok := syncMap.Load(privateKey)
if !ok {
log.Error("Load nonce map error...........")
continue
} }
nonce := new(big.Int).SetUint64(response.Nonce) nonce := new(big.Int).SetUint64(value.(uint64))
log.Infof("from addr:%s,nonce:%d", fromAddr, nonce) log.Infof("from addr:%s,nonce:%d", fromAddr, nonce)
for signCount := 0; signCount < cfg.SignCount; signCount++ { for signCount := 0; signCount < cfg.SignCount; signCount++ {
tranCfg := TranConfig{ tranCfg := TranConfig{
......
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