Search in sources :

Example 21 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class Web3ImplLogsTest method getWorld3WithBlockWithEventInContractCreation.

public static World getWorld3WithBlockWithEventInContractCreation(RskSystemProperties config, ReceiptStore receiptStore) {
    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();
    blockChain.tryToConnect(block1);
    return world;
}
Also used : ArrayList(java.util.ArrayList) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) AccountBuilder(co.rsk.test.builders.AccountBuilder) World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Example 22 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class Web3ImplLogsTest method createCallerContractWithEventsOnInvoke.

@Test
public void createCallerContractWithEventsOnInvoke() 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 = getMainContractTransaction(acc1);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    blockChain.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();
    blockChain.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();
    blockChain.tryToConnect(block3);
    Object[] logs = web3.eth_getFilterChanges(id);
    Assert.assertNotNull(id);
    Assert.assertNotNull(logs);
    Assert.assertEquals(3, logs.length);
    Assert.assertEquals("0x" + mainAddress, ((LogFilterElement) logs[0]).address);
    Assert.assertEquals("0x" + callerAddress, ((LogFilterElement) logs[1]).address);
    Assert.assertEquals("0x" + mainAddress, ((LogFilterElement) logs[2]).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)

Example 23 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class Web3ImplLogsTest method createCallerContractWithEvents.

@Test
public void createCallerContractWithEvents() 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 = getMainContractTransaction(acc1);
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    blockChain.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();
    blockChain.tryToConnect(block2);
    Object[] logs = web3.eth_getFilterChanges(id);
    Assert.assertNotNull(id);
    Assert.assertNotNull(logs);
    Assert.assertEquals(2, logs.length);
    Assert.assertEquals("0x" + mainAddress, ((LogFilterElement) logs[0]).address);
    Assert.assertEquals("0x" + callerAddress, ((LogFilterElement) logs[1]).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)

Example 24 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class Web3ImplLogsTest method getWeb3WithContractCreationWithoutEvents.

private Web3Impl getWeb3WithContractCreationWithoutEvents() {
    World world = new World();
    Account acc1 = new AccountBuilder(world).name("notDefault").balance(Coin.valueOf(10000000)).build();
    Block genesis = world.getBlockByName("g00");
    /* contract compiled in data attribute of tx
        contract greeter {

            address owner;
            modifier onlyOwner { if (msg.sender != owner) throw; _ ; }

            function greeter() public {
                owner = msg.sender;
            }
            function greet(string param) onlyOwner constant returns (string) {
                return param;
            }
        } */
    Transaction tx = new TransactionBuilder().sender(acc1).gasLimit(BigInteger.valueOf(100000)).gasPrice(BigInteger.ONE).data(compiledGreeter).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    BlockChainImpl blockChain = world.getBlockChain();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    blockChain.tryToConnect(block1);
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), blockChain.getBlockStore(), null, null, null, 10, 100);
    Web3Impl web3 = createWeb3(world.getBlockChain(), transactionPool);
    web3.personal_newAccountWithSeed("notDefault");
    return web3;
}
Also used : ArrayList(java.util.ArrayList) BlockChainImpl(co.rsk.core.bc.BlockChainImpl) TransactionBuilder(co.rsk.test.builders.TransactionBuilder) World(co.rsk.test.World) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder)

Example 25 with BlockChainImpl

use of co.rsk.core.bc.BlockChainImpl in project rskj by rsksmart.

the class Web3ImplTest method getBalanceWithAccountAndBlockWithTransaction.

@Test
public void getBalanceWithAccountAndBlockWithTransaction() throws Exception {
    World world = new World();
    BlockChainImpl blockChain = world.getBlockChain();
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepository(), blockChain.getBlockStore(), null, null, null, 10, 100);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(10000000)).build();
    Account acc2 = new AccountBuilder(world).name("acc2").build();
    Block genesis = world.getBlockByName("g00");
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(10000)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, blockChain.tryToConnect(block1));
    Web3Impl web3 = createWeb3(world, transactionPool, null);
    web3.repository = world.getBlockChain().getRepository();
    String accountAddress = Hex.toHexString(acc2.getAddress().getBytes());
    String balanceString = "0x" + Hex.toHexString(BigInteger.valueOf(10000).toByteArray());
    org.junit.Assert.assertEquals("0x0", web3.eth_getBalance(accountAddress, "0x0"));
    org.junit.Assert.assertEquals(balanceString, web3.eth_getBalance(accountAddress, "0x1"));
    org.junit.Assert.assertEquals(balanceString, web3.eth_getBalance(accountAddress, "pending"));
}
Also used : BlockChainImpl(co.rsk.core.bc.BlockChainImpl) TransactionBuilder(co.rsk.test.builders.TransactionBuilder) World(co.rsk.test.World) TransactionPoolImpl(co.rsk.core.bc.TransactionPoolImpl) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Aggregations

BlockChainImpl (co.rsk.core.bc.BlockChainImpl)28 Test (org.junit.Test)15 TransactionPoolImpl (co.rsk.core.bc.TransactionPoolImpl)14 ArrayList (java.util.ArrayList)14 World (co.rsk.test.World)11 BlockBuilder (co.rsk.test.builders.BlockBuilder)11 AccountBuilder (co.rsk.test.builders.AccountBuilder)10 BlockDifficulty (co.rsk.core.BlockDifficulty)7 BlockChainBuilder (co.rsk.test.builders.BlockChainBuilder)6 Block (org.ethereum.core.Block)6 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)4 BlockChainImplTest (co.rsk.core.bc.BlockChainImplTest)4 BigInteger (java.math.BigInteger)4 BlockHeader (org.ethereum.core.BlockHeader)4 HashMapDB (org.ethereum.datasource.HashMapDB)4 RskAddress (co.rsk.core.RskAddress)3 TransactionBuilder (co.rsk.test.builders.TransactionBuilder)3 DummyBlockValidator (co.rsk.validators.DummyBlockValidator)3 BlockStore (org.ethereum.db.BlockStore)3 SimpleEthereum (org.ethereum.rpc.Simples.SimpleEthereum)3