Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Parallelize execution of fund commands on root
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Apr 18, 2023
1 parent e37c9da commit d910491
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions command/rootchain/fund/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package fund

import (
"fmt"
"sync"

"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
"github.com/umbracle/ethgo"

Expand Down Expand Up @@ -78,7 +80,7 @@ func runCommand(cmd *cobra.Command, _ []string) {
defer outputter.WriteOutput()

paramsList := getParamsList()
resList := make(command.Results, len(paramsList))
resList := make(command.Results, 0)

txRelayer, err := txrelayer.NewTxRelayer(txrelayer.WithIPAddress(jsonRPCAddress))
if err != nil {
Expand All @@ -87,16 +89,24 @@ func runCommand(cmd *cobra.Command, _ []string) {
return
}

for i, params := range paramsList {
var (
wg sync.WaitGroup
lock sync.Mutex
errCh = make(chan error)
)

sendFundTx := func(params fundParams) {
defer wg.Done()

if err := params.initSecretsManager(); err != nil {
outputter.SetError(err)
errCh <- err

return
}

validatorAcc, err := params.getValidatorAccount()
if err != nil {
outputter.SetError(err)
errCh <- err

return
}
Expand All @@ -109,18 +119,42 @@ func runCommand(cmd *cobra.Command, _ []string) {

receipt, err := txRelayer.SendTransactionLocal(txn)
if err != nil {
outputter.SetError(err)
errCh <- err

return
}

resList[i] = &result{
lock.Lock()

resList = append(resList, &result{
ValidatorAddr: validatorAcc,
TxHash: types.Hash(receipt.TransactionHash),
}
})

lock.Unlock()
}

outputter.SetCommandResult(resList)
for _, params := range paramsList {
wg.Add(1)

go sendFundTx(params)
}

go func() {
wg.Wait()
close(errCh)
}()

var resultingError error
for err := range errCh {
resultingError = multierror.Append(resultingError, err)
}

if resultingError != nil {
outputter.SetError(resultingError)
} else {
outputter.SetCommandResult(resList)
}
}

// getParamsList creates a list of initParams with num elements.
Expand Down

0 comments on commit d910491

Please sign in to comment.