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

fix last pub rand query #36

Merged
merged 8 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions clientcontroller/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import (
"github.com/babylonlabs-io/finality-provider/types"
)

const (
babylonConsumerChainName = "babylon"
)

type ClientController interface {
// RegisterFinalityProvider registers a finality provider to the consumer chain
// it returns tx hash and error. The address of the finality provider will be
Expand Down
35 changes: 8 additions & 27 deletions clientcontroller/cosmwasm/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"sort"
"strings"

sdkErr "cosmossdk.io/errors"
wasmdparams "github.com/CosmWasm/wasmd/app/params"
Expand Down Expand Up @@ -281,11 +282,9 @@ func (wc *CosmwasmConsumerController) QueryLastPublicRandCommit(fpPk *btcec.Publ
fpBtcPk := bbntypes.NewBIP340PubKeyFromBTCPK(fpPk)

// Construct the query message
count := uint64(1)
queryMsgStruct := QueryMsgLastPubRandCommit{
LastPubRandCommit: LastPubRandCommitQuery{
BtcPkHex: fpBtcPk.MarshalHex(),
Limit: &count,
},
}

Expand All @@ -300,40 +299,22 @@ func (wc *CosmwasmConsumerController) QueryLastPublicRandCommit(fpPk *btcec.Publ
return nil, fmt.Errorf("failed to query smart contract state: %w", err)
}

// Define a response struct
var commits []PubRandCommitResponse
err = json.Unmarshal(dataFromContract.Data, &commits)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

if len(commits) == 0 {
if dataFromContract == nil || dataFromContract.Data == nil || len(dataFromContract.Data.Bytes()) == 0 || strings.Contains(string(dataFromContract.Data), "null") {
// expected when there is no PR commit at all
// `get_pub_rand_commit`'s return type is Vec<PubRandCommit> and it can be
// empty vector if no results found
return nil, nil
}

if len(commits) > 1 {
return nil, fmt.Errorf("expected length to be 1, but got :%d", len(commits))
}

// Convert the response to the expected map format
var commit *fptypes.PubRandCommit = nil
for _, commitRes := range commits {
commitCopy := commitRes // create a copy to avoid referencing the loop variable
commit = &fptypes.PubRandCommit{
StartHeight: commitCopy.StartHeight,
NumPubRand: commitCopy.NumPubRand,
Commitment: commitCopy.Commitment,
}
// Define a response struct
var commit fptypes.PubRandCommit
err = json.Unmarshal(dataFromContract.Data.Bytes(), &commit)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}

if err := commit.Validate(); err != nil {
return nil, err
}

return commit, nil
return &commit, nil
}

func (wc *CosmwasmConsumerController) QueryIsBlockFinalized(height uint64) (bool, error) {
Expand Down
20 changes: 5 additions & 15 deletions clientcontroller/cosmwasm/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ type IndexedBlock struct {
type NewFinalityProvider struct {
Description *FinalityProviderDescription `json:"description,omitempty"`
Commission string `json:"commission"`
BabylonPK *PubKey `json:"babylon_pk,omitempty"`
Addr string `json:"addr"`
BTCPKHex string `json:"btc_pk_hex"`
Pop *ProofOfPossession `json:"pop,omitempty"`
Pop *ProofOfPossessionBtc `json:"pop,omitempty"`
ConsumerID string `json:"consumer_id"`
}

Expand All @@ -78,13 +78,8 @@ type FinalityProviderDescription struct {
Details string `json:"details"`
}

type PubKey struct {
Key []byte `json:"key"`
}

type ProofOfPossession struct {
type ProofOfPossessionBtc struct {
BTCSigType int32 `json:"btc_sig_type"`
BabylonSig []byte `json:"babylon_sig"`
BTCSig []byte `json:"btc_sig"`
}

Expand All @@ -108,6 +103,7 @@ type BtcUndelegationInfo struct {
}

type ActiveBtcDelegation struct {
StakerAddr string `json:"staker_addr"`
BTCPkHex string `json:"btc_pk_hex"`
FpBtcPkList []string `json:"fp_btc_pk_list"`
StartHeight uint64 `json:"start_height"`
Expand Down Expand Up @@ -219,11 +215,5 @@ type QueryMsgLastPubRandCommit struct {
}

type LastPubRandCommitQuery struct {
BtcPkHex string `json:"btc_pk_hex"`
Limit *uint64 `json:"limit,omitempty"`
}
type PubRandCommitResponse struct {
StartHeight uint64 `json:"start_height"`
NumPubRand uint64 `json:"num_pub_rand"`
Commitment []byte `json:"commitment"`
BtcPkHex string `json:"btc_pk_hex"`
}
2 changes: 1 addition & 1 deletion finality-provider/service/fp_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ func (fp *FinalityProviderInstance) lastCommittedPublicRandWithRetry() (*types.P
return nil
}, RtyAtt, RtyDel, RtyErr, retry.OnRetry(func(n uint, err error) {
fp.logger.Debug(
"failed to query babylon for the last committed public randomness",
"failed to query the last committed public randomness",
zap.Uint("attempt", n+1),
zap.Uint("max_attempts", RtyAttNum),
zap.Error(err),
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ require (
github.com/CosmWasm/wasmd v0.52.0
github.com/avast/retry-go/v4 v4.5.1
github.com/babylonlabs-io/babylon v0.9.0
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240802071655-9fd2ddd5158b
github.com/babylonlabs-io/finality-gadget v0.1.0
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240813062800-00e11abf5338
github.com/babylonlabs-io/finality-gadget v0.1.1-0.20240813005904-2fa76c30cb74
github.com/btcsuite/btcd v0.24.2
github.com/btcsuite/btcd/btcec/v2 v2.3.2
github.com/btcsuite/btcd/btcutil v1.1.5
Expand Down Expand Up @@ -46,7 +46,7 @@ require (

require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240802071655-9fd2ddd5158b // indirect
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240813062800-00e11abf5338 // indirect
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd // indirect
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 // indirect
github.com/cosmos/ibc-go/modules/apps/callbacks v0.2.1-0.20231113120333-342c00b0f8bd // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,10 @@ github.com/babylonlabs-io/babylon-finality-gadget v0.1.1-0.20240813005904-2fa76c
github.com/babylonlabs-io/babylon-finality-gadget v0.1.1-0.20240813005904-2fa76c30cb74/go.mod h1:+BICJA7mUJpfYOSYds1G5w6nLO0gKVe9XwdmpIUiXmk=
github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2 h1:sFsAJkrYe1eIkKzWTn4sj7G6QrPQ3IK33/UlOVgeA5M=
github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2/go.mod h1:pyFZgF85gUhfiCJKbYjnxo5+9prDkti48tW4FDEu9js=
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240802071655-9fd2ddd5158b h1:RctaFr4Svn/gbTa6FkPi78Jkm81J/NFFtp2V2ogMyl0=
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240802071655-9fd2ddd5158b/go.mod h1:8t6U0E4bCQlRQQSKIXNTpTOjACVihJd5wdMLxL55jFE=
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240802071655-9fd2ddd5158b h1:DeP6CzLozsd1MFSOf/Xo3FoBqROW6YZ8fq5ayR3KPwM=
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240802071655-9fd2ddd5158b/go.mod h1:fCwb573m5JgCpuAZ+MK3Kx4WIV+q60TaPRu/m8fCcr0=
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240813062800-00e11abf5338 h1:a45+o47CqEksgeyX1IBbWoxPojIVLOIARxH9uBsHe70=
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240813062800-00e11abf5338/go.mod h1:QqEn1sL4RPG7DJ94XFYvuvEELml64s5XwPQpTayXJss=
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240813062800-00e11abf5338 h1:48YwlQpQELyuVb7D/r2NMh7Op8K9WYMcnKUpeH3a+5c=
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240813062800-00e11abf5338/go.mod h1:WJlZy0RYCtyBFeO1mr0Tlo02csrlCAQgzmp4+NVX14g=
github.com/babylonlabs-io/optimism v1.8.1-0.20240808190817-3279057d5250 h1:3FNsa55pbil/Ff7NICtaZzcdLwRs9HsCXJNmW7hDzPs=
github.com/babylonlabs-io/optimism v1.8.1-0.20240808190817-3279057d5250/go.mod h1:ZZVrpyP7weyHNHnN2Wr6A4lnMpqcJHyL5Ke7LQ0zzHs=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
Binary file modified itest/bytecode/babylon_contract.wasm
Binary file not shown.
Binary file modified itest/bytecode/btc_staking.wasm
Binary file not shown.
10 changes: 4 additions & 6 deletions itest/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,10 @@ func genRandomFinalityProvider() cosmwasm.NewFinalityProvider {
Details: "details",
},
Commission: "0.05",
BabylonPK: &cosmwasm.PubKey{
Key: []byte("mock_pub_rand"),
},
BTCPKHex: "1",
Pop: &cosmwasm.ProofOfPossession{
Addr: datagen.GenRandomAccount().Address,
BTCPKHex: "1",
Pop: &cosmwasm.ProofOfPossessionBtc{
BTCSigType: 0,
BabylonSig: []byte("mock_babylon_sig"),
BTCSig: []byte("mock_btc_sig"),
},
ConsumerID: "osmosis-1",
Expand Down Expand Up @@ -236,6 +233,7 @@ func convertBTCDelegationToActiveBtcDelegation(mockDel *bstypes.BTCDelegation) c
}

return cosmwasm.ActiveBtcDelegation{
StakerAddr: mockDel.StakerAddr,
BTCPkHex: mockDel.BtcPk.MarshalHex(),
FpBtcPkList: fpBtcPkList,
StartHeight: mockDel.StartHeight,
Expand Down
4 changes: 2 additions & 2 deletions tools/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.21.4
require (
github.com/CosmWasm/wasmd v0.52.0
github.com/babylonlabs-io/babylon v0.9.0
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240802071655-9fd2ddd5158b
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240813062800-00e11abf5338
)

require (
Expand Down Expand Up @@ -39,7 +39,7 @@ require (
github.com/DataDog/zstd v1.5.5 // indirect
github.com/aead/siphash v1.0.1 // indirect
github.com/aws/aws-sdk-go v1.44.312 // indirect
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240802071655-9fd2ddd5158b // indirect
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240813062800-00e11abf5338 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/bgentry/speakeasy v0.1.1-0.20220910012023-760eaf8b6816 // indirect
Expand Down
8 changes: 4 additions & 4 deletions tools/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ github.com/aws/aws-sdk-go v1.44.312/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2 h1:sFsAJkrYe1eIkKzWTn4sj7G6QrPQ3IK33/UlOVgeA5M=
github.com/babylonlabs-io/babylon-private v0.9.0-rc.3.0.20240801001431-74a24c962ce2/go.mod h1:pyFZgF85gUhfiCJKbYjnxo5+9prDkti48tW4FDEu9js=
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240802071655-9fd2ddd5158b h1:RctaFr4Svn/gbTa6FkPi78Jkm81J/NFFtp2V2ogMyl0=
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240802071655-9fd2ddd5158b/go.mod h1:8t6U0E4bCQlRQQSKIXNTpTOjACVihJd5wdMLxL55jFE=
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240802071655-9fd2ddd5158b h1:DeP6CzLozsd1MFSOf/Xo3FoBqROW6YZ8fq5ayR3KPwM=
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240802071655-9fd2ddd5158b/go.mod h1:fCwb573m5JgCpuAZ+MK3Kx4WIV+q60TaPRu/m8fCcr0=
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240813062800-00e11abf5338 h1:a45+o47CqEksgeyX1IBbWoxPojIVLOIARxH9uBsHe70=
github.com/babylonlabs-io/babylon-sdk/demo v0.0.0-20240813062800-00e11abf5338/go.mod h1:QqEn1sL4RPG7DJ94XFYvuvEELml64s5XwPQpTayXJss=
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240813062800-00e11abf5338 h1:48YwlQpQELyuVb7D/r2NMh7Op8K9WYMcnKUpeH3a+5c=
github.com/babylonlabs-io/babylon-sdk/x v0.0.0-20240813062800-00e11abf5338/go.mod h1:WJlZy0RYCtyBFeO1mr0Tlo02csrlCAQgzmp4+NVX14g=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
Expand Down
Loading