use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class MinerServerTest method submitBitcoinBlockTwoTags.
@Test
public void submitBitcoinBlockTwoTags() {
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
Mockito.when(ethereumImpl.addNewMinedBlock(Mockito.any())).thenReturn(ImportResult.IMPORTED_BEST);
BlockUnclesValidationRule unclesValidationRule = Mockito.mock(BlockUnclesValidationRule.class);
Mockito.when(unclesValidationRule.isValid(Mockito.any())).thenReturn(true);
MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new BlockToMineBuilder(ConfigUtils.getDefaultMiningConfig(), blockchain.getRepository(), blockchain.getBlockStore(), blockchain.getTransactionPool(), DIFFICULTY_CALCULATOR, new GasLimitCalculator(config), unclesValidationRule, config, null), ConfigUtils.getDefaultMiningConfig());
try {
byte[] extraData = ByteBuffer.allocate(4).putInt(1).array();
minerServer.setExtraData(extraData);
minerServer.start();
MinerWork work = minerServer.getWork();
Block bestBlock = blockchain.getBestBlock();
extraData = ByteBuffer.allocate(4).putInt(2).array();
minerServer.setExtraData(extraData);
minerServer.buildBlockToMine(bestBlock, false);
// only the tag is used
MinerWork work2 = minerServer.getWork();
Assert.assertNotEquals(work2.getBlockHashForMergedMining(), work.getBlockHashForMergedMining());
BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlockWithTwoTags(work, work2);
findNonce(work, bitcoinMergedMiningBlock);
SubmitBlockResult result;
result = ((MinerServerImpl) minerServer).submitBitcoinBlock(work2.getBlockHashForMergedMining(), bitcoinMergedMiningBlock, true);
Assert.assertEquals("OK", result.getStatus());
Assert.assertNotNull(result.getBlockInfo());
Assert.assertEquals("0x1", result.getBlockInfo().getBlockIncludedHeight());
Assert.assertEquals("0x494d504f525445445f42455354", result.getBlockInfo().getBlockImportedResult());
// Submit again the save PoW for a different header
result = ((MinerServerImpl) minerServer).submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock, false);
Assert.assertEquals("ERROR", result.getStatus());
Mockito.verify(ethereumImpl, Mockito.times(1)).addNewMinedBlock(Mockito.any());
} finally {
minerServer.stop();
}
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class MinerServerTest method submitBitcoinBlockTransactionsWhenBlockHasTransactions.
@Test
public void submitBitcoinBlockTransactionsWhenBlockHasTransactions() {
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
Mockito.when(ethereumImpl.addNewMinedBlock(Mockito.any())).thenReturn(ImportResult.IMPORTED_BEST);
BlockUnclesValidationRule unclesValidationRule = Mockito.mock(BlockUnclesValidationRule.class);
Mockito.when(unclesValidationRule.isValid(Mockito.any())).thenReturn(true);
MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new BlockToMineBuilder(ConfigUtils.getDefaultMiningConfig(), blockchain.getRepository(), blockchain.getBlockStore(), blockchain.getTransactionPool(), DIFFICULTY_CALCULATOR, new GasLimitCalculator(config), unclesValidationRule, config, null), ConfigUtils.getDefaultMiningConfig());
try {
minerServer.start();
MinerWork work = minerServer.getWork();
BtcTransaction otherTx = Mockito.mock(BtcTransaction.class);
Sha256Hash otherTxHash = Sha256Hash.wrap("aaaabbbbccccddddaaaabbbbccccddddaaaabbbbccccddddaaaabbbbccccdddd");
Mockito.when(otherTx.getHash()).thenReturn(otherTxHash);
Mockito.when(otherTx.getHashAsString()).thenReturn(otherTxHash.toString());
BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlock(work, Collections.singletonList(otherTx));
findNonce(work, bitcoinMergedMiningBlock);
// noinspection ConstantConditions
BtcTransaction coinbase = bitcoinMergedMiningBlock.getTransactions().get(0);
List<String> txs = Arrays.asList(coinbase.getHashAsString(), otherTxHash.toString());
SubmitBlockResult result = minerServer.submitBitcoinBlockTransactions(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock, coinbase, txs);
Assert.assertEquals("OK", result.getStatus());
Assert.assertNotNull(result.getBlockInfo());
Assert.assertEquals("0x1", result.getBlockInfo().getBlockIncludedHeight());
Assert.assertEquals("0x494d504f525445445f42455354", result.getBlockInfo().getBlockImportedResult());
Mockito.verify(ethereumImpl, Mockito.times(1)).addNewMinedBlock(Mockito.any());
} finally {
minerServer.stop();
}
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class MinerServerTest method secondBuildBlockToMineTurnsNotifyFlagOff.
@Test
public void secondBuildBlockToMineTurnsNotifyFlagOff() {
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
BlockUnclesValidationRule unclesValidationRule = Mockito.mock(BlockUnclesValidationRule.class);
Mockito.when(unclesValidationRule.isValid(Mockito.any())).thenReturn(true);
MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, this.blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new BlockToMineBuilder(ConfigUtils.getDefaultMiningConfig(), blockchain.getRepository(), blockchain.getBlockStore(), blockchain.getTransactionPool(), DIFFICULTY_CALCULATOR, new GasLimitCalculator(config), unclesValidationRule, config, null), ConfigUtils.getDefaultMiningConfig());
try {
minerServer.start();
MinerWork work = minerServer.getWork();
String hashForMergedMining = work.getBlockHashForMergedMining();
minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
work = minerServer.getWork();
assertEquals(hashForMergedMining, work.getBlockHashForMergedMining());
assertEquals(false, work.getNotify());
} finally {
minerServer.stop();
}
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class NodeMessageHandlerTest method processBlockByHashRequestMessageUsingProcessor.
@Test
public void processBlockByHashRequestMessageUsingProcessor() throws UnknownHostException {
SimpleBlockProcessor sbp = new SimpleBlockProcessor();
NodeMessageHandler processor = new NodeMessageHandler(config, sbp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
Block block = BlockChainBuilder.ofSize(1, true).getBestBlock();
Message message = new BlockRequestMessage(100, block.getHash().getBytes());
processor.processMessage(new SimpleMessageChannel(), message);
Assert.assertEquals(100, sbp.getRequestId());
Assert.assertArrayEquals(block.getHash().getBytes(), sbp.getHash());
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class NodeMessageHandlerTest method processStatusMessageUsingNodeBlockProcessor.
@Test
@Ignore("This should be reviewed with sync processor or deleted")
public void processStatusMessageUsingNodeBlockProcessor() throws UnknownHostException {
final World world = new World();
final Blockchain blockchain = world.getBlockChain();
final BlockStore store = new BlockStore();
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor bp = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final SimpleMessageChannel sender = new SimpleMessageChannel();
final NodeMessageHandler handler = new NodeMessageHandler(config, bp, null, null, null, null, null, new ProofOfWorkRule(config).setFallbackMiningEnabled(false));
BlockGenerator blockGenerator = new BlockGenerator();
final Block block = blockGenerator.createChildBlock(blockGenerator.getGenesisBlock());
final Status status = new Status(block.getNumber(), block.getHash().getBytes());
final Message message = new StatusMessage(status);
handler.processMessage(sender, message);
Assert.assertNotNull(sender.getGetBlockMessages());
Assert.assertEquals(1, sender.getGetBlockMessages().size());
final Message msg = sender.getGetBlockMessages().get(0);
Assert.assertEquals(MessageType.GET_BLOCK_MESSAGE, msg.getMessageType());
final GetBlockMessage gbMessage = (GetBlockMessage) msg;
Assert.assertArrayEquals(block.getHash().getBytes(), gbMessage.getBlockHash());
}
Aggregations