use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method callUpdateCollectionsChangeGetsOutOfDust.
@Test
public void callUpdateCollectionsChangeGetsOutOfDust() throws IOException {
// Federation is the genesis federation ATM
Federation federation = bridgeConstants.getGenesisFederation();
Map<byte[], BigInteger> preMineMap = new HashMap<>();
preMineMap.put(PrecompiledContracts.BRIDGE_ADDR.getBytes(), LIMIT_MONETARY_BASE.asBigInteger());
BlockGenerator blockGenerator = new BlockGenerator();
Genesis genesisBlock = (Genesis) blockGenerator.getNewGenesisBlock(0, preMineMap);
List<Block> blocks = blockGenerator.getSimpleBlockChain(genesisBlock, 10);
BlockChainBuilder builder = new BlockChainBuilder();
builder.setTesting(true).setGenesis(genesisBlock).build();
for (Block block : blocks) builder.getBlockStore().saveBlock(block, TEST_DIFFICULTY, true);
org.ethereum.core.Block rskCurrentBlock = blocks.get(9);
Repository repository = builder.getRepository();
Repository track = repository.startTracking();
BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
provider0.getReleaseRequestQueue().add(new BtcECKey().toAddress(btcParams), Coin.COIN);
provider0.getNewFederationBtcUTXOs().add(new UTXO(PegTestUtils.createHash(), 1, Coin.COIN.add(Coin.valueOf(100)), 0, false, ScriptBuilder.createOutputScript(federation.getAddress())));
provider0.save();
track.commit();
track = repository.startTracking();
Transaction tx = Transaction.builder().nonce(NONCE).gasPrice(GAS_PRICE).gasLimit(GAS_LIMIT).destination(Hex.decode(TO_ADDRESS)).data(Hex.decode(DATA)).chainId(Constants.REGTEST_CHAIN_ID).value(DUST_AMOUNT).build();
tx.sign(new ECKey().getPrivKeyBytes());
BridgeStorageProvider providerForSupport = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, providerForSupport, track, mock(BridgeEventLogger.class), rskCurrentBlock, null);
bridgeSupport.updateCollections(tx);
bridgeSupport.save();
track.commit();
// reusing same bridge storage configuration
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activationsBeforeForks);
Assert.assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(1, provider.getReleaseTransactionSet().getEntries().size());
Assert.assertEquals(0, provider.getRskTxsWaitingForSignatures().size());
Assert.assertEquals(LIMIT_MONETARY_BASE.subtract(co.rsk.core.Coin.fromBitcoin(Coin.valueOf(2600))), repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
Assert.assertEquals(co.rsk.core.Coin.fromBitcoin(Coin.valueOf(2600)), repository.getBalance(BridgeSupport.BURN_ADDRESS));
// Check the wallet has been emptied
Assert.assertTrue(provider.getNewFederationBtcUTXOs().isEmpty());
}
use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class AsyncNodeBlockProcessorUnclesTest method setUp.
@Before
public void setUp() {
blockChainBuilder = new BlockChainBuilder();
blockChain = blockChainBuilder.build();
listener = new AsyncNodeBlockProcessorListener();
processor = createAsyncNodeBlockProcessor(blockChain, listener);
processor.start();
}
use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class SyncProcessorTest method sendBlockHashRequest.
@Test
public void sendBlockHashRequest() {
Blockchain blockchain = new BlockChainBuilder().ofSize(0);
SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
final ChannelManager channelManager = mock(ChannelManager.class);
Peer channel = mock(Peer.class);
when(channel.getPeerNodeID()).thenReturn(sender.getPeerNodeID());
when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(channel));
SyncProcessor processor = new SyncProcessor(blockchain, mock(org.ethereum.db.BlockStore.class), mock(ConsensusValidationMainchainView.class), null, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
processor.sendBlockHashRequest(sender, 100);
Assert.assertFalse(sender.getMessages().isEmpty());
Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_HASH_REQUEST_MESSAGE, message.getMessageType());
BlockHashRequestMessage request = (BlockHashRequestMessage) message;
Assert.assertNotEquals(0, request.getId());
Assert.assertEquals(100, request.getHeight());
Assert.assertEquals(1, processor.getExpectedResponses().size());
}
use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class SyncProcessorTest method syncWithAdvancedStatusAnd5Peers.
@Test
public void syncWithAdvancedStatusAnd5Peers() {
final NetBlockStore store = new NetBlockStore();
BlockChainBuilder builder = new BlockChainBuilder();
Blockchain blockchain = builder.ofSize(0);
BlockStore blockStore = builder.getBlockStore();
byte[] hash = HashUtil.randomHash();
byte[] parentHash = HashUtil.randomHash();
Status status = new Status(100, hash, parentHash, blockchain.getTotalDifficulty().add(new BlockDifficulty(BigInteger.TEN)));
BlockNodeInformation nodeInformation = new BlockNodeInformation();
TestSystemProperties config = new TestSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
final ChannelManager channelManager = mock(ChannelManager.class);
SyncProcessor processor = new SyncProcessor(blockchain, blockStore, mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.DEFAULT, blockFactory, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.DEFAULT, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
List<SimplePeer> senders = new ArrayList<>();
int lessPeers = SyncConfiguration.DEFAULT.getExpectedPeers() - 1;
for (int i = 0; i < lessPeers; i++) {
SimplePeer sender = new SimplePeer();
senders.add(sender);
when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
}
Assert.assertTrue(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
senders.forEach(s -> processor.processStatus(s, status));
Assert.assertTrue(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
Assert.assertEquals(lessPeers, processor.getNoAdvancedPeers());
Set<NodeID> knownPeersNodeIDs = processor.getKnownPeersNodeIDs();
Assert.assertTrue(senders.stream().map(SimplePeer::getPeerNodeID).allMatch(knownPeersNodeIDs::contains));
SimplePeer lastSender = new SimplePeer();
Assert.assertFalse(processor.getKnownPeersNodeIDs().contains(lastSender.getPeerNodeID()));
processor.processStatus(lastSender, status);
// now test with all senders
senders.add(lastSender);
Set<NodeID> ids = processor.getKnownPeersNodeIDs();
Assert.assertTrue(ids.contains(lastSender.getPeerNodeID()));
Assert.assertFalse(senders.stream().map(SimplePeer::getMessages).allMatch(List::isEmpty));
Assert.assertEquals(1, senders.stream().map(SimplePeer::getMessages).mapToInt(List::size).sum());
Message message = senders.stream().map(SimplePeer::getMessages).filter(m -> !m.isEmpty()).findFirst().get().get(0);
Assert.assertEquals(MessageType.BLOCK_HEADERS_REQUEST_MESSAGE, message.getMessageType());
BlockHeadersRequestMessage request = (BlockHeadersRequestMessage) message;
Assert.assertEquals(status.getBestBlockHash(), request.getHash());
}
use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class SyncProcessorTest method findConnectionPointBlockchainWithGenesisVsBlockchainWith100Blocks.
@Test
public void findConnectionPointBlockchainWithGenesisVsBlockchainWith100Blocks() {
BlockChainBuilder builder = new BlockChainBuilder();
Blockchain blockchain = builder.ofSize(0);
Blockchain advancedBlockchain = new BlockChainBuilder().ofSize(100);
SimplePeer sender = new SimplePeer(new byte[] { 0x01 });
final ChannelManager channelManager = mock(ChannelManager.class);
when(channelManager.getActivePeers()).thenReturn(Collections.singletonList(sender));
NetBlockStore store = new NetBlockStore();
BlockNodeInformation nodeInformation = new BlockNodeInformation();
TestSystemProperties config = new TestSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING, DummyBlockValidator.VALID_RESULT_INSTANCE);
SyncProcessor processor = new SyncProcessor(blockchain, builder.getBlockStore(), mock(ConsensusValidationMainchainView.class), blockSyncService, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockFactory, new DummyBlockValidationRule(), new SyncBlockValidatorRule(new BlockUnclesHashValidationRule(), new BlockRootValidationRule(config.getActivationConfig())), DIFFICULTY_CALCULATOR, new PeersInformation(channelManager, SyncConfiguration.IMMEDIATE_FOR_TESTING, blockchain, RskMockFactory.getPeerScoringManager()), mock(Genesis.class), mock(EthereumListener.class));
processor.processStatus(sender, StatusUtils.fromBlockchain(advancedBlockchain));
Assert.assertFalse(sender.getMessages().isEmpty());
List<Message> messages = sender.getMessages();
BlockHeadersRequestMessage requestMessage = (BlockHeadersRequestMessage) messages.get(0);
processor.processBlockHeadersResponse(sender, new BlockHeadersResponseMessage(requestMessage.getId(), Collections.singletonList(advancedBlockchain.getBestBlock().getHeader())));
long[] expectedHeights = new long[] { 50, 25, 12, 6, 3, 1 };
for (int k = 0; k < expectedHeights.length; k++) {
Assert.assertEquals(k + 2, messages.size());
Message message = messages.get(k + 1);
Assert.assertEquals(MessageType.BLOCK_HASH_REQUEST_MESSAGE, message.getMessageType());
BlockHashRequestMessage request = (BlockHashRequestMessage) message;
long requestId = request.getId();
Assert.assertEquals(expectedHeights[k], request.getHeight());
Block block = advancedBlockchain.getBlockByNumber(expectedHeights[k]);
processor.processBlockHashResponse(sender, new BlockHashResponseMessage(requestId, block.getHash().getBytes()));
}
Assert.assertEquals(expectedHeights.length + 2, messages.size());
Message message = messages.get(messages.size() - 1);
Assert.assertEquals(MessageType.SKELETON_REQUEST_MESSAGE, message.getMessageType());
SkeletonRequestMessage request = (SkeletonRequestMessage) message;
Assert.assertEquals(0, request.getStartNumber());
Assert.assertEquals(1, processor.getExpectedResponses().size());
}
Aggregations