From 849019d97919173bc09ea2abeaaedd5e1e097227 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Thu, 6 Jun 2024 16:42:42 +0200 Subject: [PATCH] Added wallet archiving --- pkg/tbtc/registry.go | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/pkg/tbtc/registry.go b/pkg/tbtc/registry.go index 80334f7475..6f469862de 100644 --- a/pkg/tbtc/registry.go +++ b/pkg/tbtc/registry.go @@ -156,6 +156,45 @@ func (wr *walletRegistry) getWalletByPublicKeyHash( return wallet{}, false } +func (wr *walletRegistry) archiveWallet( + walletPublicKeyHash [20]byte, +) error { + wr.mutex.Lock() + defer wr.mutex.Unlock() + + var walletPublicKey *ecdsa.PublicKey + + for _, value := range wr.walletCache { + if value.walletPublicKeyHash == walletPublicKeyHash { + // All signers belong to one wallet. Take the wallet public key from + // the first signer. + walletPublicKey = value.signers[0].wallet.publicKey + } + } + + if walletPublicKey == nil { + logger.Infof( + "node does not control wallet with public key hash [0x%x]; "+ + "quitting wallet archiving", + walletPublicKeyHash, + ) + return nil + } + + walletStorageKey := getWalletStorageKey(walletPublicKey) + + // Archive the entire wallet storage. + err := wr.walletStorage.archiveWallet(walletStorageKey) + if err != nil { + return fmt.Errorf("could not archive wallet: [%v]", err) + } + + // Remove the wallet from the wallet cache. + delete(wr.walletCache, walletStorageKey) + + return nil +} + // walletStorage is the component that persists data of the wallets managed // by the given node using the underlying persistence layer. It should be // used directly only by the walletRegistry. @@ -194,6 +233,19 @@ func (ws *walletStorage) saveSigner(signer *signer) error { return nil } +func (ws *walletStorage) archiveWallet(walletStoragePath string) error { + err := ws.persistence.Archive(walletStoragePath) + if err != nil { + return fmt.Errorf( + "could not archive wallet storage using the "+ + "underlying persistence layer: [%w]", + err, + ) + } + + return nil +} + // loadSigners loads all signers stored using the underlying persistence layer. // This function should not be called from any other place than walletRegistry. func (ws *walletStorage) loadSigners() map[string][]*signer {