Skip to content

Commit

Permalink
Better Fund Return with More Wallets (#13517)
Browse files Browse the repository at this point in the history
  • Loading branch information
kalverra authored Jun 11, 2024
1 parent 76506ba commit ca8391f
Showing 1 changed file with 47 additions and 7 deletions.
54 changes: 47 additions & 7 deletions integration-tests/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package actions
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -362,8 +363,9 @@ func DeleteAllJobs(chainlinkNodes []*client.ChainlinkK8sClient) error {
return nil
}

// ReturnFunds attempts to return all the funds from the chainlink nodes to the network's default address
// all from a remote, k8s style environment
// ReturnFunds attempts to return all the funds from the chainlink nodes and other wallets to the network's default wallet,
// which will always be the first wallet in the list of wallets. If errors are encountered, it will keep trying other wallets
// and return all errors encountered.
func ReturnFunds(chainlinkNodes []*client.ChainlinkK8sClient, blockchainClient blockchain.EVMClient) error {
if blockchainClient == nil {
return fmt.Errorf("blockchain client is nil, unable to return funds from chainlink nodes")
Expand All @@ -375,29 +377,67 @@ func ReturnFunds(chainlinkNodes []*client.ChainlinkK8sClient, blockchainClient b
return nil
}

// If we fail to return funds from some addresses, we still want to try to return funds from the rest
encounteredErrors := []error{}

if len(blockchainClient.GetWallets()) > 1 {
if err := blockchainClient.SetDefaultWallet(0); err != nil {
encounteredErrors = append(encounteredErrors, err)
} else {
for walletIndex := 1; walletIndex < len(blockchainClient.GetWallets()); walletIndex++ {
decodedKey, err := hex.DecodeString(blockchainClient.GetWallets()[walletIndex].PrivateKey())
if err != nil {
encounteredErrors = append(encounteredErrors, err)
continue
}
privKey, err := crypto.ToECDSA(decodedKey)
if err != nil {
encounteredErrors = append(encounteredErrors, err)
continue
}

err = blockchainClient.ReturnFunds(privKey)
if err != nil {
encounteredErrors = append(encounteredErrors, err)
continue
}
}
}
}

for _, chainlinkNode := range chainlinkNodes {
fundedKeys, err := chainlinkNode.ExportEVMKeysForChain(blockchainClient.GetChainID().String())
if err != nil {
return err
encounteredErrors = append(encounteredErrors, err)
continue
}
for _, key := range fundedKeys {
keyToDecrypt, err := json.Marshal(key)
if err != nil {
return err
encounteredErrors = append(encounteredErrors, err)
continue
}
// This can take up a good bit of RAM and time. When running on the remote-test-runner, this can lead to OOM
// issues. So we avoid running in parallel; slower, but safer.
decryptedKey, err := keystore.DecryptKey(keyToDecrypt, client.ChainlinkKeyPassword)
if err != nil {
return err
encounteredErrors = append(encounteredErrors, err)
continue
}
err = blockchainClient.ReturnFunds(decryptedKey.PrivateKey)
if err != nil {
log.Error().Err(err).Str("Address", fundedKeys[0].Address).Msg("Error returning funds from Chainlink node")
encounteredErrors = append(encounteredErrors, fmt.Errorf("error returning funds from chainlink node: %w", err))
continue
}
}
}
return blockchainClient.WaitForEvents()
if err := blockchainClient.WaitForEvents(); err != nil {
encounteredErrors = append(encounteredErrors, err)
}
if len(encounteredErrors) > 0 {
return fmt.Errorf("encountered errors while returning funds: %v", encounteredErrors)
}
return nil
}

// FundAddresses will fund a list of addresses with an amount of native currency
Expand Down

0 comments on commit ca8391f

Please sign in to comment.