Skip to content

Commit 2f29801

Browse files
authored
Don't cut bytes from non-address topics + account for empty topics (#108)
### TL;DR Improved handling of topic values in the `getTopicValueFormat` function. ### What changed? - Modified the `getTopicValueFormat` function to handle empty topic strings. - Updated the logic for processing non-empty topics: - Now uses `ethereum.FromHex` to convert the topic to bytes - ⚠️ prev. used `ethereum.HexToAddress` was wrong in this context cause topics are not necessarily addresses) - Ensures the byte slice is exactly 32 bytes by left-padding with zeros. - Converts the padded bytes to a hash and returns the hexadecimal representation. ### How to test? 1. Test the function with an empty string input to ensure it returns an empty string. 2. Test with various non-empty topic strings, including: - Short strings (less than 32 bytes) - Exactly 32-byte strings - Strings longer than 32 bytes 3. Verify that the output is always a 32-byte hexadecimal string (or empty for empty input). ### Why make this change? This change improves the robustness of topic value handling: - It correctly handles empty topics, which are stored as empty strings by the indexer. - It ensures consistent 32-byte padding for all non-empty topics, regardless of their original length. - This approach aligns better with Ethereum's topic encoding standards, potentially fixing issues related to topic matching or filtering.
2 parents 81d8f50 + a55695d commit 2f29801

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

internal/storage/clickhouse.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,16 @@ func addContractAddress(table, query string, contractAddress string) string {
456456
}
457457

458458
func getTopicValueFormat(topic string) string {
459-
toAddressHex := ethereum.HexToAddress(topic)
460-
toAddressPadded := ethereum.LeftPadBytes(toAddressHex.Bytes(), 32)
461-
toAddressTopic := ethereum.BytesToHash(toAddressPadded).Hex()
462-
return toAddressTopic
459+
if topic == "" {
460+
// if there is no indexed topic, indexer stores an empty string
461+
// we shouldn't pad and hexify such an argument then
462+
return ""
463+
}
464+
asBytes := ethereum.FromHex(topic)
465+
// ensure the byte slice is exactly 32 bytes by left-padding with zeros
466+
asPadded := ethereum.LeftPadBytes(asBytes, 32)
467+
result := ethereum.BytesToHash(asPadded).Hex()
468+
return result
463469
}
464470

465471
func (c *ClickHouseConnector) executeAggregateQuery(table string, qf QueryFilter) (map[string]string, error) {

0 commit comments

Comments
 (0)