Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: query fp before register #226

Merged
merged 8 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
* [#216](https://github.com/babylonlabs-io/finality-provider/pull/216) Add multiple fpd connecting to one eotsd in e2e tests
* [#218](https://github.com/babylonlabs-io/finality-provider/pull/218) Prune used merkle proof
* [#221](https://github.com/babylonlabs-io/finality-provider/pull/221) Cleanup TODOs
* [#226](https://github.com/babylonlabs-io/finality-provider/pull/226) Update local fp before register

## v0.13.1

Expand Down
3 changes: 3 additions & 0 deletions clientcontroller/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type ClientController interface {
The following methods are queries to the consumer chain
*/

// QueryFinalityProvider queries the finality provider by pk
QueryFinalityProvider(fpPk *btcec.PublicKey) (*btcstakingtypes.QueryFinalityProviderResponse, error)

// QueryFinalityProviderVotingPower queries the voting power of the finality provider at a given height
QueryFinalityProviderVotingPower(fpPk *btcec.PublicKey, blockHeight uint64) (uint64, error)

Expand Down
57 changes: 56 additions & 1 deletion finality-provider/service/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,30 @@ func (app *FinalityProviderApp) CreateFinalityProvider(
return nil, fmt.Errorf("failed to create proof-of-possession of the finality-provider: %w", err)
}

// TODO: query consumer chain to check if the fp is already registered
// Query the consumer chain to check if the fp is already registered
// if true, update db with the fp info from the consumer chain
// otherwise, proceed registration
resp, err := app.cc.QueryFinalityProvider(eotsPk.MustToBTCPK())
if err != nil {
if !strings.Contains(err.Error(), "the finality provider is not found") {
return nil, fmt.Errorf("err getting finality provider: %w", err)
}
}
if resp != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's have a log here to indicate the registration is already done

if err := app.updateFpFromResponse(eotsPk.MustToBTCPK(), resp.FinalityProvider); err != nil {
return nil, err
}

// get updated fp from db
storedFp, err := app.fps.GetFinalityProvider(eotsPk.MustToBTCPK())
if err != nil {
return nil, err
}

return &CreateFinalityProviderResult{
FpInfo: storedFp.ToFinalityProviderInfo(),
}, nil
}

// 3. register the finality provider on the consumer chain
request := &CreateFinalityProviderRequest{
Expand Down Expand Up @@ -558,3 +579,37 @@ func (app *FinalityProviderApp) getFpPrivKey(fpPk []byte) (*btcec.PrivateKey, er

return record.PrivKey, nil
}

func (app *FinalityProviderApp) updateFpFromResponse(btcPk *btcec.PublicKey, fp *bstypes.FinalityProviderResponse) error {
if err := app.fps.SetFpDescription(btcPk, fp.Description, fp.Commission); err != nil {
return err
}

if err := app.fps.SetFpLastVotedHeight(btcPk, uint64(fp.HighestVotedHeight)); err != nil {
return err
}

power, err := app.cc.QueryFinalityProviderVotingPower(btcPk, fp.Height)
if err != nil {
return fmt.Errorf("failed to query voting power for finality provider %v: %w",
btcPk, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's print the hex string of the public key

}

var status proto.FinalityProviderStatus
switch {
case power > 0:
status = proto.FinalityProviderStatus_ACTIVE
case fp.SlashedBtcHeight > 0:
status = proto.FinalityProviderStatus_SLASHED
case fp.Jailed:
status = proto.FinalityProviderStatus_JAILED
default:
status = proto.FinalityProviderStatus_INACTIVE
}

if err := app.fps.SetFpStatus(btcPk, status); err != nil {
return fmt.Errorf("failed to update status for finality provider %v: %w", btcPk, err)
}

return nil
}
1 change: 1 addition & 0 deletions finality-provider/service/app_test.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a test case where the fp is already registered but not in db, so calling CreateFinalityProvider will save the fp in db?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func FuzzCreateFinalityProvider(f *testing.F) {
mockClientController.EXPECT().QueryLatestFinalizedBlocks(gomock.Any()).Return(nil, nil).AnyTimes()
mockClientController.EXPECT().QueryFinalityProviderVotingPower(gomock.Any(),
gomock.Any()).Return(uint64(0), nil).AnyTimes()
mockClientController.EXPECT().QueryFinalityProvider(gomock.Any()).Return(nil, nil).AnyTimes()

// Create randomized config
fpHomeDir := filepath.Join(t.TempDir(), "fp-home")
Expand Down
15 changes: 15 additions & 0 deletions testutil/mocks/babylon.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading