diff options
Diffstat (limited to 'ethchain/bloom9.go')
-rw-r--r-- | ethchain/bloom9.go | 47 |
1 files changed, 32 insertions, 15 deletions
diff --git a/ethchain/bloom9.go b/ethchain/bloom9.go index 65be6c7a2..4028231a3 100644 --- a/ethchain/bloom9.go +++ b/ethchain/bloom9.go @@ -1,38 +1,55 @@ package ethchain -import "github.com/ethereum/go-ethereum/vm" - -func CreateBloom(txs Transactions) uint64 { - var bin uint64 - for _, tx := range txs { - bin |= logsBloom(tx.logs) +import ( + "math/big" + + "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/vm" +) + +func CreateBloom(block *Block) []byte { + bin := new(big.Int) + bin.Or(bin, bloom9(block.Coinbase)) + for _, tx := range block.Transactions() { + bin.Or(bin, LogsBloom(tx.logs)) } - return bin + return bin.Bytes() } -func logsBloom(logs []vm.Log) uint64 { - var bin uint64 +func LogsBloom(logs []vm.Log) *big.Int { + bin := new(big.Int) for _, log := range logs { data := [][]byte{log.Address} for _, topic := range log.Topics { - data = append(data, topic.Bytes()) + data = append(data, topic) + } + + if log.Data != nil { + data = append(data, log.Data) } - data = append(data, log.Data) for _, b := range data { - bin |= bloom9(b) + bin.Or(bin, bloom9(b)) } } return bin } -func bloom9(b []byte) uint64 { - var r uint64 +func bloom9(b []byte) *big.Int { + r := new(big.Int) for _, i := range []int{0, 2, 4} { - r |= 1 << (uint64(b[i+1]) + 256*(uint64(b[i])&1)) + t := big.NewInt(1) + r.Or(r, t.Lsh(t, uint(b[i+1])+256*(uint(b[i])&1))) } return r } + +func BloomLookup(bin, topic []byte) bool { + bloom := ethutil.BigD(bin) + cmp := bloom9(topic) + + return bloom.And(bloom, cmp).Cmp(cmp) == 0 +} |