Skip to content

Commit

Permalink
Added waiting for wallet closure confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Jun 4, 2024
1 parent ed6b426 commit 4e19c4d
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/chain/ethereum/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,21 @@ func (tc *TbtcChain) PastNewWalletRegisteredEvents(
return convertedEvents, err
}

func (tc *TbtcChain) IsWalletRegistered(EcdsaWalletID [32]byte) (bool, error) {
isWalletRegistered, err := tc.walletRegistry.IsWalletRegistered(
EcdsaWalletID,
)
if err != nil {
return false, fmt.Errorf(
"cannot check if wallet with ECDSA ID [0x%x] is registered: [%v]",
EcdsaWalletID,
err,
)
}

return isWalletRegistered, nil
}

func (tc *TbtcChain) GetWallet(
walletPublicKeyHash [20]byte,
) (*tbtc.WalletChainData, error) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/tbtc/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ type WalletClosedEvent struct {
// BridgeChain defines the subset of the TBTC chain interface that pertains
// specifically to the tBTC Bridge operations.
type BridgeChain interface {
IsWalletRegistered(EcdsaWalletID [32]byte) (bool, error)

// GetWallet gets the on-chain data for the given wallet. Returns an error
// if the wallet was not found.
GetWallet(walletPublicKeyHash [20]byte) (*WalletChainData, error)
Expand Down
4 changes: 4 additions & 0 deletions pkg/tbtc/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ func buildDepositRequestKey(
return sha256.Sum256(append(fundingTxHash[:], buffer...))
}

func (lc *localChain) IsWalletRegistered(EcdsaWalletID [32]byte) (bool, error) {
panic("unsupported")
}

func (lc *localChain) GetWallet(walletPublicKeyHash [20]byte) (
*WalletChainData,
error,
Expand Down
56 changes: 56 additions & 0 deletions pkg/tbtc/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"math/big"
"sync"

"github.com/keep-network/keep-common/pkg/chain/ethereum"
"github.com/keep-network/keep-core/pkg/bitcoin"
"github.com/keep-network/keep-core/pkg/chain"

Expand Down Expand Up @@ -38,6 +39,12 @@ const (
// Moreover, the signature must be produced in the reasonable time.
// That being said, the value `5` seems to be reasonable trade-off.
signingAttemptsLimit = 5

// walletClosureConfirmationBlocks determines the period used when waiting
// for the wallet closure confirmation. This period ensures the wallet has
// been definitely closed and the closing transaction will not be removed by
// a chain reorganization.
walletClosureConfirmationBlocks = 32
)

// TODO: Unit tests for `node.go`.
Expand Down Expand Up @@ -1075,6 +1082,55 @@ func processCoordinationResult(node *node, result *coordinationResult) {
}
}

// handleWalletClosure handles the wallet termination or closing process.
func (n *node) handleWalletClosure(walletID [32]byte) error {
blockCounter, err := n.chain.BlockCounter()
if err != nil {
return fmt.Errorf("error getting block counter [%w]", err)
}

currentBlock, err := blockCounter.CurrentBlock()
if err != nil {
return fmt.Errorf("error getting current block [%w]", err)
}

// To verify there was no chain reorg and the wallet is really closed check
// if it is registered. Both terminated and closed wallets are removed
// from the ECDSA registry.
stateCheck := func() (bool, error) {
isRegistered, err := n.chain.IsWalletRegistered(walletID)
if err != nil {
return false, err
}

return !isRegistered, nil
}

// Wait a significant number of blocks to make sure the transaction has not
// been reverted for some reason, e.g. due to a chain reorganization.
result, err := ethereum.WaitForBlockConfirmations(
blockCounter,
currentBlock,
walletClosureConfirmationBlocks,
stateCheck,
)
if err != nil {
return fmt.Errorf(
"error while waiting for wallet closure confirmation [%w]",
err,
)
}

if !result {
return fmt.Errorf("wallet closure not confirmed")
}

// TODO: Continue with wallet closure handling: save key material and remove
// the wallet from coordination mechanism.

return nil
}

// waitForBlockFn represents a function blocking the execution until the given
// block height.
type waitForBlockFn func(context.Context, uint64) error
Expand Down
16 changes: 16 additions & 0 deletions pkg/tbtc/tbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,22 @@ func Initialize(
}()
})

_ = chain.OnWalletClosed(func(event *WalletClosedEvent) {
go func() {
// TODO: Most likely event deduplication is needed.

logger.Infof(
"Wallet with ID [0x%x] has been closed at block [%v]",
event.WalletID,
event.BlockNumber,
)

node.handleWalletClosure(
event.WalletID,
)
}()
})

return nil
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/tbtcpg/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,10 @@ func (lc *LocalChain) SetAverageBlockTime(averageBlockTime time.Duration) {
lc.averageBlockTime = averageBlockTime
}

func (lc *LocalChain) IsWalletRegistered(EcdsaWalletID [32]byte) (bool, error) {
panic("unsupported")
}

func (lc *LocalChain) GetWallet(walletPublicKeyHash [20]byte) (
*tbtc.WalletChainData,
error,
Expand Down

0 comments on commit 4e19c4d

Please sign in to comment.