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));
}
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));
}
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;
}
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()));
}
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()));
}
Aggregations