Search in sources :

Example 66 with BlockBuilder

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

the class Web3ImplTest method getBlockByNumberRetrieveEarliestBlock.

@Test
public void getBlockByNumberRetrieveEarliestBlock() {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).parent(genesis).build();
    assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String bnOrId = "earliest";
    BlockResultDTO blockResult = web3.eth_getBlockByNumber(bnOrId, false);
    assertNotNull(blockResult);
    String blockHash = genesis.getHashJsonString();
    assertEquals(blockHash, blockResult.getHash());
    String hexString = web3.rsk_getRawBlockHeaderByNumber(bnOrId).replace("0x", "");
    Keccak256 obtainedBlockHash = new Keccak256(HashUtil.keccak256(Hex.decode(hexString)));
    assertEquals(blockHash, obtainedBlockHash.toJsonString());
}
Also used : BlockResultDTO(org.ethereum.rpc.dto.BlockResultDTO) Keccak256(co.rsk.crypto.Keccak256) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 67 with BlockBuilder

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

the class AsyncNodeBlockProcessorUnclesTest method addBlockWithTwoKnownUncles.

@Test(timeout = WAIT_TIME)
public void addBlockWithTwoKnownUncles() throws InterruptedException {
    org.ethereum.db.BlockStore blockStore = blockChainBuilder.getBlockStore();
    Block genesis = blockChain.getBestBlock();
    BlockBuilder blockBuilder = new BlockBuilder(blockChain, null, blockStore).trieStore(blockChainBuilder.getTrieStore());
    blockBuilder.parent(blockChain.getBestBlock());
    Block block1 = blockBuilder.parent(genesis).build();
    Block uncle1 = blockBuilder.parent(genesis).build();
    Block uncle2 = blockBuilder.parent(genesis).build();
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle1.getHeader());
    uncles.add(uncle2.getHeader());
    Block block2 = blockBuilder.parent(block1).uncles(uncles).build();
    processor.processBlock(null, block1);
    processor.processBlock(null, uncle1);
    processor.processBlock(null, uncle2);
    SimplePeer sender = new SimplePeer();
    processor.processBlock(sender, block2);
    listener.waitForBlock(block2.getHash());
    Assert.assertEquals(2, blockChain.getBestBlock().getNumber());
    Assert.assertArrayEquals(block2.getHash().getBytes(), blockChain.getBestBlockHash());
    Assert.assertTrue(sender.getGetBlockMessages().isEmpty());
}
Also used : ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockBuilder(co.rsk.test.builders.BlockBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Example 68 with BlockBuilder

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

the class AsyncNodeBlockProcessorUnclesTest method rejectBlockWithTwoUnknownUnclesAndUnknownParent.

@Test
public void rejectBlockWithTwoUnknownUnclesAndUnknownParent() throws InterruptedException {
    Block genesis = blockChain.getBestBlock();
    Block block1 = new BlockBuilder(null, null, null).parent(genesis).build();
    Block uncle1 = new BlockBuilder(null, null, null).parent(genesis).build();
    Block uncle2 = new BlockBuilder(null, null, null).parent(genesis).build();
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle1.getHeader());
    uncles.add(uncle2.getHeader());
    Block block2 = new BlockBuilder(null, null, null).parent(block1).uncles(uncles).build();
    SimplePeer sender = new SimplePeer();
    processor.processBlock(sender, block2);
    Assert.assertEquals(0, blockChain.getBestBlock().getNumber());
    Assert.assertArrayEquals(genesis.getHash().getBytes(), blockChain.getBestBlockHash());
    Assert.assertEquals(1, sender.getGetBlockMessages().size());
    Assert.assertTrue(sender.getGetBlockMessagesHashes().contains(block1.getHash()));
}
Also used : ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockBuilder(co.rsk.test.builders.BlockBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Example 69 with BlockBuilder

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

the class AsyncNodeBlockProcessorUnclesTest method addBlockWithoutUncles.

@Test(timeout = WAIT_TIME)
public void addBlockWithoutUncles() throws InterruptedException {
    Block genesis = blockChain.getBestBlock();
    Block block1 = new BlockBuilder(null, null, null).parent(genesis).build();
    processor.processBlock(null, block1);
    listener.waitForBlock(block1.getHash());
    Assert.assertEquals(1, blockChain.getBestBlock().getNumber());
    Assert.assertArrayEquals(block1.getHash().getBytes(), blockChain.getBestBlockHash());
}
Also used : Block(org.ethereum.core.Block) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 70 with BlockBuilder

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

the class NodeBlockProcessorUnclesTest method addBlockWithTwoUnknownUncles.

@Test
public void addBlockWithTwoUnknownUncles() {
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    BlockChainImpl blockChain = blockChainBuilder.build();
    org.ethereum.db.BlockStore blockStore = blockChainBuilder.getBlockStore();
    NodeBlockProcessor processor = createNodeBlockProcessor(blockChain);
    Block genesis = blockChain.getBestBlock();
    BlockBuilder blockBuilder = new BlockBuilder(blockChain, null, blockStore).trieStore(blockChainBuilder.getTrieStore());
    blockBuilder.parent(blockChain.getBestBlock());
    Block block1 = blockBuilder.parent(genesis).build();
    Block uncle1 = blockBuilder.parent(genesis).build();
    Block uncle2 = blockBuilder.parent(genesis).build();
    List<BlockHeader> uncles = new ArrayList<>();
    uncles.add(uncle1.getHeader());
    uncles.add(uncle2.getHeader());
    Block block2 = blockBuilder.parent(block1).uncles(uncles).build();
    processor.processBlock(null, block1);
    SimplePeer sender = new SimplePeer();
    processor.processBlock(sender, block2);
    Assert.assertEquals(2, blockChain.getBestBlock().getNumber());
    Assert.assertArrayEquals(block2.getHash().getBytes(), blockChain.getBestBlockHash());
    Assert.assertEquals(0, sender.getGetBlockMessages().size());
}
Also used : BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ArrayList(java.util.ArrayList) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Aggregations

BlockBuilder (co.rsk.test.builders.BlockBuilder)79 Test (org.junit.Test)63 World (co.rsk.test.World)31 AccountBuilder (co.rsk.test.builders.AccountBuilder)30 ArrayList (java.util.ArrayList)30 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)13 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)12 BlockResultDTO (org.ethereum.rpc.dto.BlockResultDTO)12 RemascTransaction (co.rsk.remasc.RemascTransaction)11 Block (org.ethereum.core.Block)10 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)9 HashMapDB (org.ethereum.datasource.HashMapDB)8 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)7 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)7 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)6 SimplePeer (co.rsk.net.simples.SimplePeer)6 BlockHeader (org.ethereum.core.BlockHeader)6 Keccak256 (co.rsk.crypto.Keccak256)5 SimpleBlock (co.rsk.peg.simples.SimpleBlock)5