From 32c82f0e675de367903b30b35b82364377614bb4 Mon Sep 17 00:00:00 2001 From: Zsolt Felfoldi Date: Tue, 17 Sep 2024 09:56:48 +0200 Subject: [PATCH] core/filtermaps: add filtermaps tests --- core/filtermaps/filtermaps_test.go | 112 +++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 core/filtermaps/filtermaps_test.go diff --git a/core/filtermaps/filtermaps_test.go b/core/filtermaps/filtermaps_test.go new file mode 100644 index 000000000000..70a4ce3b14ca --- /dev/null +++ b/core/filtermaps/filtermaps_test.go @@ -0,0 +1,112 @@ +package filtermaps + +import ( + "math/rand" + "testing" + + "github.com/ethereum/go-ethereum/common" +) + +func TestSingleMatch(t *testing.T) { + for count := 0; count < 100000; count++ { + // generate a row with a single random entry + mapIndex := rand.Uint32() + lvIndex := uint64(mapIndex)< 0; i-- { + j := rand.Intn(i) + row[i], row[j] = row[j], row[i] + } + // check retrieved matches while also counting false positives + for i, lvHash := range lvHashes { + matches := row.potentialMatches(mapIndex, lvHash) + if i < testPmLen { + // check single entry match + if len(matches) < 1 { + t.Fatalf("Invalid length of matches (got %d, expected >=1)", len(matches)) + } + var found bool + for _, lvi := range matches { + if lvi == lvIndices[i] { + found = true + } else { + falsePositives++ + } + } + if !found { + t.Fatalf("Expected match not found (got %v, expected %d)", matches, lvIndices[i]) + } + } else { + // check "long series" match + if len(matches) < testPmLen { + t.Fatalf("Invalid length of matches (got %d, expected >=%d)", len(matches), testPmLen) + } + // since results are ordered, first testPmLen entries should always match exactly + for j := 0; j < testPmLen; j++ { + if matches[j] != lvStart+uint64(j) { + t.Fatalf("Incorrect match at index %d (got %d, expected %d)", j, matches[j], lvStart+uint64(j)) + } + } + // the rest are false positives + falsePositives += len(matches) - testPmLen + } + } + } + // Whenever looking for a certain log value hash, each entry in the row that + // was generated by another log value hash (a "foreign entry") has an + // 1 / valuesPerMap chance of yielding a false positive. + // We have testPmLen unique hash entries and a testPmLen long series of entries + // for the same hash. For each of the testPmLen unique hash entries there are + // testPmLen*2-1 foreign entries while for the long series there are testPmLen + // foreign entries. This means that after performing all these filtering runs, + // we have processed 2*testPmLen^2 foreign entries, which given us an estimate + // of how many false positives to expect. + expFalse := testPmCount * testPmLen * testPmLen * 2 / valuesPerMap + if falsePositives < expFalse/2 || falsePositives > expFalse*3/2 { + t.Fatalf("False positive rate out of expected range (got %d, expected %d +-50%%)", falsePositives, expFalse) + } +}