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);
}
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());
}
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));
}
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));
}
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()));
}
Aggregations