github.com/MetalBlockchain/subnet-evm@v0.4.9/core/bloombits/generator_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2017 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package bloombits
    28  
    29  import (
    30  	"bytes"
    31  	"math/rand"
    32  	"testing"
    33  
    34  	"github.com/MetalBlockchain/subnet-evm/core/types"
    35  )
    36  
    37  // Tests that batched bloom bits are correctly rotated from the input bloom
    38  // filters.
    39  func TestGenerator(t *testing.T) {
    40  	// Generate the input and the rotated output
    41  	var input, output [types.BloomBitLength][types.BloomByteLength]byte
    42  
    43  	for i := 0; i < types.BloomBitLength; i++ {
    44  		for j := 0; j < types.BloomBitLength; j++ {
    45  			bit := byte(rand.Int() % 2)
    46  
    47  			input[i][j/8] |= bit << byte(7-j%8)
    48  			output[types.BloomBitLength-1-j][i/8] |= bit << byte(7-i%8)
    49  		}
    50  	}
    51  	// Crunch the input through the generator and verify the result
    52  	gen, err := NewGenerator(types.BloomBitLength)
    53  	if err != nil {
    54  		t.Fatalf("failed to create bloombit generator: %v", err)
    55  	}
    56  	for i, bloom := range input {
    57  		if err := gen.AddBloom(uint(i), bloom); err != nil {
    58  			t.Fatalf("bloom %d: failed to add: %v", i, err)
    59  		}
    60  	}
    61  	for i, want := range output {
    62  		have, err := gen.Bitset(uint(i))
    63  		if err != nil {
    64  			t.Fatalf("output %d: failed to retrieve bits: %v", i, err)
    65  		}
    66  		if !bytes.Equal(have, want[:]) {
    67  			t.Errorf("output %d: bit vector mismatch have %x, want %x", i, have, want)
    68  		}
    69  	}
    70  }
    71  
    72  func BenchmarkGenerator(b *testing.B) {
    73  	var input [types.BloomBitLength][types.BloomByteLength]byte
    74  	b.Run("empty", func(b *testing.B) {
    75  		b.ReportAllocs()
    76  		b.ResetTimer()
    77  		for i := 0; i < b.N; i++ {
    78  			// Crunch the input through the generator and verify the result
    79  			gen, err := NewGenerator(types.BloomBitLength)
    80  			if err != nil {
    81  				b.Fatalf("failed to create bloombit generator: %v", err)
    82  			}
    83  			for j, bloom := range &input {
    84  				if err := gen.AddBloom(uint(j), bloom); err != nil {
    85  					b.Fatalf("bloom %d: failed to add: %v", i, err)
    86  				}
    87  			}
    88  		}
    89  	})
    90  	for i := 0; i < types.BloomBitLength; i++ {
    91  		rand.Read(input[i][:])
    92  	}
    93  	b.Run("random", func(b *testing.B) {
    94  		b.ReportAllocs()
    95  		b.ResetTimer()
    96  		for i := 0; i < b.N; i++ {
    97  			// Crunch the input through the generator and verify the result
    98  			gen, err := NewGenerator(types.BloomBitLength)
    99  			if err != nil {
   100  				b.Fatalf("failed to create bloombit generator: %v", err)
   101  			}
   102  			for j, bloom := range &input {
   103  				if err := gen.AddBloom(uint(j), bloom); err != nil {
   104  					b.Fatalf("bloom %d: failed to add: %v", i, err)
   105  				}
   106  			}
   107  		}
   108  	})
   109  }