github.com/bcnmy/go-ethereum@v1.10.27/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  	var txs = Transactions{
    96  		NewContractCreation(1, big.NewInt(1), 1, big.NewInt(1), nil),
    97  		NewTransaction(2, common.HexToAddress("0x2"), big.NewInt(2), 2, big.NewInt(2), nil),
    98  	}
    99  	var rSmall = Receipts{
   100  		&Receipt{
   101  			Status:            ReceiptStatusFailed,
   102  			CumulativeGasUsed: 1,
   103  			Logs: []*Log{
   104  				{Address: common.BytesToAddress([]byte{0x11})},
   105  				{Address: common.BytesToAddress([]byte{0x01, 0x11})},
   106  			},
   107  			TxHash:          txs[0].Hash(),
   108  			ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
   109  			GasUsed:         1,
   110  		},
   111  		&Receipt{
   112  			PostState:         common.Hash{2}.Bytes(),
   113  			CumulativeGasUsed: 3,
   114  			Logs: []*Log{
   115  				{Address: common.BytesToAddress([]byte{0x22})},
   116  				{Address: common.BytesToAddress([]byte{0x02, 0x22})},
   117  			},
   118  			TxHash:          txs[1].Hash(),
   119  			ContractAddress: common.BytesToAddress([]byte{0x02, 0x22, 0x22}),
   120  			GasUsed:         2,
   121  		},
   122  	}
   123  
   124  	var rLarge = make(Receipts, 200)
   125  	// Fill it with 200 receipts x 2 logs
   126  	for i := 0; i < 200; i += 2 {
   127  		copy(rLarge[i:], rSmall)
   128  	}
   129  	b.Run("small", func(b *testing.B) {
   130  		b.ReportAllocs()
   131  		var bl Bloom
   132  		for i := 0; i < b.N; i++ {
   133  			bl = CreateBloom(rSmall)
   134  		}
   135  		b.StopTimer()
   136  		var exp = common.HexToHash("c384c56ece49458a427c67b90fefe979ebf7104795be65dc398b280f24104949")
   137  		got := crypto.Keccak256Hash(bl.Bytes())
   138  		if got != exp {
   139  			b.Errorf("Got %x, exp %x", got, exp)
   140  		}
   141  	})
   142  	b.Run("large", func(b *testing.B) {
   143  		b.ReportAllocs()
   144  		var bl Bloom
   145  		for i := 0; i < b.N; i++ {
   146  			bl = CreateBloom(rLarge)
   147  		}
   148  		b.StopTimer()
   149  		var exp = common.HexToHash("c384c56ece49458a427c67b90fefe979ebf7104795be65dc398b280f24104949")
   150  		got := crypto.Keccak256Hash(bl.Bytes())
   151  		if got != exp {
   152  			b.Errorf("Got %x, exp %x", got, exp)
   153  		}
   154  	})
   155  }