Search in sources :

Example 91 with BlockHeader

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

the class ParentNumberRuleTest method parentNumberEqualBlockNumber.

// no pass rule
@Test
public void parentNumberEqualBlockNumber() {
    BlockHeader header = getHeader(100);
    BlockHeader parent = getHeader(100);
    assertFalse(rule.validate(header, parent));
}
Also used : BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 92 with BlockHeader

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

the class ParentNumberRuleTest method parentNumberGreaterThanBlockNumber.

// no pass rule
@Test
public void parentNumberGreaterThanBlockNumber() {
    BlockHeader header = getHeader(100);
    BlockHeader parent = getHeader(101);
    assertFalse(rule.validate(header, parent));
}
Also used : BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 93 with BlockHeader

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

the class Web3ImplUnitTest method eth_getUncleCountByBlockNumber.

@Test
public void eth_getUncleCountByBlockNumber() {
    String identifier = "notFoundable";
    Block block = mock(Block.class);
    List<BlockHeader> uncles = new LinkedList<>();
    uncles.add(mock(BlockHeader.class));
    uncles.add(mock(BlockHeader.class));
    when(block.getUncleList()).thenReturn(uncles);
    when(retriever.getBlock(eq(identifier))).thenReturn(Optional.of(block));
    String result = target.eth_getUncleCountByBlockNumber(identifier);
    assertEquals("0x2", result);
}
Also used : Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 94 with BlockHeader

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

the class Web3ImplUnitTest method eth_getUncleCountByBlockHash.

@Test
public void eth_getUncleCountByBlockHash() {
    String hash = "0x4A54";
    byte[] bytesHash = TypeConverter.stringHexToByteArray(hash);
    List<BlockHeader> uncles = new LinkedList<>();
    uncles.add(mock(BlockHeader.class));
    uncles.add(mock(BlockHeader.class));
    Block block = mock(Block.class);
    when(block.getUncleList()).thenReturn(uncles);
    when(blockchain.getBlockByHash(eq(bytesHash))).thenReturn(block);
    String result = target.eth_getUncleCountByBlockHash(hash);
    assertEquals("0x2", result);
}
Also used : Block(org.ethereum.core.Block) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 95 with BlockHeader

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

the class Web3RskImpl method ext_dumpBlockchain.

/**
 * Export the blockchain tree as a tgf file to user.dir/rskblockchain.tgf
 *
 * @param numberOfBlocks Number of block heights to include. Eg if best block is block 2300 and numberOfBlocks is 10, the graph will include blocks in heights 2290 to 2300.
 * @param includeUncles  Whether to show uncle links (recommended value is false)
 */
public void ext_dumpBlockchain(long numberOfBlocks, boolean includeUncles) {
    Block bestBlock = blockStore.getBestBlock();
    logger.info("Dumping blockchain starting on block number {}, to best block number {}", bestBlock.getNumber() - numberOfBlocks, bestBlock.getNumber());
    File graphFile = new File(System.getProperty("user.dir") + "/" + "rskblockchain.tgf");
    try (PrintWriter writer = new PrintWriter(new FileWriter(graphFile))) {
        List<Block> result = new LinkedList<>();
        long firstBlock = bestBlock.getNumber() - numberOfBlocks;
        if (firstBlock < 0) {
            firstBlock = 0;
        }
        for (long i = firstBlock; i < bestBlock.getNumber(); i++) {
            result.addAll(blockStore.getChainBlocksByNumber(i));
        }
        for (Block block : result) {
            writer.println(HashUtil.toPrintableHash(block.getHash().getBytes()) + " " + block.getNumber() + "-" + HashUtil.toPrintableHash(block.getHash().getBytes()));
        }
        writer.println("#");
        for (Block block : result) {
            writer.println(HashUtil.toPrintableHash(block.getHash().getBytes()) + " " + HashUtil.toPrintableHash(block.getParentHash().getBytes()) + " P");
            if (includeUncles) {
                for (BlockHeader uncleHeader : block.getUncleList()) {
                    writer.println(HashUtil.toPrintableHash(block.getHash().getBytes()) + " " + HashUtil.toPrintableHash(uncleHeader.getHash().getBytes()) + " U");
                }
            }
        }
    } catch (IOException e) {
        logger.error("Could nos save node graph to file", e);
    }
}
Also used : FileWriter(java.io.FileWriter) Block(org.ethereum.core.Block) IOException(java.io.IOException) BlockHeader(org.ethereum.core.BlockHeader) File(java.io.File) LinkedList(java.util.LinkedList) PrintWriter(java.io.PrintWriter)

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