2014-11-18 07:58:22 -08:00
|
|
|
package types
|
2014-10-27 03:22:27 -07:00
|
|
|
|
2014-10-29 02:34:40 -07:00
|
|
|
import (
|
|
|
|
"math/big"
|
2014-10-27 03:22:27 -07:00
|
|
|
|
2015-03-16 03:27:38 -07:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-03-16 09:27:23 -07:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2014-10-31 06:43:14 -07:00
|
|
|
"github.com/ethereum/go-ethereum/state"
|
2014-10-29 02:34:40 -07:00
|
|
|
)
|
|
|
|
|
2015-03-16 09:27:23 -07:00
|
|
|
func CreateBloom(receipts Receipts) Bloom {
|
2014-10-29 02:34:40 -07:00
|
|
|
bin := new(big.Int)
|
2014-11-14 05:17:54 -08:00
|
|
|
for _, receipt := range receipts {
|
2014-12-03 03:21:12 -08:00
|
|
|
bin.Or(bin, LogsBloom(receipt.logs))
|
2014-10-27 03:22:27 -07:00
|
|
|
}
|
|
|
|
|
2015-03-16 09:27:23 -07:00
|
|
|
return BytesToBloom(bin.Bytes())
|
2014-10-27 03:22:27 -07:00
|
|
|
}
|
|
|
|
|
2014-12-03 03:21:12 -08:00
|
|
|
func LogsBloom(logs state.Logs) *big.Int {
|
2014-10-29 02:34:40 -07:00
|
|
|
bin := new(big.Int)
|
2014-10-27 03:22:27 -07:00
|
|
|
for _, log := range logs {
|
2014-12-05 03:32:47 -08:00
|
|
|
data := make([][]byte, len(log.Topics())+1)
|
|
|
|
data[0] = log.Address()
|
|
|
|
|
|
|
|
for i, topic := range log.Topics() {
|
|
|
|
data[i+1] = topic
|
2014-10-27 03:22:27 -07:00
|
|
|
}
|
2014-10-29 12:16:18 -07:00
|
|
|
|
2014-10-27 03:22:27 -07:00
|
|
|
for _, b := range data {
|
2015-03-16 03:27:38 -07:00
|
|
|
bin.Or(bin, common.BigD(bloom9(crypto.Sha3(b)).Bytes()))
|
2014-10-27 03:22:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bin
|
|
|
|
}
|
|
|
|
|
2014-10-29 02:34:40 -07:00
|
|
|
func bloom9(b []byte) *big.Int {
|
|
|
|
r := new(big.Int)
|
2015-03-03 02:56:12 -08:00
|
|
|
|
|
|
|
for i := 0; i < 16; i += 2 {
|
2014-10-29 02:34:40 -07:00
|
|
|
t := big.NewInt(1)
|
2015-03-03 02:56:12 -08:00
|
|
|
b := uint(b[i+1]) + 1024*(uint(b[i])&1)
|
2014-11-03 16:29:49 -08:00
|
|
|
r.Or(r, t.Lsh(t, b))
|
2014-10-27 03:22:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
2014-10-29 12:16:18 -07:00
|
|
|
|
|
|
|
func BloomLookup(bin, topic []byte) bool {
|
2015-03-16 03:27:38 -07:00
|
|
|
bloom := common.BigD(bin)
|
2014-11-09 16:17:31 -08:00
|
|
|
cmp := bloom9(crypto.Sha3(topic))
|
2014-10-29 12:16:18 -07:00
|
|
|
|
|
|
|
return bloom.And(bloom, cmp).Cmp(cmp) == 0
|
|
|
|
}
|