use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.
the class MainNetMinerTest method submitBitcoinBlockProofOfWorkNotGoodEnough.
/*
* This test is probabilistic, but it has a really high chance to pass. We will generate
* a random block that it is unlikely to pass the Long.MAX_VALUE difficulty, though
* it may happen once. Twice would be suspicious.
*/
@Test
public void submitBitcoinBlockProofOfWorkNotGoodEnough() {
/* We need a low target */
BlockChainImpl bc = new BlockChainBuilder().build();
Genesis gen = (Genesis) BlockChainImplTest.getGenesisBlock(bc);
gen.getHeader().setDifficulty(new BlockDifficulty(BigInteger.valueOf(Long.MAX_VALUE)));
bc.setStatus(gen, gen.getCumulativeDifficulty());
World world = new World(bc, gen);
TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), world.getBlockChain().getBlockStore(), null, null, null, 10, 100);
blockchain = world.getBlockChain();
EthereumImpl ethereumImpl = Mockito.mock(EthereumImpl.class);
MinerServer minerServer = new MinerServerImpl(config, ethereumImpl, blockchain, null, 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(2);
SubmitBlockResult result = minerServer.submitBitcoinBlock(work.getBlockHashForMergedMining(), bitcoinMergedMiningBlock);
Assert.assertEquals("ERROR", result.getStatus());
Assert.assertNull(result.getBlockInfo());
Mockito.verify(ethereumImpl, Mockito.times(0)).addNewMinedBlock(Mockito.any());
} finally {
minerServer.stop();
}
}
use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.
the class CodeReplaceTest method replaceCodeTest1.
@Test
public void replaceCodeTest1() throws IOException, InterruptedException {
BigInteger nonce = config.getBlockchainConfig().getCommonConstants().getInitialNonce();
BlockChainImpl blockchain = org.ethereum.core.ImportLightTest.createBlockchain(GenesisLoader.loadGenesis(config, nonce, getClass().getResourceAsStream("/genesis/genesis-light.json"), false));
ECKey sender = ECKey.fromPrivate(Hex.decode("3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c"));
System.out.println("address: " + Hex.toHexString(sender.getAddress()));
String asm = // (7b) Extract real code into address 0, skip first 12 bytes, copy 20 bytes
"0x14 0x0C 0x00 CODECOPY " + // (5b) offset 0, size 0x14, now return the first code
"0x14 0x00 RETURN " + // (4b) header script v1
"HEADER !0x01 !0x01 !0x00 " + // (3b) store at offset 0, read data from offset 0. Transfer 32 bytes
"0x00 CALLDATALOAD " + // (3b) store the data at address 0
"0x00 MSTORE " + // We replace TWO TIMES to make sure that only the last replacement takes place
"0x01 0x00 CODEREPLACE " + // This is the good one.
"0x10 0x00 CODEREPLACE";
// (5b) set new code: offset 0, size 16 bytes.
EVMAssembler assembler = new EVMAssembler();
byte[] code = assembler.assemble(asm);
// Creates a contract
Transaction tx1 = createTx(blockchain, sender, new byte[0], code);
executeTransaction(blockchain, tx1);
// Now we can directly check the store and see the new code.
RskAddress createdContract = tx1.getContractAddress();
byte[] expectedCode = Arrays.copyOfRange(code, 12, 12 + 20);
byte[] installedCode = blockchain.getRepository().getContractDetails(createdContract).getCode();
// assert the contract has been created
Assert.assertTrue(Arrays.equals(expectedCode, installedCode));
// Note that this code does not have a header, then its version == 0
String asm2 = // (5b) Store at address 0x00, the value 0xFF
"0xFF 0x00 MSTORE " + // (5b) And return such value 0xFF, at offset 0x1F
"0x01 0x1F RETURN " + // fill with nops to make it 16 bytes in length
"STOP STOP STOP STOP STOP STOP";
// 16
byte[] code2 = assembler.assemble(asm2);
// The second transaction changes the contract code
Transaction tx2 = createTx(blockchain, sender, tx1.getContractAddress().getBytes(), code2);
TransactionExecutor executor2 = executeTransaction(blockchain, tx2);
byte[] installedCode2 = blockchain.getRepository().getContractDetails(createdContract).getCode();
// assert the contract code has been created
Assert.assertTrue(Arrays.equals(installedCode2, code2));
// there is one code change
Assert.assertEquals(1, executor2.getResult().getCodeChanges().size());
// We could add a third tx to execute the new code
Transaction tx3 = createTx(blockchain, sender, tx1.getContractAddress().getBytes(), new byte[0]);
TransactionExecutor executor3 = executeTransaction(blockchain, tx3);
// check return code from contract call
Assert.assertArrayEquals(Hex.decode("FF"), executor3.getResult().getHReturn());
}
use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.
the class CodeReplaceTest method replaceCodeTest2.
@Test
public void replaceCodeTest2() throws IOException, InterruptedException {
// We test code replacement during initialization: this is forbitten.
BigInteger nonce = config.getBlockchainConfig().getCommonConstants().getInitialNonce();
BlockChainImpl blockchain = org.ethereum.core.ImportLightTest.createBlockchain(GenesisLoader.loadGenesis(config, nonce, getClass().getResourceAsStream("/genesis/genesis-light.json"), false));
ECKey sender = ECKey.fromPrivate(Hex.decode("3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c"));
System.out.println("address: " + Hex.toHexString(sender.getAddress()));
String asm = // (4b) header script v1
"HEADER !0x01 !0x01 !0x00 " + // (5b) we attempt to replace the code
"0x01 0x00 CODEREPLACE " + // (7b) Extract real code into address 0, skip first 12 bytes, copy 1 bytes
"0x01 0x15 0x00 CODECOPY " + // (5b) offset 0, size 0x01, now return the first code
"0x01 0x00 RETURN " + // (1b) REAL code to install
"STOP ";
EVMAssembler assembler = new EVMAssembler();
byte[] code = assembler.assemble(asm);
// Creates a contract
Transaction tx1 = createTx(blockchain, sender, new byte[0], code);
TransactionExecutor executor1 = executeTransaction(blockchain, tx1);
// Now we can directly check the store and see the new code.
Assert.assertTrue(executor1.getResult().getException() != null);
}
use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.
the class BlockUnclesValidationRuleTest method rejectBlockWithUncleHavingHigherNumber.
@Test
public void rejectBlockWithUncleHavingHigherNumber() {
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
Block block1 = blockGenerator.createChildBlock(genesis);
Block uncle1 = blockGenerator.createChildBlock(block1);
Block uncle2 = blockGenerator.createChildBlock(uncle1);
List<BlockHeader> uncles = new ArrayList<>();
uncles.add(uncle2.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.core.bc.BlockChainImpl in project rskj by rsksmart.
the class Web3ImplLogsTest method createMainContractWithoutEvents.
@Test
public void createMainContractWithoutEvents() throws Exception {
World world = new World();
Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
BlockChainImpl blockChain = world.getBlockChain();
TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), blockChain.getBlockStore(), null, null, null, 10, 100);
SimpleEthereum eth = new SimpleEthereum();
eth.repository = world.getBlockChain().getRepository();
eth.blockchain = world.getBlockChain();
Web3Impl web3 = createWeb3(eth, world.getBlockChain(), transactionPool, WalletFactory.createWallet());
// TODO tricky link to listener
blockChain.setListener(web3.setupListener());
web3.personal_newAccountWithSeed("notDefault");
Web3.FilterRequest fr = new Web3.FilterRequest();
fr.fromBlock = "earliest";
String id = web3.eth_newFilter(fr);
Block genesis = world.getBlockByName("g00");
Transaction tx;
tx = getMainContractTransaction(acc1);
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
blockChain.tryToConnect(block1);
Object[] logs = web3.eth_getFilterChanges(id);
Assert.assertNotNull(id);
Assert.assertNotNull(logs);
Assert.assertEquals(0, logs.length);
}
Aggregations