Search in sources :

Example 16 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class BlockValidatorTest method processBlockWithInvalidPrevMGP.

@Test
public void processBlockWithInvalidPrevMGP() {
    BlockStore blockStore = Mockito.mock(org.ethereum.db.BlockStore.class);
    Repository repository = Mockito.mock(Repository.class);
    Mockito.when(repository.getSnapshotTo(Mockito.any())).thenReturn(repository);
    Mockito.when(repository.getNonce(Mockito.any())).thenReturn(BigInteger.ZERO);
    Block parent = new BlockBuilder().minGasPrice(BigInteger.ZERO).parent(new BlockGenerator().getGenesisBlock()).build();
    Block block = new BlockBuilder().minGasPrice(BigInteger.TEN).parent(parent).build();
    Mockito.when(blockStore.getBlockByHash(block.getParentHash().getBytes())).thenReturn(parent);
    BlockValidatorImpl validator = new BlockValidatorBuilder().addPrevMinGasPriceRule().blockStore(blockStore).build();
    Assert.assertFalse(validator.isValid(block));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) SimpleBlock(co.rsk.peg.simples.SimpleBlock) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 17 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class BlockValidatorTest method processBlockWithInvalidMGPTxs.

@Test
public void processBlockWithInvalidMGPTxs() {
    BlockStore blockStore = Mockito.mock(org.ethereum.db.BlockStore.class);
    Repository repository = Mockito.mock(Repository.class);
    Mockito.when(repository.getSnapshotTo(Mockito.any())).thenReturn(repository);
    Mockito.when(repository.getNonce(Mockito.any())).thenReturn(BigInteger.ZERO);
    Block parent = new BlockBuilder().minGasPrice(BigInteger.ZERO).parent(new BlockGenerator().getGenesisBlock()).build();
    List<Transaction> txs = new ArrayList<>();
    Transaction tx = Transaction.create(config, "0000000000000000000000000000000000000006", BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.TEN);
    tx.sign(new byte[] { 22, 11, 00 });
    txs.add(tx);
    Block block = new BlockBuilder().minGasPrice(BigInteger.TEN).transactions(txs).parent(parent).build();
    Mockito.when(blockStore.getBlockByHash(block.getParentHash().getBytes())).thenReturn(parent);
    BlockValidatorImpl validator = new BlockValidatorBuilder().addTxsMinGasPriceRule().blockStore(blockStore).build();
    Assert.assertFalse(validator.isValid(block));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) RemascTransaction(co.rsk.remasc.RemascTransaction) ArrayList(java.util.ArrayList) SimpleBlock(co.rsk.peg.simples.SimpleBlock) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 18 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class TransactionPoolImplTest method retractBlockAddsTransactionsAsPending.

@Test
public void retractBlockAddsTransactionsAsPending() {
    BlockChainImpl blockchain = createBlockchain();
    Coin balance = Coin.valueOf(1000000);
    TransactionPoolImpl transactionPool = createSampleNewTransactionPoolWithAccounts(3, balance, blockchain);
    transactionPool.processBest(blockchain.getBestBlock());
    Transaction tx1 = createSampleTransaction(1, 2, 1000, 0);
    Transaction tx2 = createSampleTransaction(1, 2, 3000, 1);
    Transaction tx3 = createSampleTransaction(2, 3, 1000, 0);
    Transaction tx4 = createSampleTransaction(2, 3, 3000, 1);
    transactionPool.addTransaction(tx1);
    transactionPool.addTransaction(tx2);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx3);
    txs.add(tx4);
    Block block = new BlockBuilder().parent(new BlockGenerator().getGenesisBlock()).transactions(txs).build();
    transactionPool.retractBlock(block);
    List<Transaction> alltxs = transactionPool.getPendingTransactions();
    Assert.assertNotNull(alltxs);
    Assert.assertFalse(alltxs.isEmpty());
    Assert.assertEquals(4, alltxs.size());
    Assert.assertTrue(alltxs.contains(tx1));
    Assert.assertTrue(alltxs.contains(tx2));
    Assert.assertTrue(alltxs.contains(tx3));
    Assert.assertTrue(alltxs.contains(tx4));
    List<Transaction> ptxs = transactionPool.getPendingTransactions();
    Assert.assertNotNull(ptxs);
    Assert.assertFalse(ptxs.isEmpty());
    Assert.assertEquals(4, ptxs.size());
    Assert.assertTrue(ptxs.contains(tx1));
    Assert.assertTrue(ptxs.contains(tx2));
    Assert.assertTrue(ptxs.contains(tx3));
    Assert.assertTrue(ptxs.contains(tx4));
}
Also used : Coin(co.rsk.core.Coin) Transaction(org.ethereum.core.Transaction) ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 19 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class Web3ImplTest method getTransactionReceipt.

@Test
public void getTransactionReceipt() throws Exception {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    Web3Impl web3 = createWeb3(world, receiptStore);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(2000000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String hashString = tx.getHash().toHexString();
    TransactionReceiptDTO tr = web3.eth_getTransactionReceipt(hashString);
    org.junit.Assert.assertNotNull(tr);
    org.junit.Assert.assertEquals("0x" + hashString, tr.transactionHash);
    String trxFrom = TypeConverter.toJsonHex(tx.getSender().getBytes());
    org.junit.Assert.assertEquals(trxFrom, tr.from);
    String trxTo = TypeConverter.toJsonHex(tx.getReceiveAddress().getBytes());
    org.junit.Assert.assertEquals(trxTo, tr.to);
    String blockHashString = "0x" + block1.getHash();
    org.junit.Assert.assertEquals(blockHashString, tr.blockHash);
    String blockNumberAsHex = "0x" + Long.toHexString(block1.getNumber());
    org.junit.Assert.assertEquals(blockNumberAsHex, tr.blockNumber);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) TransactionReceiptDTO(org.ethereum.rpc.dto.TransactionReceiptDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) ReceiptStore(org.ethereum.db.ReceiptStore) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 20 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class Web3ImplTest method getUnknownTransactionByBlockNumberAndIndex.

@Test
public void getUnknownTransactionByBlockNumberAndIndex() throws Exception {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).parent(genesis).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    TransactionResultDTO tr = web3.eth_getTransactionByBlockNumberAndIndex("0x1", "0x0");
    Assert.assertNull(tr);
}
Also used : TransactionResultDTO(org.ethereum.rpc.dto.TransactionResultDTO) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

BlockBuilder (co.rsk.test.builders.BlockBuilder)61 Test (org.junit.Test)54 World (co.rsk.test.World)36 ArrayList (java.util.ArrayList)27 AccountBuilder (co.rsk.test.builders.AccountBuilder)26 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)17 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)13 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)12 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)11 SimpleBlock (co.rsk.peg.simples.SimpleBlock)11 Block (org.ethereum.core.Block)11 HashMapDB (org.ethereum.datasource.HashMapDB)9 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)8 SimpleEthereum (org.ethereum.rpc.Simples.SimpleEthereum)8 RemascTransaction (co.rsk.remasc.RemascTransaction)7 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)7 TransactionResultDTO (org.ethereum.rpc.dto.TransactionResultDTO)6 BlockHeader (org.ethereum.core.BlockHeader)4 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)3 BlockStore (org.ethereum.db.BlockStore)3