Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/mnasr-nit-4757.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Fixed
- Add some micro-optimization to hashing of address filter implementation.
3 changes: 2 additions & 1 deletion execution/gethexec/addressfilter/address_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ func TestHashedAddressCheckerHeavy(t *testing.T) {
filteredAddrs := make([]common.Address, filteredCount)
filteredHashes := make([]common.Hash, filteredCount)

hashPrefix := GetHashInputPrefix(salt)
for i := range filteredAddrs {
addr := common.BytesToAddress([]byte{byte(i + 1)})
filteredAddrs[i] = addr
filteredHashes[i] = HashWithSalt(salt, addr)
filteredHashes[i] = HashWithPrefix(hashPrefix, addr)
}

store := NewHashStore(cacheSize)
Expand Down
35 changes: 20 additions & 15 deletions execution/gethexec/addressfilter/hash_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (
// Once created, this struct is never modified, making it safe for concurrent reads.
// The cache is included here so it gets swapped atomically with the hash data.
type hashData struct {
salt uuid.UUID
hashes map[common.Hash]struct{}
digest string
loadedAt time.Time
cache *lru.Cache[common.Address, bool] // LRU cache for address lookup results
salt uuid.UUID
hashInputPrefix string
hashes map[common.Hash]struct{}
digest string
loadedAt time.Time
cache *lru.Cache[common.Address, bool] // LRU cache for address lookup results
}

// HashStore provides thread-safe access to restricted address hashes.
Expand All @@ -34,10 +35,13 @@ type HashStore struct {
cacheSize int
}

func HashWithSalt(salt uuid.UUID, address common.Address) common.Hash {
hashInput := salt.String() + "::0x" + common.Bytes2Hex(address.Bytes())
saltedHash := sha256.Sum256([]byte(hashInput))
return saltedHash
func HashWithPrefix(prefix string, address common.Address) common.Hash {
hashInput := prefix + common.Bytes2Hex(address.Bytes())
return sha256.Sum256([]byte(hashInput))
}

func GetHashInputPrefix(salt uuid.UUID) string {
return salt.String() + "::0x"
}

func NewHashStore(cacheSize int) *HashStore {
Expand All @@ -56,11 +60,12 @@ func NewHashStore(cacheSize int) *HashStore {
// A new LRU cache is created for the new data, ensuring atomic consistency.
func (h *HashStore) Store(salt uuid.UUID, hashes []common.Hash, digest string) {
newData := &hashData{
salt: salt,
hashes: make(map[common.Hash]struct{}, len(hashes)),
digest: digest,
loadedAt: time.Now(),
cache: lru.NewCache[common.Address, bool](h.cacheSize),
salt: salt,
hashInputPrefix: GetHashInputPrefix(salt),
hashes: make(map[common.Hash]struct{}, len(hashes)),
digest: digest,
loadedAt: time.Now(),
cache: lru.NewCache[common.Address, bool](h.cacheSize),
}
for _, hash := range hashes {
newData.hashes[hash] = struct{}{}
Expand All @@ -81,7 +86,7 @@ func (h *HashStore) IsRestricted(addr common.Address) bool {
if restricted, ok := data.cache.Get(addr); ok {
return restricted
}
_, restricted := data.hashes[HashWithSalt(data.salt, addr)]
_, restricted := data.hashes[HashWithPrefix(data.hashInputPrefix, addr)]
// Cache the result
data.cache.Add(addr, restricted)
return restricted
Expand Down
14 changes: 7 additions & 7 deletions execution/gethexec/addressfilter/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestHashStore_AtomicSwap(t *testing.T) {

salt1, _ := uuid.Parse("3ccf0cbf-b23f-47ba-9c2f-4e7bd672b4c7")
addr1 := common.HexToAddress("0x1111111111111111111111111111111111111111")
hash1 := HashWithSalt(salt1, addr1)
hash1 := HashWithPrefix(GetHashInputPrefix(salt1), addr1)

// Store first set
store.Store(salt1, []common.Hash{hash1}, "etag1")
Expand All @@ -87,7 +87,7 @@ func TestHashStore_AtomicSwap(t *testing.T) {
// Store second set with different salt (simulating hourly rotation)
salt2, _ := uuid.Parse("2cef04bf-b23f-47ba-9c2f-4e7bd652c1c6")
addr2 := common.HexToAddress("0x2222222222222222222222222222222222222222")
hash2 := HashWithSalt(salt2, addr2)
hash2 := HashWithPrefix(GetHashInputPrefix(salt2), addr2)

store.Store(salt2, []common.Hash{hash2}, "etag2")

Expand Down Expand Up @@ -115,7 +115,7 @@ func TestHashStore_ConcurrentAccess(t *testing.T) {
addr := common.BigToAddress(common.Big1)
addr[18] = byte(i)
addresses = append(addresses, addr)
hash := HashWithSalt(salt1, addr)
hash := HashWithPrefix(GetHashInputPrefix(salt1), addr)
hashes1 = append(hashes1, hash)
}
store.Store(salt1, hashes1, "etag")
Expand All @@ -128,7 +128,7 @@ func TestHashStore_ConcurrentAccess(t *testing.T) {
addr := common.BigToAddress(common.Big2)
addr[18] = byte(i)
addresses2 = append(addresses2, addr)
hash := HashWithSalt(salt2, addr)
hash := HashWithPrefix(GetHashInputPrefix(salt2), addr)
hashes2 = append(hashes2, hash)
}

Expand Down Expand Up @@ -378,7 +378,7 @@ func TestHashStore_CustomCacheSize(t *testing.T) {
// Pre-compute hashes
hashes := make([]common.Hash, 0, len(addresses))
for _, addr := range addresses {
hash := HashWithSalt(salt, addr)
hash := HashWithPrefix(GetHashInputPrefix(salt), addr)
hashes = append(hashes, hash)
}

Expand Down Expand Up @@ -435,7 +435,7 @@ func (h *HashStore) isAllRestricted(addrs []common.Address) bool {
continue
}

hash := HashWithSalt(data.salt, addr)
hash := HashWithPrefix(data.hashInputPrefix, addr)
_, restricted := data.hashes[hash]
data.cache.Add(addr, restricted)
if !restricted {
Expand All @@ -461,7 +461,7 @@ func (h *HashStore) isAnyRestricted(addrs []common.Address) bool {
continue
}

hash := HashWithSalt(data.salt, addr)
hash := HashWithPrefix(data.hashInputPrefix, addr)
_, restricted := data.hashes[hash]
data.cache.Add(addr, restricted)
if restricted {
Expand Down
3 changes: 2 additions & 1 deletion system_tests/tx_address_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func newHashedChecker(addrs []common.Address) *addressfilter.HashedAddressChecke
if len(addrs) > 0 {
salt, _ := uuid.Parse("3ccf0cbf-b23f-47ba-9c2f-4e7bd672b4c7")
hashes := make([]common.Hash, len(addrs))
hashPrefix := addressfilter.GetHashInputPrefix(salt)
for i, addr := range addrs {
hashes[i] = addressfilter.HashWithSalt(salt, addr)
hashes[i] = addressfilter.HashWithPrefix(hashPrefix, addr)
}
store.Store(salt, hashes, "test")
}
Expand Down
Loading