From cc67a84e945ba914d8e4981106d3c75c91c00db0 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 29 Oct 2014 20:16:18 +0100 Subject: [PATCH] Added bloom 9 point lookup and bloom test --- ethchain/bloom9.go | 13 ++++++++++++- ethchain/bloom9_test.go | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 ethchain/bloom9_test.go diff --git a/ethchain/bloom9.go b/ethchain/bloom9.go index 508644b62..e841e7af7 100644 --- a/ethchain/bloom9.go +++ b/ethchain/bloom9.go @@ -3,6 +3,7 @@ package ethchain import ( "math/big" + "github.com/ethereum.backup/ethutil-go" "github.com/ethereum/go-ethereum/vm" ) @@ -23,7 +24,10 @@ func LogsBloom(logs []vm.Log) *big.Int { for _, topic := range log.Topics { data = append(data, topic) } - data = append(data, log.Data) + + if log.Data != nil { + data = append(data, log.Data) + } for _, b := range data { bin.Or(bin, bloom9(b)) @@ -42,3 +46,10 @@ func bloom9(b []byte) *big.Int { return r } + +func BloomLookup(bin, topic []byte) bool { + bloom := ethutil.BigD(bin) + cmp := bloom9(topic) + + return bloom.And(bloom, cmp).Cmp(cmp) == 0 +} diff --git a/ethchain/bloom9_test.go b/ethchain/bloom9_test.go new file mode 100644 index 000000000..ab648b7fc --- /dev/null +++ b/ethchain/bloom9_test.go @@ -0,0 +1,17 @@ +package ethchain + +import ( + "testing" + + "github.com/ethereum/go-ethereum/vm" +) + +func TestBloom9(t *testing.T) { + testCase := []byte("testtest") + bin := LogsBloom([]vm.Log{vm.Log{testCase, nil, nil}}).Bytes() + res := BloomLookup(bin, testCase) + + if !res { + t.Errorf("Bloom lookup failed") + } +}