Skip to content

Commit

Permalink
fix: pre approval flow fixes (#65)
Browse files Browse the repository at this point in the history
* Refactor stakerApp logic in regards to two flows

* Remove unnecesary state
  • Loading branch information
KonradStaniec authored Oct 16, 2024
1 parent 00555f5 commit 16c8dd0
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 464 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### Misc Improvements

* [51](https://github.com/babylonlabs-io/btc-staker/pull/51) Use int64
* [#51](https://github.com/babylonlabs-io/btc-staker/pull/51) Use int64
for satoshi amount related values.

* [#65](https://github.com/babylonlabs-io/btc-staker/pull/65) Various fixes to
pre-approval flow. Do not send signed staking transactions to Babylon.

## v0.7.2

### Bug fix
Expand Down
2 changes: 1 addition & 1 deletion itest/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ func (tm *TestManager) sendStakingTxBTC(
require.Equal(t, stakingDetails.StakingTxHash, txHash)

if sendToBabylonFirst {
require.Equal(t, stakingDetails.StakingState, proto.TransactionState_TRANSACTION_CREATED.String())
require.Equal(t, stakingDetails.StakingState, proto.TransactionState_SENT_TO_BABYLON.String())
} else {
require.Equal(t, stakingDetails.StakingState, proto.TransactionState_SENT_TO_BTC.String())
}
Expand Down
77 changes: 36 additions & 41 deletions proto/transaction.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions proto/transaction.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ package proto;
option go_package = "github.com/babylonlabs-io/btc-staker/proto";

enum TransactionState {
TRANSACTION_CREATED = 0;
SENT_TO_BTC = 1;
CONFIRMED_ON_BTC = 2;
SENT_TO_BABYLON = 3;
VERIFIED = 4;
DELEGATION_ACTIVE = 5;
UNBONDING_CONFIRMED_ON_BTC = 6;
SPENT_ON_BTC = 7;
SENT_TO_BTC = 0;
CONFIRMED_ON_BTC = 1;
SENT_TO_BABYLON = 2;
VERIFIED = 3;
DELEGATION_ACTIVE = 4;
UNBONDING_CONFIRMED_ON_BTC = 5;
SPENT_ON_BTC = 6;
}

message WatchedTxData {
Expand Down
130 changes: 130 additions & 0 deletions staker/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package staker

import (
cl "github.com/babylonlabs-io/btc-staker/babylonclient"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// we can make command to implement StakingEvent interface
var _ StakingEvent = (*stakingRequestCmd)(nil)

type stakingRequestCmd struct {
stakerAddress btcutil.Address
stakingTxHash chainhash.Hash
stakingTx *wire.MsgTx
stakingOutputIdx uint32
stakingOutputPkScript []byte
stakingTime uint16
stakingValue btcutil.Amount
fpBtcPks []*btcec.PublicKey
requiredDepthOnBtcChain uint32
pop *cl.BabylonPop
watchTxData *watchTxDataCmd
usePreApprovalFlow bool
errChan chan error
successChan chan *chainhash.Hash
}

func (req *stakingRequestCmd) isWatched() bool {
return req.watchTxData != nil
}

func newOwnedStakingCommand(
stakerAddress btcutil.Address,
stakingTx *wire.MsgTx,
stakingOutputIdx uint32,
stakingOutputPkScript []byte,
stakingTime uint16,
stakingValue btcutil.Amount,
fpBtcPks []*btcec.PublicKey,
confirmationTimeBlocks uint32,
pop *cl.BabylonPop,
usePreApprovalFlow bool,
) *stakingRequestCmd {
return &stakingRequestCmd{
stakerAddress: stakerAddress,
stakingTxHash: stakingTx.TxHash(),
stakingTx: stakingTx,
stakingOutputIdx: stakingOutputIdx,
stakingOutputPkScript: stakingOutputPkScript,
stakingTime: stakingTime,
stakingValue: stakingValue,
fpBtcPks: fpBtcPks,
requiredDepthOnBtcChain: confirmationTimeBlocks,
pop: pop,
watchTxData: nil,
usePreApprovalFlow: usePreApprovalFlow,
errChan: make(chan error, 1),
successChan: make(chan *chainhash.Hash, 1),
}
}

type watchTxDataCmd struct {
slashingTx *wire.MsgTx
slashingTxSig *schnorr.Signature
stakerBabylonAddr sdk.AccAddress
stakerBtcPk *btcec.PublicKey
// unbonding related data
unbondingTx *wire.MsgTx
slashUnbondingTx *wire.MsgTx
slashUnbondingTxSig *schnorr.Signature
unbondingTime uint16
}

func newWatchedStakingCmd(
stakerAddress btcutil.Address,
stakingTx *wire.MsgTx,
stakingOutputIdx uint32,
stakingOutputPkScript []byte,
stakingTime uint16,
stakingValue btcutil.Amount,
fpBtcPks []*btcec.PublicKey,
confirmationTimeBlocks uint32,
pop *cl.BabylonPop,
slashingTx *wire.MsgTx,
slashingTxSignature *schnorr.Signature,
stakerBabylonAddr sdk.AccAddress,
stakerBtcPk *btcec.PublicKey,
unbondingTx *wire.MsgTx,
slashUnbondingTx *wire.MsgTx,
slashUnbondingTxSig *schnorr.Signature,
unbondingTime uint16,
) *stakingRequestCmd {
return &stakingRequestCmd{
stakerAddress: stakerAddress,
stakingTxHash: stakingTx.TxHash(),
stakingTx: stakingTx,
stakingOutputIdx: stakingOutputIdx,
stakingOutputPkScript: stakingOutputPkScript,
stakingTime: stakingTime,
stakingValue: stakingValue,
fpBtcPks: fpBtcPks,
requiredDepthOnBtcChain: confirmationTimeBlocks,
pop: pop,
watchTxData: &watchTxDataCmd{
slashingTx: slashingTx,
slashingTxSig: slashingTxSignature,
stakerBabylonAddr: stakerBabylonAddr,
stakerBtcPk: stakerBtcPk,
unbondingTx: unbondingTx,
slashUnbondingTx: slashUnbondingTx,
slashUnbondingTxSig: slashUnbondingTxSig,
unbondingTime: unbondingTime,
},
errChan: make(chan error, 1),
successChan: make(chan *chainhash.Hash, 1),
}
}

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

func (event *stakingRequestCmd) EventDesc() string {
return "STAKING_REQUESTED_CMD"
}
Loading

0 comments on commit 16c8dd0

Please sign in to comment.