Search in sources :

Example 76 with Keccak256

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

the class NodeBlockProcessorTest method processStatusHavingBestBlockAsBestBlockInBlockchain.

@Test
@Ignore("Ignored when Process status deleted on block processor")
public void processStatusHavingBestBlockAsBestBlockInBlockchain() throws UnknownHostException {
    final BlockStore store = new BlockStore();
    final Blockchain blockchain = BlockChainBuilder.ofSize(2);
    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();
    final Block block = blockchain.getBestBlock();
    final Keccak256 blockHash = block.getHash();
    // final Status status = new Status(block.getNumber(), block.getHash());
    // processor.processStatus(sender, status);
    Assert.assertTrue(processor.getNodeInformation().getNodesByBlock(block.getHash().getBytes()).size() == 1);
    Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).contains(blockHash));
    Assert.assertEquals(0, sender.getGetBlockMessages().size());
    Assert.assertEquals(0, store.size());
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 77 with Keccak256

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

the class NodeMessageHandlerTest method processNewBlockHashesMessage.

@Test
public void processNewBlockHashesMessage() throws UnknownHostException {
    final World world = new World();
    final Blockchain blockchain = world.getBlockChain();
    final BlockStore store = new BlockStore();
    final List<Block> blocks = new BlockGenerator().getBlockChain(blockchain.getBestBlock(), 15);
    final List<Block> bcBlocks = blocks.subList(0, 10);
    for (Block b : bcBlocks) blockchain.tryToConnect(b);
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
    final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
    final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
    class TestCase {

        protected final NewBlockHashesMessage message;

        protected final List<Block> expected;

        public TestCase(@Nonnull final NewBlockHashesMessage message, final List<Block> expected) {
            this.message = message;
            this.expected = expected;
        }
    }
    TestCase[] testCases = { new TestCase(new NewBlockHashesMessage(new LinkedList<>()), null), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(5).getHash().getBytes(), blocks.get(5).getNumber()))), null), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(5).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(12).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11), blocks.get(12))), new TestCase(new NewBlockHashesMessage(Arrays.asList(new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()), new BlockIdentifier(blocks.get(11).getHash().getBytes(), blocks.get(5).getNumber()))), Arrays.asList(blocks.get(11))) };
    for (int i = 0; i < testCases.length; i += 1) {
        final TestCase testCase = testCases[i];
        final SimpleMessageChannel sender = new SimpleMessageChannel();
        handler.processMessage(sender, testCase.message);
        if (testCase.expected == null) {
            Assert.assertTrue(sender.getMessages().isEmpty());
            continue;
        }
        Assert.assertEquals(testCase.expected.size(), sender.getMessages().size());
        Assert.assertTrue(sender.getMessages().stream().allMatch(m -> m.getMessageType() == MessageType.GET_BLOCK_MESSAGE));
        List<Keccak256> msgs = sender.getMessages().stream().map(m -> (GetBlockMessage) m).map(m -> m.getBlockHash()).map(h -> new Keccak256(h)).collect(Collectors.toList());
        Set<Keccak256> expected = testCase.expected.stream().map(b -> b.getHash().getBytes()).map(h -> new Keccak256(h)).collect(Collectors.toSet());
        for (Keccak256 h : msgs) {
            Assert.assertTrue(expected.contains(h));
        }
        for (Keccak256 h : expected) {
            Assert.assertTrue(msgs.stream().filter(h1 -> h.equals(h1)).count() == 1);
        }
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) java.util(java.util) BeforeClass(org.junit.BeforeClass) Keccak256(co.rsk.crypto.Keccak256) PeerScoringManager(co.rsk.scoring.PeerScoringManager) BigDecimal(java.math.BigDecimal) InvocationOnMock(org.mockito.invocation.InvocationOnMock) TxHandlerImpl(co.rsk.net.handler.TxHandlerImpl) EventType(co.rsk.scoring.EventType) BigInteger(java.math.BigInteger) ChannelManager(org.ethereum.net.server.ChannelManager) Nonnull(javax.annotation.Nonnull) PunishmentParameters(co.rsk.scoring.PunishmentParameters) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) co.rsk.net.messages(co.rsk.net.messages) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) PeerScoring(co.rsk.scoring.PeerScoring) CompositeEthereumListener(org.ethereum.listener.CompositeEthereumListener) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) HashUtil(org.ethereum.crypto.HashUtil) RepositoryImpl(co.rsk.db.RepositoryImpl) Test(org.junit.Test) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) World(co.rsk.test.World) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) RskMockFactory(org.ethereum.util.RskMockFactory) SimpleTransactionPool(co.rsk.net.simples.SimpleTransactionPool) Ignore(org.junit.Ignore) TxHandler(co.rsk.net.handler.TxHandler) DummyBlockValidationRule(co.rsk.validators.DummyBlockValidationRule) Channel(org.ethereum.net.server.Channel) TransactionUtils(co.rsk.net.utils.TransactionUtils) SimpleBlockProcessor(co.rsk.net.simples.SimpleBlockProcessor) RskSystemProperties(co.rsk.config.RskSystemProperties) RegTestConfig(org.ethereum.config.blockchain.RegTestConfig) Assert(org.junit.Assert) org.ethereum.core(org.ethereum.core) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) SyncConfiguration(co.rsk.net.sync.SyncConfiguration) Nonnull(javax.annotation.Nonnull) SimpleMessageChannel(co.rsk.net.simples.SimpleMessageChannel) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 78 with Keccak256

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

the class TransactionSet method addTransaction.

public void addTransaction(Transaction transaction) {
    Keccak256 txhash = transaction.getHash();
    if (this.transactionsByHash.containsKey(txhash)) {
        return;
    }
    this.transactionsByHash.put(txhash, transaction);
    RskAddress senderAddress = transaction.getSender();
    List<Transaction> txs = this.transactionsByAddress.get(senderAddress);
    if (txs == null) {
        txs = new ArrayList<>();
        this.transactionsByAddress.put(senderAddress, txs);
    }
    txs.add(transaction);
}
Also used : RskAddress(co.rsk.core.RskAddress) Keccak256(co.rsk.crypto.Keccak256)

Example 79 with Keccak256

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

the class IndexedBlockStore method getBlockByHash.

@Override
public synchronized Block getBlockByHash(byte[] hash) {
    Block block = this.blockCache.getBlockByHash(hash);
    if (block != null) {
        return block;
    }
    byte[] blockRlp = blocks.get(hash);
    if (blockRlp == null) {
        return null;
    }
    block = new Block(blockRlp);
    this.blockCache.put(new Keccak256(hash), block);
    return block;
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256)

Example 80 with Keccak256

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

the class TransactionNodeInformationTest method twoNodesTwoTransactions.

@Test
public void twoNodesTwoTransactions() {
    final TransactionNodeInformation nodeInformation = new TransactionNodeInformation();
    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.addTransactionToNode(hash1, nodeID1);
    nodeInformation.addTransactionToNode(hash2, nodeID1);
    nodeInformation.addTransactionToNode(hash2, nodeID2);
    Set<NodeID> nodes1 = nodeInformation.getNodesByTransaction(hash1);
    Set<NodeID> nodes2 = nodeInformation.getNodesByTransaction(hash2);
    Assert.assertTrue(nodes1.size() == 1);
    Assert.assertTrue(nodes2.size() == 2);
    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)

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