Skip to content

Commit

Permalink
Use in-memory KR for the runner
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitryhil committed Feb 8, 2024
1 parent 7f9edde commit 4619ae9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion integration-tests/processes/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func createDevRunner(
// exit on errors
relayerRunnerCfg.Processes.ExitOnError = true

components, err := runner.NewComponents(relayerRunnerCfg, xrplKeyring, coreumKeyring, chains.Log, false)
components, err := runner.NewComponents(relayerRunnerCfg, xrplKeyring, coreumKeyring, chains.Log, false, false)
require.NoError(t, err)

relayerRunner, err := runner.NewRunner(ctx, components, relayerRunnerCfg)
Expand Down
2 changes: 1 addition & 1 deletion relayer/cmd/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func NewRunnerFromHome(cmd *cobra.Command) (*runner.Runner, error) {
return nil, err
}

components, err := runner.NewComponents(cfg, xrplClientCtx.Keyring, coreumClientCtx.Keyring, zapLogger, true)
components, err := runner.NewComponents(cfg, xrplClientCtx.Keyring, coreumClientCtx.Keyring, zapLogger, true, true)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion relayer/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func bridgeClientProvider(cmd *cobra.Command) (cli.BridgeClient, error) {
return nil, errors.Wrap(err, "failed to configure coreum keyring")
}

components, err := runner.NewComponents(cfg, xrplClientCtx.Keyring, coreumClientCtx.Keyring, log, true)
components, err := runner.NewComponents(cfg, xrplClientCtx.Keyring, coreumClientCtx.Keyring, log, true, false)
if err != nil {
return nil, err
}
Expand Down
37 changes: 35 additions & 2 deletions relayer/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/google/uuid"
"github.com/pkg/errors"
rippledata "github.com/rubblelabs/ripple/data"
"go.uber.org/zap"
Expand All @@ -23,6 +24,7 @@ import (
coreumchainclient "github.com/CoreumFoundation/coreum/v4/pkg/client"
coreumchainconfig "github.com/CoreumFoundation/coreum/v4/pkg/config"
coreumchainconstant "github.com/CoreumFoundation/coreum/v4/pkg/config/constant"
coreumkeyring "github.com/CoreumFoundation/coreum/v4/pkg/keyring"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/coreum"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/logger"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/metrics"
Expand Down Expand Up @@ -185,13 +187,26 @@ func NewComponents(
coreumKeyring keyring.Keyring,
log logger.Logger,
setCoreumSDKConfig bool,
reimportRelayerKeys bool,
) (Components, error) {
// if enabled, re-import the relayer keys from the config to in-memory keyring
if reimportRelayerKeys {
var err error
xrplKeyring, err = reimportKeyIntoInMemoryKR(xrplKeyring, cfg.XRPL.MultiSignerKeyName)
if err != nil {
return Components{}, err
}
coreumKeyring, err = reimportKeyIntoInMemoryKR(coreumKeyring, cfg.Coreum.RelayerKeyName)
if err != nil {
return Components{}, err
}
}

metricSet := metrics.NewRegistry()
log, err := logger.WithMetrics(log, metricSet.ErrorCounter)
if err != nil {
return Components{}, err
}

components := Components{
Metrics: metricSet,
Log: log,
Expand Down Expand Up @@ -266,7 +281,6 @@ func NewComponents(
}

components.CoreumClientCtx = coreumClientCtx

return components, nil
}

Expand All @@ -286,6 +300,25 @@ func getAddressFromKeyring(kr keyring.Keyring, keyName string) (sdk.AccAddress,
return addr, nil
}

func reimportKeyIntoInMemoryKR(sourceKr keyring.Keyring, keyName string) (keyring.Keyring, error) {
keyInfo, err := sourceKr.Key(keyName)
if err != nil {
return nil, errors.Wrapf(err, fmt.Sprintf("failed to get key from keyring, key name:%s", keyName))
}
pass := uuid.NewString()
armor, err := sourceKr.ExportPrivKeyArmor(keyInfo.Name, pass)
if err != nil {
return nil, errors.Wrapf(err, "failed to export key")
}
encodingConfig := coreumchainconfig.NewEncodingConfig(coreumapp.ModuleBasics)
kr := coreumkeyring.NewConcurrentSafeKeyring(keyring.NewInMemory(encodingConfig.Codec))
if err := kr.ImportPrivKey(keyInfo.Name, armor, pass); err != nil {
return nil, errors.Wrapf(err, "failed to import key")
}

return kr, nil
}

func getGRPCClientConn(grpcURL string) (*grpc.ClientConn, error) {
parsedURL, err := url.Parse(grpcURL)
if err != nil {
Expand Down

0 comments on commit 4619ae9

Please sign in to comment.