Search in sources :

Example 26 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class Web3ImplTest method getCode.

@Test
public void getCode() throws Exception {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    web3.repository = world.getRepository();
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(100000000)).build();
    byte[] code = new byte[] { 0x01, 0x02, 0x03 };
    world.getRepository().saveCode(acc1.getAddress(), code);
    Block genesis = world.getBlockChain().getBestBlock();
    genesis.setStateRoot(world.getRepository().getRoot());
    genesis.flushRLP();
    world.getBlockChain().getBlockStore().saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    Block block1 = new BlockBuilder(world).parent(genesis).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String accountAddress = Hex.toHexString(acc1.getAddress().getBytes());
    String scode = web3.eth_getCode(accountAddress, "0x1");
    Assert.assertNotNull(scode);
    org.junit.Assert.assertEquals("0x" + Hex.toHexString(code), scode);
}
Also used : AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 27 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class BlockBuilderTest method buildBlockWithDifficulty.

@Test
public void buildBlockWithDifficulty() {
    Block genesis = new BlockGenerator().getGenesisBlock();
    BlockBuilder builder = new BlockBuilder();
    Block block = builder.parent(genesis).difficulty(1).build();
    Assert.assertNotNull(block);
    Assert.assertEquals(1, block.getNumber());
    Assert.assertEquals(BigInteger.ONE, block.getDifficulty().asBigInteger());
    Assert.assertEquals(genesis.getHash(), block.getParentHash());
}
Also used : Block(org.ethereum.core.Block) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 28 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class BlockChainImplTest method addValidMGPBlock.

@Test
public void addValidMGPBlock() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), (DB) null);
    BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
    validatorBuilder.blockStore(blockStore).addPrevMinGasPriceRule().addTxsMinGasPriceRule();
    BlockChainImpl blockChain = createBlockChain(repository, blockStore, validatorBuilder.build());
    Repository track = repository.startTracking();
    Account account = BlockExecutorTest.createAccount("acctest1", track, Coin.valueOf(100000));
    Assert.assertTrue(account.getEcKey().hasPrivKey());
    track.commit();
    List<Transaction> txs = new ArrayList<>();
    Transaction tx = Transaction.create(config, "0000000000000000000000000000000000000100", BigInteger.ZERO, BigInteger.ZERO, BigInteger.ONE, BigInteger.valueOf(22000L));
    tx.sign(account.getEcKey().getPrivKeyBytes());
    txs.add(tx);
    Block genesis = getGenesisBlock(blockChain);
    genesis.setStateRoot(repository.getRoot());
    genesis.flushRLP();
    Block block = new BlockBuilder().minGasPrice(BigInteger.ZERO).transactions(txs).parent(genesis).build();
    BlockExecutor executor = new BlockExecutor(config, repository, null, null, null);
    executor.executeAndFill(block, genesis);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) RepositoryImpl(co.rsk.db.RepositoryImpl) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 29 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class BlockChainImplTest method addInvalidMGPBlock.

@Test
public void addInvalidMGPBlock() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    BlockValidatorBuilder validatorBuilder = new BlockValidatorBuilder();
    validatorBuilder.addBlockRootValidationRule().addBlockUnclesValidationRule(blockStore).addBlockTxsValidationRule(repository).addPrevMinGasPriceRule().addTxsMinGasPriceRule();
    BlockChainImpl blockChain = createBlockChain(repository, blockStore, validatorBuilder.build());
    Block genesis = getGenesisBlock(blockChain);
    Block block = new BlockBuilder().minGasPrice(BigInteger.ONE).parent(genesis).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.INVALID_BLOCK, blockChain.tryToConnect(block));
    List<Transaction> txs = new ArrayList<>();
    Transaction tx = Transaction.create(config, "0000000000000000000000000000000000000006", BigInteger.ZERO, BigInteger.ZERO, BigInteger.valueOf(1L), BigInteger.TEN);
    tx.sign(new byte[] { 22, 11, 00 });
    txs.add(tx);
    block = new BlockBuilder().transactions(txs).minGasPrice(BigInteger.valueOf(11L)).parent(genesis).build();
    Assert.assertEquals(ImportResult.INVALID_BLOCK, blockChain.tryToConnect(block));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) IndexedBlockStore(org.ethereum.db.IndexedBlockStore) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) RepositoryImpl(co.rsk.db.RepositoryImpl) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 30 with BlockBuilder

use of co.rsk.test.builders.BlockBuilder in project rskj by rsksmart.

the class BlockUtilsTest method unknowAncestorsHashes.

@Test
public void unknowAncestorsHashes() {
    BlockChainImpl blockChain = new BlockChainBuilder().build();
    BlockStore store = new BlockStore();
    Block genesis = new BlockGenerator().getGenesisBlock();
    genesis.setStateRoot(blockChain.getRepository().getRoot());
    genesis.flushRLP();
    Block block1 = new BlockBuilder().difficulty(2l).parent(genesis).build();
    Block block1b = new BlockBuilder().difficulty(1l).parent(genesis).build();
    Block block2 = new BlockBuilder().parent(block1).build();
    Block block3 = new BlockBuilder().parent(block2).build();
    store.saveBlock(block3);
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(genesis));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1));
    Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, blockChain.tryToConnect(block1b));
    Set<Keccak256> hashes = BlockUtils.unknownAncestorsHashes(genesis.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block1b.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertTrue(hashes.isEmpty());
    hashes = BlockUtils.unknownAncestorsHashes(block2.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(1, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
    hashes = BlockUtils.unknownAncestorsHashes(block3.getHash(), blockChain, store);
    Assert.assertNotNull(hashes);
    Assert.assertFalse(hashes.isEmpty());
    Assert.assertEquals(1, hashes.size());
    Assert.assertTrue(hashes.contains(block2.getHash()));
}
Also used : BlockStore(co.rsk.net.BlockStore) Block(org.ethereum.core.Block) Keccak256(co.rsk.crypto.Keccak256) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

BlockBuilder (co.rsk.test.builders.BlockBuilder)61 Test (org.junit.Test)54 World (co.rsk.test.World)36 ArrayList (java.util.ArrayList)27 AccountBuilder (co.rsk.test.builders.AccountBuilder)26 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)17 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)13 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)12 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)11 SimpleBlock (co.rsk.peg.simples.SimpleBlock)11 Block (org.ethereum.core.Block)11 HashMapDB (org.ethereum.datasource.HashMapDB)9 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)8 SimpleEthereum (org.ethereum.rpc.Simples.SimpleEthereum)8 RemascTransaction (co.rsk.remasc.RemascTransaction)7 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)7 TransactionResultDTO (org.ethereum.rpc.dto.TransactionResultDTO)6 BlockHeader (org.ethereum.core.BlockHeader)4 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)3 BlockStore (org.ethereum.db.BlockStore)3