Search in sources :

Example 41 with BlockDifficulty

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

the class DownloadingBackwardsBodiesSyncStateTest method onEnter_connectGenesis.

@Test
public void onEnter_connectGenesis() {
    List<BlockHeader> toRequest = new LinkedList<>();
    DownloadingBackwardsBodiesSyncState target = new DownloadingBackwardsBodiesSyncState(syncConfiguration, syncEventsHandler, peersInformation, genesis, blockFactory, blockStore, child, toRequest, peer);
    Keccak256 childHash = new Keccak256(new byte[32]);
    when(child.getHash()).thenReturn(childHash);
    when(child.getNumber()).thenReturn(1L);
    when(genesis.isParentOf(child)).thenReturn(true);
    when(child.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(50)));
    BlockDifficulty cumulativeDifficulty = new BlockDifficulty(BigInteger.valueOf(50));
    when(genesis.getCumulativeDifficulty()).thenReturn(cumulativeDifficulty);
    when(blockStore.getTotalDifficultyForHash(eq(childHash.getBytes()))).thenReturn(new BlockDifficulty(BigInteger.valueOf(100)));
    target.onEnter();
    verify(blockStore).saveBlock(eq(genesis), eq(cumulativeDifficulty), eq(true));
    verify(blockStore).flush();
    verify(syncEventsHandler).stopSyncing();
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Keccak256(co.rsk.crypto.Keccak256) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 42 with BlockDifficulty

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

the class DownloadingBackwardsBodiesSyncStateTest method connectingUntilGenesis.

@Test
public void connectingUntilGenesis() {
    LinkedList<BlockHeader> toRequest = new LinkedList<>();
    LinkedList<BodyResponseMessage> responses = new LinkedList<>();
    LinkedList<Block> expectedBlocks = new LinkedList<>();
    Function<Long, BlockDifficulty> difficultyForBlockNumber = (n) -> new BlockDifficulty(BigInteger.valueOf(n * (n + 1) / 2));
    // their indexes and each one is the children of the previous block.
    for (long i = 1; i <= 10; i++) {
        BlockHeader headerToRequest = mock(BlockHeader.class);
        when(headerToRequest.getNumber()).thenReturn(i);
        Keccak256 headerHash = new Keccak256(ByteUtil.leftPadBytes(ByteUtil.longToBytes(i), 32));
        when(headerToRequest.getHash()).thenReturn(headerHash);
        toRequest.addFirst(headerToRequest);
        when(syncEventsHandler.sendBodyRequest(any(), eq(headerToRequest))).thenReturn(i);
        BodyResponseMessage response = new BodyResponseMessage(i, new LinkedList<>(), new LinkedList<>());
        responses.addFirst(response);
        Block block = mock(Block.class);
        expectedBlocks.addFirst(block);
        when(block.getNumber()).thenReturn(i);
        when(block.getHash()).thenReturn(headerHash);
        when(blockFactory.newBlock(headerToRequest, response.getTransactions(), response.getUncles())).thenReturn(block);
        when(block.isParentOf(any())).thenReturn(true);
        when(blockStore.getTotalDifficultyForHash(headerHash.getBytes())).thenReturn(difficultyForBlockNumber.apply(i));
        when(block.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(i)));
    }
    when(genesis.isParentOf(expectedBlocks.getLast())).thenReturn(true);
    when(genesis.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(0L)));
    Keccak256 childHash = new Keccak256(ByteUtil.leftPadBytes(ByteUtil.intToBytes(11), 32));
    when(child.getHash()).thenReturn(childHash);
    when(blockStore.getTotalDifficultyForHash(childHash.getBytes())).thenReturn(difficultyForBlockNumber.apply(11L));
    when(child.getCumulativeDifficulty()).thenReturn(new BlockDifficulty(BigInteger.valueOf(11L)));
    when(child.getNumber()).thenReturn(11L);
    DownloadingBackwardsBodiesSyncState target = new DownloadingBackwardsBodiesSyncState(syncConfiguration, syncEventsHandler, peersInformation, genesis, blockFactory, blockStore, child, toRequest, peer);
    while (!responses.isEmpty()) {
        target.onEnter();
        target.newBody(responses.pop(), mock(Peer.class));
        Block block = expectedBlocks.pop();
        BlockDifficulty expectedDifficulty = difficultyForBlockNumber.apply(block.getNumber());
        verify(blockStore).saveBlock(eq(block), eq(expectedDifficulty), eq(true));
    }
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Peer(co.rsk.net.Peer) Test(org.junit.Test) BlockStore(org.ethereum.db.BlockStore) Keccak256(co.rsk.crypto.Keccak256) Function(java.util.function.Function) Mockito(org.mockito.Mockito) List(java.util.List) ByteUtil(org.ethereum.util.ByteUtil) BigInteger(java.math.BigInteger) LinkedList(java.util.LinkedList) BodyResponseMessage(co.rsk.net.messages.BodyResponseMessage) org.ethereum.core(org.ethereum.core) Before(org.junit.Before) BodyResponseMessage(co.rsk.net.messages.BodyResponseMessage) Peer(co.rsk.net.Peer) Keccak256(co.rsk.crypto.Keccak256) LinkedList(java.util.LinkedList) BlockDifficulty(co.rsk.core.BlockDifficulty) Test(org.junit.Test)

Example 43 with BlockDifficulty

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

the class RemascTestRunner method createBlock.

public static Block createBlock(Block genesis, Block parentBlock, Keccak256 blockHash, RskAddress coinbase, List<BlockHeader> uncles, Long difficulty, Transaction... txsToInlcude) {
    List<Transaction> txs = new ArrayList<>();
    if (txsToInlcude != null) {
        for (Transaction tx : txsToInlcude) {
            txs.add(tx);
        }
    }
    Transaction remascTx = new RemascTransaction(parentBlock.getNumber() + 1);
    txs.add(remascTx);
    BigInteger difficultyAsBI = difficulty == null ? parentBlock.getDifficulty().asBigInteger() : BigInteger.valueOf(difficulty);
    if (difficultyAsBI.equals(BigInteger.ZERO)) {
        difficultyAsBI = BigInteger.ONE;
    }
    BlockDifficulty difficultyAsBD = new BlockDifficulty(difficultyAsBI);
    Coin paidFees = Coin.ZERO;
    for (Transaction tx : txs) {
        BigInteger gasLimit = new BigInteger(1, tx.getGasLimit());
        Coin gasPrice = tx.getGasPrice();
        paidFees = paidFees.add(gasPrice.multiply(gasLimit));
    }
    return new Block(new HardcodedHashBlockHeader(parentBlock, coinbase, genesis, txs, difficultyAsBD, paidFees, uncles, blockHash), txs, uncles, true, false);
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Coin(co.rsk.core.Coin) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger)

Example 44 with BlockDifficulty

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

the class MainNetMinerTest method submitBitcoinBlockProofOfWorkNotGoodEnough.

/*
     * This test is probabilistic, but it has a really high chance to pass. We will generate
     * a random block that it is unlikely to pass the Long.MAX_VALUE difficulty, though
     * it may happen once. Twice would be suspicious.
     */
@Test
public void submitBitcoinBlockProofOfWorkNotGoodEnough() {
    /* We need a low target */
    BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
    BlockChainImpl blockchain = blockChainBuilder.build();
    Genesis gen = (Genesis) BlockChainImplTest.getGenesisBlock(blockChainBuilder.getTrieStore());
    gen.getHeader().setDifficulty(new BlockDifficulty(BigInteger.valueOf(Long.MAX_VALUE)));
    blockchain.setStatus(gen, gen.getCumulativeDifficulty());
    EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
    MinerClock clock = new MinerClock(true, Clock.systemUTC());
    MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, mainchainView, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), blockToMineBuilder(), clock, blockFactory, new BuildInfo("cb7f28e", "master"), ConfigUtils.getDefaultMiningConfig());
    try {
        minerServer.start();
        MinerWork work = minerServer.getWork();
        co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlock(work);
        bitcoinMergedMiningBlock.setNonce(2);
        SubmitBlockResult result = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
        Assert.assertEquals("ERROR", result.getStatus());
        Assert.assertNull(result.getBlockInfo());
        Mockito.verify(ethereumImpl, Mockito.times(0)).addNewMinedBlock(Mockito.any());
    } finally {
        minerServer.stop();
    }
}
Also used : BlockChainImpl(co.rsk.core.bc.BlockChainImpl) EthereumImpl(org.ethereum.facade.EthereumImpl) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) ProofOfWorkRule(co.rsk.validators.ProofOfWorkRule) BlockDifficulty(co.rsk.core.BlockDifficulty) BuildInfo(org.ethereum.util.BuildInfo) Genesis(org.ethereum.core.Genesis) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) BlockChainImplTest(co.rsk.core.bc.BlockChainImplTest)

Example 45 with BlockDifficulty

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

the class StatusTest method createWithCompleteArguments.

@Test
public void createWithCompleteArguments() {
    byte[] hash = HashUtil.randomHash();
    byte[] parentHash = HashUtil.randomHash();
    Status status = new Status(42, hash, parentHash, new BlockDifficulty(BigInteger.TEN));
    Assert.assertEquals(42, status.getBestBlockNumber());
    Assert.assertNotNull(status.getBestBlockHash());
    Assert.assertArrayEquals(hash, status.getBestBlockHash());
    Assert.assertNotNull(status.getBestBlockParentHash());
    Assert.assertArrayEquals(parentHash, status.getBestBlockParentHash());
    Assert.assertNotNull(status.getTotalDifficulty());
    Assert.assertEquals(new BlockDifficulty(BigInteger.TEN), status.getTotalDifficulty());
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) 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