use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class MainNetMinerTest method submitBitcoinBlockInvalidBlockDoesntEliminateCache.
/*
* This test is much more likely to fail than the
* submitBitcoinBlockProofOfWorkNotGoodEnough test. Even then
* it should almost never fail.
*/
@Test
public void submitBitcoinBlockInvalidBlockDoesntEliminateCache() {
// ////////////////////////////////////////////////////////////////////
// To make this test work we need a special network spec with
// medium minimum difficulty (this is not the mainnet nor the regnet)
// //////////////////////////////////////////////////////////////////
/* We need a low, but not too low, target */
BlockChainImpl bc = new BlockChainBuilder().build();
Genesis gen = (Genesis) BlockChainImplTest.getGenesisBlock(bc);
gen.getHeader().setDifficulty(new BlockDifficulty(BigInteger.valueOf(300000)));
bc.setStatus(gen, gen.getCumulativeDifficulty());
World world = new World(bc, gen);
blockchain = world.getBlockChain();
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
Mockito.when(ethereumImpl.addNewMinedBlock(Mockito.any())).thenReturn(ImportResult.IMPORTED_BEST);
MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, this.blockchain, world.getBlockProcessor(), DIFFICULTY_CALCULATOR, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), blockToMineBuilder(), ConfigUtils.getDefaultMiningConfig());
try {
minerServer.start();
MinerWork work = minerServer.getWork();
co.rsk.bitcoinj.core.BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlock(work);
bitcoinMergedMiningBlock.setNonce(1);
// Try to submit a block with invalid PoW, this should not eliminate the block from the cache
SubmitBlockResult result1 = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
Assert.assertEquals("ERROR", result1.getStatus());
Assert.assertNull(result1.getBlockInfo());
Mockito.verify(ethereumImpl, Mockito.times(0)).addNewMinedBlock(Mockito.any());
// Now try to submit the same block, this should work fine since the block remains in the cache
// This WON't work in mainnet because difficulty is HIGH
/*---------------------------------------------------------
findNonce(work, bitcoinMergedMiningBlock);
SubmitBlockResult result2 = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
Assert.assertEquals("OK", result2.getStatus());
Assert.assertNotNull(result2.getBlockInfo());
Mockito.verify(ethereumImpl, Mockito.times(1)).addNewMinedBlock(Mockito.any());
// Finally, submit the same block again and validate that addNewMinedBlock is called again
SubmitBlockResult result3 = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
Assert.assertEquals("OK", result3.getStatus());
Assert.assertNotNull(result3.getBlockInfo());
Mockito.verify(ethereumImpl, Mockito.times(2)).addNewMinedBlock(Mockito.any());
-------------------------------*/
} finally {
minerServer.stop();
}
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class MinerManagerTest method getMinerServer.
private static MinerServerImpl getMinerServer(Blockchain blockchain) {
SimpleEthereum ethereum = new SimpleEthereum();
ethereum.repository = blockchain.getRepository();
ethereum.blockchain = blockchain;
DifficultyCalculator difficultyCalculator = new DifficultyCalculator(config);
return new MinerServerImpl(config, ethereum, blockchain, null, difficultyCalculator, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new BlockToMineBuilder(ConfigUtils.getDefaultMiningConfig(), blockchain.getRepository(), blockchain.getBlockStore(), blockchain.getTransactionPool(), difficultyCalculator, new GasLimitCalculator(config), new BlockValidationRuleDummy(), config, null), ConfigUtils.getDefaultMiningConfig());
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class MinerServerTest method submitBitcoinBlockPartialMerkleWhenBlockHasTransactions.
@Test
public void submitBitcoinBlockPartialMerkleWhenBlockHasTransactions() {
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);
String coinbaseReversedHash = Sha256Hash.wrap(coinbase.getHash().getReversedBytes()).toString();
String otherTxHashReversed = Sha256Hash.wrap(otherTxHash.getReversedBytes()).toString();
List<String> merkleHashes = Arrays.asList(coinbaseReversedHash, otherTxHashReversed);
SubmitBlockResult result = minerServer.submitBitcoinBlockPartialMerkle(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock, coinbase, merkleHashes, 2);
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 buildBlockToMineCheckThatLastTransactionIsForREMASC.
@Test
public void buildBlockToMineCheckThatLastTransactionIsForREMASC() {
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
Repository repository = Mockito.mock(Repository.class);
Mockito.when(repository.getSnapshotTo(Mockito.any())).thenReturn(repository);
Mockito.when(repository.getRoot()).thenReturn(blockchain.getRepository().getRoot());
Mockito.when(repository.startTracking()).thenReturn(repository);
Transaction tx1 = Tx.create(config, 0, 21000, 100, 0, 0, 0);
byte[] s1 = new byte[32];
s1[0] = 0;
Mockito.when(tx1.getHash()).thenReturn(new Keccak256(s1));
Mockito.when(tx1.getEncoded()).thenReturn(new byte[32]);
Mockito.when(repository.getNonce(tx1.getSender())).thenReturn(BigInteger.ZERO);
Mockito.when(repository.getNonce(RemascTransaction.REMASC_ADDRESS)).thenReturn(BigInteger.ZERO);
Mockito.when(repository.getBalance(tx1.getSender())).thenReturn(Coin.valueOf(4200000L));
Mockito.when(repository.getBalance(RemascTransaction.REMASC_ADDRESS)).thenReturn(Coin.valueOf(4200000L));
List<Transaction> txs = new ArrayList<>(Collections.singletonList(tx1));
TransactionPool localTransactionPool = Mockito.mock(TransactionPool.class);
Mockito.when(localTransactionPool.getPendingTransactions()).thenReturn(txs);
BlockUnclesValidationRule unclesValidationRule = Mockito.mock(BlockUnclesValidationRule.class);
Mockito.when(unclesValidationRule.isValid(Mockito.any())).thenReturn(true);
MinerServerImpl minerServer = new MinerServerImpl(config, ethereumImpl, this.blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(config).setFallbackMiningEnabled(false), new BlockToMineBuilder(ConfigUtils.getDefaultMiningConfig(), repository, this.blockchain.getBlockStore(), localTransactionPool, DIFFICULTY_CALCULATOR, new GasLimitCalculator(config), unclesValidationRule, config, null), ConfigUtils.getDefaultMiningConfig());
minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
Block blockAtHeightOne = minerServer.getBlocksWaitingforPoW().entrySet().iterator().next().getValue();
List<Transaction> blockTransactions = blockAtHeightOne.getTransactionsList();
assertNotNull(blockTransactions);
assertEquals(2, blockTransactions.size());
Transaction remascTransaction = blockTransactions.get(1);
assertThat(remascTransaction, instanceOf(RemascTransaction.class));
}
use of co.rsk.validators.ProofOfWorkRule in project rskj by rsksmart.
the class NodeMessageHandlerTest method postBlockMessageUsingProcessor.
@Test
public void postBlockMessageUsingProcessor() throws InterruptedException, 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 BlockMessage(block);
processor.start();
processor.postMessage(new SimpleMessageChannel(), message);
Thread.sleep(1000);
processor.stop();
Assert.assertNotNull(sbp.getBlocks());
Assert.assertEquals(1, sbp.getBlocks().size());
Assert.assertSame(block, sbp.getBlocks().get(0));
}
Aggregations