github.com/baptiste-b-pegasys/quorum/v22@v22.4.2/core/types/bloom9_test.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package types
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/crypto"
    26  )
    27  
    28  func TestBloom(t *testing.T) {
    29  	positive := []string{
    30  		"testtest",
    31  		"test",
    32  		"hallo",
    33  		"other",
    34  	}
    35  	negative := []string{
    36  		"tes",
    37  		"lo",
    38  	}
    39  
    40  	var bloom Bloom
    41  	for _, data := range positive {
    42  		bloom.Add([]byte(data))
    43  	}
    44  
    45  	for _, data := range positive {
    46  		if !bloom.Test([]byte(data)) {
    47  			t.Error("expected", data, "to test true")
    48  		}
    49  	}
    50  	for _, data := range negative {
    51  		if bloom.Test([]byte(data)) {
    52  			t.Error("did not expect", data, "to test true")
    53  		}
    54  	}
    55  }
    56  
    57  // TestBloomExtensively does some more thorough tests
    58  func TestBloomExtensively(t *testing.T) {
    59  	var exp = common.HexToHash("c8d3ca65cdb4874300a9e39475508f23ed6da09fdbc487f89a2dcf50b09eb263")
    60  	var b Bloom
    61  	// Add 100 "random" things
    62  	for i := 0; i < 100; i++ {
    63  		data := fmt.Sprintf("xxxxxxxxxx data %d yyyyyyyyyyyyyy", i)
    64  		b.Add([]byte(data))
    65  		//b.Add(new(big.Int).SetBytes([]byte(data)))
    66  	}
    67  	got := crypto.Keccak256Hash(b.Bytes())
    68  	if got != exp {
    69  		t.Errorf("Got %x, exp %x", got, exp)
    70  	}
    71  	var b2 Bloom
    72  	b2.SetBytes(b.Bytes())
    73  	got2 := crypto.Keccak256Hash(b2.Bytes())
    74  	if got != got2 {
    75  		t.Errorf("Got %x, exp %x", got, got2)
    76  	}
    77  }
    78  
    79  func BenchmarkBloom9(b *testing.B) {
    80  	test := []byte("testestestest")
    81  	for i := 0; i < b.N; i++ {
    82  		Bloom9(test)
    83  	}
    84  }
    85  
    86  func BenchmarkBloom9Lookup(b *testing.B) {
    87  	toTest := []byte("testtest")
    88  	bloom := new(Bloom)
    89  	for i := 0; i < b.N; i++ {
    90  		bloom.Test(toTest)
    91  	}
    92  }
    93  
    94  func BenchmarkCreateBloom(b *testing.B) {
    95  
    96  	var txs = Transactions{
    97  		NewContractCreation(1, big.NewInt(1), 1, big.NewInt(1), nil),
    98  		NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil),
    99  	}
   100  	var rSmall = Receipts{
   101  		&Receipt{
   102  			Status:            ReceiptStatusFailed,
   103  			CumulativeGasUsed: 1,
   104  			Logs: []*Log{
   105  				{Address: common.BytesToAddress([]byte{0x11})},
   106  				{Address: common.BytesToAddress([]byte{0x01, 0x11})},
   107  			},
   108  			TxHash:          txs[0].Hash(),
   109  			ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
   110  			GasUsed:         1,
   111  		},
   112  		&Receipt{
   113  			PostState:         common.Hash{2}.Bytes(),
   114  			CumulativeGasUsed: 3,
   115  			Logs: []*Log{
   116  				{Address: common.BytesToAddress([]byte{0x22})},
   117  				{Address: common.BytesToAddress([]byte{0x02, 0x22})},
   118  			},
   119  			TxHash:          txs[1].Hash(),
   120  			ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
   121  			GasUsed:         2,
   122  		},
   123  	}
   124  
   125  	var rLarge = make(Receipts, 200)
   126  	// Fill it with 200 receipts x 2 logs
   127  	for i := 0; i < 200; i += 2 {
   128  		copy(rLarge[i:], rSmall)
   129  	}
   130  	b.Run("small", func(b *testing.B) {
   131  		b.ReportAllocs()
   132  		var bl Bloom
   133  		for i := 0; i < b.N; i++ {
   134  			bl = CreateBloom(rSmall)
   135  		}
   136  		b.StopTimer()
   137  		var exp = common.HexToHash("c384c56ece49458a427c67b90fefe979ebf7104795be65dc398b280f24104949")
   138  		got := crypto.Keccak256Hash(bl.Bytes())
   139  		if got != exp {
   140  			b.Errorf("Got %x, exp %x", got, exp)
   141  		}
   142  	})
   143  	b.Run("large", func(b *testing.B) {
   144  		b.ReportAllocs()
   145  		var bl Bloom
   146  		for i := 0; i < b.N; i++ {
   147  			bl = CreateBloom(rLarge)
   148  		}
   149  		b.StopTimer()
   150  		var exp = common.HexToHash("c384c56ece49458a427c67b90fefe979ebf7104795be65dc398b280f24104949")
   151  		got := crypto.Keccak256Hash(bl.Bytes())
   152  		if got != exp {
   153  			b.Errorf("Got %x, exp %x", got, exp)
   154  		}
   155  	})
   156  }