Skip to content

Commit

Permalink
Added moving funds commitment implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Dec 1, 2023
1 parent 13e1044 commit 6ff4af7
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 1 deletion.
53 changes: 53 additions & 0 deletions pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,37 @@ func (tc *TbtcChain) GetWalletLock(
return time.Unix(int64(lock.ExpiresAt), 0), cause, nil
}

func (tc *TbtcChain) GetWalletParameters() (
creationPeriod uint32,
creationMinBtcBalance uint64,
creationMaxBtcBalance uint64,
closureMinBtcBalance uint64,
maxAge uint32,
maxBtcTransfer uint64,
closingPeriod uint32,
err error,
) {
parameters, callErr := tc.bridge.WalletParameters()
if callErr != nil {
err = callErr
return
}

creationPeriod = parameters.WalletCreationPeriod
creationMinBtcBalance = parameters.WalletCreationMinBtcBalance
creationMaxBtcBalance = parameters.WalletCreationMaxBtcBalance
closureMinBtcBalance = parameters.WalletClosureMinBtcBalance
maxAge = parameters.WalletMaxAge
maxBtcTransfer = parameters.WalletMaxBtcTransfer
closingPeriod = parameters.WalletClosingPeriod

return
}

func (tc *TbtcChain) GetLiveWalletsCount() (uint32, error) {
return tc.bridge.LiveWalletsCount()
}

func parseWalletActionType(value uint8) (tbtc.WalletActionType, error) {
switch value {
case 0:
Expand Down Expand Up @@ -1824,6 +1855,28 @@ func (tc *TbtcChain) OnRedemptionProposalSubmitted(
OnEvent(onEvent)
}

func (tc *TbtcChain) SubmitMovingFundsCommitment(
walletPublicKeyHash [20]byte,
walletMainUTXO bitcoin.UnspentTransactionOutput,
walletMembersIDs []uint32,
walletMemberIndex uint32,
targetWallets [][20]byte,
) error {
mainUtxo := tbtcabi.BitcoinTxUTXO{
TxHash: walletMainUTXO.Outpoint.TransactionHash,
TxOutputIndex: walletMainUTXO.Outpoint.OutputIndex,
TxOutputValue: uint64(walletMainUTXO.Value),
}
_, err := tc.bridge.SubmitMovingFundsCommitment(
walletPublicKeyHash,
mainUtxo,
walletMembersIDs,
big.NewInt(int64(walletMemberIndex)),
targetWallets,
)
return err
}

func (tc *TbtcChain) ValidateRedemptionProposal(
proposal *tbtc.RedemptionProposal,
) error {
Expand Down
33 changes: 33 additions & 0 deletions pkg/tbtc/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,22 @@ type BridgeChain interface {
// if the wallet was not found.
GetWallet(walletPublicKeyHash [20]byte) (*WalletChainData, error)

// GetWalletParameters gets the current value of parameters relevant to
// wallet.
GetWalletParameters() (
creationPeriod uint32,
creationMinBtcBalance uint64,
creationMaxBtcBalance uint64,
closureMinBtcBalance uint64,
maxAge uint32,
maxBtcTransfer uint64,
closingPeriod uint32,
err error,
)

// GetLiveWalletsCount gets the current count of live wallets.
GetLiveWalletsCount() (uint32, error)

// ComputeMainUtxoHash computes the hash of the provided main UTXO
// according to the on-chain Bridge rules.
ComputeMainUtxoHash(mainUtxo *bitcoin.UnspentTransactionOutput) [32]byte
Expand Down Expand Up @@ -221,6 +237,14 @@ type BridgeChain interface {
fundingTxHash bitcoin.Hash,
fundingOutputIndex uint32,
) (*DepositChainRequest, bool, error)

// PastNewWalletRegisteredEvents fetches past new wallet registered events
// according to the provided filter or unfiltered if the filter is nil. Returned
// events are sorted by the block number in the ascending order, i.e. the
// latest event is at the end of the slice.
PastNewWalletRegisteredEvents(
filter *NewWalletRegisteredEventFilter,
) ([]*NewWalletRegisteredEvent, error)
}

// NewWalletRegisteredEvent represents a new wallet registered event.
Expand Down Expand Up @@ -343,6 +367,15 @@ type WalletCoordinatorChain interface {
func(event *RedemptionProposalSubmittedEvent),
) subscription.EventSubscription

// Submits the moving funds target wallets commitment.
SubmitMovingFundsCommitment(
walletPublicKeyHash [20]byte,
walletMainUTXO bitcoin.UnspentTransactionOutput,
walletMembersIDs []uint32,
walletMemberIndex uint32,
targetWallets [][20]byte,
) error

// ValidateDepositSweepProposal validates the given deposit sweep proposal
// against the chain. It requires some additional data about the deposits
// that must be fetched externally. Returns an error if the proposal is
Expand Down
33 changes: 33 additions & 0 deletions pkg/tbtc/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,12 @@ func (lc *localChain) GetDepositRequest(
panic("not supported")
}

func (lc *localChain) PastNewWalletRegisteredEvents(
filter *NewWalletRegisteredEventFilter,
) ([]*NewWalletRegisteredEvent, error) {
panic("not supported")
}

func (lc *localChain) setPendingRedemptionRequest(
walletPublicKeyHash [20]byte,
request *RedemptionRequest,
Expand Down Expand Up @@ -683,6 +689,23 @@ func (lc *localChain) GetWalletLock(walletPublicKeyHash [20]byte) (
panic("unsupported")
}

func (lc *localChain) GetWalletParameters() (
creationPeriod uint32,
creationMinBtcBalance uint64,
creationMaxBtcBalance uint64,
closureMinBtcBalance uint64,
maxAge uint32,
maxBtcTransfer uint64,
closingPeriod uint32,
err error,
) {
panic("unsupported")
}

func (lc *localChain) GetLiveWalletsCount() (uint32, error) {
panic("unsupported")
}

func (lc *localChain) ValidateDepositSweepProposal(
proposal *DepositSweepProposal,
depositsExtraInfo []struct {
Expand Down Expand Up @@ -773,6 +796,16 @@ func (lc *localChain) OnRedemptionProposalSubmitted(
panic("unsupported")
}

func (lc *localChain) SubmitMovingFundsCommitment(
walletPublicKeyHash [20]byte,
walletMainUTXO bitcoin.UnspentTransactionOutput,
walletMembersIDs []uint32,
walletMemberIndex uint32,
targetWallets [][20]byte,
) error {
panic("unsupported")
}

func (lc *localChain) ValidateRedemptionProposal(
proposal *RedemptionProposal,
) error {
Expand Down
5 changes: 5 additions & 0 deletions pkg/tbtc/moving_funds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tbtc

type MovingFundsProposal struct {
WalletPublicKeyHash [20]byte
}
Loading

0 comments on commit 6ff4af7

Please sign in to comment.