diff --git a/cmd/covd/start.go b/cmd/covd/start.go index f96f9ad..b76e1d7 100644 --- a/cmd/covd/start.go +++ b/cmd/covd/start.go @@ -65,16 +65,13 @@ func start(ctx *cli.Context) error { if cfg.UseRemoteSigner { signer, err = newRemoteSignerFromConfig(cfg) - if err != nil { return fmt.Errorf("failed to create remote signer from config: %w", err) - - } else { - signer, err = newSignerFromConfig(cfg, pwd) - - if err != nil { - return fmt.Errorf("failed to create keyring signer from config: %w", err) - } + } + } else { + signer, err = newSignerFromConfig(cfg, pwd) + if err != nil { + return fmt.Errorf("failed to create keyring signer from config: %w", err) } } diff --git a/itest/e2e_test.go b/itest/e2e_test.go index e425971..f575b7c 100644 --- a/itest/e2e_test.go +++ b/itest/e2e_test.go @@ -18,7 +18,41 @@ var ( // TestCovenantEmulatorLifeCycle tests the whole life cycle of a covenant emulator // in two flows depending on whether the delegation is following pre-approval flow func TestCovenantEmulatorLifeCycle(t *testing.T) { - tm, btcPks := StartManagerWithFinalityProvider(t, 1) + tm, btcPks := StartManagerWithFinalityProvider(t, 1, false) + defer tm.Stop(t) + + // send a BTC delegation that is not following pre-approval flow + _ = tm.InsertBTCDelegation(t, btcPks, stakingTime, stakingAmount, false) + + // check the BTC delegation is pending + _ = tm.WaitForNPendingDels(t, 1) + + // check the BTC delegation is active + _ = tm.WaitForNActiveDels(t, 1) + + // send a BTC delegation that is following pre-approval flow + _ = tm.InsertBTCDelegation(t, btcPks, stakingTime, stakingAmount, true) + + // check the BTC delegation is pending + _ = tm.WaitForNPendingDels(t, 1) + + time.Sleep(10 * time.Second) + + // check the BTC delegation is verified + dels := tm.WaitForNVerifiedDels(t, 1) + + // test duplicate, should expect no error + // remove covenant sigs + dels[0].CovenantSigs = nil + dels[0].BtcUndelegation.CovenantSlashingSigs = nil + dels[0].BtcUndelegation.CovenantUnbondingSigs = nil + res, err := tm.CovenantEmulator.AddCovenantSignatures(dels) + require.NoError(t, err) + require.Empty(t, res) +} + +func TestCovenantEmulatorLifeCycleWithRemoteSigner(t *testing.T) { + tm, btcPks := StartManagerWithFinalityProvider(t, 1, true) defer tm.Stop(t) // send a BTC delegation that is not following pre-approval flow diff --git a/itest/test_manager.go b/itest/test_manager.go index 29ccd0c..008b70e 100644 --- a/itest/test_manager.go +++ b/itest/test_manager.go @@ -1,6 +1,7 @@ package e2etest import ( + "context" "math/rand" "os" "sync" @@ -12,6 +13,18 @@ import ( btcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types" btclctypes "github.com/babylonlabs-io/babylon/x/btclightclient/types" bstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types" + covcc "github.com/babylonlabs-io/covenant-emulator/clientcontroller" + covcfg "github.com/babylonlabs-io/covenant-emulator/config" + "github.com/babylonlabs-io/covenant-emulator/covenant" + signerCfg "github.com/babylonlabs-io/covenant-emulator/covenant-signer/config" + "github.com/babylonlabs-io/covenant-emulator/covenant-signer/keystore/cosmos" + signerMetrics "github.com/babylonlabs-io/covenant-emulator/covenant-signer/observability/metrics" + signerApp "github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerapp" + signerService "github.com/babylonlabs-io/covenant-emulator/covenant-signer/signerservice" + covdkeyring "github.com/babylonlabs-io/covenant-emulator/keyring" + "github.com/babylonlabs-io/covenant-emulator/remotesigner" + "github.com/babylonlabs-io/covenant-emulator/testutil" + "github.com/babylonlabs-io/covenant-emulator/types" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/wire" @@ -20,13 +33,6 @@ import ( stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" "github.com/stretchr/testify/require" "go.uber.org/zap" - - covcc "github.com/babylonlabs-io/covenant-emulator/clientcontroller" - covcfg "github.com/babylonlabs-io/covenant-emulator/config" - "github.com/babylonlabs-io/covenant-emulator/covenant" - covdkeyring "github.com/babylonlabs-io/covenant-emulator/keyring" - "github.com/babylonlabs-io/covenant-emulator/testutil" - "github.com/babylonlabs-io/covenant-emulator/types" ) var ( @@ -71,21 +77,81 @@ type testFinalityProviderData struct { PoP *bstypes.ProofOfPossessionBTC } -func StartManager(t *testing.T) *TestManager { +func StartManager(t *testing.T, useRemoteSigner bool) *TestManager { testDir, err := baseDir("cee2etest") require.NoError(t, err) logger := zap.NewNop() - - // 1. prepare covenant key, which will be used as input of Babylon node covenantConfig := defaultCovenantConfig(testDir) err = covenantConfig.Validate() require.NoError(t, err) - covKeyPair, err := covdkeyring.CreateCovenantKey(testDir, chainID, covenantKeyName, keyring.BackendTest, passphrase, hdPath) - require.NoError(t, err) + + // 1. prepare covenant key, which will be used as input of Babylon node + var signer covenant.Signer + var covPubKey *btcec.PublicKey + if useRemoteSigner { + covenantConfig.UseRemoteSigner = true + signerConfig := signerCfg.DefaultConfig() + signerConfig.KeyStore.CosmosKeyStore.ChainID = covenantConfig.BabylonConfig.ChainID + signerConfig.KeyStore.CosmosKeyStore.Passphrase = passphrase + signerConfig.KeyStore.CosmosKeyStore.KeyName = covenantConfig.BabylonConfig.Key + signerConfig.KeyStore.CosmosKeyStore.KeyringBackend = covenantConfig.BabylonConfig.KeyringBackend + signerConfig.KeyStore.CosmosKeyStore.KeyDirectory = covenantConfig.BabylonConfig.KeyDirectory + keyRetriever, err := cosmos.NewCosmosKeyringRetriever(signerConfig.KeyStore.CosmosKeyStore) + require.NoError(t, err) + keyInfo, err := keyRetriever.Kr.CreateChainKey( + passphrase, + hdPath, + ) + require.NoError(t, err) + require.NotNil(t, keyInfo) + + app := signerApp.NewSignerApp( + keyRetriever, + ) + + met := signerMetrics.NewCovenantSignerMetrics() + parsedConfig, err := signerConfig.Parse() + require.NoError(t, err) + + server, err := signerService.New( + context.Background(), + parsedConfig, + app, + met, + ) + + require.NoError(t, err) + + signer = remotesigner.NewRemoteSigner(covenantConfig.RemoteSigner) + covPubKey = keyInfo.PublicKey + + go func() { + _ = server.Start() + }() + + // Give some time to launch server + time.Sleep(3 * time.Second) + + t.Cleanup(func() { + _ = server.Stop(context.TODO()) + }) + } else { + covKeyPair, err := covdkeyring.CreateCovenantKey(testDir, chainID, covenantKeyName, keyring.BackendTest, passphrase, hdPath) + require.NoError(t, err) + signer, err = covdkeyring.NewKeyringSigner( + covenantConfig.BabylonConfig.ChainID, + covenantConfig.BabylonConfig.Key, + covenantConfig.BabylonConfig.KeyDirectory, + covenantConfig.BabylonConfig.KeyringBackend, + passphrase, + ) + require.NoError(t, err) + covPubKey = covKeyPair.PublicKey + } // 2. prepare Babylon node - bh := NewBabylonNodeHandler(t, bbntypes.NewBIP340PubKeyFromBTCPK(covKeyPair.PublicKey)) + bh := NewBabylonNodeHandler(t, bbntypes.NewBIP340PubKeyFromBTCPK(covPubKey)) err = bh.Start() require.NoError(t, err) @@ -94,7 +160,6 @@ func StartManager(t *testing.T) *TestManager { covbc, err := covcc.NewBabylonController(bbnCfg, &covenantConfig.BTCNetParams, logger) require.NoError(t, err) - signer, err := covdkeyring.NewKeyringSigner(covenantConfig.BabylonConfig.ChainID, covenantConfig.BabylonConfig.Key, covenantConfig.BabylonConfig.KeyDirectory, covenantConfig.BabylonConfig.KeyringBackend, passphrase) require.NoError(t, err) ce, err := covenant.NewCovenantEmulator(covenantConfig, covbc, logger, signer) @@ -129,8 +194,8 @@ func (tm *TestManager) WaitForServicesStart(t *testing.T) { t.Logf("Babylon node is started") } -func StartManagerWithFinalityProvider(t *testing.T, n int) (*TestManager, []*btcec.PublicKey) { - tm := StartManager(t) +func StartManagerWithFinalityProvider(t *testing.T, n int, useRemoteSigner bool) (*TestManager, []*btcec.PublicKey) { + tm := StartManager(t, useRemoteSigner) var btcPks []*btcec.PublicKey for i := 0; i < n; i++ {