Search in sources :

Example 11 with RepositoryLocator

use of co.rsk.db.RepositoryLocator in project rskj by rsksmart.

the class TransactionModuleTest method sendRawTransactionWithAutoMining.

@Test
public void sendRawTransactionWithAutoMining() throws Exception {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    BlockChainImpl blockchain = world.getBlockChain();
    TrieStore trieStore = world.getTrieStore();
    RepositoryLocator repositoryLocator = world.getRepositoryLocator();
    BlockStore blockStore = world.getBlockStore();
    TransactionPool transactionPool = new TransactionPoolImpl(config, repositoryLocator, blockStore, blockFactory, null, buildTransactionExecutorFactory(blockStore, receiptStore, world.getBlockTxSignatureCache()), world.getReceivedTxSignatureCache(), 10, 100);
    TransactionGateway transactionGateway = new TransactionGateway(new SimpleChannelManager(), transactionPool);
    Web3Impl web3 = createEnvironment(blockchain, receiptStore, trieStore, transactionPool, blockStore, true, world.getBlockTxSignatureCache(), transactionGateway);
    String txHash = sendRawTransaction(web3);
    Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
    Assert.assertEquals(2, blockchain.getBestBlock().getTransactionsList().size());
    Transaction txInBlock = getTransactionFromBlockWhichWasSend(blockchain, txHash);
    // Transaction tx must be in the block mined.
    Assert.assertEquals(txHash, txInBlock.getHash().toJsonString());
}
Also used : BlockStore(org.ethereum.db.BlockStore) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) TransactionGateway(co.rsk.net.TransactionGateway) TrieStore(co.rsk.trie.TrieStore) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) RepositoryLocator(co.rsk.db.RepositoryLocator) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test)

Example 12 with RepositoryLocator

use of co.rsk.db.RepositoryLocator in project rskj by rsksmart.

the class TransactionModuleTest method sendTransactionMustBeMined.

@Test
public void sendTransactionMustBeMined() {
    World world = new World();
    BlockChainImpl blockchain = world.getBlockChain();
    TrieStore trieStore = world.getTrieStore();
    RepositoryLocator repositoryLocator = world.getRepositoryLocator();
    RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
    BlockStore blockStore = world.getBlockStore();
    TransactionPool transactionPool = new TransactionPoolImpl(config, repositoryLocator, blockStore, blockFactory, null, buildTransactionExecutorFactory(blockStore, null, world.getBlockTxSignatureCache()), world.getReceivedTxSignatureCache(), 10, 100);
    TransactionGateway transactionGateway = new TransactionGateway(new SimpleChannelManager(), transactionPool);
    Web3Impl web3 = createEnvironment(blockchain, null, trieStore, transactionPool, blockStore, true, world.getBlockTxSignatureCache(), transactionGateway);
    String tx = sendTransaction(web3, repository);
    Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
    Assert.assertEquals(2, blockchain.getBestBlock().getTransactionsList().size());
    Transaction txInBlock = getTransactionFromBlockWhichWasSend(blockchain, tx);
    // Transaction tx must be in the block mined.
    Assert.assertEquals(tx, txInBlock.getHash().toJsonString());
}
Also used : BlockStore(org.ethereum.db.BlockStore) World(co.rsk.test.World) TransactionGateway(co.rsk.net.TransactionGateway) TrieStore(co.rsk.trie.TrieStore) RepositoryLocator(co.rsk.db.RepositoryLocator) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) RepositorySnapshot(co.rsk.db.RepositorySnapshot) Test(org.junit.Test)

Example 13 with RepositoryLocator

use of co.rsk.db.RepositoryLocator in project rskj by rsksmart.

the class TransactionModuleTest method sendRawTransactionWithoutAutoMining.

@Test
public void sendRawTransactionWithoutAutoMining() {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    BlockChainImpl blockchain = world.getBlockChain();
    TrieStore trieStore = world.getTrieStore();
    RepositoryLocator repositoryLocator = world.getRepositoryLocator();
    BlockStore blockStore = world.getBlockStore();
    TransactionPool transactionPool = new TransactionPoolImpl(config, repositoryLocator, blockStore, blockFactory, null, buildTransactionExecutorFactory(blockStore, receiptStore, world.getBlockTxSignatureCache()), world.getReceivedTxSignatureCache(), 10, 100);
    TransactionGateway transactionGateway = new TransactionGateway(new SimpleChannelManager(), transactionPool);
    Web3Impl web3 = createEnvironment(blockchain, receiptStore, trieStore, transactionPool, blockStore, false, world.getBlockTxSignatureCache(), transactionGateway);
    String txHash = sendRawTransaction(web3);
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    Assert.assertEquals(1, transactionPool.getPendingTransactions().size());
    Assert.assertEquals(txHash, transactionPool.getPendingTransactions().get(0).getHash().toJsonString());
}
Also used : BlockStore(org.ethereum.db.BlockStore) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) TransactionGateway(co.rsk.net.TransactionGateway) TrieStore(co.rsk.trie.TrieStore) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) RepositoryLocator(co.rsk.db.RepositoryLocator) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ReceiptStore(org.ethereum.db.ReceiptStore) Test(org.junit.Test)

Example 14 with RepositoryLocator

use of co.rsk.db.RepositoryLocator in project rskj by rsksmart.

the class SyncProcessorTest method processBodyResponseWithTransactionAddsToBlockchain.

@Test
public void processBodyResponseWithTransactionAddsToBlockchain() {
    Account senderAccount = createAccount("sender");
    Account receiverAccount = createAccount("receiver");
    Map<RskAddress, AccountState> accounts = new HashMap<>();
    accounts.put(senderAccount.getAddress(), new AccountState(BigInteger.ZERO, Coin.valueOf(20000000)));
    accounts.put(receiverAccount.getAddress(), new AccountState(BigInteger.ZERO, Coin.ZERO));
    final NetBlockStore store = new NetBlockStore();
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    Blockchain blockchain = blockChainBuilder.ofSize(0, false, accounts);
    Block genesis = blockchain.getBestBlock();
    SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
    Assert.assertEquals(0, blockchain.getBestBlock().getNumber());
    List<Transaction> txs = Collections.singletonList(createTransaction(senderAccount, receiverAccount, BigInteger.valueOf(1000000), BigInteger.ZERO));
    Block block = new BlockGenerator().createChildBlock(genesis, txs, blockChainBuilder.getRepository().getRoot());
    StateRootHandler stateRootHandler = new StateRootHandler(config.getActivationConfig(), new StateRootsStoreImpl(new HashMapDB()));
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
    BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), new RepositoryLocator(blockChainBuilder.getTrieStore(), stateRootHandler), new TransactionExecutorFactory(config, blockChainBuilder.getBlockStore(), null, blockFactory, new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
    Assert.assertEquals(1, block.getTransactionsList().size());
    blockExecutor.executeAndFillAll(block, genesis.getHeader());
    Assert.assertEquals(21000, block.getFeesPaidToMiner().asBigInteger().intValueExact());
    Assert.assertEquals(1, block.getTransactionsList().size());
    Assert.assertEquals(1, block.getNumber());
    Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
    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, mock(org.ethereum.db.BlockStore.class), 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(getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
    List<Transaction> transactions = block.getTransactionsList();
    List<BlockHeader> uncles = block.getUncleList();
    long lastRequestId = new Random().nextLong();
    BodyResponseMessage response = new BodyResponseMessage(lastRequestId, transactions, uncles);
    processor.registerExpectedMessage(response);
    Deque<BlockHeader> headerStack = new ArrayDeque<>();
    headerStack.add(block.getHeader());
    List<Deque<BlockHeader>> headers = new ArrayList<>();
    headers.add(headerStack);
    List<BlockIdentifier> bids = new ArrayList<>();
    bids.add(new BlockIdentifier(blockchain.getBlockByNumber(0).getHash().getBytes(), 0));
    bids.add(new BlockIdentifier(block.getHash().getBytes(), 1));
    processor.startDownloadingBodies(headers, Collections.singletonMap(sender, bids), sender);
    ((DownloadingBodiesSyncState) processor.getSyncState()).expectBodyResponseFor(lastRequestId, sender.getPeerNodeID(), block.getHeader());
    processor.processBodyResponse(sender, response);
    Assert.assertEquals(1, blockchain.getBestBlock().getNumber());
    Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
    Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
Also used : StateRootsStoreImpl(co.rsk.db.StateRootsStoreImpl) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) BlockStore(org.ethereum.db.BlockStore) HashMapDB(org.ethereum.datasource.HashMapDB) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) EthereumListener(org.ethereum.listener.EthereumListener) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) StateRootHandler(co.rsk.db.StateRootHandler) RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) RepositoryLocator(co.rsk.db.RepositoryLocator) ConsensusValidationMainchainView(co.rsk.core.bc.ConsensusValidationMainchainView) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) BlockExecutor(co.rsk.core.bc.BlockExecutor) TestSystemProperties(co.rsk.config.TestSystemProperties) Test(org.junit.Test)

Example 15 with RepositoryLocator

use of co.rsk.db.RepositoryLocator in project rskj by rsksmart.

the class BlockToMineBuilderTest method setUp.

@Before
public void setUp() {
    validationRules = mock(BlockValidationRule.class);
    RepositoryLocator repositoryLocator = mock(RepositoryLocator.class);
    StateRootHandler stateRootHandler = mock(StateRootHandler.class);
    MiningConfig miningConfig = mock(MiningConfig.class);
    DifficultyCalculator difficultyCalculator = mock(DifficultyCalculator.class);
    MinimumGasPriceCalculator minimumGasPriceCalculator = mock(MinimumGasPriceCalculator.class);
    MinerUtils minerUtils = mock(MinerUtils.class);
    activationConfig = mock(ActivationConfig.class);
    blockExecutor = mock(BlockExecutor.class);
    blockBuilder = new BlockToMineBuilder(activationConfig, miningConfig, repositoryLocator, mock(BlockStore.class), mock(TransactionPool.class), difficultyCalculator, new GasLimitCalculator(Constants.mainnet()), new ForkDetectionDataCalculator(), validationRules, mock(MinerClock.class), new BlockFactory(activationConfig), blockExecutor, minimumGasPriceCalculator, minerUtils);
    BlockDifficulty blockDifficulty = mock(BlockDifficulty.class);
    Repository snapshot = mock(Repository.class);
    GasLimitConfig gasLimitConfig = new GasLimitConfig(0, 0, false);
    when(minerUtils.getAllTransactions(any())).thenReturn(new ArrayList<>());
    when(minerUtils.filterTransactions(any(), any(), any(), any(), any())).thenReturn(new ArrayList<>());
    when(repositoryLocator.snapshotAt(any())).thenReturn(snapshot);
    when(minimumGasPriceCalculator.calculate(any())).thenReturn(mock(Coin.class));
    when(stateRootHandler.translate(any())).thenReturn(TestUtils.randomHash());
    when(miningConfig.getGasLimit()).thenReturn(gasLimitConfig);
    when(miningConfig.getUncleListLimit()).thenReturn(10);
    when(miningConfig.getCoinbaseAddress()).thenReturn(TestUtils.randomAddress());
    when(difficultyCalculator.calcDifficulty(any(), any())).thenReturn(blockDifficulty);
}
Also used : BlockExecutor(co.rsk.core.bc.BlockExecutor) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) RepositoryLocator(co.rsk.db.RepositoryLocator) StateRootHandler(co.rsk.db.StateRootHandler) MiningConfig(co.rsk.config.MiningConfig) DifficultyCalculator(co.rsk.core.DifficultyCalculator) BlockDifficulty(co.rsk.core.BlockDifficulty) Coin(co.rsk.core.Coin) GasLimitConfig(co.rsk.config.GasLimitConfig) BlockValidationRule(co.rsk.validators.BlockValidationRule) Before(org.junit.Before)

Aggregations

RepositoryLocator (co.rsk.db.RepositoryLocator)38 Test (org.junit.Test)25 BlockStore (org.ethereum.db.BlockStore)21 RepositorySnapshot (co.rsk.db.RepositorySnapshot)18 BlockExecutor (co.rsk.core.bc.BlockExecutor)15 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)14 HashMapDB (org.ethereum.datasource.HashMapDB)10 TestSystemProperties (co.rsk.config.TestSystemProperties)9 Coin (co.rsk.core.Coin)7 World (co.rsk.test.World)7 RskAddress (co.rsk.core.RskAddress)6 StateRootHandler (co.rsk.db.StateRootHandler)6 TransactionGateway (co.rsk.net.TransactionGateway)6 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)6 TrieStore (co.rsk.trie.TrieStore)6 SimpleChannelManager (org.ethereum.rpc.Simples.SimpleChannelManager)6 ProgramInvokeFactoryImpl (org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl)6 RskSystemProperties (co.rsk.config.RskSystemProperties)5 TransactionExecutorFactory (co.rsk.core.TransactionExecutorFactory)5 StateRootsStoreImpl (co.rsk.db.StateRootsStoreImpl)5