Skip to content

Commit

Permalink
Merge branch 'main' of github.com:babylonlabs-io/btc-staker into rafi…
Browse files Browse the repository at this point in the history
…lx/migrate-stk-to-phase2
  • Loading branch information
RafilxTenfen committed Nov 18, 2024
2 parents d76b933 + 10a89d0 commit 7a53e09
Show file tree
Hide file tree
Showing 41 changed files with 2,644 additions and 1,005 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ jobs:
lint_test:
uses: babylonlabs-io/.github/.github/workflows/[email protected]
with:
run-unit-tests: true
run-integration-tests: true
run-lint: true
go-version: '1.23'
go-lint-version: 'v1.60.2'
run-unit-tests: true
run-integration-tests: true
run-lint: true

docker_pipeline:
uses: babylonlabs-io/.github/.github/workflows/[email protected]
secrets: inherit
with:
publish: false
publish: false
10 changes: 6 additions & 4 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ jobs:
lint_test:
uses: babylonlabs-io/.github/.github/workflows/[email protected]
with:
run-unit-tests: true
run-integration-tests: true
run-lint: true
go-version: '1.23'
go-lint-version: 'v1.60.2'
run-unit-tests: true
run-integration-tests: true
run-lint: true

docker_pipeline:
needs: ["lint_test"]
uses: babylonlabs-io/.github/.github/workflows/[email protected]
secrets: inherit
with:
publish: true
publish: true
80 changes: 80 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
run:
timeout: 5m

linters:
disable-all: true
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- containedctx
- contextcheck
- decorder
- dogsled
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
- exhaustive
- forbidigo
- forcetypeassert
- goconst
- gocritic
- gocyclo
- goheader
- gomodguard
- goprintffuncname
- gosimple
- govet
- grouper
- importas
- ineffassign
- loggercheck
- maintidx
- makezero
- misspell
- nakedret
- nilerr
# - nlreturn # Style wise I personally like this one, todo(lazar): unlax at somepoint, good practice
- noctx
- nonamedreturns
- nosprintfhostport
- paralleltest
- reassign
- revive
- rowserrcheck
- sqlclosecheck
- staticcheck
- stylecheck
- tenv
- testableexamples
- tparallel
- typecheck
- unconvert
- unparam
- usestdlibvars
- unused
- wastedassign
- whitespace
# - wrapcheck # we really should be using this, lax for now todo(lazar): unlax at somepoint, good practice

issues:
max-same-issues: 0
# Default: https://golangci-lint.run/usage/false-positives/#default-exclusions
exclude-dirs:
- e2etest
- itest
exclude-rules:
# Exclude some linters from running on tests files.
- path: _test\.go
linters:
- gocyclo
- errcheck
- dupl
- gosec
- gocritic
- path-except: _test\.go
linters:
- forbidigo
30 changes: 29 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,35 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

### Misc Improvements
## v0.11.0

### Improvements

* [#93](https://github.com/babylonlabs-io/btc-staker/pull/93) Fix linting config
* [#95](https://github.com/babylonlabs-io/btc-staker/pull/95) Bump babylon to v0.16
* [#96](https://github.com/babylonlabs-io/btc-staker/pull/96) Check allow list
expiration height before creating new delegations

## v0.10.0

* [#87](https://github.com/babylonlabs-io/btc-staker/pull/87) Bump babylon v15

## v0.9.0

### Bug fix

* [#78](https://github.com/babylonlabs-io/btc-staker/pull/78) Fix
`withdrawable-transactions` query bug, introduced when adding pre-approval
transactions handling

### Improvements

* [#79](https://github.com/babylonlabs-io/btc-staker/pull/79) Remove `BTCUndelegate`
message sending

## v0.8.0

### Improvements

* [#51](https://github.com/babylonlabs-io/btc-staker/pull/51) Use int64
for satoshi amount related values.
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22.3 AS builder
FROM golang:1.23.1 AS builder

# Install cli tools for building and final image
RUN apt-get update && apt-get install -y make git bash gcc curl jq
Expand Down
102 changes: 48 additions & 54 deletions babylonclient/babyloncontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/avast/retry-go/v4"
bbnclient "github.com/babylonlabs-io/babylon/client/client"
bbntypes "github.com/babylonlabs-io/babylon/types"
bcctypes "github.com/babylonlabs-io/babylon/x/btccheckpoint/types"
btclctypes "github.com/babylonlabs-io/babylon/x/btclightclient/types"
btcstypes "github.com/babylonlabs-io/babylon/x/btcstaking/types"
"github.com/babylonlabs-io/btc-staker/stakercfg"
Expand Down Expand Up @@ -100,18 +99,19 @@ func NewBabylonController(
}

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

type FinalityProviderInfo struct {
Expand All @@ -135,7 +135,7 @@ func (bc *BabylonController) Stop() error {

func (bc *BabylonController) Params() (*StakingParams, error) {
// TODO: it would probably be good to have separate methods for those
var bccParams *bcctypes.Params
var bccParams *btcctypes.Params
if err := retry.Do(func() error {
response, err := bc.bbnClient.BTCCheckpointParams()
if err != nil {
Expand Down Expand Up @@ -181,8 +181,8 @@ func (bc *BabylonController) Params() (*StakingParams, error) {
}

return &StakingParams{
ConfirmationTimeBlocks: uint32(bccParams.BtcConfirmationDepth),
FinalizationTimeoutBlocks: uint32(bccParams.CheckpointFinalizationTimeout),
ConfirmationTimeBlocks: bccParams.BtcConfirmationDepth,
FinalizationTimeoutBlocks: bccParams.CheckpointFinalizationTimeout,
SlashingPkScript: stakingTrackerParams.SlashingPkScript,
CovenantPks: stakingTrackerParams.CovenantPks,
MinSlashingTxFeeSat: stakingTrackerParams.MinSlashingFee,
Expand All @@ -194,6 +194,7 @@ func (bc *BabylonController) Params() (*StakingParams, error) {
MaxStakingTime: stakingTrackerParams.MaxStakingTime,
MinStakingValue: stakingTrackerParams.MinStakingValue,
MaxStakingValue: stakingTrackerParams.MaxStakingValue,
AllowListExpirationHeight: stakingTrackerParams.AllowListExpirationHeight,
}, nil
}

Expand All @@ -218,6 +219,18 @@ func (bc *BabylonController) GetKeyAddress() sdk.AccAddress {
return addr
}

func (bc *BabylonController) GetLatestBlockHeight() (uint64, error) {
ctx, cancel := getQueryContext(bc.cfg.Timeout)
defer cancel()

status, err := bc.bbnClient.RPCClient.Status(ctx)
if err != nil {
return 0, err
}

return uint64(status.SyncInfo.LatestBlockHeight), nil
}

func (bc *BabylonController) getTxSigner() string {
signer := bc.GetKeyAddress()
prefix := bc.cfg.AccountPrefix
Expand Down Expand Up @@ -375,13 +388,13 @@ func delegationDataToMsg(dg *DelegationData) (*btcstypes.MsgCreateBTCDelegation,

slashUnbondingTxSig := bbntypes.NewBIP340SignatureFromBTCSig(dg.Ud.SlashUnbondingTransactionSig)

var stakingTransactionInclusionProof *btcstypes.InclusionProof = nil
var stakingTransactionInclusionProof *btcstypes.InclusionProof

if dg.StakingTransactionInclusionInfo != nil {
inclusionBlockHash := bbntypes.NewBTCHeaderHashBytesFromChainhash(
dg.StakingTransactionInclusionInfo.StakingTransactionInclusionBlockHash,
)
txKey := &bcctypes.TransactionKey{
txKey := &btcctypes.TransactionKey{
Index: dg.StakingTransactionInclusionInfo.StakingTransactionIdx,
Hash: &inclusionBlockHash,
}
Expand Down Expand Up @@ -435,21 +448,6 @@ func (bc *BabylonController) Delegate(dg *DelegationData) (*pv.RelayerTxResponse
return bc.reliablySendMsgs([]sdk.Msg{delegateMsg})
}

func (bc *BabylonController) Undelegate(
req *UndelegationRequest,
) (*pv.RelayerTxResponse, error) {

ubSig := bbntypes.NewBIP340SignatureFromBTCSig(req.StakerUnbondingSig)

msg := &btcstypes.MsgBTCUndelegate{
Signer: bc.getTxSigner(),
StakingTxHash: req.StakingTxHash.String(),
UnbondingTxSig: ubSig,
}

return bc.reliablySendMsgs([]sdk.Msg{msg})
}

func getQueryContext(timeout time.Duration) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
return ctx, cancel
Expand Down Expand Up @@ -517,18 +515,19 @@ func (bc *BabylonController) QueryStakingTracker() (*StakingTrackerResponse, err
}

return &StakingTrackerResponse{
SlashingPkScript: response.Params.SlashingPkScript,
SlashingRate: response.Params.SlashingRate,
MinComissionRate: response.Params.MinCommissionRate,
CovenantPks: covenantPks,
MinSlashingFee: btcutil.Amount(response.Params.MinSlashingTxFeeSat),
CovenantQuruomThreshold: response.Params.CovenantQuorum,
MinUnbondingTime: uint16(minUnbondingTimeBlocksU32),
UnbondingFee: btcutil.Amount(response.Params.UnbondingFeeSat),
MinStakingTime: uint16(minStakingTimeBlocksU32),
MaxStakingTime: uint16(maxStakingTimeBlocksU32),
MinStakingValue: btcutil.Amount(response.Params.MinStakingValueSat),
MaxStakingValue: btcutil.Amount(response.Params.MaxStakingValueSat),
SlashingPkScript: response.Params.SlashingPkScript,
SlashingRate: response.Params.SlashingRate,
MinComissionRate: response.Params.MinCommissionRate,
CovenantPks: covenantPks,
MinSlashingFee: btcutil.Amount(response.Params.MinSlashingTxFeeSat),
CovenantQuruomThreshold: response.Params.CovenantQuorum,
MinUnbondingTime: uint16(minUnbondingTimeBlocksU32),
UnbondingFee: btcutil.Amount(response.Params.UnbondingFeeSat),
MinStakingTime: uint16(minStakingTimeBlocksU32),
MaxStakingTime: uint16(maxStakingTimeBlocksU32),
MinStakingValue: btcutil.Amount(response.Params.MinStakingValueSat),
MaxStakingValue: btcutil.Amount(response.Params.MaxStakingValueSat),
AllowListExpirationHeight: response.Params.AllowListExpirationHeight,
}, nil
}

Expand Down Expand Up @@ -685,7 +684,6 @@ func (bc *BabylonController) QueryHeaderDepth(headerHash *chainhash.Hash) (uint3
"error": err,
}).Error("Failed to query babylon for the depth of the header")
})); err != nil {

// translate errors to locally handable ones
if strings.Contains(err.Error(), btclctypes.ErrHeaderDoesNotExist.Error()) {
return 0, fmt.Errorf("%s: %w", err.Error(), ErrHeaderNotKnownToBabylon)
Expand All @@ -696,10 +694,9 @@ func (bc *BabylonController) QueryHeaderDepth(headerHash *chainhash.Hash) (uint3
}

return response.Depth, nil

}

// Insert BTC block header using rpc client
// InsertBtcBlockHeaders Insert BTC block header using rpc client
func (bc *BabylonController) InsertBtcBlockHeaders(headers []*wire.BlockHeader) (*pv.RelayerTxResponse, error) {
msg := &btclctypes.MsgInsertHeaders{
Signer: bc.getTxSigner(),
Expand Down Expand Up @@ -763,7 +760,7 @@ func (bc *BabylonController) QueryDelegationInfo(stakingTxHash *chainhash.Hash)
return err
}

var udi *UndelegationInfo = nil
var udi *UndelegationInfo

if resp.BtcDelegation.UndelegationResponse != nil {
var coventSigInfos []CovenantSignatureInfo
Expand Down Expand Up @@ -888,7 +885,7 @@ func (bc *BabylonController) QueryPendingBTCDelegations() ([]*btcstypes.BTCDeleg

res, err := queryClient.BTCDelegations(ctx, &queryRequest)
if err != nil {
return nil, fmt.Errorf("failed to query BTC delegations: %v", err)
return nil, fmt.Errorf("failed to query BTC delegations: %w", err)
}

return res.BtcDelegations, nil
Expand All @@ -915,17 +912,15 @@ func (bc *BabylonController) InsertSpvProofs(submitter string, proofs []*btcctyp
func (bc *BabylonController) QueryBtcLightClientTip() (*btclctypes.BTCHeaderInfoResponse, error) {
res, err := bc.bbnClient.QueryClient.BTCHeaderChainTip()
if err != nil {
return nil, fmt.Errorf("failed to query BTC tip: %v", err)
return nil, fmt.Errorf("failed to query BTC tip: %w", err)
}

return res.Header, nil
}

func (bc *BabylonController) ActivateDelegation(
ctx context.Context,
stakingTxHash chainhash.Hash,
proof *btcctypes.BTCSpvProof) (*pv.RelayerTxResponse, error) {

msg := &btcstypes.MsgAddBTCDelegationInclusionProof{
Signer: bc.getTxSigner(),
StakingTxHash: stakingTxHash.String(),
Expand All @@ -938,5 +933,4 @@ func (bc *BabylonController) ActivateDelegation(
}

return res, nil

}
Loading

0 comments on commit 7a53e09

Please sign in to comment.