Skip to content

Commit

Permalink
Simplified wallet archiving
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Jun 11, 2024
1 parent ce85a89 commit 88f3bf6
Showing 1 changed file with 16 additions and 43 deletions.
59 changes: 16 additions & 43 deletions pkg/tbtc/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -1097,59 +1097,32 @@ func processCoordinationResult(node *node, result *coordinationResult) {

// archiveClosedWallets archives closed or terminated wallets.
func (n *node) archiveClosedWallets() error {
getClosedWallets := func(walletPublicKeyHashes [][20]byte) (
closedWallets [][20]byte,
) {
for _, walletPublicKeyHash := range walletPublicKeyHashes {
walletChainData, err := n.chain.GetWallet(walletPublicKeyHash)
if err != nil {
// Continue if there was an error getting wallet data. Try to
// get as many closed wallets as possible.
logger.Errorf(
"could not get wallet data for wallet [0x%x]: [%v]",
walletPublicKeyHash,
err,
)
continue
}

if walletChainData.State == StateClosed ||
walletChainData.State == StateTerminated {
closedWallets = append(closedWallets, walletPublicKeyHash)
}
}

return
}

// Get all the wallets controlled by the node.
walletPublicKeys := n.walletRegistry.getWalletsPublicKeys()

walletPublicKeyHashes := [][20]byte{}
for _, walletPublicKey := range walletPublicKeys {
walletPublicKeyHashes = append(
walletPublicKeyHashes,
bitcoin.PublicKeyHash(walletPublicKey),
)
}

// Find the wallets that are closed.
closedWallets := getClosedWallets(walletPublicKeyHashes)
if len(closedWallets) == 0 {
logger.Infof("there are no closed wallets to archive")
return nil
}
walletPublicKeyHash := bitcoin.PublicKeyHash(walletPublicKey)

// Archive the closed wallets.
for _, walletPublicKeyHash := range closedWallets {
err := n.walletRegistry.archiveWallet(walletPublicKeyHash)
walletChainData, err := n.chain.GetWallet(walletPublicKeyHash)
if err != nil {
return fmt.Errorf(
"could not archive wallet with public key hash [0x%x]: [%v]",
"could not get wallet data for wallet [0x%x]: [%v]",
walletPublicKeyHash,
err,
)
} else {
}

if walletChainData.State == StateClosed ||
walletChainData.State == StateTerminated {
err := n.walletRegistry.archiveWallet(walletPublicKeyHash)
if err != nil {
return fmt.Errorf(
"could not archive wallet with public key hash [0x%x]: [%v]",
walletPublicKeyHash,
err,
)
}

logger.Infof(
"successfully archived wallet with public key hash [0x%x]",
walletPublicKeyHash,
Expand Down

0 comments on commit 88f3bf6

Please sign in to comment.