Search in sources :

Example 16 with World

use of co.rsk.test.World 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 17 with World

use of co.rsk.test.World 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 18 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplLogsTest method newFilterGetLogsAfterBlock.

@Test
public void newFilterGetLogsAfterBlock() throws Exception {
    World world = new World();
    Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), world.getBlockChain().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
    world.getBlockChain().setListener(web3.setupListener());
    web3.personal_newAccountWithSeed("notDefault");
    Web3.FilterRequest fr = new Web3.FilterRequest();
    fr.fromBlock = "latest";
    String id = web3.eth_newFilter(fr);
    Block genesis = world.getBlockByName("g00");
    Transaction tx;
    tx = getContractTransaction(acc1);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    world.getBlockChain().tryToConnect(block1);
    Object[] logs = web3.eth_getFilterLogs(id);
    Assert.assertNotNull(id);
    Assert.assertNotNull(logs);
    Assert.assertEquals(1, logs.length);
    Assert.assertEquals("0x" + tx.getContractAddress().toString(), ((LogFilterElement) logs[0]).address);
}
Also used : ArrayList(java.util.ArrayList) World(co.rsk.test.World) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) SimpleEthereum(org.ethereum.rpc.Simples.SimpleEthereum) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 19 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplLogsTest method createCallerContractWithEventsOnInvokeUsingGetFilterLogs.

@Test
public void createCallerContractWithEventsOnInvokeUsingGetFilterLogs() throws Exception {
    World world = new World();
    Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), world.getBlockChain().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
    world.getBlockChain().setListener(web3.setupListener());
    web3.personal_newAccountWithSeed("notDefault");
    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();
    world.getBlockChain().tryToConnect(block1);
    String mainAddress = tx.getContractAddress().toString();
    Transaction tx2;
    tx2 = getCallerContractTransaction(acc1, mainAddress);
    String callerAddress = Hex.toHexString(tx2.getContractAddress().getBytes());
    List<Transaction> txs2 = new ArrayList<>();
    txs2.add(tx2);
    Block block2 = new BlockBuilder(world).parent(block1).transactions(txs2).build();
    world.getBlockChain().tryToConnect(block2);
    Transaction tx3;
    tx3 = getCallerContractTransactionWithInvoke(acc1, tx2.getContractAddress().getBytes(), mainAddress);
    List<Transaction> txs3 = new ArrayList<>();
    txs3.add(tx3);
    Block block3 = new BlockBuilder(world).parent(block2).transactions(txs3).build();
    world.getBlockChain().tryToConnect(block3);
    Web3.FilterRequest fr = new Web3.FilterRequest();
    fr.address = "0x" + mainAddress;
    String id = web3.eth_newFilter(fr);
    Object[] logs = web3.eth_getFilterLogs(id);
    Assert.assertNotNull(id);
    Assert.assertNotNull(logs);
    Assert.assertEquals(1, logs.length);
    Assert.assertEquals("0x" + mainAddress, ((LogFilterElement) logs[0]).address);
}
Also used : ArrayList(java.util.ArrayList) World(co.rsk.test.World) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) SimpleEthereum(org.ethereum.rpc.Simples.SimpleEthereum) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 20 with World

use of co.rsk.test.World in project rskj by rsksmart.

the class Web3ImplLogsTest method newFilterGetChangesAfterBlock.

@Test
public void newFilterGetChangesAfterBlock() 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 = getContractTransaction(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);
    // TODO Fix
    Assert.assertEquals(1, logs.length);
    Assert.assertEquals("0x" + tx.getContractAddress().toString(), ((LogFilterElement) logs[0]).address);
}
Also used : BlockChainImpl(co.rsk.core.bc.BlockChainImpl) ArrayList(java.util.ArrayList) World(co.rsk.test.World) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) SimpleEthereum(org.ethereum.rpc.Simples.SimpleEthereum) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

World (co.rsk.test.World)134 Test (org.junit.Test)114 BlockBuilder (co.rsk.test.builders.BlockBuilder)36 AccountBuilder (co.rsk.test.builders.AccountBuilder)33 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)24 DslParser (co.rsk.test.dsl.DslParser)23 Block (org.ethereum.core.Block)20 Blockchain (org.ethereum.core.Blockchain)19 SyncConfiguration (co.rsk.net.sync.SyncConfiguration)15 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)15 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)14 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)14 ArrayList (java.util.ArrayList)13 SimpleEthereum (org.ethereum.rpc.Simples.SimpleEthereum)13 BlockChainImpl (co.rsk.core.bc.BlockChainImpl)11 SimpleMessageChannel (co.rsk.net.simples.SimpleMessageChannel)11 ProofOfWorkRule (co.rsk.validators.ProofOfWorkRule)11 BigInteger (java.math.BigInteger)10 DummyBlockValidationRule (co.rsk.validators.DummyBlockValidationRule)9 Account (org.ethereum.core.Account)8