@@ -24,8 +24,8 @@ import (
2424 "github.com/ethereum/go-ethereum/common/hexutil"
2525)
2626
27- // Tests that data compression and decompression works correctly .
28- func TestCompressCycle (t * testing.T ) {
27+ // Tests that data bitset encoding and decoding works and is bijective .
28+ func TestEncodingCycle (t * testing.T ) {
2929 tests := []string {
3030 // Tests generated by go-fuzz to maximize code coverage
3131 "0x000000000000000000" ,
@@ -50,7 +50,7 @@ func TestCompressCycle(t *testing.T) {
5050 for i , tt := range tests {
5151 data := hexutil .MustDecode (tt )
5252
53- proc , err := DecompressBytes ( CompressBytes (data ), len (data ))
53+ proc , err := bitsetDecodeBytes ( bitsetEncodeBytes (data ), len (data ))
5454 if err != nil {
5555 t .Errorf ("test %d: failed to decompress compressed data: %v" , i , err )
5656 continue
@@ -61,8 +61,8 @@ func TestCompressCycle(t *testing.T) {
6161 }
6262}
6363
64- // Tests that data decompression works
65- func TestDecompress (t * testing.T ) {
64+ // Tests that data bitset decoding and rencoding works and is bijective.
65+ func TestDecodingCycle (t * testing.T ) {
6666 tests := []struct {
6767 size int
6868 input string
@@ -71,22 +71,22 @@ func TestDecompress(t *testing.T) {
7171 {size : 0 , input : "0x" },
7272
7373 // Crashers generated by go-fuzz
74- {size : 0 , input : "0x0020" , fail : ErrUnreferencedData },
75- {size : 0 , input : "0x30" , fail : ErrUnreferencedData },
76- {size : 1 , input : "0x00" , fail : ErrUnreferencedData },
77- {size : 2 , input : "0x07" , fail : ErrMissingData },
78- {size : 1024 , input : "0x8000" , fail : ErrZeroContent },
74+ {size : 0 , input : "0x0020" , fail : errUnreferencedData },
75+ {size : 0 , input : "0x30" , fail : errUnreferencedData },
76+ {size : 1 , input : "0x00" , fail : errUnreferencedData },
77+ {size : 2 , input : "0x07" , fail : errMissingData },
78+ {size : 1024 , input : "0x8000" , fail : errZeroContent },
7979
8080 // Tests generated by go-fuzz to maximize code coverage
81- {size : 29490 , input : "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e" , fail : ErrMissingData },
82- {size : 59395 , input : "0x00" , fail : ErrUnreferencedData },
83- {size : 52574 , input : "0x70706336346c65c0de" , fail : ErrExceededTarget },
84- {size : 42264 , input : "0x07" , fail : ErrMissingData },
85- {size : 52 , input : "0xa5045bad48f4" , fail : ErrExceededTarget },
86- {size : 52574 , input : "0xc0de" , fail : ErrMissingData },
81+ {size : 29490 , input : "0x343137343733323134333839373334323073333930783e3078333930783e70706336346c65303e" , fail : errMissingData },
82+ {size : 59395 , input : "0x00" , fail : errUnreferencedData },
83+ {size : 52574 , input : "0x70706336346c65c0de" , fail : errExceededTarget },
84+ {size : 42264 , input : "0x07" , fail : errMissingData },
85+ {size : 52 , input : "0xa5045bad48f4" , fail : errExceededTarget },
86+ {size : 52574 , input : "0xc0de" , fail : errMissingData },
8787 {size : 52574 , input : "0x" },
88- {size : 29490 , input : "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e" , fail : ErrMissingData },
89- {size : 29491 , input : "0x3973333930783e30783e" , fail : ErrMissingData },
88+ {size : 29490 , input : "0x34313734373332313433383937333432307333393078073034333839373334323073333930783e3078333937333432307333393078073061333930783e70706336346c65303e" , fail : errMissingData },
89+ {size : 29491 , input : "0x3973333930783e30783e" , fail : errMissingData },
9090
9191 {size : 1024 , input : "0x808080608080" },
9292 {size : 1024 , input : "0x808470705e3632383337363033313434303137393130306c6580ef46806380635a80" },
@@ -101,37 +101,66 @@ func TestDecompress(t *testing.T) {
101101 for i , tt := range tests {
102102 data := hexutil .MustDecode (tt .input )
103103
104- orig , err := DecompressBytes (data , tt .size )
104+ orig , err := bitsetDecodeBytes (data , tt .size )
105105 if err != tt .fail {
106106 t .Errorf ("test %d: failure mismatch: have %v, want %v" , i , err , tt .fail )
107107 }
108108 if err != nil {
109109 continue
110110 }
111- if comp := CompressBytes (orig ); ! bytes .Equal (comp , data ) {
111+ if comp := bitsetEncodeBytes (orig ); ! bytes .Equal (comp , data ) {
112112 t .Errorf ("test %d: decompress/compress mismatch: have %x, want %x" , i , comp , data )
113113 }
114114 }
115115}
116116
117+ // TestCompression tests that compression works by returning either the bitset
118+ // encoded input, or the actual input if the bitset version is longer.
119+ func TestCompression (t * testing.T ) {
120+ // Check the the compression returns the bitset encoding is shorter
121+ in := hexutil .MustDecode ("0x4912385c0e7b64000000" )
122+ out := hexutil .MustDecode ("0x80fe4912385c0e7b64" )
123+
124+ if data := CompressBytes (in ); bytes .Compare (data , out ) != 0 {
125+ t .Errorf ("encoding mismatch for sparse data: have %x, want %x" , data , out )
126+ }
127+ if data , err := DecompressBytes (out , len (in )); err != nil || bytes .Compare (data , in ) != 0 {
128+ t .Errorf ("decoding mismatch for sparse data: have %x, want %x, error %v" , data , in , err )
129+ }
130+ // Check the the compression returns the input if the bitset encoding is longer
131+ in = hexutil .MustDecode ("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb" )
132+ out = hexutil .MustDecode ("0xdf7070533534333636313639343638373532313536346c1bc33339343837313070706336343035336336346c65fefb3930393233383838ac2f65fefb" )
133+
134+ if data := CompressBytes (in ); bytes .Compare (data , out ) != 0 {
135+ t .Errorf ("encoding mismatch for dense data: have %x, want %x" , data , out )
136+ }
137+ if data , err := DecompressBytes (out , len (in )); err != nil || bytes .Compare (data , in ) != 0 {
138+ t .Errorf ("decoding mismatch for dense data: have %x, want %x, error %v" , data , in , err )
139+ }
140+ // Check that decompressing a longer input than the target fails
141+ if _ , err := DecompressBytes ([]byte {0xc0 , 0x01 , 0x01 }, 2 ); err != errExceededTarget {
142+ t .Errorf ("decoding error mismatch for long data: have %v, want %v" , err , errExceededTarget )
143+ }
144+ }
145+
117146// Crude benchmark for compressing random slices of bytes.
118- func BenchmarkCompress1KBVerySparse (b * testing.B ) { benchmarkCompress (b , 1024 , 0.0001 ) }
119- func BenchmarkCompress2KBVerySparse (b * testing.B ) { benchmarkCompress (b , 2048 , 0.0001 ) }
120- func BenchmarkCompress4KBVerySparse (b * testing.B ) { benchmarkCompress (b , 4096 , 0.0001 ) }
147+ func BenchmarkEncoding1KBVerySparse (b * testing.B ) { benchmarkEncoding (b , 1024 , 0.0001 ) }
148+ func BenchmarkEncoding2KBVerySparse (b * testing.B ) { benchmarkEncoding (b , 2048 , 0.0001 ) }
149+ func BenchmarkEncoding4KBVerySparse (b * testing.B ) { benchmarkEncoding (b , 4096 , 0.0001 ) }
121150
122- func BenchmarkCompress1KBSparse (b * testing.B ) { benchmarkCompress (b , 1024 , 0.001 ) }
123- func BenchmarkCompress2KBSparse (b * testing.B ) { benchmarkCompress (b , 2048 , 0.001 ) }
124- func BenchmarkCompress4KBSparse (b * testing.B ) { benchmarkCompress (b , 4096 , 0.001 ) }
151+ func BenchmarkEncoding1KBSparse (b * testing.B ) { benchmarkEncoding (b , 1024 , 0.001 ) }
152+ func BenchmarkEncoding2KBSparse (b * testing.B ) { benchmarkEncoding (b , 2048 , 0.001 ) }
153+ func BenchmarkEncoding4KBSparse (b * testing.B ) { benchmarkEncoding (b , 4096 , 0.001 ) }
125154
126- func BenchmarkCompress1KBDense (b * testing.B ) { benchmarkCompress (b , 1024 , 0.1 ) }
127- func BenchmarkCompress2KBDense (b * testing.B ) { benchmarkCompress (b , 2048 , 0.1 ) }
128- func BenchmarkCompress4KBDense (b * testing.B ) { benchmarkCompress (b , 4096 , 0.1 ) }
155+ func BenchmarkEncoding1KBDense (b * testing.B ) { benchmarkEncoding (b , 1024 , 0.1 ) }
156+ func BenchmarkEncoding2KBDense (b * testing.B ) { benchmarkEncoding (b , 2048 , 0.1 ) }
157+ func BenchmarkEncoding4KBDense (b * testing.B ) { benchmarkEncoding (b , 4096 , 0.1 ) }
129158
130- func BenchmarkCompress1KBSaturated (b * testing.B ) { benchmarkCompress (b , 1024 , 0.5 ) }
131- func BenchmarkCompress2KBSaturated (b * testing.B ) { benchmarkCompress (b , 2048 , 0.5 ) }
132- func BenchmarkCompress4KBSaturated (b * testing.B ) { benchmarkCompress (b , 4096 , 0.5 ) }
159+ func BenchmarkEncoding1KBSaturated (b * testing.B ) { benchmarkEncoding (b , 1024 , 0.5 ) }
160+ func BenchmarkEncoding2KBSaturated (b * testing.B ) { benchmarkEncoding (b , 2048 , 0.5 ) }
161+ func BenchmarkEncoding4KBSaturated (b * testing.B ) { benchmarkEncoding (b , 4096 , 0.5 ) }
133162
134- func benchmarkCompress (b * testing.B , bytes int , fill float64 ) {
163+ func benchmarkEncoding (b * testing.B , bytes int , fill float64 ) {
135164 // Generate a random slice of bytes to compress
136165 random := rand .NewSource (0 ) // reproducible and comparable
137166
@@ -143,10 +172,10 @@ func benchmarkCompress(b *testing.B, bytes int, fill float64) {
143172 bit := uint (random .Int63 () % 8 )
144173 data [idx ] |= 1 << bit
145174 }
146- // Reset the benchmark and measure compression/decompression
175+ // Reset the benchmark and measure encoding/decoding
147176 b .ResetTimer ()
148177 b .ReportAllocs ()
149178 for i := 0 ; i < b .N ; i ++ {
150- DecompressBytes ( CompressBytes (data ), len (data ))
179+ bitsetDecodeBytes ( bitsetEncodeBytes (data ), len (data ))
151180 }
152181}
0 commit comments