Search in sources :

Example 21 with BlockBuilder

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

the class Web3ImplTest method getTransactionByHash.

@Test
public void getTransactionByHash() 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).parent(genesis).transactions(txs).build();
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String hashString = tx.getHash().toHexString();
    TransactionResultDTO tr = web3.eth_getTransactionByHash(hashString);
    Assert.assertNotNull(tr);
    org.junit.Assert.assertEquals("0x" + hashString, tr.hash);
    String blockHashString = "0x" + block1.getHash();
    org.junit.Assert.assertEquals(blockHashString, tr.blockHash);
    org.junit.Assert.assertEquals("0x00", tr.input);
    org.junit.Assert.assertEquals("0x" + Hex.toHexString(tx.getReceiveAddress().getBytes()), tr.to);
}
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 22 with BlockBuilder

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

the class Web3ImplTest method getBlockByHashWithTransactionsHashAsResult.

@Test
public void getBlockByHashWithTransactionsHashAsResult() throws Exception {
    World world = new World();
    Web3Impl web3 = createWeb3(world);
    Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(220000)).build();
    Account acc2 = new AccountBuilder().name("acc2").build();
    Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(0)).build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block genesis = world.getBlockChain().getBestBlock();
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    block1.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    org.junit.Assert.assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
    String block1HashString = block1.getHashJsonString();
    Web3.BlockResult bresult = web3.eth_getBlockByHash(block1HashString, false);
    Assert.assertNotNull(bresult);
    org.junit.Assert.assertEquals(block1HashString, bresult.hash);
    org.junit.Assert.assertEquals(1, bresult.transactions.length);
    org.junit.Assert.assertEquals(tx.getHash().toJsonString(), bresult.transactions[0]);
    org.junit.Assert.assertEquals(0, bresult.uncles.length);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) World(co.rsk.test.World) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 23 with BlockBuilder

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

the class Web3ImplTest method getBlockByNumber.

@Test
public void getBlockByNumber() 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(2).parent(genesis).build();
    block1b.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    Block block2b = new BlockBuilder(world).difficulty(11).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.BlockResult bresult = web3.eth_getBlockByNumber("0x1", false);
    Assert.assertNotNull(bresult);
    String blockHash = "0x" + block1b.getHash();
    org.junit.Assert.assertEquals(blockHash, bresult.hash);
    bresult = web3.eth_getBlockByNumber("0x2", true);
    Assert.assertNotNull(bresult);
    blockHash = "0x" + block2b.getHash();
    org.junit.Assert.assertEquals(blockHash, bresult.hash);
}
Also used : World(co.rsk.test.World) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 24 with BlockBuilder

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

the class Web3ImplTest method callFromAddressInWallet.

@Test
public void callFromAddressInWallet() throws Exception {
    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("60606040525b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff02191690836c010000000000000000000000009081020402179055505b610181806100516000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063ead710c41461003c57610037565b610002565b34610002576100956004808035906020019082018035906020019191908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050909091905050610103565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156100f55780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6020604051908101604052806000815260200150600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561017357610002565b81905061017b565b5b91905056").build();
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    Block block1 = new BlockBuilder(world).parent(genesis).transactions(txs).build();
    world.getBlockChain().tryToConnect(block1);
    Web3Impl web3 = createWeb3Mocked(world);
    web3.personal_newAccountWithSeed("default");
    web3.personal_newAccountWithSeed("notDefault");
    Web3.CallArguments argsForCall = new Web3.CallArguments();
    argsForCall.from = TypeConverter.toJsonHex(acc1.getAddress().getBytes());
    argsForCall.to = TypeConverter.toJsonHex(tx.getContractAddress().getBytes());
    argsForCall.data = "0xead710c40000000000000000000000000000000000000000000000000000000064617665";
    String result = web3.eth_call(argsForCall, "latest");
    org.junit.Assert.assertEquals("0x0000000000000000000000000000000000000000000000000000000064617665", result);
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) World(co.rsk.test.World) AccountBuilder(co.rsk.test.builders.AccountBuilder) BlockBuilder(co.rsk.test.builders.BlockBuilder) Test(org.junit.Test)

Example 25 with BlockBuilder

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

the class Web3ImplTest method getBlockByHash.

@Test
public void getBlockByHash() 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();
    block1.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
    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));
    String block1HashString = "0x" + block1.getHash();
    String block1bHashString = "0x" + block1b.getHash();
    String block2bHashString = "0x" + block2b.getHash();
    Web3.BlockResult bresult = web3.eth_getBlockByHash(block1HashString, false);
    Assert.assertNotNull(bresult);
    org.junit.Assert.assertEquals(block1HashString, bresult.hash);
    org.junit.Assert.assertEquals("0x00", bresult.extraData);
    org.junit.Assert.assertEquals(0, bresult.transactions.length);
    org.junit.Assert.assertEquals(0, bresult.uncles.length);
    bresult = web3.eth_getBlockByHash(block1bHashString, true);
    Assert.assertNotNull(bresult);
    org.junit.Assert.assertEquals(block1bHashString, bresult.hash);
    bresult = web3.eth_getBlockByHash(block2bHashString, true);
    Assert.assertNotNull(bresult);
    org.junit.Assert.assertEquals(block2bHashString, bresult.hash);
}
Also used : 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