Search in sources :

Example 1 with Genesis

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();
    }
}
Also used : BlockChainImpl(co.rsk.core.bc.BlockChainImpl) EthereumImpl(org.ethereum.facade.EthereumImpl) World(co.rsk.test.World) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) BlockDifficulty(co.rsk.core.BlockDifficulty) Genesis(org.ethereum.core.Genesis) Test(org.junit.Test) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Example 2 with Genesis

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;
}
Also used : BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) Blockchain(org.ethereum.core.Blockchain) Genesis(org.ethereum.core.Genesis) IOException(java.io.IOException) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BridgeSupport(co.rsk.peg.BridgeSupport) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 3 with Genesis

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;
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) RskAddress(co.rsk.core.RskAddress) Genesis(org.ethereum.core.Genesis) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 4 with Genesis

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()));
}
Also used : BlockStore(co.rsk.net.BlockStore) ArrayList(java.util.ArrayList) Genesis(org.ethereum.core.Genesis) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockHeader(org.ethereum.core.BlockHeader) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 5 with Genesis

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();
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) Genesis(org.ethereum.core.Genesis) Test(org.junit.Test)

Aggregations

Genesis (org.ethereum.core.Genesis)6 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 Test (org.junit.Test)4 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)2 BlockDifficulty (co.rsk.core.BlockDifficulty)2 RskAddress (co.rsk.core.RskAddress)2 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)2 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)2 World (co.rsk.test.World)2 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)2 EthereumImpl (org.ethereum.facade.EthereumImpl)2 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)1 RskSystemProperties (co.rsk.config.RskSystemProperties)1 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)1 Keccak256 (co.rsk.crypto.Keccak256)1 BlockStore (co.rsk.net.BlockStore)1 BridgeSupport (co.rsk.peg.BridgeSupport)1 BlockBuilder (co.rsk.test.builders.BlockBuilder)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1