Search in sources :

Example 51 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 52 with BlockDifficulty

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

the class StatusResolver method currentStatus.

/**
 * Resolves the current status to broadcast and send to peers.
 */
public Status currentStatus() {
    Status status;
    if (blockStore.getMinNumber() != 0) {
        status = new Status(genesis.getNumber(), genesis.getHash().getBytes(), genesis.getParentHash().getBytes(), genesis.getCumulativeDifficulty());
    } else {
        Block block = blockStore.getBestBlock();
        BlockDifficulty totalDifficulty = blockStore.getTotalDifficultyForHash(block.getHash().getBytes());
        status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), totalDifficulty);
    }
    return status;
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block)

Example 53 with BlockDifficulty

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

the class NodeMessageHandlerTest method processStatusMessageUsingSyncProcessor.

@Test()
public void processStatusMessageUsingSyncProcessor() {
    final SimplePeer sender = new SimplePeer();
    final ChannelManager channelManager = mock(ChannelManager.class);
    when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
    final NodeMessageHandler handler = NodeMessageHandlerUtil.createHandlerWithSyncProcessor(SyncConfiguration.IMMEDIATE_FOR_TESTING, channelManager);
    BlockGenerator blockGenerator = new BlockGenerator();
    final Block block = blockGenerator.createChildBlock(blockGenerator.getGenesisBlock());
    final Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), new BlockDifficulty(BigInteger.TEN));
    final Message message = new StatusMessage(status);
    handler.processMessage(sender, message);
    Assert.assertFalse(sender.getMessages().isEmpty());
    Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, sender.getMessages().get(0).getMessageType());
}
Also used : BlockDifficulty(co.rsk.core.BlockDifficulty) SimpleChannelManager(org.ethereum.rpc.Simples.SimpleChannelManager) ChannelManager(org.ethereum.net.server.ChannelManager) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) SimplePeer(co.rsk.net.simples.SimplePeer) Test(org.junit.Test)

Example 54 with BlockDifficulty

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

the class RskWireProtocolTest method activate_sendStatus.

@Test
public void activate_sendStatus() {
    BlockDifficulty blockDifficulty = new BlockDifficulty(BigInteger.valueOf(20));
    Status status = mock(Status.class);
    when(status.getTotalDifficulty()).thenReturn(blockDifficulty);
    when(statusResolver.currentStatus()).thenReturn(status);
    when(genesis.getHash()).thenReturn(new Keccak256(new byte[32]));
    NodeStatistics nodeStatistics = mock(NodeStatistics.class);
    when(nodeStatistics.getEthOutbound()).thenReturn(mock(NodeStatistics.StatHandler.class));
    when(channel.getNodeStatistics()).thenReturn(nodeStatistics);
    target.activate();
    verify(messageQueue, times(2)).sendMessage(any());
}
Also used : Status(co.rsk.net.Status) BlockDifficulty(co.rsk.core.BlockDifficulty) NodeStatistics(org.ethereum.net.NodeStatistics) Keccak256(co.rsk.crypto.Keccak256) Test(org.junit.Test)

Example 55 with BlockDifficulty

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

the class BlockDifficultyValidationRuleTest method testDifficulty.

@Test
public void testDifficulty() {
    DifficultyCalculator difficultyCalculator = new DifficultyCalculator(activationConfig, networkConstants);
    BlockDifficultyRule validationRule = new BlockDifficultyRule(difficultyCalculator);
    Block block = Mockito.mock(Block.class);
    Block parent = Mockito.mock(Block.class);
    long parentTimestamp = 0;
    long blockTimeStamp = 10;
    BlockDifficulty parentDifficulty = new BlockDifficulty(new BigInteger("2048"));
    BlockDifficulty blockDifficulty = new BlockDifficulty(new BigInteger("2049"));
    // blockDifficulty = blockDifficulty.add(AbstractConfig.getConstants().getDifficultyBoundDivisor());
    Mockito.when(block.getDifficulty()).thenReturn(blockDifficulty);
    BlockHeader blockHeader = getEmptyHeader(blockDifficulty, blockTimeStamp, 1);
    BlockHeader parentHeader = Mockito.mock(BlockHeader.class);
    Mockito.when(parentHeader.getDifficulty()).thenReturn(parentDifficulty);
    Mockito.when(block.getHeader()).thenReturn(blockHeader);
    Mockito.when(parent.getHeader()).thenReturn(parentHeader);
    Mockito.when(parent.getDifficulty()).thenReturn(parentDifficulty);
    Mockito.when(parent.getTimestamp()).thenReturn(parentTimestamp);
    Assert.assertEquals(validationRule.isValid(block, parent), true);
}
Also used : DifficultyCalculator(co.rsk.core.DifficultyCalculator) BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) BigInteger(java.math.BigInteger) BlockHeader(org.ethereum.core.BlockHeader) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

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