Skip to content

Commit

Permalink
batch 3 of fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Nov 12, 2024
1 parent a66341b commit 9a089a5
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 152 deletions.
6 changes: 3 additions & 3 deletions babylonclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,16 @@ func (m *MockBabylonClient) QueryHeaderDepth(_ *chainhash.Hash) (uint32, error)
return m.ClientParams.ConfirmationTimeBlocks + 1, nil
}

func (m *MockBabylonClient) IsTxAlreadyPartOfDelegation(stakingTxHash *chainhash.Hash) (bool, error) {
func (m *MockBabylonClient) IsTxAlreadyPartOfDelegation(_ *chainhash.Hash) (bool, error) {
return false, nil
}

func (m *MockBabylonClient) QueryDelegationInfo(stakingTxHash *chainhash.Hash) (*DelegationInfo, error) {
func (m *MockBabylonClient) QueryDelegationInfo(_ *chainhash.Hash) (*DelegationInfo, error) {
return nil, fmt.Errorf("delegation do not exist")
}

func (m *MockBabylonClient) Undelegate(
req *UndelegationRequest) (*pv.RelayerTxResponse, error) {
_ *UndelegationRequest) (*pv.RelayerTxResponse, error) {
return &pv.RelayerTxResponse{Code: 0}, nil
}

Expand Down
57 changes: 28 additions & 29 deletions cmd/stakercli/transaction/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,41 +954,40 @@ func createWithdrawalInfo(
withdrawalFundingUtxo: unbondingTx.TxOut[0],
withdrawalSpendInfo: timeLockPathInfo,
}, nil
} else {
stakingInfo, err := btcstaking.BuildStakingInfo(
parsedStakingTransaction.OpReturnData.StakerPublicKey.PubKey,
[]*btcec.PublicKey{parsedStakingTransaction.OpReturnData.FinalityProviderPublicKey.PubKey},
paramsForHeight.CovenantPks,
paramsForHeight.CovenantQuorum,
parsedStakingTransaction.OpReturnData.StakingTime,
btcutil.Amount(parsedStakingTransaction.StakingOutput.Value),
net,
)

if err != nil {
return nil, fmt.Errorf("error building staking info: %w", err)
}
}
stakingInfo, err := btcstaking.BuildStakingInfo(
parsedStakingTransaction.OpReturnData.StakerPublicKey.PubKey,
[]*btcec.PublicKey{parsedStakingTransaction.OpReturnData.FinalityProviderPublicKey.PubKey},
paramsForHeight.CovenantPks,
paramsForHeight.CovenantQuorum,
parsedStakingTransaction.OpReturnData.StakingTime,
btcutil.Amount(parsedStakingTransaction.StakingOutput.Value),
net,
)

timelockPathInfo, err := stakingInfo.TimeLockPathSpendInfo()
if err != nil {
return nil, fmt.Errorf("error building staking info: %w", err)
}

if err != nil {
return nil, fmt.Errorf("error building timelock path spend info: %w", err)
}
timelockPathInfo, err := stakingInfo.TimeLockPathSpendInfo()

withdrawalOutputValue := parsedStakingTransaction.StakingOutput.Value - int64(withdrawalFee)
if err != nil {
return nil, fmt.Errorf("error building timelock path spend info: %w", err)
}

if withdrawalOutputValue <= 0 {
return nil, fmt.Errorf("too low staking output value to create withdrawal transaction. Staking amount: %d, Withdrawal fee: %d", parsedStakingTransaction.StakingOutput.Value, withdrawalFee)
}
withdrawalOutputValue := parsedStakingTransaction.StakingOutput.Value - int64(withdrawalFee)

return &withdrawalInfo{
withdrawalOutputvalue: btcutil.Amount(withdrawalOutputValue),
withdrawalSequence: uint32(parsedStakingTransaction.OpReturnData.StakingTime),
withdrawalInput: wire.NewOutPoint(stakingTxHash, uint32(parsedStakingTransaction.StakingOutputIdx)),
withdrawalFundingUtxo: parsedStakingTransaction.StakingOutput,
withdrawalSpendInfo: timelockPathInfo,
}, nil
if withdrawalOutputValue <= 0 {
return nil, fmt.Errorf("too low staking output value to create withdrawal transaction. Staking amount: %d, Withdrawal fee: %d", parsedStakingTransaction.StakingOutput.Value, withdrawalFee)
}

return &withdrawalInfo{
withdrawalOutputvalue: btcutil.Amount(withdrawalOutputValue),
withdrawalSequence: uint32(parsedStakingTransaction.OpReturnData.StakingTime),
withdrawalInput: wire.NewOutPoint(stakingTxHash, uint32(parsedStakingTransaction.StakingOutputIdx)),
withdrawalFundingUtxo: parsedStakingTransaction.StakingOutput,
withdrawalSpendInfo: timelockPathInfo,
}, nil
}

func createPhase1WitdrawalTransaction(ctx *cli.Context) error {
Expand Down
6 changes: 4 additions & 2 deletions cmd/stakerd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"
"net/http"
"os"
Expand All @@ -25,10 +26,11 @@ func main() {
cfg, cfgLogger, zapLogger, err := scfg.LoadConfig()

if err != nil {
if e, ok := err.(*flags.Error); !ok || e.Type != flags.ErrHelp {
// Print error if not due to help request.
var flagsErr *flags.Error
if !errors.As(err, &flagsErr) || flagsErr.Type != flags.ErrHelp {
err = fmt.Errorf("failed to load config: %w", err)
_, _ = fmt.Fprintln(os.Stderr, err)
//nolint:gocritic
os.Exit(1)
}

Expand Down
2 changes: 1 addition & 1 deletion itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func defaultStakerConfig(t *testing.T, walletName, passphrase, bitcoindHost stri
type TestManager struct {
Config *stakercfg.Config
Db kvdb.Backend
Sa *staker.StakerApp
Sa *staker.App
BabylonClient *babylonclient.BabylonController
WalletPubKey *btcec.PublicKey
MinerAddr btcutil.Address
Expand Down
1 change: 1 addition & 0 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metrics
import (
"errors"
"net/http"
//nolint:revive
_ "net/http/pprof"
"regexp"

Expand Down
21 changes: 10 additions & 11 deletions staker/babylontypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type sendDelegationRequest struct {
requiredInclusionBlockDepth uint32
}

func (app *StakerApp) buildOwnedDelegation(
func (app *App) buildOwnedDelegation(
req *sendDelegationRequest,
stakerAddress btcutil.Address,
storedTx *stakerdb.StoredTransaction,
Expand Down Expand Up @@ -133,7 +133,7 @@ func (app *StakerApp) buildOwnedDelegation(
return dg, nil
}

func (app *StakerApp) buildDelegation(
func (app *App) buildDelegation(
req *sendDelegationRequest,
stakerAddress btcutil.Address,
storedTx *stakerdb.StoredTransaction) (*cl.DelegationData, error) {
Expand Down Expand Up @@ -168,19 +168,18 @@ func (app *StakerApp) buildDelegation(
&undelegationData,
)
return dg, nil
} else {
return app.buildOwnedDelegation(
req,
stakerAddress,
storedTx,
)
}
return app.buildOwnedDelegation(
req,
stakerAddress,
storedTx,
)
}

// TODO for now we launch this handler indefinitely. At some point we may introduce
// timeout, and if signatures are not find in this timeout, then we may submit
// evidence that covenant members are censoring our staking transactions
func (app *StakerApp) checkForUnbondingTxSignaturesOnBabylon(stakingTxHash *chainhash.Hash) {
func (app *App) checkForUnbondingTxSignaturesOnBabylon(stakingTxHash *chainhash.Hash) {
checkSigTicker := time.NewTicker(app.config.StakerConfig.UnbondingTxCheckInterval)
defer checkSigTicker.Stop()
defer app.wg.Done()
Expand Down Expand Up @@ -265,7 +264,7 @@ func (app *StakerApp) checkForUnbondingTxSignaturesOnBabylon(stakingTxHash *chai
}
}

func (app *StakerApp) finalityProviderExists(fpPk *btcec.PublicKey) error {
func (app *App) finalityProviderExists(fpPk *btcec.PublicKey) error {
if fpPk == nil {
return fmt.Errorf("provided finality provider public key is nil")
}
Expand Down Expand Up @@ -300,7 +299,7 @@ func isTransacionFullySigned(tx *wire.MsgTx) (bool, error) {
// reaches verified state. i.e
// - delegation is on babylon
// - delegation has received enough covenant signatures
func (app *StakerApp) activateVerifiedDelegation(
func (app *App) activateVerifiedDelegation(
stakingTransaction *wire.MsgTx,
stakingOutputIndex uint32,
stakingTxHash *chainhash.Hash) {
Expand Down
2 changes: 1 addition & 1 deletion staker/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func newWatchedStakingCmd(
}
}

func (req *stakingRequestCmd) EventId() chainhash.Hash {
func (req *stakingRequestCmd) EventID() chainhash.Hash {
// we do not have has for this event
return chainhash.Hash{}
}
Expand Down
27 changes: 13 additions & 14 deletions staker/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
)

type StakingEvent interface {
// Each staking event is identified by initial staking transaction hash
EventId() chainhash.Hash
EventID() chainhash.Hash // Each staking event is identified by initial staking transaction hash
EventDesc() string
}

Expand All @@ -32,7 +31,7 @@ type stakingTxBtcConfirmedEvent struct {
inlusionBlock *wire.MsgBlock
}

func (event *stakingTxBtcConfirmedEvent) EventId() chainhash.Hash {
func (event *stakingTxBtcConfirmedEvent) EventID() chainhash.Hash {
return event.stakingTxHash
}

Expand All @@ -46,7 +45,7 @@ type delegationSubmittedToBabylonEvent struct {
unbondingTime uint16
}

func (event *delegationSubmittedToBabylonEvent) EventId() chainhash.Hash {
func (event *delegationSubmittedToBabylonEvent) EventID() chainhash.Hash {
return event.stakingTxHash
}

Expand All @@ -60,7 +59,7 @@ type unbondingTxSignaturesConfirmedOnBabylonEvent struct {
covenantUnbondingSignatures []cl.CovenantSignatureInfo
}

func (event *unbondingTxSignaturesConfirmedOnBabylonEvent) EventId() chainhash.Hash {
func (event *unbondingTxSignaturesConfirmedOnBabylonEvent) EventID() chainhash.Hash {
return event.stakingTxHash
}

Expand All @@ -74,7 +73,7 @@ type unbondingTxConfirmedOnBtcEvent struct {
blockHeight uint32
}

func (event *unbondingTxConfirmedOnBtcEvent) EventId() chainhash.Hash {
func (event *unbondingTxConfirmedOnBtcEvent) EventID() chainhash.Hash {
return event.stakingTxHash
}

Expand All @@ -86,7 +85,7 @@ type spendStakeTxConfirmedOnBtcEvent struct {
stakingTxHash chainhash.Hash
}

func (event *spendStakeTxConfirmedOnBtcEvent) EventId() chainhash.Hash {
func (event *spendStakeTxConfirmedOnBtcEvent) EventID() chainhash.Hash {
return event.stakingTxHash
}

Expand All @@ -100,24 +99,24 @@ type criticalErrorEvent struct {
additionalContext string
}

func (event *criticalErrorEvent) EventId() chainhash.Hash {
func (event *criticalErrorEvent) EventID() chainhash.Hash {
return event.stakingTxHash
}

func (event *criticalErrorEvent) EventDesc() string {
return "CRITICAL_ERROR"
}

func (app *StakerApp) logStakingEventReceived(event StakingEvent) {
func (app *App) logStakingEventReceived(event StakingEvent) {
app.logger.WithFields(logrus.Fields{
"eventId": event.EventId(),
"eventId": event.EventID(),
"event": event.EventDesc(),
}).Debug("Received staking event")
}

func (app *StakerApp) logStakingEventProcessed(event StakingEvent) {
func (app *App) logStakingEventProcessed(event StakingEvent) {
app.logger.WithFields(logrus.Fields{
"eventId": event.EventId(),
"eventId": event.EventID(),
"event": event.EventDesc(),
}).Debug("Processed staking event")
}
Expand All @@ -126,7 +125,7 @@ type delegationActivatedPostApprovalEvent struct {
stakingTxHash chainhash.Hash
}

func (event *delegationActivatedPostApprovalEvent) EventId() chainhash.Hash {
func (event *delegationActivatedPostApprovalEvent) EventID() chainhash.Hash {
return event.stakingTxHash
}

Expand All @@ -140,7 +139,7 @@ type delegationActivatedPreApprovalEvent struct {
blockHeight uint32
}

func (event *delegationActivatedPreApprovalEvent) EventId() chainhash.Hash {
func (event *delegationActivatedPreApprovalEvent) EventID() chainhash.Hash {
return event.stakingTxHash
}

Expand Down
Loading

0 comments on commit 9a089a5

Please sign in to comment.