github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/evm/types/indexer_test.go (about) 1 package types 2 3 import ( 4 "bytes" 5 "testing" 6 7 "github.com/ethereum/go-ethereum/common" 8 ethtypes "github.com/ethereum/go-ethereum/core/types" 9 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 10 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/log" 11 "github.com/stretchr/testify/require" 12 13 dbm "github.com/fibonacci-chain/fbc/libs/tm-db" 14 ) 15 16 func TestIndexer_ProcessSection(t *testing.T) { 17 db := dbm.NewMemDB() 18 enableBloomFilter = true 19 InitIndexer(db) 20 require.Equal(t, uint64(0), indexer.StoredSection()) 21 22 mock := mockKeeper{ 23 db: db, 24 } 25 26 blocks := 10000 27 for i := 0; i < blocks; i++ { 28 mock.SetBlockBloom(sdk.Context{}, int64(i), ethtypes.Bloom{}) 29 } 30 31 bf := []*KV{} 32 ctx := sdk.Context{} 33 ctx.SetLogger(log.NewNopLogger()) 34 indexer.ProcessSection(ctx, mock, uint64(blocks), &bf) 35 36 require.Equal(t, uint64(2), indexer.StoredSection()) 37 require.Equal(t, uint64(2), indexer.GetValidSections()) 38 require.Equal(t, common.Hash{0x01}, indexer.sectionHead(0)) 39 require.Equal(t, common.Hash{0x01}, indexer.sectionHead(1)) 40 CloseIndexer() 41 } 42 43 type mockKeeper struct { 44 db dbm.DB 45 } 46 47 func (m mockKeeper) GetBlockBloom(_ sdk.Context, height int64) ethtypes.Bloom { 48 has, _ := m.db.Has(BloomKey(height)) 49 if !has { 50 return ethtypes.Bloom{} 51 } 52 53 bz, _ := m.db.Get(BloomKey(height)) 54 return ethtypes.BytesToBloom(bz) 55 } 56 57 func (m mockKeeper) SetBlockBloom(ctx sdk.Context, height int64, bloom ethtypes.Bloom) { 58 m.db.Set(BloomKey(height), bloom.Bytes()) 59 } 60 61 func (m mockKeeper) GetHeightHash(ctx sdk.Context, height uint64) common.Hash { 62 return common.Hash{0x01} 63 } 64 65 func TestReadBloomBits(t *testing.T) { 66 // Prepare testing data 67 mdb := dbm.NewMemDB() 68 db := mdb.NewBatch() 69 hash1 := common.HexToHash("0x11111111111111111111111111111111") 70 hash2 := common.HexToHash("0xffffffffffffffffffffffffffffffff") 71 for i := uint(0); i < 2; i++ { 72 for s := uint64(0); s < 2; s++ { 73 WriteBloomBits(db, i, s, hash1, []byte{0x01, 0x02}) 74 WriteBloomBits(db, i, s, hash2, []byte{0x01, 0x02}) 75 } 76 } 77 db.WriteSync() 78 check := func(bit uint, section uint64, head common.Hash, exist bool) { 79 bits, _ := ReadBloomBits(mdb, bit, section, head) 80 if exist && !bytes.Equal(bits, []byte{0x01, 0x02}) { 81 t.Fatalf("Bloombits mismatch") 82 } 83 if !exist && len(bits) > 0 { 84 t.Fatalf("Bloombits should be removed") 85 } 86 } 87 // Check the existence of written data. 88 check(0, 0, hash1, true) 89 check(0, 0, hash2, true) 90 check(1, 0, hash1, true) 91 check(1, 0, hash2, true) 92 check(0, 1, hash1, true) 93 check(0, 1, hash2, true) 94 check(1, 1, hash1, true) 95 check(1, 1, hash2, true) 96 // Check the not existence of data 97 check(3, 1, hash2, false) 98 }