github.com/amazechain/amc@v0.1.3/common/block/bloom9_test.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  package block
    17  
    18  import (
    19  	"fmt"
    20  	"github.com/amazechain/amc/common/transaction"
    21  	"github.com/amazechain/amc/common/types"
    22  	"github.com/holiman/uint256"
    23  	"testing"
    24  
    25  	"github.com/amazechain/amc/common/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 = types.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  	to := types.HexToAddress("0x2")
    96  	var txs = transaction.Transactions{
    97  		transaction.NewTx(&transaction.LegacyTx{Nonce: 1, Value: uint256.NewInt(1), Gas: 1, GasPrice: uint256.NewInt(1), Data: nil}),
    98  		transaction.NewTx(&transaction.LegacyTx{Nonce: 2, To: &to, Value: uint256.NewInt(2), Gas: 2, GasPrice: uint256.NewInt(2), Data: nil}),
    99  	}
   100  	var rSmall = Receipts{
   101  		&Receipt{
   102  			Status:            ReceiptStatusFailed,
   103  			CumulativeGasUsed: 1,
   104  			Logs: []*Log{
   105  				{Address: types.BytesToAddress([]byte{0x11})},
   106  				{Address: types.BytesToAddress([]byte{0x01, 0x11})},
   107  			},
   108  			TxHash:          txs[0].Hash(),
   109  			ContractAddress: types.BytesToAddress([]byte{0x01, 0x11, 0x11}),
   110  			GasUsed:         1,
   111  		},
   112  		&Receipt{
   113  			PostState:         types.Hash{2}.Bytes(),
   114  			CumulativeGasUsed: 3,
   115  			Logs: []*Log{
   116  				{Address: types.BytesToAddress([]byte{0x22})},
   117  				{Address: types.BytesToAddress([]byte{0x02, 0x22})},
   118  			},
   119  			TxHash:          txs[1].Hash(),
   120  			ContractAddress: types.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 = types.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 = types.HexToHash("c384c56ece49458a427c67b90fefe979ebf7104795be65dc398b280f24104949")
   151  		got := crypto.Keccak256Hash(bl.Bytes())
   152  		if got != exp {
   153  			b.Errorf("Got %x, exp %x", got, exp)
   154  		}
   155  	})
   156  }