Search in sources :

Example 71 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class BlockCacheTest method putAndGetValue.

@Test
public void putAndGetValue() {
    BlockCache store = getSubject();
    Block block = Mockito.mock(Block.class);
    store.put(new Keccak256(HASH_1), block);
    assertThat(store.getBlockByHash(HASH_1), is(block));
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 72 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class BlockNodeInformationTest method nodeEvictionPolicy.

@Test
public void nodeEvictionPolicy() {
    final BlockNodeInformation nodeInformation = new BlockNodeInformation();
    final Keccak256 block = createBlockHash(10);
    // These nodes should contain the block when we call getBlocksByNode
    for (int i = 0; i < 30; i++) {
        final NodeID node = createNodeID(i);
        nodeInformation.addBlockToNode(block, node);
    }
    Assert.assertTrue(nodeInformation.getBlocksByNode(createNodeID(10)).contains(block));
    Assert.assertTrue(nodeInformation.getBlocksByNode(createNodeID(20)).contains(block));
    Assert.assertFalse(nodeInformation.getBlocksByNode(createNodeID(80)).contains(block));
    // Except for node 10, which is being constantly accessed.
    for (int i = 30; i < 100; i++) {
        final NodeID node = createNodeID(i);
        nodeInformation.addBlockToNode(block, node);
        nodeInformation.getBlocksByNode(createNodeID(10));
    }
    Assert.assertTrue(nodeInformation.getBlocksByNode(createNodeID(10)).contains(block));
    Assert.assertFalse(nodeInformation.getBlocksByNode(createNodeID(20)).contains(block));
    Assert.assertFalse(nodeInformation.getBlocksByNode(createNodeID(210)).contains(block));
    Assert.assertTrue(nodeInformation.getBlocksByNode(createNodeID(80)).contains(block));
}
Also used : Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 73 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class BlockNodeInformationTest method blockEvictionPolicy.

@Test
public void blockEvictionPolicy() {
    final BlockNodeInformation nodeInformation = new BlockNodeInformation();
    final NodeID nodeID1 = new NodeID(new byte[] { 2 });
    // Add a few blocks, without exceeding the block limit. NodeID1 should contain them all.
    for (int i = 0; i < 500; i++) {
        final Keccak256 hash1 = createBlockHash(i);
        nodeInformation.addBlockToNode(hash1, nodeID1);
    }
    Assert.assertTrue(nodeInformation.getNodesByBlock(createBlockHash(15)).contains(nodeID1));
    Assert.assertTrue(nodeInformation.getNodesByBlock(createBlockHash(200)).contains(nodeID1));
    Assert.assertTrue(nodeInformation.getBlocksByNode(nodeID1).contains(createBlockHash(15)));
    Assert.assertTrue(nodeInformation.getBlocksByNode(nodeID1).contains(createBlockHash(300)));
    // Except from block 10, which is being constantly accessed.
    for (int i = 500; i < 2000; i++) {
        final Keccak256 hash1 = createBlockHash(i);
        nodeInformation.addBlockToNode(hash1, nodeID1);
        nodeInformation.getNodesByBlock(createBlockHash(10));
    }
    Assert.assertFalse(nodeInformation.getNodesByBlock(createBlockHash(1)).contains(nodeID1));
    Assert.assertFalse(nodeInformation.getNodesByBlock(createBlockHash(700)).contains(nodeID1));
    Assert.assertFalse(nodeInformation.getNodesByBlock(createBlockHash(200)).contains(nodeID1));
    Assert.assertTrue(nodeInformation.getNodesByBlock(createBlockHash(1900)).contains(nodeID1));
    Assert.assertTrue(nodeInformation.getNodesByBlock(createBlockHash(10)).contains(nodeID1));
    Assert.assertFalse(nodeInformation.getBlocksByNode(nodeID1).contains(createBlockHash(25)));
    Assert.assertFalse(nodeInformation.getBlocksByNode(nodeID1).contains(createBlockHash(70)));
    Assert.assertTrue(nodeInformation.getBlocksByNode(nodeID1).contains(createBlockHash(1901)));
}
Also used : Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 74 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class NodeBlockProcessorTest method processBlockHashRequestMessageUsingBlockInBlockchain.

@Test
public void processBlockHashRequestMessageUsingBlockInBlockchain() throws UnknownHostException {
    final Blockchain blockchain = BlockChainBuilder.ofSize(10);
    final Block block = blockchain.getBlockByNumber(5);
    final Keccak256 blockHash = block.getHash();
    final BlockStore store = new BlockStore();
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    final SimpleMessageChannel sender = new SimpleMessageChannel();
    Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
    processor.processBlockRequest(sender, 100, block.getHash().getBytes());
    Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).contains(blockHash));
    Assert.assertFalse(sender.getMessages().isEmpty());
    Assert.assertEquals(1, sender.getMessages().size());
    final Message message = sender.getMessages().get(0);
    Assert.assertEquals(MessageType.BLOCK_RESPONSE_MESSAGE, message.getMessageType());
    final BlockResponseMessage bMessage = (BlockResponseMessage) message;
    Assert.assertEquals(100, bMessage.getId());
    Assert.assertEquals(block.getHash(), bMessage.getBlock().getHash());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Example 75 with Keccak256

use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.

the class NodeBlockProcessorTest method processBlockHashRequestMessageUsingEmptyStore.

@Test
public void processBlockHashRequestMessageUsingEmptyStore() throws UnknownHostException {
    final Block block = new BlockGenerator().getBlock(3);
    final Keccak256 blockHash = block.getHash();
    final BlockStore store = new BlockStore();
    final Blockchain blockchain = BlockChainBuilder.ofSize(0);
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    RskSystemProperties config = new RskSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    final SimpleMessageChannel sender = new SimpleMessageChannel();
    Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
    processor.processBlockRequest(sender, 100, block.getHash().getBytes());
    Assert.assertFalse(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).contains(blockHash));
    Assert.assertTrue(sender.getMessages().isEmpty());
}
Also used : SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Blockchain(org.ethereum.core.Blockchain) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) RskSystemProperties(co.rsk.config.RskSystemProperties) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Test(org.junit.Test)

Aggregations

Keccak256 (co.rsk.crypto.Keccak256)102 Test (org.junit.Test)53 Block (org.ethereum.core.Block)40 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 BigInteger (java.math.BigInteger)14 RskSystemProperties (co.rsk.config.RskSystemProperties)8 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)8 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)8 HashMapDB (org.ethereum.datasource.HashMapDB)8 RepositoryImpl (co.rsk.db.RepositoryImpl)7 ArrayList (java.util.ArrayList)7 Blockchain (org.ethereum.core.Blockchain)7 BlockStore (org.ethereum.db.BlockStore)7 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)7 RLPList (org.ethereum.util.RLPList)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 Script (co.rsk.bitcoinj.script.Script)5 Coin (co.rsk.core.Coin)5 IOException (java.io.IOException)5 BlockHeader (org.ethereum.core.BlockHeader)5