Search in sources :

Example 11 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class StatusResolverTest method status_incompleteBlockchain.

@Test
public void status_incompleteBlockchain() {
    when(blockStore.getMinNumber()).thenReturn(1L);
    Keccak256 genesisHash = new Keccak256("ee5c851e70650111887bb6c04e18ef4353391abe37846234c17895a9ca2b33d5");
    Keccak256 parentHash = new Keccak256("133e83bb305ef21ea7fc86fcced355db2300887274961a136ca5e8c8763687d9");
    when(genesis.getHash()).thenReturn(genesisHash);
    when(genesis.getParentHash()).thenReturn(parentHash);
    when(genesis.getNumber()).thenReturn(0L);
    BlockDifficulty genesisDifficulty = new BlockDifficulty(BigInteger.valueOf(10L));
    when(genesis.getCumulativeDifficulty()).thenReturn(genesisDifficulty);
    Status status = target.currentStatus();
    Assert.assertEquals(0L, status.getBestBlockNumber());
    Assert.assertEquals(genesisHash.getBytes(), status.getBestBlockHash());
    Assert.assertEquals(parentHash.getBytes(), status.getBestBlockParentHash());
    Assert.assertEquals(genesisDifficulty, status.getTotalDifficulty());
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 12 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class StatusResolverTest method status.

@Test
public void status() {
    Block bestBlock = mock(Block.class);
    Keccak256 blockHash = mock(Keccak256.class);
    byte[] hashBytes = new byte[] { 0x00 };
    long blockNumber = 52;
    BlockDifficulty totalDifficulty = mock(BlockDifficulty.class);
    when(blockStore.getBestBlock()).thenReturn(bestBlock);
    when(bestBlock.getHash()).thenReturn(blockHash);
    when(bestBlock.getParentHash()).thenReturn(blockHash);
    when(blockHash.getBytes()).thenReturn(hashBytes);
    when(bestBlock.getNumber()).thenReturn(blockNumber);
    when(blockStore.getTotalDifficultyForHash(eq(hashBytes))).thenReturn(totalDifficulty);
    Status status = target.currentStatus();
    Assert.assertEquals(blockNumber, status.getBestBlockNumber());
    Assert.assertEquals(hashBytes, status.getBestBlockHash());
    Assert.assertEquals(hashBytes, status.getBestBlockParentHash());
    Assert.assertEquals(totalDifficulty, status.getTotalDifficulty());
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 13 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class RemascTestRunner method start.

public void start() {
    this.blockchain = this.builder.build();
    ((BlockChainImpl) this.blockchain).setNoValidation(true);
    this.addedSiblings = new ArrayList<>();
    List<Block> mainChainBlocks = new ArrayList<>();
    this.blockchain.tryToConnect(this.genesis);
    BlockFactory blockFactory = new BlockFactory(builder.getConfig().getActivationConfig());
    final ProgramInvokeFactoryImpl programInvokeFactory = new ProgramInvokeFactoryImpl();
    BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(builder.getConfig().getNetworkConstants().getBridgeConstants().getBtcParams()), builder.getConfig().getNetworkConstants().getBridgeConstants(), builder.getConfig().getActivationConfig());
    PrecompiledContracts precompiledContracts = new PrecompiledContracts(builder.getConfig(), bridgeSupportFactory);
    BlockExecutor blockExecutor = new BlockExecutor(builder.getConfig().getActivationConfig(), builder.getRepositoryLocator(), new TransactionExecutorFactory(builder.getConfig(), builder.getBlockStore(), null, blockFactory, programInvokeFactory, precompiledContracts, blockTxSignatureCache));
    for (int i = 0; i <= this.initialHeight; i++) {
        int finalI = i;
        List<SiblingElement> siblingsForCurrentHeight = this.siblingElements.stream().filter(siblingElement -> siblingElement.getHeightToBeIncluded() == finalI).collect(Collectors.toList());
        List<BlockHeader> blockSiblings = new ArrayList<>();
        // Going to add siblings
        BlockDifficulty cummDifficulty = BlockDifficulty.ZERO;
        if (siblingsForCurrentHeight.size() > 0) {
            cummDifficulty = blockchain.getTotalDifficulty();
        }
        for (SiblingElement sibling : siblingsForCurrentHeight) {
            RskAddress siblingCoinbase = TestUtils.randomAddress();
            Block mainchainSiblingParent = mainChainBlocks.get(sibling.getHeight() - 1);
            Block siblingBlock = createBlock(this.genesis, mainchainSiblingParent, PegTestUtils.createHash3(), siblingCoinbase, Collections.emptyList(), minerFee, this.gasPrice, (long) i, this.txValue, this.txSigningKey, null);
            blockSiblings.add(siblingBlock.getHeader());
            builder.getBlockStore().saveBlock(siblingBlock, cummDifficulty.add(siblingBlock.getCumulativeDifficulty()), false);
            this.addedSiblings.add(siblingBlock);
        }
        long txNonce = i;
        RskAddress coinbase = fixedCoinbase != null ? fixedCoinbase : TestUtils.randomAddress();
        Block block = createBlock(this.genesis, this.blockchain.getBestBlock(), PegTestUtils.createHash3(), coinbase, blockSiblings, minerFee, this.gasPrice, txNonce, this.txValue, this.txSigningKey, null);
        mainChainBlocks.add(block);
        blockExecutor.executeAndFillAll(block, this.blockchain.getBestBlock().getHeader());
        block.seal();
        ImportResult result = this.blockchain.tryToConnect(block);
        System.out.println(result);
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) TestUtils(org.ethereum.TestUtils) RskAddress(co.rsk.core.RskAddress) Coin(co.rsk.core.Coin) Keccak256(co.rsk.crypto.Keccak256) ArrayList(java.util.ArrayList) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) BlockHashesHelper(co.rsk.core.bc.BlockHashesHelper) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) HashUtil(org.ethereum.crypto.HashUtil) RLP(org.ethereum.util.RLP) TransactionExecutorFactory(co.rsk.core.TransactionExecutorFactory) BlockExecutor(co.rsk.core.bc.BlockExecutor) PegTestUtils(co.rsk.peg.PegTestUtils) Collectors(java.util.stream.Collectors) List(java.util.List) RepositorySnapshot(co.rsk.db.RepositorySnapshot) RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) org.ethereum.core(org.ethereum.core) Collections(java.util.Collections) Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ArrayList(java.util.ArrayList) ProgramInvokeFactoryImpl(org.ethereum.vm.program.invoke.ProgramInvokeFactoryImpl) TransactionExecutorFactory(co.rsk.core.TransactionExecutorFactory) RepositoryBtcBlockStoreWithCache(co.rsk.peg.RepositoryBtcBlockStoreWithCache) BlockDifficulty(co.rsk.core.BlockDifficulty) RskAddress(co.rsk.core.RskAddress) BridgeSupportFactory(co.rsk.peg.BridgeSupportFactory) BlockExecutor(co.rsk.core.bc.BlockExecutor) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts)

Example 14 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class AccountBuilder method build.

public Account build() {
    byte[] privateKeyBytes = HashUtil.keccak256(name.getBytes());
    ECKey key = ECKey.fromPrivate(privateKeyBytes);
    Account account = new Account(key);
    if (blockChain != null) {
        Block best = blockChain.getStatus().getBestBlock();
        BlockDifficulty td = blockChain.getStatus().getTotalDifficulty();
        RepositorySnapshot repository = repositoryLocator.snapshotAt(blockChain.getBestBlock().getHeader());
        Repository track = repository.startTracking();
        track.createAccount(account.getAddress());
        if (this.balance != null)
            track.addBalance(account.getAddress(), this.balance);
        if (this.code != null) {
            track.saveCode(account.getAddress(), this.code);
            track.getCode(account.getAddress());
        }
        track.commit();
        track.save();
        // Check that code is there...
        repository.getCode(account.getAddress());
        best.setStateRoot(repository.getRoot());
        best.flushRLP();
        blockStore.saveBlock(best, td, true);
    }
    return account;
}
Also used : Account(org.ethereum.core.Account) BlockDifficulty(co.rsk.core.BlockDifficulty) RepositorySnapshot(co.rsk.db.RepositorySnapshot) Repository(org.ethereum.core.Repository) Block(org.ethereum.core.Block) ECKey(org.ethereum.crypto.ECKey)

Example 15 with BlockDifficulty

use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.

the class IndexedBlockStoreTest method test6.

// leveldb + mapdb, multi branch, total difficulty test
@Test
@Ignore("Ethereum block format")
public void test6() throws IOException {
    BigInteger bi = new BigInteger(32, new Random());
    String testDir = "test_db_" + bi;
    config.setDataBaseDir(testDir);
    DB indexDB = createMapDB(testDir);
    KeyValueDataSource blocksDB = new LevelDbDataSource("blocks", config.databaseDir());
    blocksDB.init();
    try {
        IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, blocksDB, new MapDBBlocksIndex(indexDB));
        Block genesis = RskTestFactory.getGenesisInstance(config);
        List<Block> bestLine = getRandomChain(blockFactory, genesis.getHash().getBytes(), 1, 100);
        indexedBlockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
        BlockDifficulty td = genesis.getCumulativeDifficulty();
        for (int i = 0; i < bestLine.size(); ++i) {
            Block newBlock = bestLine.get(i);
            td = td.add(newBlock.getCumulativeDifficulty());
            indexedBlockStore.saveBlock(newBlock, td, true);
        }
        byte[] forkParentHash = bestLine.get(60).getHash().getBytes();
        long forkParentNumber = bestLine.get(60).getNumber();
        List<Block> forkLine = getRandomChain(blockFactory, forkParentHash, forkParentNumber + 1, 50);
        for (int i = 0; i < forkLine.size(); ++i) {
            Block newBlock = forkLine.get(i);
            Block parentBlock = indexedBlockStore.getBlockByHash(newBlock.getParentHash().getBytes());
            td = indexedBlockStore.getTotalDifficultyForHash(parentBlock.getHash().getBytes());
            td = td.add(newBlock.getCumulativeDifficulty());
            indexedBlockStore.saveBlock(newBlock, td, false);
        }
        // calc all TDs
        Map<Keccak256, BlockDifficulty> tDiffs = new HashMap<>();
        td = RskTestFactory.getGenesisInstance(config).getCumulativeDifficulty();
        for (Block block : bestLine) {
            td = td.add(block.getCumulativeDifficulty());
            tDiffs.put(block.getHash(), td);
        }
        Map<Keccak256, BlockDifficulty> tForkDiffs = new HashMap<>();
        Block block = forkLine.get(0);
        td = tDiffs.get(block.getParentHash());
        for (Block currBlock : forkLine) {
            td = td.add(currBlock.getCumulativeDifficulty());
            tForkDiffs.put(currBlock.getHash(), td);
        }
        // Assert tds on bestLine
        for (Keccak256 hash : tDiffs.keySet()) {
            BlockDifficulty currTD = tDiffs.get(hash);
            BlockDifficulty checkTd = indexedBlockStore.getTotalDifficultyForHash(hash.getBytes());
            assertEquals(checkTd, currTD);
        }
        // Assert tds on forkLine
        for (Keccak256 hash : tForkDiffs.keySet()) {
            BlockDifficulty currTD = tForkDiffs.get(hash);
            BlockDifficulty checkTd = indexedBlockStore.getTotalDifficultyForHash(hash.getBytes());
            assertEquals(checkTd, currTD);
        }
        indexedBlockStore.flush();
        // Assert tds on bestLine
        for (Keccak256 hash : tDiffs.keySet()) {
            BlockDifficulty currTD = tDiffs.get(hash);
            BlockDifficulty checkTd = indexedBlockStore.getTotalDifficultyForHash(hash.getBytes());
            assertEquals(checkTd, currTD);
        }
        // check total difficulty
        Block bestBlock = bestLine.get(bestLine.size() - 1);
        BlockDifficulty totalDifficulty = indexedBlockStore.getTotalDifficultyForHash(bestBlock.getHash().getBytes());
        BlockDifficulty totalDifficulty_ = tDiffs.get(bestBlock.getHash());
        assertEquals(totalDifficulty_, totalDifficulty);
        // Assert tds on forkLine
        for (Keccak256 hash : tForkDiffs.keySet()) {
            BlockDifficulty currTD = tForkDiffs.get(hash);
            BlockDifficulty checkTd = indexedBlockStore.getTotalDifficultyForHash(hash.getBytes());
            assertEquals(checkTd, currTD);
        }
    } finally {
        blocksDB.close();
        indexDB.close();
        FileUtil.recursiveDelete(testDir);
    }
}
Also used : Keccak256(co.rsk.crypto.Keccak256) LevelDbDataSource(org.ethereum.datasource.LevelDbDataSource) BlockDifficulty(co.rsk.core.BlockDifficulty) BigInteger(java.math.BigInteger) KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) Block(org.ethereum.core.Block) MapDBBlocksIndex(co.rsk.db.MapDBBlocksIndex) HashMapDB(org.ethereum.datasource.HashMapDB) DB(org.mapdb.DB) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

BlockDifficulty (co.rsk.core.BlockDifficulty)60 Test (org.junit.Test)32 Block (org.ethereum.core.Block)23 BigInteger (java.math.BigInteger)14 HashMapDB (org.ethereum.datasource.HashMapDB)11 Keccak256 (co.rsk.crypto.Keccak256)9 Ignore (org.junit.Ignore)9 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)8 BlockHeader (org.ethereum.core.BlockHeader)7 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)6 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)6 Coin (co.rsk.core.Coin)5 MapDBBlocksIndex (co.rsk.db.MapDBBlocksIndex)5 LevelDbDataSource (org.ethereum.datasource.LevelDbDataSource)5 BlockStore (org.ethereum.db.BlockStore)5 DifficultyCalculator (co.rsk.core.DifficultyCalculator)4 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)4 HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)4 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)4 ArrayList (java.util.ArrayList)4