Skip to content

Commit

Permalink
Used map for efficient storing of hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszslabon committed Mar 15, 2024
1 parent 64485fc commit cbd5bf8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 28 deletions.
18 changes: 9 additions & 9 deletions pkg/maintainer/spv/moved_funds_sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions pkg/maintainer/spv/moved_funds_sweep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package spv
import (
"encoding/hex"
"fmt"
"sort"
"testing"

"github.com/go-test/deep"
Expand Down Expand Up @@ -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)
}
Expand Down
19 changes: 0 additions & 19 deletions pkg/maintainer/spv/spv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit cbd5bf8

Please sign in to comment.