How can make logsBloom in Ethereum?

April 11, 2024

If you were known well about blockchain, have you ever heard of "LogsBloom"? Maybe you aren't familiar with that data. But when you want to find something which are a transaction including specefic events, you will use this data. For deep dive to this, we should refer a data structure, "Bloom Filter".

Bloom Filter

Bloom Filter is a data structure which use probabilistic algorithm to find if a set include some element. Commonly, Bloom Filter is a bit array. To using Bloom Filter, you should decide two condition.

  • The number of bit in array for Bloom Filter
  • The number of hash function to record in bit array.

Controlling the size of these two options, you can make your Bloom Filter efficient to solve your problem.

By the Bloom Filter's features, it gurantes that any element you find don't persist in sets if sets really don't have the element. But You should check if the element is in sets when Bloom Filter tell you the element is in sets. because Bloom Filter made by probablistic logic. If results of hash functions by two element were same, those could be overlapped in Bloom Filter.

LogsBloom

LogsBloom is a kind of Bloom Filter. you can see the specfication in Ethereum Yellow Paper. But I handle it easily in this post as I understand.

Role

You can save time and cost by using LogsBloom to seek block having event you want. If there weren't LogsBloom in the block header, you should check all of transcations in block.

Spec

The LogsBloom has 2048 bit arrays. In transaction receipt, the value of field, LogsBloom, consists of 256 bytes with hex. As a Bloom Filter, it's values refer if there is the transaction you find. Ethereum client take a Concept of "Topic" to manage Event. Topic is a hashed value from Event declaration without argument's name or indexed argument value. It's also consisted of 256 bytes. So, when the indexed value don't have enough value for size, just filling 0 value in empty place. Following is Example.

There is a Event, 'Transfer(address indexed from,address indexed to, unit256 amount)'
Topic0 is, `Keccak256('Transfer(address,address,unit256)')`
Topic1 is, `keccak256( paddingZeroLeft(from) )`
Topic2 is, `keccak256( paddingZeroLeft(to) )`

Before detail description, I will show you the Geth code to create logsBloom.

func bloomValues(data []byte, hashbuf []byte) (uint, byte, uint, byte, uint, byte) {
	sha := hasherPool.Get().(crypto.KeccakState)
	sha.Reset()
	sha.Write(data)
	sha.Read(hashbuf)
	hasherPool.Put(sha)
	// The actual bits to flip
	v1 := byte(1 << (hashbuf[1] & 0x7))
	v2 := byte(1 << (hashbuf[3] & 0x7))
	v3 := byte(1 << (hashbuf[5] & 0x7))
	// The indices for the bytes to OR in
	i1 := BloomByteLength - uint((binary.BigEndian.Uint16(hashbuf)&0x7ff)>>3) - 1
	i2 := BloomByteLength - uint((binary.BigEndian.Uint16(hashbuf[2:])&0x7ff)>>3) - 1
	i3 := BloomByteLength - uint((binary.BigEndian.Uint16(hashbuf[4:])&0x7ff)>>3) - 1

	return i1, v1, i2, v2, i3, v3
}

LogsBloom has only one hash function, keccak256, which change Event into bit array. Ethereum client records this value in LogsBloom. Topic refered above must take one more chainging by keccak256. From this hashed value, the indices recorded in LogsBloom is derived. So, index 1, 3, 5 of hashed value will be parsed 3 bits by & 0x7 operations. That will be value in recorded in Logsbloom. And index 0-1, 2-3, 4-5 of hashed value will be parsed last 11 bits to decide index in which record the value in LogsBloom.

Conclusion

It makes me little annoying beacuase I first met binary in My developer life. But It's good experience to release my qurious and understand more about Ethereum. And I will notice my reference below. If you would see these reference, It would be good for your understanding about LogsBloom. Thanks!

Ref

StackOverFlow For Ethereum: How does Ethereum make use of bloom filters?

Ethereum under the hood- Part 8( Blocks -2 )

StackOverFlow For Ethereum: How can I parse an Ethereum block-level logsBloom?

How can make logsBloom in Ethereum?