Commit 05d36acb authored by duanjinfei's avatar duanjinfei

batch get acc nonce

parent bca8d77d
......@@ -26,7 +26,12 @@ func startTest() {
cfg.GoRoutineCount = goRoutineCount
cfg.SignCount = txCount / 100
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 err := transaction.BroadcastTxArr(arr, cfg); err != nil {
log.Errorf("Bath Send Tran error: %s", err)
......
......@@ -45,7 +45,7 @@ var (
signTranArr = &SignTranArr{
TranArr: make([]*types.Transaction, 0),
}
batchSignCount int32
batchSignCount, handleNonceCount int32
)
func newTransactor(cfg TranConfig) (*Transactor, error) {
......@@ -87,32 +87,63 @@ func ProtocolAndAddress(listenAddr string) (string, string) {
return protocol, address
}
// SignedTxArr 获取全部签名数据
func SignedTxArr(sendTxAccountArr [][]string, cfg *tool.Config) []*types.Transaction {
for i := 0; i < cfg.GoRoutineCount; i++ {
go signedTxFunc()
}
func InitAccNonce(sendTxAccountArr [][]string, cfg *tool.Config) (error, sync.Map) {
var accountsNonceMap sync.Map
rowsCh := make(chan []string, 1000000)
client, err := grpc.Dial(cfg.RpcNode, grpc.WithInsecure(), grpc.WithContextDialer(dialerFunc))
if err != nil {
log.Error("grpc dial error:", err)
return nil
return err, sync.Map{}
}
defer client.Close()
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 {
privateKey := rows[1]
fromAddr := metatypes.HexToAddress(rows[0])
nonceReq := &ring.NonceRequest{
Address: (*metatypes.Address)(fromAddr.Bytes()),
rowsCh <- rows
}
for {
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 {
log.Error("get account nonce error:", err)
return nil
}
}
// SignedTxArr 获取全部签名数据
func SignedTxArr(syncMap sync.Map, sendTxAccountArr [][]string, cfg *tool.Config) []*types.Transaction {
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)
for signCount := 0; signCount < cfg.SignCount; signCount++ {
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