aboutsummaryrefslogtreecommitdiffstats
path: root/core/types/bloom9.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/types/bloom9.go')
-rw-r--r--core/types/bloom9.go24
1 files changed, 22 insertions, 2 deletions
diff --git a/core/types/bloom9.go b/core/types/bloom9.go
index ecf2bffc2..d3945a734 100644
--- a/core/types/bloom9.go
+++ b/core/types/bloom9.go
@@ -31,28 +31,34 @@ type bytesBacked interface {
const bloomLength = 256
+// Bloom represents a 256 bit bloom filter.
type Bloom [bloomLength]byte
+// BytesToBloom converts a byte slice to a bloom filter.
+// It panics if b is not of suitable size.
func BytesToBloom(b []byte) Bloom {
var bloom Bloom
bloom.SetBytes(b)
return bloom
}
+// SetBytes sets the content of b to the given bytes.
+// It panics if d is not of suitable size.
func (b *Bloom) SetBytes(d []byte) {
if len(b) < len(d) {
panic(fmt.Sprintf("bloom bytes too big %d %d", len(b), len(d)))
}
-
copy(b[bloomLength-len(d):], d)
}
+// Add adds d to the filter. Future calls of Test(d) will return true.
func (b *Bloom) Add(d *big.Int) {
bin := new(big.Int).SetBytes(b[:])
bin.Or(bin, bloom9(d.Bytes()))
b.SetBytes(bin.Bytes())
}
+// Big converts b to a big integer.
func (b Bloom) Big() *big.Int {
return common.Bytes2Big(b[:])
}
@@ -69,8 +75,22 @@ func (b Bloom) TestBytes(test []byte) bool {
return b.Test(common.BytesToBig(test))
}
+// MarshalJSON encodes b as a hex string with 0x prefix.
func (b Bloom) MarshalJSON() ([]byte, error) {
- return []byte(fmt.Sprintf(`"%#x"`, b.Bytes())), nil
+ return []byte(fmt.Sprintf(`"%#x"`, b[:])), nil
+}
+
+// UnmarshalJSON b as a hex string with 0x prefix.
+func (b *Bloom) UnmarshalJSON(input []byte) error {
+ var dec hexBytes
+ if err := dec.UnmarshalJSON(input); err != nil {
+ return err
+ }
+ if len(dec) != bloomLength {
+ return fmt.Errorf("invalid bloom size, want %d bytes", bloomLength)
+ }
+ copy((*b)[:], dec)
+ return nil
}
func CreateBloom(receipts Receipts) Bloom {