From cbd5bf87d03e051b5d8720a7d51e76622959921d Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Fri, 15 Mar 2024 14:09:16 +0100 Subject: [PATCH] Used map for efficient storing of hashes --- pkg/maintainer/spv/moved_funds_sweep.go | 18 +++++++++--------- pkg/maintainer/spv/moved_funds_sweep_test.go | 7 +++++++ pkg/maintainer/spv/spv.go | 19 ------------------- 3 files changed, 16 insertions(+), 28 deletions(-) diff --git a/pkg/maintainer/spv/moved_funds_sweep.go b/pkg/maintainer/spv/moved_funds_sweep.go index e38c666d83..5ba49bbe79 100644 --- a/pkg/maintainer/spv/moved_funds_sweep.go +++ b/pkg/maintainer/spv/moved_funds_sweep.go @@ -165,20 +165,20 @@ func getUnprovenMovedFundsSweepTransactions( } // Any wallet that was among target wallets recently could have created - // a moved funds sweep transaction. - targetWallets := [][20]byte{} + // a moved funds sweep transaction. One target wallet can appear in multiple + // events as multiple source wallets can use the same target wallets. Store + // wallet public key hashes in a map to get rid of duplicates. + walletPublicKeyHashes := make(map[[20]byte]bool) + for _, event := range events { - targetWallets = append(targetWallets, event.TargetWallets...) + for _, targetWallet := range event.TargetWallets { + walletPublicKeyHashes[targetWallet] = true + } } - // Some target wallets may appear on the list multiple times. It can happen - // if multiple source wallets used the same target wallet. Make a list - // of unique wallets. - walletPublicKeyHashes := uniqueKeyHashes(targetWallets) - unprovenMovedFundsSweepTransactions := []*bitcoin.Transaction{} - for _, walletPublicKeyHash := range walletPublicKeyHashes { + for walletPublicKeyHash := range walletPublicKeyHashes { wallet, err := spvChain.GetWallet(walletPublicKeyHash) if err != nil { return nil, fmt.Errorf("failed to get wallet: [%v]", err) diff --git a/pkg/maintainer/spv/moved_funds_sweep_test.go b/pkg/maintainer/spv/moved_funds_sweep_test.go index f52b449a53..01e65700bd 100644 --- a/pkg/maintainer/spv/moved_funds_sweep_test.go +++ b/pkg/maintainer/spv/moved_funds_sweep_test.go @@ -3,6 +3,7 @@ package spv import ( "encoding/hex" "fmt" + "sort" "testing" "github.com/go-test/deep" @@ -372,6 +373,12 @@ func TestGetUnprovenMovedFundsSweepTransactions(t *testing.T) { wallets[2].transactions[0].Hash(), // Wallet 3 - Transaction 1 } + // The order of returned transaction hashes is random. Sort them before + // comapring. + sort.Slice(transactionsHashes, func(i, j int) bool { + return transactionsHashes[i].String() < transactionsHashes[j].String() + }) + if diff := deep.Equal(expectedTransactionsHashes, transactionsHashes); diff != nil { t.Errorf("invalid unproven transaction hashes: %v", diff) } diff --git a/pkg/maintainer/spv/spv.go b/pkg/maintainer/spv/spv.go index 9701c57d7f..ef1bef69e2 100644 --- a/pkg/maintainer/spv/spv.go +++ b/pkg/maintainer/spv/spv.go @@ -457,25 +457,6 @@ func uniqueWalletPublicKeyHashes[T walletEvent](events []T) [][20]byte { return publicKeyHashes } -// uniqueKeyHashes parses the list of 20-byte-long key hashes and returns a list -// of unique key hashes. -func uniqueKeyHashes(keyHashes [][20]byte) [][20]byte { - cache := make(map[string]struct{}) - var unique [][20]byte - - for _, keyHash := range keyHashes { - strKey := hex.EncodeToString(keyHash[:]) - - // Check for uniqueness - if _, exists := cache[strKey]; !exists { - cache[strKey] = struct{}{} - unique = append(unique, keyHash) - } - } - - return unique -} - // spvProofAssembler is a type representing a function that is used // to assemble an SPV proof for the given transaction hash and confirmations // count.