From c4b5c68f390d475b290c5347fa56c782c48b5b40 Mon Sep 17 00:00:00 2001 From: Runchao Han Date: Fri, 9 Aug 2024 14:22:45 +1000 Subject: [PATCH] debug --- clientcontroller/api/interface.go | 7 +++++-- finality-provider/service/app.go | 14 ++++++-------- finality-provider/service/fp_manager_test.go | 5 ++--- keyring/keyringcontroller.go | 16 ++++------------ keyring/keyringcontroller_test.go | 5 ++--- testutil/mocks/clientcontroller.go | 15 +++++++++++++++ types/chainkey.go | 2 -- 7 files changed, 34 insertions(+), 30 deletions(-) diff --git a/clientcontroller/api/interface.go b/clientcontroller/api/interface.go index bf0919f4..a8165959 100644 --- a/clientcontroller/api/interface.go +++ b/clientcontroller/api/interface.go @@ -2,10 +2,10 @@ package api import ( "cosmossdk.io/math" + "github.com/babylonlabs-io/finality-provider/types" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcec/v2/schnorr" - - "github.com/babylonlabs-io/finality-provider/types" + sdk "github.com/cosmos/cosmos-sdk/types" ) const ( @@ -13,6 +13,9 @@ const ( ) type ClientController interface { + // GetKeyAddress returns the address of the Babylon key in the keyring + GetKeyAddress() sdk.AccAddress + // RegisterFinalityProvider registers a finality provider to the consumer chain // it returns tx hash and error. The address of the finality provider will be // the signer of the msg. diff --git a/finality-provider/service/app.go b/finality-provider/service/app.go index e0bcf68f..908a0049 100644 --- a/finality-provider/service/app.go +++ b/finality-provider/service/app.go @@ -386,14 +386,12 @@ func (app *FinalityProviderApp) handleCreateFinalityProviderRequest(req *createF return nil, err } - fpAddr, err := kr.Address(req.passPhrase) - if err != nil { + if !kr.HasKey(req.passPhrase) { // the chain key does not exist, should create the chain key first - keyInfo, err := kr.CreateChainKey(req.passPhrase, req.hdPath, "") + _, err := kr.CreateChainKey(req.passPhrase, req.hdPath, "") if err != nil { return nil, fmt.Errorf("failed to create chain key %s: %w", req.keyName, err) } - fpAddr = keyInfo.AccAddress } // 2. create EOTS key @@ -410,6 +408,9 @@ func (app *FinalityProviderApp) handleCreateFinalityProviderRequest(req *createF return nil, fmt.Errorf("failed to get finality-provider record: %w", err) } + // get the Babylon address used by the finality provider + fpAddr := app.cc.GetKeyAddress() + // 3. create proof-of-possession pop, err := kr.CreatePop(fpAddr, fpRecord.PrivKey) if err != nil { @@ -489,10 +490,7 @@ func (app *FinalityProviderApp) StoreFinalityProvider( if err != nil { return nil, err } - fpAddr, err := kr.Address(passPhrase) - if err != nil { - return nil, err - } + fpAddr := app.cc.GetKeyAddress() // 2. create EOTS key fpPkBytes, err := app.eotsManager.CreateKey(keyName, passPhrase, hdPath) diff --git a/finality-provider/service/fp_manager_test.go b/finality-provider/service/fp_manager_test.go index bfa48709..3bc3081a 100644 --- a/finality-provider/service/fp_manager_test.go +++ b/finality-provider/service/fp_manager_test.go @@ -132,9 +132,8 @@ func newFinalityProviderManagerWithRegisteredFp(t *testing.T, r *rand.Rand, cc c require.NoError(t, err) btcPk, err := bbntypes.NewBIP340PubKey(btcPkBytes) require.NoError(t, err) - keyInfo, err := kc.CreateChainKey(passphrase, hdPath, "") - require.NoError(t, err) - fpAddr := keyInfo.AccAddress + + fpAddr := datagen.GenRandomAccount().GetAddress() fpRecord, err := em.KeyRecord(btcPk.MustMarshal(), passphrase) require.NoError(t, err) pop, err := kc.CreatePop(fpAddr, fpRecord.PrivKey) diff --git a/keyring/keyringcontroller.go b/keyring/keyringcontroller.go index fd0bf84a..4d656de9 100644 --- a/keyring/keyringcontroller.go +++ b/keyring/keyringcontroller.go @@ -99,17 +99,12 @@ func (kc *ChainKeyringController) CreateChainKey(passphrase, hdPath, mnemonic st } privKey := record.GetLocal().PrivKey.GetCachedValue() - accAddress, err := record.GetAddress() - if err != nil { - return nil, err - } switch v := privKey.(type) { case *sdksecp256k1.PrivKey: sk, pk := btcec.PrivKeyFromBytes(v.Key) return &types.ChainKeyInfo{ Name: kc.fpName, - AccAddress: accAddress, PublicKey: pk, PrivateKey: sk, Mnemonic: mnemonic, @@ -126,15 +121,12 @@ func (kc *ChainKeyringController) CreatePop(fpAddr sdk.AccAddress, btcPrivKey *b return bstypes.NewPoPBTC(fpAddr, btcPrivKey) } -// Address returns the address from the keyring -func (kc *ChainKeyringController) Address(passphrase string) (sdk.AccAddress, error) { +// HasKey returns whether the key with the given passphrase exists in the keyring +func (kc *ChainKeyringController) HasKey(passphrase string) bool { kc.input.Reset(passphrase) - k, err := kc.kr.Key(kc.fpName) - if err != nil { - return nil, fmt.Errorf("failed to get address: %w", err) - } + _, err := kc.kr.Key(kc.fpName) - return k.GetAddress() + return err == nil } func (kc *ChainKeyringController) GetChainPrivKey(passphrase string) (*sdksecp256k1.PrivKey, error) { diff --git a/keyring/keyringcontroller_test.go b/keyring/keyringcontroller_test.go index 19c2315b..b38b7254 100644 --- a/keyring/keyringcontroller_test.go +++ b/keyring/keyringcontroller_test.go @@ -8,6 +8,7 @@ import ( "go.uber.org/zap" + "github.com/babylonlabs-io/babylon/testutil/datagen" "github.com/babylonlabs-io/babylon/types" "github.com/btcsuite/btcd/chaincfg" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -54,10 +55,8 @@ func FuzzCreatePoP(f *testing.F) { require.NoError(t, err) btcPk, err := types.NewBIP340PubKey(btcPkBytes) require.NoError(t, err) - keyInfo, err := kc.CreateChainKey(passphrase, hdPath, "") - require.NoError(t, err) - fpAddr := keyInfo.AccAddress + fpAddr := datagen.GenRandomAccount().GetAddress() fpRecord, err := em.KeyRecord(btcPk.MustMarshal(), passphrase) require.NoError(t, err) pop, err := kc.CreatePop(fpAddr, fpRecord.PrivKey) diff --git a/testutil/mocks/clientcontroller.go b/testutil/mocks/clientcontroller.go index 7cc7474c..aeff4abe 100644 --- a/testutil/mocks/clientcontroller.go +++ b/testutil/mocks/clientcontroller.go @@ -11,6 +11,7 @@ import ( types "github.com/babylonlabs-io/finality-provider/types" btcec "github.com/btcsuite/btcd/btcec/v2" schnorr "github.com/btcsuite/btcd/btcec/v2/schnorr" + types0 "github.com/cosmos/cosmos-sdk/types" gomock "github.com/golang/mock/gomock" ) @@ -51,6 +52,20 @@ func (mr *MockClientControllerMockRecorder) Close() *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockClientController)(nil).Close)) } +// GetKeyAddress mocks base method. +func (m *MockClientController) GetKeyAddress() types0.AccAddress { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetKeyAddress") + ret0, _ := ret[0].(types0.AccAddress) + return ret0 +} + +// GetKeyAddress indicates an expected call of GetKeyAddress. +func (mr *MockClientControllerMockRecorder) GetKeyAddress() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetKeyAddress", reflect.TypeOf((*MockClientController)(nil).GetKeyAddress)) +} + // QueryFinalityProviderSlashed mocks base method. func (m *MockClientController) QueryFinalityProviderSlashed(fpPk *btcec.PublicKey) (bool, error) { m.ctrl.T.Helper() diff --git a/types/chainkey.go b/types/chainkey.go index 6d5df870..4e56c55f 100644 --- a/types/chainkey.go +++ b/types/chainkey.go @@ -2,13 +2,11 @@ package types import ( "github.com/btcsuite/btcd/btcec/v2" - sdk "github.com/cosmos/cosmos-sdk/types" ) type ChainKeyInfo struct { Name string Mnemonic string - AccAddress sdk.AccAddress PublicKey *btcec.PublicKey PrivateKey *btcec.PrivateKey }