Search in sources :

Example 96 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class ExecutionBlockRetriever method retrieveExecutionBlock.

public BlockResult retrieveExecutionBlock(String bnOrId) {
    if (LATEST_ID.equals(bnOrId)) {
        return newBlockResult(blockchain.getBestBlock());
    }
    if (PENDING_ID.equals(bnOrId)) {
        Optional<Block> latestBlock = minerServer.getLatestBlock();
        if (latestBlock.isPresent()) {
            return newBlockResult(latestBlock.get());
        }
        Block bestBlock = blockchain.getBestBlock();
        if (cachedBlock == null || !bestBlock.isParentOf(cachedBlock)) {
            // This is just a provisional fix not intended to remain in the long run
            if (!minerServer.isRunning()) {
                miningMainchainView.addBest(bestBlock.getHeader());
            }
            List<BlockHeader> mainchainHeaders = miningMainchainView.get();
            cachedResult = builder.build(mainchainHeaders, null);
        }
        return cachedResult;
    }
    // Is the block specifier either a hexadecimal or decimal number?
    Optional<Long> executionBlockNumber = Optional.empty();
    if (Utils.isHexadecimalString(bnOrId)) {
        executionBlockNumber = Optional.of(Utils.hexadecimalStringToLong(bnOrId));
    } else if (Utils.isDecimalString(bnOrId)) {
        executionBlockNumber = Optional.of(Utils.decimalStringToLong(bnOrId));
    }
    if (executionBlockNumber.isPresent()) {
        Block executionBlock = blockchain.getBlockByNumber(executionBlockNumber.get());
        if (executionBlock == null) {
            throw invalidParamError(String.format("Invalid block number %d", executionBlockNumber.get()));
        }
        return newBlockResult(executionBlock);
    }
    // If we got here, the specifier given is unsupported
    throw invalidParamError(String.format("Unsupported block specifier '%s'. Can only be either 'latest', " + "'pending' or a specific block number (either hex - prepending '0x' or decimal).", bnOrId));
}
Also used : Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader)

Example 97 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class FamilyUtilsTest method getUnclesHeaders.

@Test
public void getUnclesHeaders() {
    BlockStore store = createBlockStore();
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block block1 = blockGenerator.createChildBlock(genesis);
    Block uncle11 = blockGenerator.createChildBlock(genesis);
    Block uncle111 = blockGenerator.createChildBlock(uncle11);
    Block uncle12 = blockGenerator.createChildBlock(genesis);
    Block uncle121 = blockGenerator.createChildBlock(uncle12);
    Block block2 = blockGenerator.createChildBlock(block1);
    Block uncle21 = blockGenerator.createChildBlock(block1);
    Block uncle22 = blockGenerator.createChildBlock(block1);
    Block block3 = blockGenerator.createChildBlock(block2);
    Block uncle31 = blockGenerator.createChildBlock(block2);
    Block uncle32 = blockGenerator.createChildBlock(block2);
    store.saveBlock(genesis, TEST_DIFFICULTY, true);
    store.saveBlock(block1, TEST_DIFFICULTY, true);
    store.saveBlock(uncle11, TEST_DIFFICULTY, false);
    store.saveBlock(uncle12, TEST_DIFFICULTY, false);
    store.saveBlock(uncle111, TEST_DIFFICULTY, false);
    store.saveBlock(uncle121, TEST_DIFFICULTY, false);
    store.saveBlock(block2, TEST_DIFFICULTY, true);
    store.saveBlock(uncle21, TEST_DIFFICULTY, false);
    store.saveBlock(uncle22, TEST_DIFFICULTY, false);
    store.saveBlock(block3, TEST_DIFFICULTY, true);
    store.saveBlock(uncle31, TEST_DIFFICULTY, false);
    store.saveBlock(uncle32, TEST_DIFFICULTY, false);
    List<BlockHeader> list = FamilyUtils.getUnclesHeaders(store, block3, 3);
    Assert.assertNotNull(list);
    Assert.assertFalse(list.isEmpty());
    Assert.assertEquals(4, list.size());
    Assert.assertTrue(containsHash(uncle11.getHash(), list));
    Assert.assertTrue(containsHash(uncle12.getHash(), list));
    Assert.assertTrue(containsHash(uncle21.getHash(), list));
    Assert.assertTrue(containsHash(uncle22.getHash(), list));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) BlockStore(org.ethereum.db.BlockStore) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 98 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class MiningMainchainViewImplTest method createBlock.

private Block createBlock(long number, Keccak256 parentHash) {
    BlockHeader header = createHeader(number, parentHash);
    Block block = mock(Block.class);
    when(block.getHeader()).thenReturn(header);
    when(block.getNumber()).thenReturn(number);
    when(block.getParentHash()).thenReturn(parentHash);
    Keccak256 headerHash = header.getHash();
    when(block.getHash()).thenReturn(headerHash);
    return block;
}
Also used : Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockHeader(org.ethereum.core.BlockHeader)

Example 99 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class MiningMainchainViewImplTest method addNewBestBlockAtLowerHeight.

/**
 * Blockchain has blocks A (genesis) -> B -> C (best block)
 * A new block B' has been added to the real blockchain triggering an add on the abstract blockchain
 * After the add, abstract blockchain must be A (genesis) -> B'(best block)
 */
@Test
public void addNewBestBlockAtLowerHeight() {
    BlockStore blockStore = createBlockStore(3);
    MiningMainchainViewImpl testBlockchain = new MiningMainchainViewImpl(blockStore, 448);
    Block newBestBlockB = createBlock(1, blockStore.getChainBlockByNumber(0L).getHash());
    testBlockchain.addBest(newBestBlockB.getHeader());
    List<BlockHeader> result = testBlockchain.get();
    assertThat(result.size(), is(2));
    assertThat(result.get(0).getNumber(), is(1L));
    assertThat(result.get(0).getHash(), is(newBestBlockB.getHash()));
}
Also used : BlockStore(org.ethereum.db.BlockStore) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 100 with BlockHeader

use of org.ethereum.core.BlockHeader in project rskj by rsksmart.

the class MiningMainchainViewImplTest method addBlockToTheTipOfTheBlockchainGettingOverMaxHeight.

/**
 * Blockchain has blocks A (genesis) -> B -> C (best block)
 * A new block D has been added to the real blockchain triggering an add on the abstract blockchain
 * After the add, abstract blockchain must be B -> C -> D (best block) because max height is 3
 */
@Test
public void addBlockToTheTipOfTheBlockchainGettingOverMaxHeight() {
    BlockStore blockStore = createBlockStore(3);
    MiningMainchainViewImpl testBlockchain = new MiningMainchainViewImpl(blockStore, 3);
    Block newBestBlockD = createBlock(3, blockStore.getBestBlock().getHash());
    testBlockchain.addBest(newBestBlockD.getHeader());
    List<BlockHeader> result = testBlockchain.get();
    assertThat(result.size(), is(3));
    BlockHeader bestHeader = result.get(0);
    assertThat(bestHeader.getNumber(), is(3L));
    assertThat(bestHeader.getHash(), is(newBestBlockD.getHash()));
}
Also used : BlockStore(org.ethereum.db.BlockStore) Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Aggregations

BlockHeader (org.ethereum.core.BlockHeader)189 Test (org.junit.Test)128 Block (org.ethereum.core.Block)83 Keccak256 (co.rsk.crypto.Keccak256)31 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)30 ArrayList (java.util.ArrayList)23 BlockStore (org.ethereum.db.BlockStore)21 RLPList (org.ethereum.util.RLPList)20 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)14 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)9 BlockDifficulty (co.rsk.core.BlockDifficulty)7 ConsensusValidationMainchainView (co.rsk.core.bc.ConsensusValidationMainchainView)7 ForkDetectionDataCalculator (co.rsk.mine.ForkDetectionDataCalculator)7 BigInteger (java.math.BigInteger)7 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)6 SimplePeer (co.rsk.net.simples.SimplePeer)6 BlockBuilder (co.rsk.test.builders.BlockBuilder)6 Trie (co.rsk.trie.Trie)4 Before (org.junit.Before)4 DifficultyCalculator (co.rsk.core.DifficultyCalculator)3