mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
990 B
56 lines
990 B
10 years ago
|
package chain
|
||
10 years ago
|
|
||
10 years ago
|
import (
|
||
|
"math/big"
|
||
10 years ago
|
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/ethstate"
|
||
10 years ago
|
"github.com/ethereum/go-ethereum/ethutil"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
func CreateBloom(block *Block) []byte {
|
||
10 years ago
|
bin := new(big.Int)
|
||
10 years ago
|
bin.Or(bin, bloom9(block.Coinbase))
|
||
10 years ago
|
for _, receipt := range block.Receipts() {
|
||
|
bin.Or(bin, LogsBloom(receipt.logs))
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
return bin.Bytes()
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
func LogsBloom(logs ethstate.Logs) *big.Int {
|
||
10 years ago
|
bin := new(big.Int)
|
||
10 years ago
|
for _, log := range logs {
|
||
10 years ago
|
data := [][]byte{log.Address}
|
||
10 years ago
|
for _, topic := range log.Topics {
|
||
10 years ago
|
data = append(data, topic)
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
if log.Data != nil {
|
||
|
data = append(data, log.Data)
|
||
|
}
|
||
10 years ago
|
|
||
|
for _, b := range data {
|
||
10 years ago
|
bin.Or(bin, bloom9(b))
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return bin
|
||
|
}
|
||
|
|
||
10 years ago
|
func bloom9(b []byte) *big.Int {
|
||
|
r := new(big.Int)
|
||
10 years ago
|
for _, i := range []int{0, 2, 4} {
|
||
10 years ago
|
t := big.NewInt(1)
|
||
10 years ago
|
r.Or(r, t.Lsh(t, uint(b[i+1])+256*(uint(b[i])&1)))
|
||
10 years ago
|
}
|
||
|
|
||
|
return r
|
||
|
}
|
||
10 years ago
|
|
||
|
func BloomLookup(bin, topic []byte) bool {
|
||
|
bloom := ethutil.BigD(bin)
|
||
|
cmp := bloom9(topic)
|
||
|
|
||
|
return bloom.And(bloom, cmp).Cmp(cmp) == 0
|
||
|
}
|