Skip to content

Commit

Permalink
Merge pull request #180 from babylonchain/release/v0.3.0
Browse files Browse the repository at this point in the history
Release/v0.3.0
  • Loading branch information
KonradStaniec authored Jul 3, 2024
2 parents 515865b + 55abcea commit 9250416
Show file tree
Hide file tree
Showing 31 changed files with 1,410 additions and 1,016 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
resource_class: large
steps:
- go/install:
version: "1.21.4"
version: "1.22.3"
- checkout
- run:
name: Print Go environment
Expand Down
44 changes: 18 additions & 26 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,43 +1,35 @@
FROM golang:1.21-alpine as builder
FROM golang:1.22.3 as builder

# Version to build. Default is the Git HEAD.
ARG VERSION="HEAD"

# Use muslc for static libs
ARG BUILD_TAGS="muslc"


RUN apk add --no-cache --update openssh git make build-base linux-headers libc-dev \
pkgconfig zeromq-dev musl-dev alpine-sdk libsodium-dev \
libzmq-static libsodium-static gcc
# Install cli tools for building and final image
RUN apt-get update && apt-get install -y make git bash gcc curl jq

# Build
WORKDIR /go/src/github.com/babylonchain/btc-staker
# Cache dependencies
COPY go.mod go.sum /go/src/github.com/babylonchain/btc-staker/
RUN go mod download

# Copy the rest of the files
COPY ./ /go/src/github.com/babylonchain/btc-staker/

# Cosmwasm - Download correct libwasmvm version
RUN WASMVM_VERSION=$(go list -m github.com/CosmWasm/wasmvm | cut -d ' ' -f 2) && \
wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm_muslc.$(uname -m).a \
-O /lib/libwasmvm_muslc.a && \
# verify checksum
wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/checksums.txt -O /tmp/checksums.txt && \
sha256sum /lib/libwasmvm_muslc.a | grep $(cat /tmp/checksums.txt | grep libwasmvm_muslc.$(uname -m) | cut -d ' ' -f 1)

RUN CGO_LDFLAGS="$CGO_LDFLAGS -lstdc++ -lm -lsodium" \
CGO_ENABLED=1 \
BUILD_TAGS=$BUILD_TAGS \
LINK_STATICALLY=true \
RUN BUILD_TAGS=netgo \
LDFLAGS="-w -s" \
make build

# FINAL IMAGE
FROM alpine:3.16 AS run
FROM debian:bookworm-slim AS run

RUN addgroup --gid 1138 -S btcstaker && adduser --uid 1138 -S btcstaker -G btcstaker
RUN addgroup --gid 1138 --system btcstaker && adduser --uid 1138 --system --home /home/btcstaker btcstaker
RUN apt-get update && apt-get install -y bash curl jq wget

RUN apk add bash curl jq
COPY --from=builder /go/src/github.com/babylonchain/btc-staker/go.mod /tmp
RUN WASMVM_VERSION=$(grep github.com/CosmWasm/wasmvm /tmp/go.mod | cut -d' ' -f2) && \
wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/libwasmvm.$(uname -m).so \
-O /lib/libwasmvm.$(uname -m).so && \
# verify checksum
wget https://github.com/CosmWasm/wasmvm/releases/download/$WASMVM_VERSION/checksums.txt -O /tmp/checksums.txt && \
sha256sum /lib/libwasmvm.$(uname -m).so | grep $(cat /tmp/checksums.txt | grep libwasmvm.$(uname -m) | cut -d ' ' -f 1)
RUN rm -f /tmp/go.mod

COPY --from=builder /go/src/github.com/babylonchain/btc-staker/build/stakerd /bin/stakerd
COPY --from=builder /go/src/github.com/babylonchain/btc-staker/build/stakercli /bin/stakercli
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ git clone https://github.com/babylonchain/btc-staker.git
```
You can choose a specific version from
the [official releases page](https://github.com/babylonchain/btcstaker/releases)
the [official releases page](https://github.com/babylonchain/btc-staker/releases)
```bash
cd btc-staker # cd into the project directory
Expand Down Expand Up @@ -352,7 +352,7 @@ In the following, we go through important parameters of the `stakerd.conf` file.
1. The `Key` parameter in the config below is the name of the key in the keyring to
use for signing transactions. Use the key name you created
in [Create a Babylon keyring with funds](#create-a-babylon-keyring-keyring-backend-test-with-funds)
2. Ensure that the `KeyringDirectory` is set to the location where the keyring is
2. Ensure that the `KeyDirectory` is set to the location where the keyring is
stored.
3. Ensure to use the `test` keyring backend
4. Ensure you use the correct `ChainID` for the network you're connecting to. For
Expand Down
82 changes: 38 additions & 44 deletions babylonclient/babyloncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
// TODO: Maybe configurable?
RtyAttNum = uint(5)
RtyAtt = retry.Attempts(RtyAttNum)
RtyDel = retry.Delay(time.Millisecond * 400)
RtyDel = retry.Delay(time.Millisecond * 600)
RtyErr = retry.LastErrorOnly(true)
)

Expand Down Expand Up @@ -110,8 +110,8 @@ type StakingTrackerResponse struct {
}

type FinalityProviderInfo struct {
BabylonPk secp256k1.PubKey
BtcPk btcec.PublicKey
BabylonAddr sdk.AccAddress
BtcPk btcec.PublicKey
}

type FinalityProvidersClientResponse struct {
Expand Down Expand Up @@ -273,7 +273,7 @@ type DelegationData struct {
FinalityProvidersBtcPks []*btcec.PublicKey
SlashingTransaction *wire.MsgTx
SlashingTransactionSig *schnorr.Signature
BabylonPk *secp256k1.PubKey
BabylonStakerAddr sdk.AccAddress
StakerBtcPk *btcec.PublicKey
BabylonPop *stakerdb.ProofOfPossession
Ud *UndelegationData
Expand Down Expand Up @@ -308,7 +308,7 @@ type DelegationInfo struct {
UndelegationInfo *UndelegationInfo
}

func delegationDataToMsg(signer string, dg *DelegationData) (*btcstypes.MsgCreateBTCDelegation, error) {
func delegationDataToMsg(dg *DelegationData) (*btcstypes.MsgCreateBTCDelegation, error) {
if dg == nil {
return nil, fmt.Errorf("nil delegation data")
}
Expand Down Expand Up @@ -365,13 +365,11 @@ func delegationDataToMsg(signer string, dg *DelegationData) (*btcstypes.MsgCreat
slashUnbondingTxSig := bbntypes.NewBIP340SignatureFromBTCSig(dg.Ud.SlashUnbondingTransactionSig)

return &btcstypes.MsgCreateBTCDelegation{
Signer: signer,
BabylonPk: dg.BabylonPk,
Pop: &btcstypes.ProofOfPossession{
// Note: this should be always safe conversion as we received data from our db
// Note: this should be always safe conversion as we received data from our db
StakerAddr: dg.BabylonStakerAddr.String(),
Pop: &btcstypes.ProofOfPossessionBTC{
BtcSigType: btcstypes.BTCSigType(dg.BabylonPop.BtcSigType),
BabylonSig: dg.BabylonPop.BabylonSigOverBtcPk,
BtcSig: dg.BabylonPop.BtcSigOverBabylonSig,
BtcSig: dg.BabylonPop.BtcSigOverBabylonAddr,
},
BtcPk: bbntypes.NewBIP340PubKeyFromBTCPK(dg.StakerBtcPk),
FpBtcPkList: fpPksList,
Expand Down Expand Up @@ -408,10 +406,7 @@ func (bc *BabylonController) reliablySendMsgs(
// TODO: for now return sdk.TxResponse, it will ease up debugging/testing
// ultimately we should create our own type ate
func (bc *BabylonController) Delegate(dg *DelegationData) (*pv.RelayerTxResponse, error) {
signer := bc.getTxSigner()

delegateMsg, err := delegationDataToMsg(signer, dg)

delegateMsg, err := delegationDataToMsg(dg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -540,11 +535,15 @@ func (bc *BabylonController) QueryFinalityProviders(
if err != nil {
return nil, fmt.Errorf("query finality providers error: %w", err)
}
fpBabylonPk := finalityProvider.BabylonPk

fpAddr, err := sdk.AccAddressFromBech32(finalityProvider.Addr)
if err != nil {
return nil, fmt.Errorf("query finality providers error transform address: %s - %w", finalityProvider.Addr, err)
}

fpInfo := FinalityProviderInfo{
BabylonPk: *fpBabylonPk,
BtcPk: *fpBtcKey,
BabylonAddr: fpAddr,
BtcPk: *fpBtcKey,
}

finalityProviders = append(finalityProviders, fpInfo)
Expand Down Expand Up @@ -603,15 +602,19 @@ func (bc *BabylonController) QueryFinalityProvider(btcPubKey *btcec.PublicKey) (
}

btcPk, err := response.FinalityProvider.BtcPk.ToBTCPK()

if err != nil {
return nil, fmt.Errorf("received malformed btc pk in babylon response: %w", err)
}

fpAddr, err := sdk.AccAddressFromBech32(response.FinalityProvider.Addr)
if err != nil {
return nil, fmt.Errorf("received malformed fp addr in babylon response: %s - %w", response.FinalityProvider.Addr, err)
}

return &FinalityProviderClientResponse{
FinalityProvider: FinalityProviderInfo{
BabylonPk: *response.FinalityProvider.BabylonPk,
BtcPk: *btcPk,
BabylonAddr: fpAddr,
BtcPk: *btcPk,
},
}, nil
}
Expand Down Expand Up @@ -673,24 +676,26 @@ func chainToChainBytes(chain []*wire.BlockHeader) []bbntypes.BTCHeaderBytes {
// RegisterFinalityProvider registers a BTC finality provider via a MsgCreateFinalityProvider to Babylon
// it returns tx hash and error
func (bc *BabylonController) RegisterFinalityProvider(
bbnPubKey *secp256k1.PubKey,
fpAddr sdk.AccAddress,
fpPrivKeyBBN *secp256k1.PrivKey,
btcPubKey *bbntypes.BIP340PubKey,
commission *sdkmath.LegacyDec,
description *sttypes.Description,
pop *btcstypes.ProofOfPossession,
masterRandomness string,
) (*pv.RelayerTxResponse, error) {
pop *btcstypes.ProofOfPossessionBTC,
) error {
registerMsg := &btcstypes.MsgCreateFinalityProvider{
Signer: bc.getTxSigner(),
Commission: commission,
BabylonPk: bbnPubKey,
BtcPk: btcPubKey,
Description: description,
Pop: pop,
MasterPubRand: masterRandomness,
Addr: fpAddr.String(),
Commission: commission,
BtcPk: btcPubKey,
Description: description,
Pop: pop,
}

return bc.reliablySendMsgs([]sdk.Msg{registerMsg})
fpPrivKeyBBN.PubKey()
relayerMsgs := bbnclient.ToProviderMsgs([]sdk.Msg{registerMsg})

_, err := bc.bbnClient.SendMessageWithSigner(context.Background(), fpAddr, fpPrivKeyBBN, relayerMsgs)
return err
}

func (bc *BabylonController) QueryDelegationInfo(stakingTxHash *chainhash.Hash) (*DelegationInfo, error) {
Expand Down Expand Up @@ -859,14 +864,3 @@ func (bc *BabylonController) QueryBtcLightClientTip() (*btclctypes.BTCHeaderInfo

return res.Header, nil
}

func (bc *BabylonController) QueryFinalityProviderRegisteredEpoch(fpPk *btcec.PublicKey) (uint64, error) {
res, err := bc.bbnClient.QueryClient.FinalityProvider(
bbntypes.NewBIP340PubKeyFromBTCPK(fpPk).MarshalHex(),
)
if err != nil {
return 0, fmt.Errorf("failed to query finality provider registered epoch: %w", err)
}

return res.FinalityProvider.RegisteredEpoch, nil
}
11 changes: 4 additions & 7 deletions babylonclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

sdkmath "cosmossdk.io/math"
"github.com/babylonchain/babylon/testutil/datagen"
"github.com/babylonchain/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcutil"
Expand Down Expand Up @@ -98,8 +99,7 @@ func (m *MockBabylonClient) GetPubKey() *secp256k1.PubKey {
}

func (m *MockBabylonClient) Delegate(dg *DelegationData) (*pv.RelayerTxResponse, error) {
msg, err := delegationDataToMsg("signer", dg)

msg, err := delegationDataToMsg(dg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -159,12 +159,9 @@ func GetMockClient() *MockBabylonClient {
panic(err)
}

fpBabylonPrivKey := secp256k1.GenPrivKey()
fpBabylonPubKey := fpBabylonPrivKey.PubKey().(*secp256k1.PubKey)

vi := FinalityProviderInfo{
BabylonPk: *fpBabylonPubKey,
BtcPk: *fpBtcPrivKey.PubKey(),
BabylonAddr: datagen.GenRandomAccount().GetAddress(),
BtcPk: *fpBtcPrivKey.PubKey(),
}

return &MockBabylonClient{
Expand Down
28 changes: 12 additions & 16 deletions babylonclient/pop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
btcstypes "github.com/babylonchain/babylon/x/btcstaking/types"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/chaincfg"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
)

type BabylonBtcPopType int
Expand All @@ -19,20 +19,18 @@ const (
)

type BabylonPop struct {
popType BabylonBtcPopType
BabylonEcdsaSigOverBtcPk []byte
BtcSig []byte
popType BabylonBtcPopType
BtcSig []byte
}

func NewBabylonPop(t BabylonBtcPopType, babylonSig []byte, btcSig []byte) (*BabylonPop, error) {
if len(babylonSig) == 0 || len(btcSig) == 0 {
func NewBabylonPop(t BabylonBtcPopType, btcSigOverBbnAddr []byte) (*BabylonPop, error) {
if len(btcSigOverBbnAddr) == 0 {
return nil, fmt.Errorf("cannot create BabylonPop with empty signatures")
}

return &BabylonPop{
popType: t,
BabylonEcdsaSigOverBtcPk: babylonSig,
BtcSig: btcSig,
popType: t,
BtcSig: btcSigOverBbnAddr,
}, nil
}

Expand Down Expand Up @@ -66,26 +64,25 @@ func (pop *BabylonPop) PopTypeNum() uint32 {
return uint32(pop.popType)
}

func (pop *BabylonPop) ToBtcStakingPop() (*btcstypes.ProofOfPossession, error) {
func (pop *BabylonPop) ToBtcStakingPop() (*btcstypes.ProofOfPossessionBTC, error) {
popType, err := NewBTCSigType(pop.popType)

if err != nil {
return nil, err
}

return &btcstypes.ProofOfPossession{
return &btcstypes.ProofOfPossessionBTC{
BtcSigType: popType,
BabylonSig: pop.BabylonEcdsaSigOverBtcPk,
BtcSig: pop.BtcSig,
}, nil
}

func (pop *BabylonPop) ValidatePop(
babylonPk *secp256k1.PubKey,
bbnAddr sdk.AccAddress,
btcPk *btcec.PublicKey,
net *chaincfg.Params,
) error {
if babylonPk == nil || btcPk == nil || net == nil {
if btcPk == nil || net == nil {
return fmt.Errorf("cannot validate pop with nil parameters")
}

Expand All @@ -96,9 +93,8 @@ func (pop *BabylonPop) ValidatePop(
}

btcPkBabylonFormat := bbn.NewBIP340PubKeyFromBTCPK(btcPk)

return bPop.Verify(
babylonPk,
bbnAddr,
btcPkBabylonFormat,
net,
)
Expand Down
2 changes: 0 additions & 2 deletions cmd/stakercli/helpers/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,4 @@ package helpers
const (
StakingAmountFlag = "staking-amount"
StakingTimeBlocksFlag = "staking-time"
UnbondingFee = "unbonding-fee"
UnbondingTimeFlag = "unbonding-time"
)
Loading

0 comments on commit 9250416

Please sign in to comment.