use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class SyncProcessorTest method processBlockResponseAddsToBlockchain.
@Test
public void processBlockResponseAddsToBlockchain() {
final BlockStore store = new BlockStore();
Blockchain blockchain = BlockChainBuilder.ofSize(10);
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
Block block = new BlockGenerator().createChildBlock(blockchain.getBlockByNumber(10));
Assert.assertEquals(11, block.getNumber());
Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
BlockNodeInformation nodeInformation = new BlockNodeInformation();
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
BlockResponseMessage response = new BlockResponseMessage(new Random().nextLong(), block);
processor.registerExpectedMessage(response);
processor.processBlockResponse(sender, response);
Assert.assertEquals(11, blockchain.getBestBlock().getNumber());
Assert.assertArrayEquals(block.getHash().getBytes(), blockchain.getBestBlockHash());
Assert.assertTrue(processor.getExpectedResponses().isEmpty());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class SyncProcessorTest method doesntProcessUnexpectedBodyResponse.
@Test
public void doesntProcessUnexpectedBodyResponse() {
final BlockStore store = new BlockStore();
Blockchain blockchain = BlockChainBuilder.ofSize(10);
SimpleMessageChannel sender = new SimpleMessageChannel(new byte[] { 0x01 });
Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
Block block = new BlockGenerator().createChildBlock(blockchain.getBlockByNumber(10));
Blockchain extended = BlockChainBuilder.copy(blockchain);
extended.tryToConnect(block);
Assert.assertEquals(11, block.getNumber());
Assert.assertArrayEquals(blockchain.getBestBlockHash(), block.getParentHash().getBytes());
BlockNodeInformation nodeInformation = new BlockNodeInformation();
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, SyncConfiguration.IMMEDIATE_FOR_TESTING);
SyncProcessor processor = new SyncProcessor(config, blockchain, blockSyncService, RskMockFactory.getPeerScoringManager(), getChannelManager(), SyncConfiguration.IMMEDIATE_FOR_TESTING, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), DIFFICULTY_CALCULATOR);
processor.setSelectedPeer(sender, StatusUtils.getFakeStatus(), 0);
List<Transaction> transactions = blockchain.getBestBlock().getTransactionsList();
List<BlockHeader> uncles = blockchain.getBestBlock().getUncleList();
long lastRequestId = new Random().nextLong();
BodyResponseMessage response = new BodyResponseMessage(lastRequestId, transactions, uncles);
processor.registerExpectedMessage(response);
Deque<BlockHeader> headerStack = new ArrayDeque<>();
headerStack.add(block.getHeader());
List<Deque<BlockHeader>> headers = new ArrayList<>();
headers.add(headerStack);
int connectionPoint = 10;
int step = 192;
int linkCount = 1;
processor.startDownloadingBodies(headers, Collections.singletonMap(sender.getPeerNodeID(), buildSkeleton(extended, connectionPoint, step, linkCount)));
// ((DownloadingBodiesSyncState)processor.getSyncState()).expectBodyResponseFor(lastRequestId, sender.getPeerNodeID(), block.getHeader());
processor.processBodyResponse(sender, response);
Assert.assertEquals(10, blockchain.getBestBlock().getNumber());
Assert.assertNotEquals(block.getNumber(), blockchain.getBestBlock().getNumber());
// if an invalid body arrives then stops syncing
Assert.assertFalse(processor.getSyncState().isSyncing());
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class LogFilterTest method noEventsAfterEmptyBlock.
@Test
public void noEventsAfterEmptyBlock() {
LogFilter filter = new LogFilter(null, null, false, false);
Block block = new BlockGenerator().getBlock(1);
filter.newBlockReceived(block);
Object[] result = filter.getEvents();
Assert.assertNotNull(result);
Assert.assertEquals(0, result.length);
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class BlockUnclesValidationRuleTest method rejectBlockWithSiblingUncle.
@Test
public void rejectBlockWithSiblingUncle() {
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
Block block1 = blockGenerator.createChildBlock(genesis);
Block uncle = blockGenerator.createChildBlock(block1);
List<BlockHeader> uncles = new ArrayList<>();
uncles.add(uncle.getHeader());
Block block = blockGenerator.createChildBlock(block1, null, uncles, 1, null);
BlockChainImpl blockChain = BlockChainImplTest.createBlockChain();
BlockStore store = blockChain.getBlockStore();
store.saveBlock(genesis, new BlockDifficulty(BigInteger.valueOf(1)), true);
store.saveBlock(block1, new BlockDifficulty(BigInteger.valueOf(2)), true);
BlockUnclesValidationRule rule = new BlockUnclesValidationRule(config, store, 10, 10, new BlockCompositeRule(), new BlockParentCompositeRule());
Assert.assertFalse(rule.isValid(block));
}
use of co.rsk.blockchain.utils.BlockGenerator in project rskj by rsksmart.
the class BlockchainVMTest method testSEND_1.
@Test
public void testSEND_1() {
NewBlockChainInfo binfo = createNewBlockchain();
Blockchain blockchain = binfo.blockchain;
BlockGenerator blockGenerator = new BlockGenerator();
Block block1 = blockGenerator.createChildBlock(blockchain.getBestBlock(), null, binfo.repository.getRoot());
List<Transaction> txs = new ArrayList<>();
Coin transferAmount = Coin.valueOf(100L);
// Add a single transaction paying to a new address
byte[] dstAddress = randomAddress();
BigInteger transactionGasLimit = new BigInteger("21000");
Coin transactionGasPrice = Coin.valueOf(1);
Transaction t = new Transaction(ZERO_BYTE_ARRAY, transactionGasPrice.getBytes(), transactionGasLimit.toByteArray(), dstAddress, transferAmount.getBytes(), null, new RskSystemProperties().getBlockchainConfig().getCommonConstants().getChainId());
t.sign(binfo.faucetKey.getPrivKeyBytes());
txs.add(t);
Block block2 = blockGenerator.createChildBlock(block1, txs, binfo.repository.getRoot());
Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(block1));
MinerHelper mh = new MinerHelper(binfo.repository, binfo.blockchain);
mh.completeBlock(block2, block1);
Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(block2));
Assert.assertEquals(blockchain.getBestBlock(), block2);
Assert.assertEquals(2, block2.getNumber());
Coin srcAmount = faucetAmount.subtract(transferAmount);
srcAmount = srcAmount.subtract(transactionGasPrice.multiply(transactionGasLimit));
Assert.assertEquals(binfo.repository.getBalance(new RskAddress(binfo.faucetKey.getAddress())), srcAmount);
Assert.assertEquals(binfo.repository.getBalance(new RskAddress(dstAddress)), transferAmount);
}
Aggregations