Search in sources :

Example 1 with BlockBuilder

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

the class ReceiptStoreImplTest method addTwoTransactionsAndGetTransactionByDescendantBlocks.

@Test
public void addTwoTransactionsAndGetTransactionByDescendantBlocks() {
    World world = new World();
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1a = new BlockBuilder().difficulty(10).parent(genesis).build();
    Block block1b = new BlockBuilder().difficulty(block1a.getDifficulty().asBigInteger().longValue() - 1).parent(genesis).build();
    Block block2a = new BlockBuilder().parent(block1a).build();
    Block block2b = new BlockBuilder().parent(block1b).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1a));
    Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
    Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2a));
    Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block2b));
    ReceiptStore store = new ReceiptStoreImpl(new HashMapDB());
    TransactionReceipt receipt0 = createReceipt();
    byte[] blockHash0 = Hex.decode("010203040506070809");
    store.add(block1a.getHash().getBytes(), 3, receipt0);
    TransactionReceipt receipt = createReceipt();
    byte[] blockHash = Hex.decode("0102030405060708");
    store.add(block1b.getHash().getBytes(), 42, receipt);
    TransactionInfo result = store.get(receipt.getTransaction().getHash().getBytes(), block2a.getHash().getBytes(), world.getBlockChain().getBlockStore());
    Assert.assertNotNull(result.getBlockHash());
    Assert.assertArrayEquals(block1a.getHash().getBytes(), result.getBlockHash());
    Assert.assertEquals(3, result.getIndex());
    Assert.assertArrayEquals(receipt.getEncoded(), result.getReceipt().getEncoded());
    result = store.get(receipt.getTransaction().getHash().getBytes(), block2b.getHash().getBytes(), world.getBlockChain().getBlockStore());
    Assert.assertNotNull(result.getBlockHash());
    Assert.assertArrayEquals(block1b.getHash().getBytes(), result.getBlockHash());
    Assert.assertEquals(42, result.getIndex());
    Assert.assertArrayEquals(receipt.getEncoded(), result.getReceipt().getEncoded());
    result = store.get(receipt.getTransaction().getHash().getBytes(), genesis.getHash().getBytes(), world.getBlockChain().getBlockStore());
    Assert.assertNull(result);
}
Also used : World(co.rsk.test.World) HashMapDB(org.ethereum.datasource.HashMapDB) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 2 with BlockBuilder

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

the class Web3ImplLogsTest method getWeb3WithThreeEmptyBlocks.

private Web3Impl getWeb3WithThreeEmptyBlocks() {
    World world = new World();
    Web3Impl web3 = createWeb3(world.getBlockChain(), null);
    Block genesis = world.getBlockByName("g00");
    Block block1 = new BlockBuilder().parent(genesis).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    Block block2 = new BlockBuilder().parent(block1).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2));
    return web3;
}
Also used : World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Example 3 with BlockBuilder

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

the class Web3ImplLogsTest method getWeb3WithContractInvoke.

private Web3Impl getWeb3WithContractInvoke() {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
    Block genesis = world.getBlockByName("g00");
    Transaction tx;
    tx = getContractTransaction(acc1);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    BlockChainImpl blockChain = world.getBlockChain();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1));
    byte[] contractAddress = tx.getContractAddress().getBytes();
    Transaction tx2 = getContractTransactionWithInvoke(acc1, contractAddress);
    List<Transaction> tx2s = new ArrayList<>();
    tx2s.add(tx2);
    Block block2 = new BlockBuilder(world).parent(block1).transactions(tx2s).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2));
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), blockChain.getBlockStore(), receiptStore, null, null, 10, 100);
    Web3Impl web3 = createWeb3(world.getBlockChain(), transactionPool, receiptStore);
    web3.personal_newAccountWithSeed("default");
    web3.personal_newAccountWithSeed("notDefault");
    return web3;
}
Also used : ArrayList(java.util.ArrayList) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) AccountBuilder(co.rsk.test.builders.AccountBuilder) ReceiptStore(org.ethereum.db.ReceiptStore) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Example 4 with BlockBuilder

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

the class Web3ImplLogsTest method getWeb3WithContractCall.

private Web3Impl getWeb3WithContractCall(World world) {
    Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
    // acc1 Account created address should be 661b05ca9eb621164906671efd2731ce0d7dd8b4
    Block genesis = world.getBlockByName("g00");
    Transaction tx;
    tx = getContractTransaction(acc1);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    BlockChainImpl blockChain = world.getBlockChain();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1));
    byte[] contractAddress = tx.getContractAddress().getBytes();
    // Now create a transaction that invokes Increment()
    Transaction tx2 = getContractTransactionWithInvoke(acc1, contractAddress);
    List<Transaction> tx2s = new ArrayList<>();
    tx2s.add(tx2);
    Block block2 = new BlockBuilder(world).parent(block1).transactions(tx2s).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block2));
    Transaction tx3 = getContractTransactionWithCall(acc1, contractAddress);
    List<Transaction> tx3s = new ArrayList<>();
    tx3s.add(tx3);
    Block block3 = new BlockBuilder(world).parent(block2).transactions(tx3s).build();
    Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block3));
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), blockChain.getBlockStore(), null, null, null, 10, 100);
    Web3Impl web3 = createWeb3(world.getBlockChain(), transactionPool);
    web3.personal_newAccountWithSeed("default");
    web3.personal_newAccountWithSeed("notDefault");
    return web3;
}
Also used : TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) ArrayList(java.util.ArrayList) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Example 5 with BlockBuilder

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

the class BlockValidatorTest method validateEmptyBlock.

@Test
public void validateEmptyBlock() {
    IndexedBlockStore blockStore = new IndexedBlockStore(new HashMap<>(), new HashMapDB(), null);
    Block genesis = new BlockGenerator().getGenesisBlock();
    blockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
    Block block = new BlockBuilder().parent(genesis).build();
    BlockValidator validator = createValidator(blockStore);
    Assert.assertTrue(validator.isValid(block));
}
Also used : IndexedBlockStore(org.ethereum.db.IndexedBlockStore) SimpleBlock(co.rsk.peg.simples.SimpleBlock) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) BlockValidator(co.rsk.validators.BlockValidator) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

BlockBuilder (co.rsk.test.builders.BlockBuilder)79 Test (org.junit.Test)63 World (co.rsk.test.World)31 AccountBuilder (co.rsk.test.builders.AccountBuilder)30 ArrayList (java.util.ArrayList)30 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)24 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)13 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)12 BlockResultDTO (org.ethereum.rpc.dto.BlockResultDTO)12 RemascTransaction (co.rsk.remasc.RemascTransaction)11 Block (org.ethereum.core.Block)10 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)9 HashMapDB (org.ethereum.datasource.HashMapDB)8 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)7 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)7 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)6 SimplePeer (co.rsk.net.simples.SimplePeer)6 BlockHeader (org.ethereum.core.BlockHeader)6 Keccak256 (co.rsk.crypto.Keccak256)5 SimpleBlock (co.rsk.peg.simples.SimpleBlock)5