Search in sources :

Example 11 with ChannelManager

use of org.ethereum.net.server.ChannelManager in project rskj by rsksmart.

the class TransactionModuleTest method internalCreateEnvironment.

private Web3Impl internalCreateEnvironment(Blockchain blockchain, MiningMainchainView mainchainView, ReceiptStore receiptStore, TransactionPool transactionPool, BlockStore blockStore, boolean mineInstant, StateRootHandler stateRootHandler, RepositoryLocator repositoryLocator, TransactionGateway transactionGateway, TransactionExecutorFactory transactionExecutorFactory) {
    transactionPool.processBest(blockchain.getBestBlock());
    ConfigCapabilities configCapabilities = new SimpleConfigCapabilities();
    CompositeEthereumListener compositeEthereumListener = new CompositeEthereumListener();
    Ethereum eth = new EthereumImpl(new ChannelManagerImpl(config, new SyncPool(compositeEthereumListener, blockchain, config, null, null, null)), transactionGateway, compositeEthereumListener, blockchain);
    MinerClock minerClock = new MinerClock(true, Clock.systemUTC());
    this.transactionExecutorFactory = transactionExecutorFactory;
    MiningConfig miningConfig = ConfigUtils.getDefaultMiningConfig();
    BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), repositoryLocator, // stateRootHandler,
    this.transactionExecutorFactory);
    MinerServer minerServer = new MinerServerImpl(config, eth, mainchainView, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new BlockToMineBuilder(config.getActivationConfig(), miningConfig, repositoryLocator, blockStore, transactionPool, new DifficultyCalculator(config.getActivationConfig(), config.getNetworkConstants()), new GasLimitCalculator(config.getNetworkConstants()), new ForkDetectionDataCalculator(), mock(BlockUnclesValidationRule.class), minerClock, blockFactory, blockExecutor, new MinimumGasPriceCalculator(Coin.valueOf(miningConfig.getMinGasPriceTarget())), new MinerUtils()), minerClock, blockFactory, new BuildInfo("cb7f28e", "master"), miningConfig);
    Wallet wallet = WalletFactory.createWallet();
    PersonalModuleWalletEnabled personalModule = new PersonalModuleWalletEnabled(config, eth, wallet, transactionPool);
    MinerClient minerClient = new MinerClientImpl(null, minerServer, config.minerClientDelayBetweenBlocks(), config.minerClientDelayBetweenRefreshes());
    EthModuleTransaction transactionModule;
    ReversibleTransactionExecutor reversibleTransactionExecutor1 = new ReversibleTransactionExecutor(repositoryLocator, this.transactionExecutorFactory);
    if (mineInstant) {
        transactionModule = new EthModuleTransactionInstant(config.getNetworkConstants(), wallet, transactionPool, minerServer, minerClient, blockchain, transactionGateway, blockExecutor);
    } else {
        transactionModule = new EthModuleTransactionBase(config.getNetworkConstants(), wallet, transactionPool, transactionGateway);
    }
    final RepositoryBtcBlockStoreWithCache.Factory btcBlockStoreFactory = new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams());
    EthModule ethModule = new EthModule(config.getNetworkConstants().getBridgeConstants(), config.getNetworkConstants().getChainId(), blockchain, transactionPool, reversibleTransactionExecutor1, new ExecutionBlockRetriever(mainchainView, blockchain, null, null), repositoryLocator, new EthModuleWalletEnabled(wallet), transactionModule, new BridgeSupportFactory(btcBlockStoreFactory, config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig()), config.getGasEstimationCap());
    TxPoolModule txPoolModule = new TxPoolModuleImpl(transactionPool);
    DebugModule debugModule = new DebugModuleImpl(null, null, Web3Mocks.getMockMessageHandler(), null);
    ChannelManager channelManager = new SimpleChannelManager();
    return new Web3RskImpl(eth, blockchain, config, minerClient, Web3Mocks.getMockMinerServer(), personalModule, ethModule, null, txPoolModule, null, debugModule, null, null, channelManager, null, null, blockStore, receiptStore, null, null, null, configCapabilities, null, null, null, null);
}
Also used : ConfigCapabilities(org.ethereum.net.client.ConfigCapabilities) SimpleConfigCapabilities(org.ethereum.rpc.Simples.SimpleConfigCapabilities) Web3RskImpl(co.rsk.rpc.Web3RskImpl) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) PersonalModuleWalletEnabled(co.rsk.rpc.modules.personal.PersonalModuleWalletEnabled) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) SyncPool(org.ethereum.sync.SyncPool) Ethereum(org.ethereum.facade.Ethereum) DebugModule(co.rsk.rpc.modules.debug.DebugModule) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) ExecutionBlockRetriever(co.rsk.rpc.ExecutionBlockRetriever) ChannelManagerImpl(org.ethereum.net.server.ChannelManagerImpl) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) EthereumImpl(org.ethereum.facade.EthereumImpl) TxPoolModuleImpl(co.rsk.rpc.modules.txpool.TxPoolModuleImpl) DebugModuleImpl(co.rsk.rpc.modules.debug.DebugModuleImpl) MiningConfig(co.rsk.config.MiningConfig) CompositeEthereumListener(org.ethereum.listener.CompositeEthereumListener) BuildInfo(org.ethereum.util.BuildInfo) SimpleConfigCapabilities(org.ethereum.rpc.Simples.SimpleConfigCapabilities) TxPoolModule(co.rsk.rpc.modules.txpool.TxPoolModule)

Example 12 with ChannelManager

use of org.ethereum.net.server.ChannelManager in project rskj by rsksmart.

the class SyncProcessorTest method sendBlockHashRequest.

@Test
public void sendBlockHashRequest() {
    Blockchain blockchain = new BlockChainBuilder().ofSize(0);
    SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
    final ChannelManager channelManager = mock(ChannelManager.class);
    Peer channel = mock(Peer.class);
    when(channel.getPeerNodeID()).thenReturn(sender.getPeerNodeID());
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
    SyncProcessor processor = new SyncProcessor(blockchain, mock(org.ethereum.db.BlockStore.class), mock(ConsensusValidationMainchainView.class), null, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    processor.sendBlockHashRequest(sender, 100);
    Assert.assertFalse(sender.getMessages().isEmpty());
    Message message = sender.getMessages().get(0);
    Assert.assertEquals(MessageType.BLOCK_HASH_REQUEST_MESSAGE, message.getMessageType());
    BlockHashRequestMessage request = (BlockHashRequestMessage) message;
    Assert.assertNotEquals(0, request.getId());
    Assert.assertEquals(100, request.getHeight());
    Assert.assertEquals(1, processor.getExpectedResponses().size());
}
Also used : EthereumListener(org.ethereum.listener.EthereumListener) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) BlockStore(org.ethereum.db.BlockStore) SimplePeer(co.rsk.net.simples.SimplePeer) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) Test(org.junit.Test)

Example 13 with ChannelManager

use of org.ethereum.net.server.ChannelManager in project rskj by rsksmart.

the class SyncProcessorTest method syncWithAdvancedStatusAnd5Peers.

@Test
public void syncWithAdvancedStatusAnd5Peers() {
    final NetBlockStore store = new NetBlockStore();
    BlockChainBuilder builder = new BlockChainBuilder();
    Blockchain blockchain = builder.ofSize(0);
    BlockStore blockStore = builder.getBlockStore();
    byte[] hash = HashUtil.randomHash();
    byte[] parentHash = HashUtil.randomHash();
    Status status = new Status(100, hash, parentHash, blockchain.getTotalDifficulty().add(new BlockDifficulty(BigInteger.TEN)));
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    final ChannelManager channelManager = mock(ChannelManager.class);
    SyncProcessor processor = new SyncProcessor(blockchain, blockStore, mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.DEFAULT, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.DEFAULT, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    List<SimplePeer> senders = new ArrayList<>();
    int lessPeers = SyncConfiguration.DEFAULT.getExpectedPeers() - 1;
    for (int i = 0; i < lessPeers; i++) {
        SimplePeer sender = new SimplePeer();
        senders.add(sender);
        when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
    }
    Assert.assertTrue(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
    senders.forEach(s -> processor.processStatus(s, status));
    Assert.assertTrue(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
    Assert.assertEquals(lessPeers, processor.getNoAdvancedPeers());
    Set<NodeID> knownPeersNodeIDs = processor.getKnownPeersNodeIDs();
    Assert.assertTrue(senders.stream().map(SimplePeer::getPeerNodeID).allMatch(knownPeersNodeIDs::contains));
    SimplePeer lastSender = new SimplePeer();
    Assert.assertFalse(processor.getKnownPeersNodeIDs().contains(lastSender.getPeerNodeID()));
    processor.processStatus(lastSender, status);
    // now test with all senders
    senders.add(lastSender);
    Set<NodeID> ids = processor.getKnownPeersNodeIDs();
    Assert.assertTrue(ids.contains(lastSender.getPeerNodeID()));
    Assert.assertFalse(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
    Assert.assertEquals(1, senders.stream().map(SimplePeer::getMessages).mapToInt(List::size).sum());
    Message message = senders.stream().map(SimplePeer::getMessages).filter(m -> !m.isEmpty()).findFirst().get().get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertEquals(status.getBestBlockHash(), request.getHash());
}
Also used : SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) java.util(java.util) StatusUtils(co.rsk.net.utils.StatusUtils) StateRootHandler(co.rsk.db.StateRootHandler) HashMapDB(org.ethereum.datasource.HashMapDB) Keccak256(co.rsk.crypto.Keccak256) Hex(org.bouncycastle.util.encoders.Hex) co.rsk.core(co.rsk.core) PeerScoringManager(co.rsk.scoring.PeerScoringManager) Duration(java.time.Duration) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) ChannelManager(org.ethereum.net.server.ChannelManager) co.rsk.net.sync(co.rsk.net.sync) RepositoryLocator(co.rsk.db.RepositoryLocator) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) co.rsk.net.messages(co.rsk.net.messages) HashUtil(org.ethereum.crypto.HashUtil) Test(org.junit.Test) BlockExecutor(co.rsk.core.bc.BlockExecutor) BlockStore(org.ethereum.db.BlockStore) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) EthereumListener(org.ethereum.listener.EthereumListener) SimplePeer(co.rsk.net.simples.SimplePeer) StateRootsStoreImpl(co.rsk.db.StateRootsStoreImpl) Mockito(org.mockito.Mockito) RskMockFactory(org.ethereum.util.RskMockFactory) TestSystemProperties(co.rsk.config.TestSystemProperties) ByteUtil(org.ethereum.util.ByteUtil) RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) co.rsk.validators(co.rsk.validators) Assert(org.junit.Assert) org.ethereum.core(org.ethereum.core) ECKey(org.ethereum.crypto.ECKey) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) EthereumListener(org.ethereum.listener.EthereumListener) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BlockStore(org.ethereum.db.BlockStore) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 14 with ChannelManager

use of org.ethereum.net.server.ChannelManager in project rskj by rsksmart.

the class SyncProcessorTest method findConnectionPointBlockchainWithGenesisVsBlockchainWith100Blocks.

@Test
public void findConnectionPointBlockchainWithGenesisVsBlockchainWith100Blocks() {
    BlockChainBuilder builder = new BlockChainBuilder();
    Blockchain blockchain = builder.ofSize(0);
    Blockchain advancedBlockchain = new BlockChainBuilder().ofSize(100);
    SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
    final ChannelManager channelManager = mock(ChannelManager.class);
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
    NetBlockStore store = new NetBlockStore();
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    SyncProcessor processor = new SyncProcessor(blockchain, builder.getBlockStore(), mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new DummyBlockValidationRule(), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    processor.processStatus(sender, StatusUtils.fromBlockchain(advancedBlockchain));
    Assert.assertFalse(sender.getMessages().isEmpty());
    List<Message> messages = sender.getMessages();
    BlockHeadersRequestMessage requestMessage = (BlockHeadersRequestMessage) messages.get(0);
    processor.processBlockHeadersResponse(sender, new BlockHeadersResponseMessage(requestMessage.getId(), Collections.singletonList(advancedBlockchain.getBestBlock().getHeader())));
    long[] expectedHeights = new long[] { 50, 25, 12, 6, 3, 1 };
    for (int k = 0; k < expectedHeights.length; k++) {
        Assert.assertEquals(k + 2, messages.size());
        Message message = messages.get(k + 1);
        Assert.assertEquals(MessageType.BLOCK_HASH_REQUEST_MESSAGE, message.getMessageType());
        BlockHashRequestMessage request = (BlockHashRequestMessage) message;
        long requestId = request.getId();
        Assert.assertEquals(expectedHeights[k], request.getHeight());
        Block block = advancedBlockchain.getBlockByNumber(expectedHeights[k]);
        processor.processBlockHashResponse(sender, new BlockHashResponseMessage(requestId, block.getHash().getBytes()));
    }
    Assert.assertEquals(expectedHeights.length + 2, messages.size());
    Message message = messages.get(messages.size() - 1);
    Assert.assertEquals(MessageType.SKELETON_REQUEST_MESSAGE, message.getMessageType());
    SkeletonRequestMessage request = (SkeletonRequestMessage) message;
    Assert.assertEquals(0, request.getStartNumber());
    Assert.assertEquals(1, processor.getExpectedResponses().size());
}
Also used : EthereumListener(org.ethereum.listener.EthereumListener) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 15 with ChannelManager

use of org.ethereum.net.server.ChannelManager in project rskj by rsksmart.

the class SyncProcessorTest method processStatusWithAdvancedPeers.

@Test
public void processStatusWithAdvancedPeers() {
    final NetBlockStore store = new NetBlockStore();
    BlockChainBuilder builder = new BlockChainBuilder();
    Blockchain blockchain = builder.ofSize(0);
    BlockStore blockStore = builder.getBlockStore();
    byte[] hash = HashUtil.randomHash();
    byte[] parentHash = HashUtil.randomHash();
    Status status = new Status(100, hash, parentHash, blockchain.getTotalDifficulty().add(new BlockDifficulty(BigInteger.TEN)));
    BlockNodeInformation nodeInformation = new BlockNodeInformation();
    TestSystemProperties config = new TestSystemProperties();
    BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
    SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
    final ChannelManager channelManager = mock(ChannelManager.class);
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
    SyncProcessor processor = new SyncProcessor(blockchain, blockStore, mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    processor.processStatus(sender, status);
    Assert.assertEquals(1, processor.getPeersCount());
    Assert.assertEquals(1, processor.getNoAdvancedPeers());
    Set<NodeID> ids = processor.getKnownPeersNodeIDs();
    Assert.assertFalse(ids.isEmpty());
    Assert.assertTrue(ids.contains(sender.getPeerNodeID()));
    List<Message> messages = sender.getMessages();
    Assert.assertFalse(messages.isEmpty());
    Assert.assertEquals(1, messages.size());
    Message message = messages.get(0);
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
    BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
    Assert.assertEquals(status.getBestBlockHash(), request.getHash());
}
Also used : EthereumListener(org.ethereum.listener.EthereumListener) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BlockStore(org.ethereum.db.BlockStore) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Aggregations

ChannelManager (org.ethereum.net.server.ChannelManager)17 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)12 Test (org.junit.Test)12 SimplePeer (co.rsk.net.simples.SimplePeer)10 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)9 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)9 BlockStore (org.ethereum.db.BlockStore)9 EthereumListener (org.ethereum.listener.EthereumListener)9 TestSystemProperties (co.rsk.config.TestSystemProperties)7 BridgeSupportFactory (co.rsk.peg.BridgeSupportFactory)5 ExecutionBlockRetriever (co.rsk.rpc.ExecutionBlockRetriever)5 Web3RskImpl (co.rsk.rpc.Web3RskImpl)5 PersonalModuleWalletEnabled (co.rsk.rpc.modules.personal.PersonalModuleWalletEnabled)5 TxPoolModule (co.rsk.rpc.modules.txpool.TxPoolModule)5 TxPoolModuleImpl (co.rsk.rpc.modules.txpool.TxPoolModuleImpl)5 MinerClient (co.rsk.mine.MinerClient)4 DebugModule (co.rsk.rpc.modules.debug.DebugModule)4 DebugModuleImpl (co.rsk.rpc.modules.debug.DebugModuleImpl)4 EthModule (co.rsk.rpc.modules.eth.EthModule)4 EthModuleWalletEnabled (co.rsk.rpc.modules.eth.EthModuleWalletEnabled)4