Search in sources :

Example 6 with Keccak256

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

the class MinerUtilsTest method getAllTransactionsTest.

@Test
public void getAllTransactionsTest() {
    TransactionPool transactionPool = Mockito.mock(TransactionPool.class);
    Transaction tx1 = Mockito.mock(Transaction.class);
    Transaction tx2 = Mockito.mock(Transaction.class);
    byte[] s1 = new byte[32];
    byte[] s2 = new byte[32];
    s1[0] = 0;
    s2[0] = 1;
    Mockito.when(tx1.getHash()).thenReturn(new Keccak256(s1));
    Mockito.when(tx2.getHash()).thenReturn(new Keccak256(s2));
    List<Transaction> txs = new LinkedList<>();
    txs.add(tx1);
    txs.add(tx2);
    Mockito.when(transactionPool.getPendingTransactions()).thenReturn(txs);
    List<Transaction> res = new MinerUtils().getAllTransactions(transactionPool);
    Assert.assertEquals(2, res.size());
}
Also used : TransactionPool(org.ethereum.core.TransactionPool) Transaction(org.ethereum.core.Transaction) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 7 with Keccak256

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

the class BlockCacheTest method repeatingValueAtEndPreventsCleanup.

@Test
public void repeatingValueAtEndPreventsCleanup() {
    BlockCache store = getSubject();
    store.put(new Keccak256(HASH_1), Mockito.mock(Block.class));
    store.put(new Keccak256(HASH_2), Mockito.mock(Block.class));
    store.put(new Keccak256(HASH_3), Mockito.mock(Block.class));
    store.put(new Keccak256(HASH_4), Mockito.mock(Block.class));
    store.put(new Keccak256(HASH_1), Mockito.mock(Block.class));
    store.put(new Keccak256(HASH_5), Mockito.mock(Block.class));
    assertThat(store.getBlockByHash(HASH_1), notNullValue());
    assertThat(store.getBlockByHash(HASH_2), nullValue());
    assertThat(store.getBlockByHash(HASH_3), notNullValue());
    assertThat(store.getBlockByHash(HASH_4), notNullValue());
    assertThat(store.getBlockByHash(HASH_5), notNullValue());
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 8 with Keccak256

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

the class BlockNodeInformationTest method getIsNotEmptyIfPresent.

@Test
public void getIsNotEmptyIfPresent() {
    final BlockNodeInformation nodeInformation = new BlockNodeInformation();
    final Keccak256 hash1 = createBlockHash(1);
    final NodeID nodeID1 = new NodeID(new byte[] { 2 });
    final Keccak256 badHash = createBlockHash(3);
    final NodeID badNode = new NodeID(new byte[] { 4 });
    nodeInformation.addBlockToNode(hash1, nodeID1);
    Set<Keccak256> blocks = nodeInformation.getBlocksByNode(nodeID1);
    Assert.assertTrue(blocks.size() == 1);
    Assert.assertTrue(blocks.contains(hash1));
    Assert.assertFalse(blocks.contains(badHash));
    blocks = nodeInformation.getBlocksByNode(badNode);
    Assert.assertTrue(blocks.size() == 0);
    Set<NodeID> nodes = nodeInformation.getNodesByBlock(hash1);
    Assert.assertTrue(nodes.size() == 1);
    Assert.assertTrue(nodes.contains(nodeID1));
    Assert.assertFalse(nodes.contains(badNode));
    nodes = nodeInformation.getNodesByBlock(badHash);
    Assert.assertTrue(nodes.size() == 0);
}
Also used : Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 9 with Keccak256

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

the class BlockNodeInformationTest method twoNodesTwoBlocks.

@Test
public void twoNodesTwoBlocks() {
    final BlockNodeInformation nodeInformation = new BlockNodeInformation();
    final Keccak256 hash1 = createBlockHash(1);
    final NodeID nodeID1 = new NodeID(new byte[] { 2 });
    final Keccak256 hash2 = createBlockHash(3);
    final NodeID nodeID2 = new NodeID(new byte[] { 4 });
    nodeInformation.addBlockToNode(hash1, nodeID1);
    nodeInformation.addBlockToNode(hash2, nodeID1);
    nodeInformation.addBlockToNode(hash2, nodeID2);
    Set<Keccak256> blocks1 = nodeInformation.getBlocksByNode(nodeID1);
    Set<Keccak256> blocks2 = nodeInformation.getBlocksByNode(nodeID2);
    Set<NodeID> nodes1 = nodeInformation.getNodesByBlock(hash1);
    Set<NodeID> nodes2 = nodeInformation.getNodesByBlock(hash2);
    Assert.assertTrue(blocks1.size() == 2);
    Assert.assertTrue(blocks2.size() == 1);
    Assert.assertTrue(nodes1.size() == 1);
    Assert.assertTrue(nodes2.size() == 2);
    Assert.assertTrue(blocks1.contains(hash1));
    Assert.assertTrue(blocks1.contains(hash2));
    Assert.assertTrue(blocks2.contains(hash2));
    Assert.assertTrue(nodes1.contains(nodeID1));
    Assert.assertTrue(nodes2.contains(nodeID1));
    Assert.assertTrue(nodes2.contains(nodeID2));
}
Also used : Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 10 with Keccak256

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

the class NodeBlockProcessorTest method processBlockRequestMessageUsingBlockInStore.

@Test
public void processBlockRequestMessageUsingBlockInStore() throws UnknownHostException {
    final Block block = new BlockGenerator().getBlock(3);
    final Keccak256 blockHash = block.getHash();
    final BlockStore store = new BlockStore();
    store.saveBlock(block);
    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.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) Keccak256(co.rsk.crypto.Keccak256) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Block(org.ethereum.core.Block) 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