diff options
author | Felix Lange <fjl@twurst.com> | 2016-08-04 09:55:33 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2016-08-04 09:55:33 +0800 |
commit | 704fde01e8ed28877fe0f69fc8059b1f0c850d04 (patch) | |
tree | b57e6dd9b93806d5803b6a45c5d4c9ac8d382481 /core/types/bloom9.go | |
parent | 0c9a858f2d8094d48371deee97493a1b1412e7de (diff) | |
download | go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar.gz go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar.bz2 go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar.lz go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar.xz go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.tar.zst go-tangerine-704fde01e8ed28877fe0f69fc8059b1f0c850d04.zip |
core/types, core/vm: improve docs, add JSON marshaling methods
In this commit, core/types's types learn how to encode and decode
themselves as JSON. The encoding is very similar to what the RPC API
uses. The RPC API is missing some output fields (e.g. transaction
signature values) which will be added to the API in a later commit. Some
fields that the API generates are ignored by the decoder methods here.
Diffstat (limited to 'core/types/bloom9.go')
-rw-r--r-- | core/types/bloom9.go | 24 |
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 { |