Skip to content

Commit

Permalink
Merge branch 'dev' into hotfix-stakingoputputidx
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Apr 2, 2024
2 parents 6ef9258 + 6cb25a9 commit 6e592a1
Show file tree
Hide file tree
Showing 15 changed files with 692 additions and 224 deletions.
44 changes: 44 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Changelog

## [Unreleased](https://github.com/babylonchain/covenant-emulator/tree/HEAD)

[Full Changelog](https://github.com/babylonchain/covenant-emulator/compare/v0.1.0...HEAD)

**Merged pull requests:**

- chore: Bump Babylon v0.8.0 [\#17](https://github.com/babylonchain/covenant-emulator/pull/17) ([gitferry](https://github.com/gitferry))
- Bumps babylon to latest version [\#16](https://github.com/babylonchain/covenant-emulator/pull/16) ([KonradStaniec](https://github.com/KonradStaniec))
- feat: Allow the covenant emulator to submit signatures in batches [\#15](https://github.com/babylonchain/covenant-emulator/pull/15) ([vitsalis](https://github.com/vitsalis))
- CI: Remove redundant SSH key logic [\#14](https://github.com/babylonchain/covenant-emulator/pull/14) ([filippos47](https://github.com/filippos47))
- chore: Remove dependency on finality-provider [\#13](https://github.com/babylonchain/covenant-emulator/pull/13) ([gitferry](https://github.com/gitferry))
- chore: Remove private repo thingy [\#11](https://github.com/babylonchain/covenant-emulator/pull/11) ([gitferry](https://github.com/gitferry))

## [v0.1.0](https://github.com/babylonchain/covenant-emulator/tree/v0.1.0) (2024-02-08)

[Full Changelog](https://github.com/babylonchain/covenant-emulator/compare/v0.1.0-rc.0...v0.1.0)

**Implemented enhancements:**

- Cycle dependency with finality-provider [\#9](https://github.com/babylonchain/covenant-emulator/issues/9)

## [v0.1.0-rc.0](https://github.com/babylonchain/covenant-emulator/tree/v0.1.0-rc.0) (2024-01-22)

[Full Changelog](https://github.com/babylonchain/covenant-emulator/compare/5187e721981f61e36012e183af9171068d9b2544...v0.1.0-rc.0)

**Closed issues:**

- Log directory won't be created automatically if not existing [\#3](https://github.com/babylonchain/covenant-emulator/issues/3)

**Merged pull requests:**

- chore: Upgrade bbn to 0.8.0-rc.0 [\#12](https://github.com/babylonchain/covenant-emulator/pull/12) ([vitsalis](https://github.com/vitsalis))
- chore: Prepare public release [\#7](https://github.com/babylonchain/covenant-emulator/pull/7) ([gitferry](https://github.com/gitferry))
- chore: Bump babylon with checking of slashing time lock [\#6](https://github.com/babylonchain/covenant-emulator/pull/6) ([gitferry](https://github.com/gitferry))
- doc: Add initial documentation of covenant emulator [\#5](https://github.com/babylonchain/covenant-emulator/pull/5) ([gitferry](https://github.com/gitferry))
- chore: Fix directory permission when `init` [\#4](https://github.com/babylonchain/covenant-emulator/pull/4) ([gitferry](https://github.com/gitferry))
- chore: display the error when failing to load docs [\#2](https://github.com/babylonchain/covenant-emulator/pull/2) ([vitsalis](https://github.com/vitsalis))
- Init covenant emulator [\#1](https://github.com/babylonchain/covenant-emulator/pull/1) ([gitferry](https://github.com/gitferry))



\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ endif
BUILD_TARGETS := build install
BUILD_FLAGS := --tags "$(build_tags)" --ldflags '$(ldflags)'

# Update changelog vars
ifneq (,$(SINCE_TAG))
sinceTag := --since-tag $(SINCE_TAG)
endif
ifneq (,$(UPCOMING_TAG))
upcomingTag := --future-release $(UPCOMING_TAG)
endif

all: build install

build: BUILD_ARGS := $(build_args) -o $(BUILDDIR)
Expand All @@ -55,3 +63,11 @@ test-e2e:
mock-gen:
mkdir -p $(MOCKS_DIR)
$(MOCKGEN_CMD) -source=clientcontroller/interface.go -package mocks -destination $(MOCKS_DIR)/babylon.go

.PHONY: mock-gen

update-changelog:
@echo ./scripts/update_changelog.sh $(sinceTag) $(upcomingTag)
./scripts/update_changelog.sh $(sinceTag) $(upcomingTag)

.PHONY: update-changelog
85 changes: 45 additions & 40 deletions clientcontroller/babylon.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package clientcontroller

import (
"context"
"encoding/hex"
"fmt"
"time"

Expand Down Expand Up @@ -189,8 +188,13 @@ func (bc *BabylonController) queryDelegationsWithStatus(status btcstakingtypes.B
}

dels := make([]*types.Delegation, 0, len(res.BtcDelegations))
for _, d := range res.BtcDelegations {
dels = append(dels, ConvertDelegationType(d))
for _, delResp := range res.BtcDelegations {
del, err := DelegationRespToDelegation(delResp)
if err != nil {
return nil, err
}

dels = append(dels, del)
}

return dels, nil
Expand All @@ -209,26 +213,21 @@ func (bc *BabylonController) Close() error {
return bc.bbnClient.Stop()
}

func ConvertDelegationType(del *btcstakingtypes.BTCDelegation) *types.Delegation {
func DelegationRespToDelegation(del *btcstakingtypes.BTCDelegationResponse) (*types.Delegation, error) {
var (
stakingTxHex string
slashingTxHex string
covenantSigs []*types.CovenantAdaptorSigInfo
undelegation *types.Undelegation
covenantSigs []*types.CovenantAdaptorSigInfo
undelegation *types.Undelegation
err error
)

if del.StakingTx == nil {
panic(fmt.Errorf("staking tx should not be empty in delegation"))
if del.StakingTxHex == "" {
return nil, fmt.Errorf("staking tx should not be empty in delegation")
}

if del.SlashingTx == nil {
panic(fmt.Errorf("slashing tx should not be empty in delegation"))
if del.SlashingTxHex == "" {
return nil, fmt.Errorf("slashing tx should not be empty in delegation")
}

stakingTxHex = hex.EncodeToString(del.StakingTx)

slashingTxHex = del.SlashingTx.ToHexStr()

for _, s := range del.CovenantSigs {
covSigInfo := &types.CovenantAdaptorSigInfo{
Pk: s.CovPk.MustToBTCPK(),
Expand All @@ -237,8 +236,11 @@ func ConvertDelegationType(del *btcstakingtypes.BTCDelegation) *types.Delegation
covenantSigs = append(covenantSigs, covSigInfo)
}

if del.BtcUndelegation != nil {
undelegation = ConvertUndelegationType(del.BtcUndelegation)
if del.UndelegationResponse != nil {
undelegation, err = UndelegationRespToUndelegation(del.UndelegationResponse)
if err != nil {
return nil, err
}
}

fpBtcPks := make([]*btcec.PublicKey, 0, len(del.FpBtcPkList))
Expand All @@ -252,39 +254,34 @@ func ConvertDelegationType(del *btcstakingtypes.BTCDelegation) *types.Delegation
TotalSat: del.TotalSat,
StartHeight: del.StartHeight,
EndHeight: del.EndHeight,
StakingTxHex: stakingTxHex,
SlashingTxHex: slashingTxHex,
StakingTxHex: del.StakingTxHex,
SlashingTxHex: del.SlashingTxHex,
StakingOutputIdx: del.StakingOutputIdx,
CovenantSigs: covenantSigs,
UnbondingTime: del.UnbondingTime,
BtcUndelegation: undelegation,
}
}, nil
}

func ConvertUndelegationType(undel *btcstakingtypes.BTCUndelegation) *types.Undelegation {
func UndelegationRespToUndelegation(undel *btcstakingtypes.BTCUndelegationResponse) (*types.Undelegation, error) {
var (
unbondingTxHex string
slashingTxHex string
covenantSlashingSigs []*types.CovenantAdaptorSigInfo
covenantUnbondingSigs []*types.CovenantSchnorrSigInfo
err error
)

if undel.UnbondingTx == nil {
panic(fmt.Errorf("staking tx should not be empty in undelegation"))
if undel.UnbondingTxHex == "" {
return nil, fmt.Errorf("staking tx should not be empty in undelegation")
}

if undel.SlashingTx == nil {
panic(fmt.Errorf("slashing tx should not be empty in undelegation"))
if undel.SlashingTxHex == "" {
return nil, fmt.Errorf("slashing tx should not be empty in undelegation")
}

unbondingTxHex = hex.EncodeToString(undel.UnbondingTx)

slashingTxHex = undel.SlashingTx.ToHexStr()

for _, unbondingSig := range undel.CovenantUnbondingSigList {
sig, err := unbondingSig.Sig.ToBTCSig()
if err != nil {
panic(err)
return nil, err
}
sigInfo := &types.CovenantSchnorrSigInfo{
Pk: unbondingSig.Pk.MustToBTCPK(),
Expand All @@ -301,13 +298,21 @@ func ConvertUndelegationType(undel *btcstakingtypes.BTCUndelegation) *types.Unde
covenantSlashingSigs = append(covenantSlashingSigs, covSigInfo)
}

delegatorUnbondingSig := new(bbntypes.BIP340Signature)
if undel.DelegatorUnbondingSigHex != "" {
delegatorUnbondingSig, err = bbntypes.NewBIP340SignatureFromHex(undel.DelegatorUnbondingSigHex)
if err != nil {
return nil, err
}
}

return &types.Undelegation{
UnbondingTxHex: unbondingTxHex,
SlashingTxHex: slashingTxHex,
UnbondingTxHex: undel.UnbondingTxHex,
SlashingTxHex: undel.SlashingTxHex,
CovenantSlashingSigs: covenantSlashingSigs,
CovenantUnbondingSigs: covenantUnbondingSigs,
DelegatorUnbondingSig: undel.DelegatorUnbondingSig,
}
DelegatorUnbondingSig: delegatorUnbondingSig,
}, nil
}

// Currently this is only used for e2e tests, probably does not need to add it into the interface
Expand Down Expand Up @@ -392,8 +397,8 @@ func (bc *BabylonController) InsertBtcBlockHeaders(headers []bbntypes.BTCHeaderB

// QueryFinalityProvider queries finality providers
// Currently this is only used for e2e tests, probably does not need to add this into the interface
func (bc *BabylonController) QueryFinalityProviders() ([]*btcstakingtypes.FinalityProvider, error) {
var fps []*btcstakingtypes.FinalityProvider
func (bc *BabylonController) QueryFinalityProviders() ([]*btcstakingtypes.FinalityProviderResponse, error) {
var fps []*btcstakingtypes.FinalityProviderResponse
pagination := &sdkquery.PageRequest{
Limit: 100,
}
Expand Down Expand Up @@ -425,7 +430,7 @@ func (bc *BabylonController) QueryFinalityProviders() ([]*btcstakingtypes.Finali
}

// Currently this is only used for e2e tests, probably does not need to add this into the interface
func (bc *BabylonController) QueryBtcLightClientTip() (*btclctypes.BTCHeaderInfo, error) {
func (bc *BabylonController) QueryBtcLightClientTip() (*btclctypes.BTCHeaderInfoResponse, error) {
ctx, cancel := getContextWithCancel(bc.cfg.Timeout)
defer cancel()

Expand Down
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Config struct {

BTCNetParams chaincfg.Params

Metrics *MetricsConfig `group:"metrics" namespace:"metrics"`

BabylonConfig *BBNConfig `group:"babylon" namespace:"babylon"`
}

Expand Down Expand Up @@ -83,6 +85,14 @@ func LoadConfig(homePath string) (*Config, error) {
// illegal values or combination of values are set. All file system paths are
// normalized. The cleaned up config is returned on success.
func (cfg *Config) Validate() error {
if cfg.Metrics == nil {
return fmt.Errorf("empty metrics config")
}

if err := cfg.Metrics.Validate(); err != nil {
return fmt.Errorf("invalid metrics config")
}

switch cfg.BitcoinNetwork {
case "mainnet":
cfg.BTCNetParams = chaincfg.MainNetParams
Expand Down Expand Up @@ -117,13 +127,15 @@ func DefaultConfigWithHomePath(homePath string) Config {
bbnCfg := DefaultBBNConfig()
bbnCfg.Key = defaultCovenantKeyName
bbnCfg.KeyDirectory = homePath
metricsCfg := DefaultMetricsConfig()
cfg := Config{
LogLevel: defaultLogLevel,
QueryInterval: defaultQueryInterval,
DelegationLimit: defaultDelegationLimit,
SigsBatchSize: defaultSigsBatchSize,
BitcoinNetwork: defaultBitcoinNetwork,
BTCNetParams: defaultBTCNetParams,
Metrics: &metricsCfg,
BabylonConfig: &bbnCfg,
}

Expand Down
48 changes: 48 additions & 0 deletions config/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package config

import (
"fmt"
"net"
"time"
)

const (
defaultMetricsPort = 2112
defaultMetricsHost = "127.0.0.1"
defaultMetricsUpdateInterval = 100 * time.Millisecond
)

// MetricsConfig defines the server's basic configuration
type MetricsConfig struct {
Host string `long:"host" description:"IP of the Prometheus server"`
Port int `long:"port" description:"Port of the Prometheus server"`
UpdateInterval time.Duration `long:"updateinterval" description:"The interval of Prometheus metrics updated"`
}

func (cfg *MetricsConfig) Validate() error {
if cfg.Port < 0 || cfg.Port > 65535 {
return fmt.Errorf("invalid port: %d", cfg.Port)
}

ip := net.ParseIP(cfg.Host)
if ip == nil {
return fmt.Errorf("invalid host: %v", cfg.Host)
}

return nil
}

func (cfg *MetricsConfig) Address() (string, error) {
if err := cfg.Validate(); err != nil {
return "", err
}
return fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), nil
}

func DefaultMetricsConfig() MetricsConfig {
return MetricsConfig{
Port: defaultMetricsPort,
Host: defaultMetricsHost,
UpdateInterval: defaultMetricsUpdateInterval,
}
}
Loading

0 comments on commit 6e592a1

Please sign in to comment.