Search in sources :

Example 61 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class BlocksBloomProcessorTest method processBlocksToFillRangeAndStartTheNextOne.

@Test
public void processBlocksToFillRangeAndStartTheNextOne() {
    World world = new World();
    Blockchain blockchain = world.getBlockChain();
    BlockChainBuilder.extend(blockchain, 8, false, false);
    KeyValueDataSource dataSource = new HashMapDB();
    BlocksBloomStore blocksBloomStore = new BlocksBloomStore(4, 2, dataSource);
    BlocksBloomProcessor blocksBloomProcessor = new BlocksBloomProcessor(blocksBloomStore, world.getBlockStore());
    blocksBloomProcessor.processNewBlockNumber(4);
    blocksBloomProcessor.processNewBlockNumber(6);
    BlocksBloom result = blocksBloomProcessor.getBlocksBloomInProcess();
    Assert.assertNotNull(result);
    Assert.assertEquals(4, result.fromBlock());
    Assert.assertEquals(4, result.toBlock());
    Assert.assertFalse(dataSource.keys().isEmpty());
    Assert.assertEquals(1, dataSource.keys().size());
}
Also used : Blockchain(org.ethereum.core.Blockchain) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) World(co.rsk.test.World) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Example 62 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class BlocksBloomProcessorTest method processBlocksToFillRange.

@Test
public void processBlocksToFillRange() {
    World world = new World();
    Blockchain blockchain = world.getBlockChain();
    BlockChainBuilder.extend(blockchain, 8, false, false);
    KeyValueDataSource dataSource = new HashMapDB();
    BlocksBloomStore blocksBloomStore = new BlocksBloomStore(4, 2, dataSource);
    BlocksBloomProcessor blocksBloomProcessor = new BlocksBloomProcessor(blocksBloomStore, world.getBlockStore());
    blocksBloomProcessor.processNewBlockNumber(4);
    blocksBloomProcessor.processNewBlockNumber(5);
    BlocksBloom result = blocksBloomProcessor.getBlocksBloomInProcess();
    Assert.assertNull(result);
    Assert.assertFalse(dataSource.keys().isEmpty());
    Assert.assertEquals(1, dataSource.keys().size());
}
Also used : Blockchain(org.ethereum.core.Blockchain) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) World(co.rsk.test.World) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Example 63 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplTest method getUncleByBlockHashAndIndexBlockWithUncles.

@Test
public void getUncleByBlockHashAndIndexBlockWithUncles() {
    /* Structure:
         *    Genesis
         * |     |     |
         * A     B     C
         * | \  / ____/
         * D  E
         * | /
         * F
         *
         * A-D-F mainchain
         * B and C uncles of E
         * E uncle of F
         * */
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Block genesis = world.getBlockChain().getBestBlock();
    Block blockA = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
    blockA.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(blockA));
    Block blockB = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
    blockB.setBitcoinMergedMiningHeader(new byte[] { 0x02 });
    assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(blockB));
    Block blockC = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
    blockC.setBitcoinMergedMiningHeader(new byte[] { 0x03 });
    assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(blockC));
    // block D must have a higher difficulty than block E and its uncles so it doesn't fall behind due to a reorg
    Block blockD = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(100).parent(blockA).build();
    blockD.setBitcoinMergedMiningHeader(new byte[] { 0x04 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(blockD));
    List<BlockHeader> blockEUncles = Arrays.asList(blockB.getHeader(), blockC.getHeader());
    Block blockE = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(blockA).uncles(blockEUncles).build();
    blockE.setBitcoinMergedMiningHeader(new byte[] { 0x05 });
    assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(blockE));
    List<BlockHeader> blockFUncles = Arrays.asList(blockE.getHeader());
    Block blockF = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(blockD).uncles(blockFUncles).build();
    blockF.setBitcoinMergedMiningHeader(new byte[] { 0x06 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(blockF));
    String blockFhash = "0x" + blockF.getHash();
    String blockEhash = "0x" + blockE.getHash();
    String blockBhash = "0x" + blockB.getHash();
    String blockChash = "0x" + blockC.getHash();
    BlockResultDTO result = web3.eth_getUncleByBlockHashAndIndex(blockFhash, "0x00");
    assertEquals(blockEhash, result.getHash());
    assertEquals(2, result.getUncles().size());
    assertTrue(result.getUncles().contains(blockBhash));
    assertTrue(result.getUncles().contains(blockChash));
    assertEquals(TypeConverter.toQuantityJsonHex(30), result.getCumulativeDifficulty());
}
Also used : BlockResultDTO(org.ethereum.rpc.dto.BlockResultDTO) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 64 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplTest method getTransactionByHash.

@Test
public void getTransactionByHash() {
    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 block1 = createCanonicalBlock(world, txs);
    String hashString = tx.getHash().toHexString();
    TransactionResultDTO tr = web3.eth_getTransactionByHash(hashString);
    assertNotNull(tr);
    assertEquals("0x" + hashString, tr.getHash());
    String blockHashString = "0x" + block1.getHash();
    assertEquals(blockHashString, tr.getBlockHash());
    assertEquals("0x", tr.getInput());
    assertEquals("0x" + ByteUtil.toHexString(tx.getReceiveAddress().getBytes()), tr.getTo());
    // Check the v value used to encode the transaction
    // NOT the v value used in signature
    // the encoded value includes chain id
    Assert.assertArrayEquals(new byte[] { tx.getEncodedV() }, TypeConverter.stringHexToByteArray(tr.getV()));
    Assert.assertThat(TypeConverter.stringHexToBigInteger(tr.getS()), is(tx.getSignature().getS()));
    Assert.assertThat(TypeConverter.stringHexToBigInteger(tr.getR()), is(tx.getSignature().getR()));
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) TransactionResultDTO(org.ethereum.rpc.dto.TransactionResultDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) ReceiptStore(org.ethereum.db.ReceiptStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 65 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplTest method getBlockByHashWithTransactionsHashAsResult.

@Test
public void getBlockByHashWithTransactionsHashAsResult() {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(220000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(0)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).parent(genesis).transactions(txs).build();
    block1.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String block1HashString = block1.getHashJsonString();
    BlockResultDTO bresult = web3.eth_getBlockByHash(block1HashString, false);
    assertNotNull(bresult);
    assertEquals(block1HashString, bresult.getHash());
    assertEquals(1, bresult.getTransactions().size());
    assertEquals(tx.getHash().toJsonString(), bresult.getTransactions().get(0));
    assertEquals(0, bresult.getUncles().size());
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) BlockResultDTO(org.ethereum.rpc.dto.BlockResultDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

World (co.rsk.test.World)198 Test (org.junit.Test)169 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)55 DslParser (co.rsk.test.dsl.DslParser)52 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 HashMapDB (org.ethereum.datasource.HashMapDB)45 AccountBuilder (co.rsk.test.builders.AccountBuilder)36 Block (org.ethereum.core.Block)36 ReceiptStore (org.ethereum.db.ReceiptStore)34 BlockBuilder (co.rsk.test.builders.BlockBuilder)31 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)31 Blockchain (org.ethereum.core.Blockchain)30 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)24 Transaction (org.ethereum.core.Transaction)23 BigInteger (java.math.BigInteger)17 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)15 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 Account (org.ethereum.core.Account)15 TestSystemProperties (co.rsk.config.TestSystemProperties)14