bloom9.go 1.0 KB
Newer Older
1
package types
2

O
obscuren 已提交
3 4
import (
	"math/big"
5

O
obscuren 已提交
6
	"github.com/ethereum/go-ethereum/common"
O
obscuren 已提交
7
	"github.com/ethereum/go-ethereum/core/state"
O
obscuren 已提交
8
	"github.com/ethereum/go-ethereum/crypto"
O
obscuren 已提交
9 10
)

O
obscuren 已提交
11
func CreateBloom(receipts Receipts) Bloom {
O
obscuren 已提交
12
	bin := new(big.Int)
O
obscuren 已提交
13
	for _, receipt := range receipts {
O
obscuren 已提交
14
		bin.Or(bin, LogsBloom(receipt.logs))
15 16
	}

O
obscuren 已提交
17
	return BytesToBloom(bin.Bytes())
18 19
}

O
obscuren 已提交
20
func LogsBloom(logs state.Logs) *big.Int {
O
obscuren 已提交
21
	bin := new(big.Int)
22
	for _, log := range logs {
O
obscuren 已提交
23 24
		data := make([]common.Hash, len(log.Topics))
		bin.Or(bin, bloom9(log.Address.Bytes()))
25

O
obscuren 已提交
26
		for i, topic := range log.Topics {
O
obscuren 已提交
27
			data[i] = topic
28
		}
29

30
		for _, b := range data {
O
obscuren 已提交
31
			bin.Or(bin, bloom9(b[:]))
32 33 34 35 36 37
		}
	}

	return bin
}

O
obscuren 已提交
38
func bloom9(b []byte) *big.Int {
O
obscuren 已提交
39 40
	b = crypto.Sha3(b[:])

O
obscuren 已提交
41
	r := new(big.Int)
O
obscuren 已提交
42

O
bloom  
obscuren 已提交
43
	for i := 0; i < 6; i += 2 {
O
obscuren 已提交
44
		t := big.NewInt(1)
O
obscuren 已提交
45
		b := (uint(b[i+1]) + (uint(b[i]) << 8)) & 2047
O
obscuren 已提交
46
		r.Or(r, t.Lsh(t, b))
47 48 49 50
	}

	return r
}
51

O
obscuren 已提交
52 53
var Bloom9 = bloom9

O
obscuren 已提交
54 55
func BloomLookup(bin Bloom, topic common.Hash) bool {
	bloom := bin.Big()
O
obscuren 已提交
56
	cmp := bloom9(topic[:])
57 58 59

	return bloom.And(bloom, cmp).Cmp(cmp) == 0
}