use of org.ethereum.facade.EthereumImpl 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 org.ethereum.facade.EthereumImpl 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 org.ethereum.facade.EthereumImpl 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 org.ethereum.facade.EthereumImpl in project rskj by rsksmart.
the class MainNetMinerTest method generateFallbackMinedBlock.
@Test
public void generateFallbackMinedBlock() throws InterruptedException, IOException {
// generate private keys for testing now.
ECKey privateMiningKey0 = ECKey.fromPrivate(BigInteger.TEN);
ECKey privateMiningKey1 = ECKey.fromPrivate(BigInteger.TEN.add(BigInteger.ONE));
byte[] privKey0 = privateMiningKey0.getPrivKeyBytes();
saveToFile(privKey0, new File(folder.getRoot().getCanonicalPath(), "privkey0.bin"));
byte[] privKey1 = privateMiningKey1.getPrivKeyBytes();
saveToFile(privKey1, new File(folder.getRoot().getCanonicalPath(), "privkey1.bin"));
RskSystemProperties tempConfig = new RskSystemProperties() {
BlockchainNetConfig blockchainNetConfig = config.getBlockchainConfig();
@Override
public String fallbackMiningKeysDir() {
try {
return folder.getRoot().getCanonicalPath();
} catch (Exception e) {
}
return null;
}
@Override
public BlockchainNetConfig getBlockchainConfig() {
return new BlockchainNetConfig() {
@Override
public BlockchainConfig getConfigForBlock(long blockNumber) {
return blockchainNetConfig.getConfigForBlock(blockNumber);
}
@Override
public Constants getCommonConstants() {
return new Constants() {
@Override
public byte[] getFallbackMiningPubKey0() {
return privateMiningKey0.getPubKey();
}
@Override
public byte[] getFallbackMiningPubKey1() {
return privateMiningKey1.getPubKey();
}
};
}
};
}
};
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
Mockito.when(ethereumImpl.addNewMinedBlock(Mockito.any())).thenReturn(ImportResult.IMPORTED_BEST);
MinerServer minerServer = new MinerServerImpl(tempConfig, ethereumImpl, blockchain, null, DIFFICULTY_CALCULATOR, new ProofOfWorkRule(tempConfig).setFallbackMiningEnabled(true), blockToMineBuilder(), ConfigUtils.getDefaultMiningConfig());
try {
minerServer.setFallbackMining(true);
// Accelerate mining
((MinerServerImpl) minerServer).setSecsBetweenFallbackMinedBlocks(1);
minerServer.start();
// Blocks are generated auomatically
// but we can call minerServer.generateFallbackBlock() to generate it manually
// boolean result = minerServer.generateFallbackBlock();
// Assert.assertTrue(result);
long start = System.currentTimeMillis();
while (((MinerServerImpl) minerServer).getFallbackBlocksGenerated() == 0) {
if (System.currentTimeMillis() - start > 20 * 1000) {
Assert.assertTrue(false);
}
//
Thread.sleep(1000);
}
Mockito.verify(ethereumImpl, Mockito.times(1)).addNewMinedBlock(Mockito.any());
// mine another
// NOTE that is NOT using the next block (parity change) because of the blockchain mockito
// to mine a subsequent block, use a real blockchain, not the mockito.
minerServer.buildBlockToMine(blockchain.getBestBlock(), false);
// result = minerServer.generateFallbackBlock();
// Assert.assertTrue(result);
start = System.currentTimeMillis();
while (((MinerServerImpl) minerServer).getFallbackBlocksGenerated() == 1) {
if (System.currentTimeMillis() - start > 20 * 1000) {
Assert.assertTrue(false);
}
//
Thread.sleep(1000);
}
Mockito.verify(ethereumImpl, Mockito.times(2)).addNewMinedBlock(Mockito.any());
} finally {
minerServer.stop();
}
}
use of org.ethereum.facade.EthereumImpl in project rskj by rsksmart.
the class MinerServerTest method submitBitcoinBlockPartialMerkleWhenBlockIsEmpty.
@Test
public void submitBitcoinBlockPartialMerkleWhenBlockIsEmpty() {
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();
BtcBlock bitcoinMergedMiningBlock = getMergedMiningBlockWithOnlyCoinbase(work);
findNonce(work, bitcoinMergedMiningBlock);
// noinspection ConstantConditions
BtcTransaction coinbase = bitcoinMergedMiningBlock.getTransactions().get(0);
List<String> coinbaseReversedHash = Collections.singletonList(Sha256Hash.wrap(coinbase.getHash().getReversedBytes()).toString());
SubmitBlockResult result = minerServer.submitBitcoinBlockPartialMerkle(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock, coinbase, coinbaseReversedHash, 1);
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();
}
}
Aggregations