use of org.ethereum.core.Genesis in project rskj by rsksmart.
the class MainNetMinerTest method submitBitcoinBlockInvalidBlockDoesntEliminateCache.
/*
* This test is much more likely to fail than the
* submitBitcoinBlockProofOfWorkNotGoodEnough test. Even then
* it should almost never fail.
*/
@Test
public void submitBitcoinBlockInvalidBlockDoesntEliminateCache() {
// ////////////////////////////////////////////////////////////////////
// To make this test work we need a special network spec with
// medium minimum difficulty (this is not the mainnet nor the regnet)
// //////////////////////////////////////////////////////////////////
/* We need a low, but not too low, target */
BlockChainImpl bc = new BlockChainBuilder().build();
Genesis gen = (Genesis) BlockChainImplTest.getGenesisBlock(bc);
gen.getHeader().setDifficulty(new BlockDifficulty(BigInteger.valueOf(300000)));
bc.setStatus(gen, gen.getCumulativeDifficulty());
World world = new World(bc, gen);
blockchain = world.getBlockChain();
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
Mockito.when(ethereumImpl.addNewMinedBlock(Mockito.any())).thenReturn(ImportResult.IMPORTED_BEST);
MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, this.blockchain, world.getBlockProcessor(), DIFFICULTY_CALCULATOR, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), blockToMineBuilder(), ConfigUtils.getDefaultMiningConfig());
try {
minerServer.start();
MinerWork work = minerServer.getWork();
co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlock(work);
bitcoinMergedMiningBlock.setNonce(1);
// Try to submit a block with invalid PoW, this should not eliminate the block from the cache
SubmitBlockResult result1 = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
Assert.assertEquals("ERROR", result1.getStatus());
Assert.assertNull(result1.getBlockInfo());
Mockito.verify(ethereumImpl, Mockito.times(0)).addNewMinedBlock(Mockito.any());
// Now try to submit the same block, this should work fine since the block remains in the cache
// This WON't work in mainnet because difficulty is HIGH
/*---------------------------------------------------------
findNonce(work, bitcoinMergedMiningBlock);
SubmitBlockResult result2 = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
Assert.assertEquals("OK", result2.getStatus());
Assert.assertNotNull(result2.getBlockInfo());
Mockito.verify(ethereumImpl, Mockito.times(1)).addNewMinedBlock(Mockito.any());
// Finally, submit the same block again and validate that addNewMinedBlock is called again
SubmitBlockResult result3 = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
Assert.assertEquals("OK", result3.getStatus());
Assert.assertNotNull(result3.getBlockInfo());
Mockito.verify(ethereumImpl, Mockito.times(2)).addNewMinedBlock(Mockito.any());
-------------------------------*/
} finally {
minerServer.stop();
}
}
use of org.ethereum.core.Genesis in project rskj by rsksmart.
the class RemascFederationProviderTest method getRemascFederationProvider.
private static RemascFederationProvider getRemascFederationProvider() throws IOException, BlockStoreException {
Genesis genesisBlock = new BlockGenerator().getGenesisBlock();
BlockChainBuilder builder = new BlockChainBuilder().setTesting(true).setGenesis(genesisBlock);
Blockchain blockchain = builder.build();
BridgeSupport bridgeSupport = new BridgeSupport(new RskSystemProperties(), blockchain.getRepository(), null, PrecompiledContracts.BRIDGE_ADDR, null);
RemascFederationProvider provider = null;
try {
provider = new RemascFederationProvider(bridgeSupport);
} catch (BlockStoreException | IOException e) {
e.printStackTrace();
}
return provider;
}
use of org.ethereum.core.Genesis in project rskj by rsksmart.
the class GenesisLoader method loadGenesis.
public static Genesis loadGenesis(RskSystemProperties config, BigInteger initialNonce, InputStream genesisJsonIS, boolean isRsk) {
try {
String json = new String(ByteStreams.toByteArray(genesisJsonIS));
ObjectMapper mapper = new ObjectMapper();
JavaType type = mapper.getTypeFactory().constructType(GenesisJson.class);
GenesisJson genesisJson = new ObjectMapper().readValue(json, type);
Genesis genesis = new GenesisMapper().mapFromJson(genesisJson, isRsk);
Map<RskAddress, InitialAddressState> premine = generatePreMine(config, initialNonce, genesisJson.getAlloc());
genesis.setPremine(premine);
byte[] rootHash = generateRootHash(premine);
genesis.setStateRoot(rootHash);
genesis.flushRLP();
return genesis;
} catch (Exception e) {
System.err.println("Genesis block configuration is corrupted or not found ./resources/genesis/...");
logger.error("Genesis block configuration is corrupted or not found ./resources/genesis/...", e);
System.exit(-1);
return null;
}
}
use of org.ethereum.core.Genesis in project rskj by rsksmart.
the class BlockUtilsTest method unknowAncestorsHashesUsingUncles.
@Test
public void unknowAncestorsHashesUsingUncles() {
BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
BlockGenerator blockGenerator = new BlockGenerator();
Genesis genesis = blockGenerator.getGenesisBlock();
BlockChainImpl blockChain = blockChainBuilder.setGenesis(genesis).build();
BlockStore store = new BlockStore();
genesis.setStateRoot(blockChain.getRepository().getRoot());
genesis.flushRLP();
BlockBuilder blockBuilder = new BlockBuilder(blockChain, blockGenerator);
Block block1 = blockBuilder.parent(genesis).build();
Block block1b = blockBuilder.parent(genesis).build();
Block block2 = blockBuilder.parent(block1).build();
Block uncle1 = blockBuilder.parent(block1).build();
Block uncle2 = blockBuilder.parent(block1).build();
List<BlockHeader> uncles = new ArrayList<>();
uncles.add(uncle1.getHeader());
uncles.add(uncle2.getHeader());
Block block3 = blockBuilder.parent(block2).uncles(uncles).build();
store.saveBlock(block3);
blockChain.tryToConnect(genesis);
blockChain.tryToConnect(block1);
blockChain.tryToConnect(block1b);
Set<Keccak256> hashes = BlockUtils.unknownAncestorsHashes(genesis.getHash(), blockChain, store);
Assert.assertNotNull(hashes);
Assert.assertTrue(hashes.isEmpty());
hashes = BlockUtils.unknownAncestorsHashes(block1.getHash(), blockChain, store);
Assert.assertNotNull(hashes);
Assert.assertTrue(hashes.isEmpty());
hashes = BlockUtils.unknownAncestorsHashes(block1b.getHash(), blockChain, store);
Assert.assertNotNull(hashes);
Assert.assertTrue(hashes.isEmpty());
hashes = BlockUtils.unknownAncestorsHashes(block2.getHash(), blockChain, store);
Assert.assertNotNull(hashes);
Assert.assertFalse(hashes.isEmpty());
Assert.assertEquals(1, hashes.size());
Assert.assertTrue(hashes.contains(block2.getHash()));
hashes = BlockUtils.unknownAncestorsHashes(block3.getHash(), blockChain, store);
Assert.assertNotNull(hashes);
Assert.assertFalse(hashes.isEmpty());
Assert.assertEquals(3, hashes.size());
Assert.assertTrue(hashes.contains(block2.getHash()));
Assert.assertTrue(hashes.contains(uncle1.getHash()));
Assert.assertTrue(hashes.contains(uncle2.getHash()));
}
use of org.ethereum.core.Genesis in project rskj by rsksmart.
the class RepositoryImplOriginalTest method test13.
// Let's upload genesis pre-mine just like in the real world
@Test
public void test13() {
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
Genesis genesis = (Genesis) Genesis.getInstance(config);
for (RskAddress addr : genesis.getPremine().keySet()) {
repository.createAccount(addr);
repository.addBalance(addr, genesis.getPremine().get(addr).getAccountState().getBalance());
}
track.commit();
// To Review: config Genesis should have an State Root according to the new trie algorithm
// assertArrayEquals(Genesis.getInstance(SystemProperties.CONFIG).getStateRoot(), repository.getRoot());
repository.close();
}
Aggregations