Skip to content

Commit

Permalink
Merge pull request #34 from babylonlabs-io/try-rebase
Browse files Browse the repository at this point in the history
rebase to latest main
  • Loading branch information
SebastianElvis authored Sep 25, 2024
2 parents 7dd8646 + 59cb769 commit 08a3fb4
Show file tree
Hide file tree
Showing 23 changed files with 3,303 additions and 534 deletions.
1 change: 0 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
push:
branches:
- 'main'
- 'dev'
tags:
- '*'

Expand Down
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Contributing

Btc-staker repository follows the same contributing rules as
[Babylon node](https://github.com/babylonlabs-io/babylon/blob/main/CONTRIBUTING.md)
repository.
8 changes: 2 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ FROM golang:1.22.3 as builder
# Install cli tools for building and final image
RUN apt-get update && apt-get install -y make git bash gcc curl jq

RUN mkdir -p /root/.ssh && ssh-keyscan github.com >> /root/.ssh/known_hosts
RUN git config --global url."[email protected]:".insteadOf "https://github.com/"
ENV GOPRIVATE=github.com/babylonlabs-io/*

# Build
WORKDIR /go/src/github.com/babylonlabs-io/btc-staker
# Cache dependencies
COPY go.mod go.sum /go/src/github.com/babylonlabs-io/btc-staker/
RUN --mount=type=secret,id=sshKey,target=/root/.ssh/id_rsa go mod download
RUN go mod download

# Copy the rest of the files
COPY ./ /go/src/github.com/babylonlabs-io/btc-staker/
Expand Down Expand Up @@ -44,4 +40,4 @@ USER btcstaker

ENTRYPOINT ["/bin/stakerd"]
CMD []
STOPSIGNAL SIGTERM
STOPSIGNAL SIGTERM
78 changes: 74 additions & 4 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,82 @@ License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.

Parameters

Licensor: Babylon Labs, Ltd.
Licensor: Babylon Labs Ltd.

Licensed Work: btc-staker
The Licensed Work is (c) 2023 Babylon Labs, Ltd.

Additional Use Grant: None.
The Licensed Work is (c) 2024 Babylon Labs Ltd.

Additional Use Grant:

In addition to the uses permitted under the non-production license below,
we grant you a limited, non-exclusive, non-transferable, non-sublicensable,
revocable license, limited as set forth below, to use the Licensed Work
in this repository for production commercial uses built on or using and integrated
with the Babylon Protocol, not including Competing Uses.

Any use beyond the scope of this license, including uses that are not built on or
using and integrated with the Babylon Protocol, or are Competing Uses,
will terminate this license to you, automatically and without notice.

This License applies separately and solely to the specific versions of the Licensed Work
in the specified repository on or before the date of this License, and we may require a
different license and different provisions for each subsequent version of the
Licensed Work released by us.

"Babylon Labs", "we", "our", or "us" means Babylon Labs Ltd.

"Babylon Protocol" means the Bitcoin staking protocol as further described in the documentation
here (https://docs.babylonlabs.io/docs/introduction/babylon-overview), as updated from time to time.

"Competing Use" means any use of the Licensed Work in any product, software, protocol, network,
application, or service that is made available to any party and that
(i) substitutes for the use of the Babylon Protocol,
(ii) offers the same or substantially similar functionality as the Babylon Protocol or
(iii) is built on or uses a protocol with substantially similar functionality as the Babylon Protocol
or otherwise facilitates the staking of bitcoin other than by utilizing the Babylon Protocol.

The provisions in this License apply to each individual, entity, group, or association
(each and collectively, "you" or "your") who uses the Licensed Work for production,
and you agree to such provisions. Your production use of the Licensed Work is conditional on your
agreement to this License. If you do not agree and consent to be bound to this License,
do not so use the Software.

If you do not fall within the limited scope of this license as described above or below,
or are otherwise not in strict compliance with this License, then this production license does not
extend to you and does not grant you any production use, including any copying, distributing, or
making any derivative use of the Licensed Work for production purposes.

Limited Scope of License. Your commercial production license to the Licensed Work
under this License does not extend to any use:

1. unless you have legal capacity to consent and agree to be bound by this License;
2. unless you have the technical knowledge necessary or advisable to understand and evaluate the
risks of using the Licensed Work and the Babylon Protocol;
3. if you are a resident or agent of, or an entity organized, incorporated or doing business in,
Afghanistan, Belarus, Bosnia and Herzegovina, Burundi, Central African Republic, Crimea, Cuba,
Democratic People's Republic of Korea, Democratic Republic of the Congo,
Donetsk or Luhansk Regions of Ukraine, Eritrea, Guinea, Guinea-Bissau, Haiti, Iran, Iraq, Lebanon,
Libya, Mali, Myanmar, Nicaragua, Russia, Somalia, South Sudan, Sudan, Syria, Venezuela, Yemen,
or Zimbabwe or any other country to which the United States, the United Kingdom,
the European Union or any of its member states or the United Nations or any of its member states
(collectively, the "Major Jurisdictions") embargoes goods or imposes sanctions
(such embargoed or sanctioned territories, collectively, the "Restricted Territories");
4. if you are, or if you directly or indirectly own or control, from any person or entity that is
listed on any sanctions list or equivalent maintained by any of the Major Jurisdictions
(collectively, "Sanctions Lists Persons");
5. to transact in or with any Restricted Territories or Sanctions List Persons;
6. if you are a U.S. Person as defined in 17 CFR § 230.902, or currently or ordinarily located or
resident in (or incorporated or organized in) the United States of America, Canada, or
Australia (collectively, "Excluded Jurisdictions"), or to transact in or with Excluded Jurisdictions;
7. to defraud, or otherwise materially mislead, any person;
8. in violation of applicable laws, rules or regulations in your relevant jurisdiction;
9. that circumvents any sanctions or export controls targeting you or the country or territory where
you are located; or
10. in any activity that transmits, exchanges, or is otherwise supported by the direct or indirect
proceeds of criminal or fraudulent activity.

Any production use of the Licensed Work by you confirms your agreement to the foregoing limitations,
and your understanding and agreement that they are limitations and not restrictions.

Change Date: 2027-01-20 (January 20th, 2027)

Expand Down
5 changes: 5 additions & 0 deletions RELEASE_PROCESS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Release Process

Btc-staker repository follows the same release process rules as
[Babylon node](https://github.com/babylonlabs-io/babylon/blob/main/RELEASE_PROCESS.md)
repository.
54 changes: 42 additions & 12 deletions babylonclient/babyloncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,18 @@ func NewBabylonController(
}

type StakingTrackerResponse struct {
SlashingAddress btcutil.Address
SlashingPkScript []byte
SlashingRate sdkmath.LegacyDec
MinComissionRate sdkmath.LegacyDec
CovenantPks []*btcec.PublicKey
CovenantQuruomThreshold uint32
MinSlashingFee btcutil.Amount
MinUnbodningTime uint16
MinUnbondingTime uint16
UnbondingFee btcutil.Amount
MinStakingTime uint16
MaxStakingTime uint16
MinStakingValue btcutil.Amount
MaxStakingValue btcutil.Amount
}

type FinalityProviderInfo struct {
Expand Down Expand Up @@ -176,18 +181,23 @@ func (bc *BabylonController) Params() (*StakingParams, error) {

minUnbondingTime := sdkmath.Max[uint16](
uint16(bccParams.CheckpointFinalizationTimeout),
stakingTrackerParams.MinUnbodningTime,
stakingTrackerParams.MinUnbondingTime,
)

return &StakingParams{
ConfirmationTimeBlocks: uint32(bccParams.BtcConfirmationDepth),
FinalizationTimeoutBlocks: uint32(bccParams.CheckpointFinalizationTimeout),
SlashingAddress: stakingTrackerParams.SlashingAddress,
SlashingPkScript: stakingTrackerParams.SlashingPkScript,
CovenantPks: stakingTrackerParams.CovenantPks,
MinSlashingTxFeeSat: stakingTrackerParams.MinSlashingFee,
SlashingRate: stakingTrackerParams.SlashingRate,
CovenantQuruomThreshold: stakingTrackerParams.CovenantQuruomThreshold,
MinUnbondingTime: minUnbondingTime,
UnbondingFee: stakingTrackerParams.UnbondingFee,
MinStakingTime: stakingTrackerParams.MinStakingTime,
MaxStakingTime: stakingTrackerParams.MaxStakingTime,
MinStakingValue: stakingTrackerParams.MinStakingValue,
MaxStakingValue: stakingTrackerParams.MaxStakingValue,
}, nil
}

Expand Down Expand Up @@ -447,11 +457,6 @@ func (bc *BabylonController) QueryStakingTracker() (*StakingTrackerResponse, err
return nil, err
}

slashingAddress, err := btcutil.DecodeAddress(response.Params.SlashingAddress, bc.btcParams)
if err != nil {
return nil, err
}

// check this early than covenant config makes sense, so that rest of the
// code can assume that:
// 1. covenant quorum is less or equal to number of covenant pks
Expand All @@ -474,18 +479,43 @@ func (bc *BabylonController) QueryStakingTracker() (*StakingTrackerResponse, err
covenantPks = append(covenantPks, covenantBtcPk)
}

if response.Params.MinUnbondingTime > math.MaxUint16 {
if response.Params.MinUnbondingTimeBlocks > math.MaxUint16 {
return nil, fmt.Errorf("min unbonding time is bigger than uint16: %w", ErrInvalidValueReceivedFromBabylonNode)
}

if response.Params.MinStakingTimeBlocks > math.MaxUint16 {
return nil, fmt.Errorf("min staking time is bigger than uint16: %w", ErrInvalidValueReceivedFromBabylonNode)
}

if response.Params.MaxStakingTimeBlocks > math.MaxUint16 {
return nil, fmt.Errorf("max staking time is bigger than uint16: %w", ErrInvalidValueReceivedFromBabylonNode)
}

if response.Params.MinStakingValueSat < 0 {
return nil, fmt.Errorf("min staking value is negative: %w", ErrInvalidValueReceivedFromBabylonNode)
}

if response.Params.MaxStakingValueSat < 0 {
return nil, fmt.Errorf("max staking value is negative: %w", ErrInvalidValueReceivedFromBabylonNode)
}

if response.Params.UnbondingFeeSat < 0 {
return nil, fmt.Errorf("unbonding fee is negative: %w", ErrInvalidValueReceivedFromBabylonNode)
}

return &StakingTrackerResponse{
SlashingAddress: slashingAddress,
SlashingPkScript: response.Params.SlashingPkScript,
SlashingRate: response.Params.SlashingRate,
MinComissionRate: response.Params.MinCommissionRate,
CovenantPks: covenantPks,
MinSlashingFee: btcutil.Amount(response.Params.MinSlashingTxFeeSat),
CovenantQuruomThreshold: response.Params.CovenantQuorum,
MinUnbodningTime: uint16(response.Params.MinUnbondingTime),
MinUnbondingTime: uint16(response.Params.MinUnbondingTimeBlocks),
UnbondingFee: btcutil.Amount(response.Params.UnbondingFeeSat),
MinStakingTime: uint16(response.Params.MinStakingTimeBlocks),
MaxStakingTime: uint16(response.Params.MaxStakingTimeBlocks),
MinStakingValue: btcutil.Amount(response.Params.MinStakingValueSat),
MaxStakingValue: btcutil.Amount(response.Params.MaxStakingValueSat),
}, nil
}

Expand Down
28 changes: 25 additions & 3 deletions babylonclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
pv "github.com/cosmos/relayer/v2/relayer/provider"
Expand All @@ -27,8 +28,8 @@ type StakingParams struct {
// Bitcoin public key of the current covenant
CovenantPks []*btcec.PublicKey

// Address to which slashing transactions are sent
SlashingAddress btcutil.Address
// PkScript that must be inserted in the slashing output of the slashing transaction
SlashingPkScript []byte

// The rate at which the staked funds will be slashed, expressed as a decimal.
SlashingRate sdkmath.LegacyDec
Expand All @@ -38,6 +39,21 @@ type StakingParams struct {

// Minimum unbonding time required by bayblon
MinUnbondingTime uint16

// Fee required by unbonding transaction
UnbondingFee btcutil.Amount

// Minimum staking time required by bayblon
MinStakingTime uint16

// Maximum staking time required by bayblon
MaxStakingTime uint16

// Minimum staking value required by bayblon
MinStakingValue btcutil.Amount

// Maximum staking value required by bayblon
MaxStakingValue btcutil.Amount
}

// SingleKeyCosmosKeyring represents a keyring that supports only one pritvate/public key pair
Expand Down Expand Up @@ -154,6 +170,12 @@ func GetMockClient() *MockBabylonClient {

slashingAddress, _ := btcutil.NewAddressPubKey(covenantPk.PubKey().SerializeCompressed(), &chaincfg.SimNetParams)

slashingPkScript, err := txscript.PayToAddrScript(slashingAddress)

if err != nil {
panic(err)
}

fpBtcPrivKey, err := btcec.NewPrivateKey()
if err != nil {
panic(err)
Expand All @@ -170,7 +192,7 @@ func GetMockClient() *MockBabylonClient {
FinalizationTimeoutBlocks: 5,
MinSlashingTxFeeSat: btcutil.Amount(1000),
CovenantPks: []*btcec.PublicKey{covenantPk.PubKey()},
SlashingAddress: slashingAddress,
SlashingPkScript: slashingPkScript,
SlashingRate: sdkmath.LegacyNewDecWithPrec(1, 1), // 1 * 10^{-1} = 0.1
},
babylonKey: priv,
Expand Down
Loading

0 comments on commit 08a3fb4

Please sign in to comment.