Skip to content

Commit

Permalink
Use errgroup
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Apr 18, 2023
1 parent 7ebc8ca commit 617d795
Showing 1 changed file with 46 additions and 58 deletions.
104 changes: 46 additions & 58 deletions command/rootchain/fund/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"sync"

"github.com/hashicorp/go-multierror"
"github.com/spf13/cobra"
"github.com/umbracle/ethgo"
"golang.org/x/sync/errgroup"

"github.com/0xPolygon/polygon-edge/command"
"github.com/0xPolygon/polygon-edge/txrelayer"
Expand Down Expand Up @@ -90,71 +90,59 @@ func runCommand(cmd *cobra.Command, _ []string) {
}

var (
wg sync.WaitGroup
lock sync.Mutex
errCh = make(chan error)
lock sync.Mutex
g, ctx = errgroup.WithContext(cmd.Context())
)

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

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

return
}

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

return
}

fundAddr := ethgo.Address(validatorAcc)
txn := &ethgo.Transaction{
To: &fundAddr,
Value: ethgo.Ether(100),
}

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

return
}

lock.Lock()

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

lock.Unlock()
}

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

go sendFundTx(params)
params := params

g.Go(func() error {
select {
case <-ctx.Done():
return ctx.Err()
default:
if err := params.initSecretsManager(); err != nil {
return err
}

validatorAcc, err := params.getValidatorAccount()
if err != nil {
return err
}

fundAddr := ethgo.Address(validatorAcc)
txn := &ethgo.Transaction{
To: &fundAddr,
Value: ethgo.Ether(100),
}

receipt, err := txRelayer.SendTransactionLocal(txn)
if err != nil {
return err
}

lock.Lock()

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

lock.Unlock()

return nil
}
})
}

go func() {
wg.Wait()
close(errCh)
}()
if err = g.Wait(); err != nil {
outputter.SetError(fmt.Errorf("fund command failed. error: %w", err))

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

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

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

0 comments on commit 617d795

Please sign in to comment.