Search in sources :

Example 1 with BlocksBloom

use of co.rsk.logfilter.BlocksBloom in project rskj by rsksmart.

the class CliToolsTest method indexBlooms.

@Test
public void indexBlooms() {
    Block block = mock(Block.class);
    BlockStore blockStore = mock(BlockStore.class);
    BlocksBloomStore blocksBloomStore = mock(BlocksBloomStore.class);
    ArgumentCaptor<BlocksBloom> captor = ArgumentCaptor.forClass(BlocksBloom.class);
    doReturn(new byte[Bloom.BLOOM_BYTES]).when(block).getLogBloom();
    doReturn(block).when(blockStore).getChainBlockByNumber(anyLong());
    doAnswer(i -> {
        long num = i.getArgument(0);
        return num - (num % 64);
    }).when(blocksBloomStore).firstNumberInRange(anyLong());
    doAnswer(i -> {
        long num = i.getArgument(0);
        return num - (num % 64) + 64 - 1;
    }).when(blocksBloomStore).lastNumberInRange(anyLong());
    IndexBlooms.execute(new IndexBlooms.Range(0, 63), blockStore, blocksBloomStore);
    verify(blocksBloomStore, times(1)).addBlocksBloom(captor.capture());
    verify(blockStore, times(64)).getChainBlockByNumber(anyLong());
    BlocksBloom blocksBloom = captor.getValue();
    assertEquals(0, blocksBloom.fromBlock());
    assertEquals(63, blocksBloom.toBlock());
    assertArrayEquals(new byte[Bloom.BLOOM_BYTES], blocksBloom.getBloom().getData());
    clearInvocations(blocksBloomStore, blockStore);
    IndexBlooms.execute(new IndexBlooms.Range(60, 300), blockStore, blocksBloomStore);
    // saved 3 block blooms in range [60..300]
    verify(blocksBloomStore, times(3)).addBlocksBloom(captor.capture());
    int i = 0;
    for (BlocksBloom bb : captor.getAllValues()) {
        assertEquals(i * 64L, bb.fromBlock());
        assertEquals(i * 64L + 63L, bb.toBlock());
        assertArrayEquals(new byte[Bloom.BLOOM_BYTES], bb.getBloom().getData());
        i++;
    }
    // [60..63] - ignored, [64..300] - processed
    // 192 (3*64) processed and saved, and 45 processed but not saved
    verify(blockStore, times(192 + 45)).getChainBlockByNumber(anyLong());
}
Also used : BlocksBloom(co.rsk.logfilter.BlocksBloom) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) Block(org.ethereum.core.Block) BlocksBloomStore(co.rsk.logfilter.BlocksBloomStore) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 2 with BlocksBloom

use of co.rsk.logfilter.BlocksBloom in project rskj by rsksmart.

the class IndexBlooms method execute.

/**
 * Indexes block blooms in the {@link blockRange} range.
 */
static void execute(Range blockRange, BlockStore blockStore, BlocksBloomStore blocksBloomStore) {
    BlocksBloom auxiliaryBlocksBloom = null;
    long curProgress = 0L;
    for (long blockNum = blockRange.fromBlockNumber; blockNum <= blockRange.toBlockNumber; blockNum++) {
        if (blocksBloomStore.firstNumberInRange(blockNum) == blockNum) {
            auxiliaryBlocksBloom = new BlocksBloom();
        }
        if (auxiliaryBlocksBloom == null) {
            continue;
        }
        Block block = blockStore.getChainBlockByNumber(blockNum);
        auxiliaryBlocksBloom.addBlockBloom(blockNum, new Bloom(block.getLogBloom()));
        if (blocksBloomStore.lastNumberInRange(blockNum) == blockNum) {
            blocksBloomStore.addBlocksBloom(auxiliaryBlocksBloom);
        }
        long progress = 100 * (blockNum - blockRange.fromBlockNumber + 1) / (blockRange.toBlockNumber - blockRange.fromBlockNumber + 1);
        if (progress > curProgress) {
            curProgress = progress;
            logger.info("Processed {}% of blocks", progress);
        }
    }
}
Also used : BlocksBloom(co.rsk.logfilter.BlocksBloom) Bloom(org.ethereum.core.Bloom) BlocksBloom(co.rsk.logfilter.BlocksBloom) Block(org.ethereum.core.Block)

Example 3 with BlocksBloom

use of co.rsk.logfilter.BlocksBloom in project rskj by rsksmart.

the class LogFilter method processBlocks.

private static void processBlocks(long fromBlockNumber, long toBlockNumber, LogFilter filter, Blockchain blockchain, BlocksBloomStore blocksBloomStore) {
    BlocksBloom auxiliaryBlocksBloom = null;
    long bestBlockNumber = blockchain.getBestBlock().getNumber();
    for (long blockNum = fromBlockNumber; blockNum <= toBlockNumber; blockNum++) {
        boolean isConfirmedBlock = blockNum <= bestBlockNumber - blocksBloomStore.getNoConfirmations();
        if (isConfirmedBlock) {
            if (blocksBloomStore.firstNumberInRange(blockNum) == blockNum) {
                if (blocksBloomStore.hasBlockNumber(blockNum)) {
                    BlocksBloom blocksBloom = blocksBloomStore.getBlocksBloomByNumber(blockNum);
                    if (!filter.addressesTopicsFilter.matchBloom(blocksBloom.getBloom())) {
                        blockNum = blocksBloomStore.lastNumberInRange(blockNum);
                        continue;
                    }
                }
                auxiliaryBlocksBloom = new BlocksBloom();
            }
            Block block = blockchain.getBlockByNumber(blockNum);
            if (auxiliaryBlocksBloom != null) {
                auxiliaryBlocksBloom.addBlockBloom(blockNum, new Bloom(block.getLogBloom()));
            }
            if (auxiliaryBlocksBloom != null && blocksBloomStore.lastNumberInRange(blockNum) == blockNum) {
                blocksBloomStore.addBlocksBloom(auxiliaryBlocksBloom);
            }
            filter.onBlock(block);
        } else {
            filter.onBlock(blockchain.getBlockByNumber(blockNum));
        }
    }
}
Also used : BlocksBloom(co.rsk.logfilter.BlocksBloom) BlocksBloom(co.rsk.logfilter.BlocksBloom)

Aggregations

BlocksBloom (co.rsk.logfilter.BlocksBloom)3 Block (org.ethereum.core.Block)2 BlocksBloomStore (co.rsk.logfilter.BlocksBloomStore)1 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)1 Bloom (org.ethereum.core.Bloom)1 BlockStore (org.ethereum.db.BlockStore)1 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)1 Test (org.junit.Test)1