Search in sources :

Example 46 with BlockDifficulty

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

the class BridgeTestPowerMock method callUpdateCollectionsWithTransactionsWaitingForConfirmationWithEnoughConfirmations.

@Test
public void callUpdateCollectionsWithTransactionsWaitingForConfirmationWithEnoughConfirmations() throws IOException, VMException {
    BtcTransaction tx1 = createTransaction();
    BtcTransaction tx2 = createTransaction();
    BtcTransaction tx3 = createTransaction();
    Repository repository = createRepository();
    Repository track = repository.startTracking();
    BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationConfig.forBlock(0));
    provider0.getReleaseTransactionSet().add(tx1, 1L);
    provider0.getReleaseTransactionSet().add(tx2, 2L);
    provider0.getReleaseTransactionSet().add(tx3, 3L);
    provider0.save();
    track.commit();
    track = repository.startTracking();
    World world = new World();
    List<Block> blocks = new BlockGenerator().getSimpleBlockChain(world.getBlockChain().getBestBlock(), 10);
    Transaction rskTx = Transaction.builder().nonce(NONCE).gasPrice(GAS_PRICE).gasLimit(GAS_LIMIT).destination(Hex.decode(PrecompiledContracts.BRIDGE_ADDR_STR)).data(Hex.decode(DATA)).chainId(Constants.REGTEST_CHAIN_ID).value(AMOUNT).build();
    rskTx.sign(fedECPrivateKey.getPrivKeyBytes());
    world.getBlockStore().saveBlock(blocks.get(1), new BlockDifficulty(BigInteger.ONE), true);
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams()), bridgeConstants, activationConfig);
    Bridge bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory);
    bridge.init(rskTx, blocks.get(9), track, world.getBlockStore(), null, new LinkedList<>());
    bridge.execute(Bridge.UPDATE_COLLECTIONS.encode());
    track.commit();
    // reusing same storage configuration as the height doesn't affect storage configurations for releases.
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationConfigAll);
    Assert.assertEquals(2, provider.getReleaseTransactionSet().getEntries().size());
    Assert.assertEquals(1, provider.getRskTxsWaitingForSignatures().size());
}
Also used : SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) World(co.rsk.test.World) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockDifficulty(co.rsk.core.BlockDifficulty) MutableRepository(org.ethereum.db.MutableRepository) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 47 with BlockDifficulty

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

the class MessageTest method encodeDecodeStatusMessageWithCompleteArguments.

@Test
public void encodeDecodeStatusMessageWithCompleteArguments() {
    Block block = blockGenerator.getBlock(1);
    Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), new BlockDifficulty(BigInteger.TEN));
    StatusMessage message = new StatusMessage(status);
    byte[] encoded = message.getEncoded();
    Message result = Message.create(blockFactory, encoded);
    Assert.assertNotNull(result);
    Assert.assertArrayEquals(encoded, result.getEncoded());
    Assert.assertEquals(MessageType.STATUS_MESSAGE, result.getMessageType());
    StatusMessage newmessage = (StatusMessage) result;
    Assert.assertArrayEquals(block.getHash().getBytes(), newmessage.getStatus().getBestBlockHash());
    Assert.assertEquals(block.getNumber(), newmessage.getStatus().getBestBlockNumber());
}
Also used : Status(co.rsk.net.Status) BlockDifficulty(co.rsk.core.BlockDifficulty) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 48 with BlockDifficulty

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

the class StatusMessageTest method createWithCompleteArguments.

@Test
public void createWithCompleteArguments() {
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    Block block = blockGenerator.createChildBlock(genesis);
    Status status = new Status(block.getNumber(), block.getHash().getBytes(), block.getParentHash().getBytes(), new BlockDifficulty(BigInteger.TEN));
    StatusMessage message = new StatusMessage(status);
    Assert.assertEquals(MessageType.STATUS_MESSAGE, message.getMessageType());
    Assert.assertSame(status, message.getStatus());
    Assert.assertEquals(1, message.getStatus().getBestBlockNumber());
    Assert.assertArrayEquals(block.getHash().getBytes(), message.getStatus().getBestBlockHash());
    Assert.assertNotNull(message.getStatus().getBestBlockParentHash());
    Assert.assertArrayEquals(block.getParentHash().getBytes(), message.getStatus().getBestBlockParentHash());
    Assert.assertNotNull(message.getStatus().getTotalDifficulty());
    Assert.assertEquals(new BlockDifficulty(BigInteger.TEN), message.getStatus().getTotalDifficulty());
}
Also used : Status(co.rsk.net.Status) BlockDifficulty(co.rsk.core.BlockDifficulty) Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Test(org.junit.Test)

Example 49 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 50 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)

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