Skip to content

Commit

Permalink
Merge branch 'main' into devops/add-docker-security-r3
Browse files Browse the repository at this point in the history
  • Loading branch information
huynaism authored Nov 21, 2024
2 parents 66227f3 + 10a89d0 commit 73f8345
Show file tree
Hide file tree
Showing 41 changed files with 2,407 additions and 800 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
8 changes: 5 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ 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/reusable_docker_pipeline.yml@huy/add-docker-sec
Expand Down
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## Unreleased

## 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
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.7 AS builder
FROM golang:1.23.1 AS builder

# Install cli tools for building and final image
# hadolint ignore=DL3008
Expand Down
87 changes: 48 additions & 39 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 @@ -502,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 @@ -670,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 @@ -681,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 @@ -748,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 @@ -873,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 @@ -900,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 @@ -923,5 +933,4 @@ func (bc *BabylonController) ActivateDelegation(
}

return res, nil

}
Loading

0 comments on commit 73f8345

Please sign in to comment.