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());
}
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());
}
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());
}
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();
}
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));
}
}
Aggregations