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
|
|
|
|
2014-11-03 16:29:49 -08:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2014-10-29 12:39:01 -07:00
|
|
|
"github.com/ethereum/go-ethereum/ethutil"
|
2014-10-31 06:43:14 -07:00
|
|
|
"github.com/ethereum/go-ethereum/state"
|
2014-10-29 02:34:40 -07:00
|
|
|
)
|
|
|
|
|
2014-11-14 05:17:54 -08:00
|
|
|
func CreateBloom(receipts Receipts) []byte {
|
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
|
|
|
}
|
|
|
|
|
2014-11-03 16:29:49 -08:00
|
|
|
return ethutil.LeftPadBytes(bin.Bytes(), 64)
|
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 {
|
2014-11-09 16:17:31 -08:00
|
|
|
bin.Or(bin, ethutil.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)
|
2014-10-27 03:22:27 -07:00
|
|
|
for _, i := range []int{0, 2, 4} {
|
2014-10-29 02:34:40 -07:00
|
|
|
t := big.NewInt(1)
|
2014-11-03 16:29:49 -08:00
|
|
|
b := uint(b[i+1]) + 256*(uint(b[i])&1)
|
|
|
|
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 {
|
|
|
|
bloom := ethutil.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
|
|
|
|
}
|