use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class BridgeSupportTest method callUpdateCollectionsWithTransactionsWaitingForConfirmationWithEnoughConfirmations.
@Test
public void callUpdateCollectionsWithTransactionsWaitingForConfirmationWithEnoughConfirmations() throws IOException, BlockStoreException {
// Bridge constants and btc context
BridgeConstants bridgeConstants = config.getBlockchainConfig().getCommonConstants().getBridgeConstants();
Context context = new Context(bridgeConstants.getBtcParams());
// Fake wallet returned every time
PowerMockito.mockStatic(BridgeUtils.class);
PowerMockito.when(BridgeUtils.getFederationSpendWallet(any(Context.class), any(Federation.class), any(List.class))).thenReturn(new SimpleWallet(context));
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
BtcTransaction txs = new BtcTransaction(btcParams);
txs.addOutput(Coin.FIFTY_COINS, new BtcECKey());
BtcTransaction tx1 = new BtcTransaction(btcParams);
tx1.addInput(txs.getOutput(0));
tx1.getInput(0).disconnect();
tx1.addOutput(Coin.COIN, new BtcECKey());
BtcTransaction tx2 = new BtcTransaction(btcParams);
tx2.addInput(txs.getOutput(0));
tx2.getInput(0).disconnect();
tx2.addOutput(Coin.COIN, new BtcECKey());
BtcTransaction tx3 = new BtcTransaction(btcParams);
tx3.addInput(txs.getOutput(0));
tx3.getInput(0).disconnect();
tx3.addOutput(Coin.COIN, new BtcECKey());
provider0.getReleaseTransactionSet().add(tx1, 1L);
provider0.getReleaseTransactionSet().add(tx2, 1L);
provider0.getReleaseTransactionSet().add(tx3, 1L);
provider0.save();
track.commit();
track = repository.startTracking();
BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
BlockGenerator blockGenerator = new BlockGenerator();
List<Block> blocks = blockGenerator.getSimpleBlockChain(blockGenerator.getGenesisBlock(), 10);
BlockChainBuilder builder = new BlockChainBuilder();
Blockchain blockchain = builder.setTesting(true).build();
for (Block block : blocks) blockchain.getBlockStore().saveBlock(block, TEST_DIFFICULTY, true);
org.ethereum.core.Block rskCurrentBlock = blocks.get(9);
Transaction rskTx = Transaction.create(config, TO_ADDRESS, DUST_AMOUNT, NONCE, GAS_PRICE, GAS_LIMIT, DATA);
rskTx.sign(new ECKey().getPrivKeyBytes());
BridgeSupport bridgeSupport = new BridgeSupport(config, track, mock(BridgeEventLogger.class), provider, rskCurrentBlock);
bridgeSupport.updateCollections(rskTx);
bridgeSupport.save();
track.commit();
BridgeStorageProvider provider2 = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Assert.assertEquals(0, provider2.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(2, provider2.getReleaseTransactionSet().getEntries().size());
Assert.assertEquals(1, provider2.getRskTxsWaitingForSignatures().size());
}
use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class BridgeSupportTest method callUpdateCollectionsThrowsExceededMaxTransactionSize.
@Test
public void callUpdateCollectionsThrowsExceededMaxTransactionSize() throws IOException, BlockStoreException {
// Federation is the genesis federation ATM
Federation federation = bridgeConstants.getGenesisFederation();
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
provider0.getReleaseRequestQueue().add(new BtcECKey().toAddress(btcParams), Coin.COIN.multiply(7));
for (int i = 0; i < 2000; i++) {
provider0.getNewFederationBtcUTXOs().add(new UTXO(PegTestUtils.createHash(), 1, Coin.CENT, 0, false, ScriptBuilder.createOutputScript(federation.getAddress())));
}
provider0.save();
track.commit();
track = repository.startTracking();
BlockGenerator blockGenerator = new BlockGenerator();
List<Block> blocks = blockGenerator.getSimpleBlockChain(blockGenerator.getGenesisBlock(), 10);
BlockChainBuilder builder = new BlockChainBuilder();
Blockchain blockchain = builder.setTesting(true).build();
for (Block block : blocks) blockchain.getBlockStore().saveBlock(block, TEST_DIFFICULTY, true);
org.ethereum.core.Block rskCurrentBlock = blocks.get(9);
ReceiptStore rskReceiptStore = null;
org.ethereum.db.BlockStore rskBlockStore = blockchain.getBlockStore();
Transaction tx = Transaction.create(config, TO_ADDRESS, DUST_AMOUNT, NONCE, GAS_PRICE, GAS_LIMIT, DATA);
tx.sign(new ECKey().getPrivKeyBytes());
BridgeSupport bridgeSupport = new BridgeSupport(config, track, mock(BridgeEventLogger.class), PrecompiledContracts.BRIDGE_ADDR, rskCurrentBlock);
bridgeSupport.updateCollections(tx);
bridgeSupport.save();
track.commit();
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
Assert.assertEquals(1, provider.getReleaseRequestQueue().getEntries().size());
Assert.assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
Assert.assertEquals(0, provider.getRskTxsWaitingForSignatures().size());
// Check the wallet has not been emptied
Assert.assertFalse(provider.getNewFederationBtcUTXOs().isEmpty());
}
use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class BridgeSupportTest method callUpdateCollectionsChangeGetsOutOfDust.
@Test
public void callUpdateCollectionsChangeGetsOutOfDust() throws IOException, BlockStoreException {
// Federation is the genesis federation ATM
Federation federation = bridgeConstants.getGenesisFederation();
Map<byte[], BigInteger> preMineMap = new HashMap<byte[], BigInteger>();
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();
Blockchain blockchain = builder.setTesting(true).setGenesis(genesisBlock).build();
for (Block block : blocks) blockchain.getBlockStore().saveBlock(block, TEST_DIFFICULTY, true);
org.ethereum.core.Block rskCurrentBlock = blocks.get(9);
Repository repository = blockchain.getRepository();
Repository track = repository.startTracking();
BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
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.create(config, TO_ADDRESS, DUST_AMOUNT, NONCE, GAS_PRICE, GAS_LIMIT, DATA);
tx.sign(new ECKey().getPrivKeyBytes());
BridgeSupport bridgeSupport = new BridgeSupport(config, track, mock(BridgeEventLogger.class), PrecompiledContracts.BRIDGE_ADDR, rskCurrentBlock);
bridgeSupport.updateCollections(tx);
bridgeSupport.save();
track.commit();
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
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(config.getBlockchainConfig().getCommonConstants().getBurnAddress()));
// 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 BlockChainBuilderTest method createBlockChain.
@Test
public void createBlockChain() {
BlockChainBuilder builder = new BlockChainBuilder();
BlockChainImpl blockChain = builder.build();
Assert.assertNotNull(blockChain);
Assert.assertNotNull(blockChain.getRepository());
Assert.assertNotNull(blockChain.getBlockStore());
Assert.assertNotNull(blockChain.getListener());
Assert.assertNotNull(blockChain.getBlockValidator());
}
use of co.rsk.test.builders.BlockChainBuilder in project rskj by rsksmart.
the class SyncProcessorTest method sendSkeletonRequest.
@Test
public void sendSkeletonRequest() {
Blockchain blockchain = new BlockChainBuilder().ofSize(100);
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.sendSkeletonRequest(sender, 0);
Assert.assertFalse(sender.getMessages().isEmpty());
Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.SKELETON_REQUEST_MESSAGE, message.getMessageType());
SkeletonRequestMessage request = (SkeletonRequestMessage) message;
Assert.assertNotEquals(0, request.getId());
Assert.assertEquals(0, request.getStartNumber());
Assert.assertEquals(1, processor.getExpectedResponses().size());
}
Aggregations