Search in sources :

Example 51 with BlockBuilder

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

the class Web3ImplTest method getBlocksByNumber.

@Test
public void getBlocksByNumber() throws Exception {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).difficulty(10).parent(genesis).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    Block block1b = new BlockBuilder(world).difficulty(block1.getDifficulty().asBigInteger().longValue() - 1).parent(genesis).build();
    block1b.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    Block block2b = new BlockBuilder(world).difficulty(2).parent(block1b).build();
    block2b.setBitcoinMergedMiningHeader(new byte[] { 0x02 });
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2b));
    Web3.BlockInformationResult[] bresult = web3.eth_getBlocksByNumber("0x1");
    Assert.assertNotNull(bresult);
    org.junit.Assert.assertEquals(2, bresult.length);
    org.junit.Assert.assertEquals(block1.getHashJsonString(), bresult[0].hash);
    org.junit.Assert.assertEquals(block1b.getHashJsonString(), bresult[1].hash);
}
Also used : World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 52 with BlockBuilder

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

the class Web3ImplTest method getBlockByNumberRetrieveLatestBlock.

@Test
public void getBlockByNumberRetrieveLatestBlock() throws Exception {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).parent(genesis).build();
    block1.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    Web3.BlockResult blockResult = web3.eth_getBlockByNumber("latest", false);
    Assert.assertNotNull(blockResult);
    String blockHash = TypeConverter.toJsonHex(block1.getHash().toString());
    org.junit.Assert.assertEquals(blockHash, blockResult.hash);
}
Also used : World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 53 with BlockBuilder

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

the class Web3ImplTest method getTransactionByHashNotInMainBlockchain.

@Test
public void getTransactionByHashNotInMainBlockchain() throws Exception {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(2000000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).difficulty(10).parent(genesis).transactions(txs).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    Block block1b = new BlockBuilder(world).difficulty(block1.getDifficulty().asBigInteger().longValue() - 1).parent(genesis).build();
    Block block2b = new BlockBuilder(world).difficulty(block1.getDifficulty().asBigInteger().longValue() + 1).parent(block1b).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2b));
    String hashString = tx.getHash().toHexString();
    TransactionResultDTO tr = web3.eth_getTransactionByHash(hashString);
    Assert.assertNull(tr);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) TransactionResultDTO(org.ethereum.rpc.dto.TransactionResultDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 54 with BlockBuilder

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

the class Web3ImplTest method getTransactionReceiptNotInMainBlockchain.

@Test
public void getTransactionReceiptNotInMainBlockchain() throws Exception {
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    Web3Impl web3 = createWeb3(world, receiptStore);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(2000000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).parent(genesis).difficulty(3l).transactions(txs).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    Block block1b = new BlockBuilder(world).parent(genesis).difficulty(block1.getDifficulty().asBigInteger().longValue() - 1).build();
    Block block2b = new BlockBuilder(world).difficulty(2).parent(block1b).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(block1b));
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block2b));
    String hashString = tx.getHash().toHexString();
    TransactionReceiptDTO tr = web3.eth_getTransactionReceipt(hashString);
    Assert.assertNull(tr);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) TransactionReceiptDTO(org.ethereum.rpc.dto.TransactionReceiptDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) ReceiptStore(org.ethereum.db.ReceiptStore) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 55 with BlockBuilder

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

the class Web3ImplTest method getTransactionCount.

@Test
public void getTransactionCount() 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();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String accountAddress = Hex.toHexString(acc1.getAddress().getBytes());
    String count = web3.eth_getTransactionCount(accountAddress, "0x1");
    Assert.assertNotNull(count);
    org.junit.Assert.assertEquals("0x1", count);
    count = web3.eth_getTransactionCount(accountAddress, "0x0");
    Assert.assertNotNull(count);
    org.junit.Assert.assertEquals("0x0", count);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) 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