Search in sources :

Example 96 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class HashMapDBTest method putKeyValue.

@Test
public void putKeyValue() {
    KeyValueDataSource ds = new HashMapDB();
    byte[] key = new byte[] { 0x01, 0x02 };
    byte[] value = new byte[] { 0x03, 0x03 };
    byte[] result = ds.put(key, value);
    Assert.assertNull(result);
}
Also used : KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Example 97 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class HashMapDBTest method putAndDeleteKeyValue.

@Test
public void putAndDeleteKeyValue() {
    KeyValueDataSource ds = new HashMapDB();
    byte[] key = new byte[] { 0x01, 0x02 };
    byte[] value = new byte[] { 0x03, 0x03 };
    ds.put(key, value);
    ds.delete(key);
    byte[] result = ds.get(key);
    Assert.assertNull(result);
}
Also used : KeyValueDataSource(org.ethereum.datasource.KeyValueDataSource) HashMapDB(org.ethereum.datasource.HashMapDB) Test(org.junit.Test)

Example 98 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class Web3ImplTest method getTransactionByHash.

@Test
public void getTransactionByHash() {
    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 block1 = createCanonicalBlock(world, txs);
    String hashString = tx.getHash().toHexString();
    TransactionResultDTO tr = web3.eth_getTransactionByHash(hashString);
    assertNotNull(tr);
    assertEquals("0x" + hashString, tr.getHash());
    String blockHashString = "0x" + block1.getHash();
    assertEquals(blockHashString, tr.getBlockHash());
    assertEquals("0x", tr.getInput());
    assertEquals("0x" + ByteUtil.toHexString(tx.getReceiveAddress().getBytes()), tr.getTo());
    // Check the v value used to encode the transaction
    // NOT the v value used in signature
    // the encoded value includes chain id
    Assert.assertArrayEquals(new byte[] { tx.getEncodedV() }, TypeConverter.stringHexToByteArray(tr.getV()));
    Assert.assertThat(TypeConverter.stringHexToBigInteger(tr.getS()), is(tx.getSignature().getS()));
    Assert.assertThat(TypeConverter.stringHexToBigInteger(tr.getR()), is(tx.getSignature().getR()));
}
Also used : TransactionBuilder(co.rsk.test.builders.TransactionBuilder) HashMapDB(org.ethereum.datasource.HashMapDB) World(co.rsk.test.World) ReceiptStoreImpl(org.ethereum.db.ReceiptStoreImpl) TransactionResultDTO(org.ethereum.rpc.dto.TransactionResultDTO) AccountBuilder(co.rsk.test.builders.AccountBuilder) ReceiptStore(org.ethereum.db.ReceiptStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 99 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class Web3ImplTest method getTransactionReceipt.

@Test
public void getTransactionReceipt() {
    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 block1 = createCanonicalBlock(world, txs);
    String hashString = tx.getHash().toHexString();
    TransactionReceiptDTO tr = web3.eth_getTransactionReceipt(hashString);
    assertNotNull(tr);
    assertEquals("0x" + hashString, tr.getTransactionHash());
    String trxFrom = TypeConverter.toJsonHex(tx.getSender().getBytes());
    assertEquals(trxFrom, tr.getFrom());
    String trxTo = TypeConverter.toJsonHex(tx.getReceiveAddress().getBytes());
    assertEquals(trxTo, tr.getTo());
    String blockHashString = "0x" + block1.getHash();
    assertEquals(blockHashString, tr.getBlockHash());
    String blockNumberAsHex = "0x" + Long.toHexString(block1.getNumber());
    assertEquals(blockNumberAsHex, tr.getBlockNumber());
    String rawTransactionReceipt = web3.rsk_getRawTransactionReceiptByHash(hashString);
    String expectedRawTxReceipt = "0xf9010c01825208b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c082520801";
    assertEquals(expectedRawTxReceipt, rawTransactionReceipt);
    String[] transactionReceiptNodes = web3.rsk_getTransactionReceiptNodesByHash(blockHashString, hashString);
    ArrayList<String> expectedRawTxReceiptNodes = new ArrayList<>();
    expectedRawTxReceiptNodes.add("0x70078048ee76b19fc451dba9dbee8b3e73084f79ea540d3940b3b36b128e8024e9302500010f");
    assertEquals(1, transactionReceiptNodes.length);
    assertEquals(expectedRawTxReceiptNodes.get(0), transactionReceiptNodes[0]);
}
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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 100 with HashMapDB

use of org.ethereum.datasource.HashMapDB in project rskj by rsksmart.

the class Web3ImplTest method checkSendTransaction.

private void checkSendTransaction(Byte chainId) {
    BigInteger nonce = BigInteger.ONE;
    ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
    World world = new World(receiptStore);
    BlockChainImpl blockChain = world.getBlockChain();
    BlockStore blockStore = world.getBlockStore();
    TransactionExecutorFactory transactionExecutorFactory = buildTransactionExecutorFactory(blockStore, world.getBlockTxSignatureCache());
    TransactionPool transactionPool = new TransactionPoolImpl(config, world.getRepositoryLocator(), blockStore, blockFactory, null, transactionExecutorFactory, world.getReceivedTxSignatureCache(), 10, 100);
    Web3Impl web3 = createWeb3(world, transactionPool, receiptStore);
    // **** Initializes data ******************
    String[] accounts = web3.personal_listAccounts();
    String addr1 = accounts[0];
    String addr2 = accounts[1];
    transactionPool.processBest(blockChain.getBestBlock());
    String toAddress = addr2;
    BigInteger value = BigInteger.valueOf(7);
    BigInteger gasPrice = BigInteger.valueOf(8);
    BigInteger gasLimit = BigInteger.valueOf(300000);
    String data = "0xff";
    // ***** Executes the transaction *******************
    CallArguments args = new CallArguments();
    args.setFrom(addr1);
    args.setTo(addr2);
    args.setData(data);
    args.setGas(TypeConverter.toQuantityJsonHex(gasLimit));
    args.setGasPrice(TypeConverter.toQuantityJsonHex(gasPrice));
    args.setValue(value.toString());
    args.setNonce(nonce.toString());
    if (chainId != null) {
        args.setChainId(TypeConverter.toJsonHex(new byte[] { chainId }));
    }
    String txHash = web3.eth_sendTransaction(args);
    // ***** Verifies tx hash
    String to = toAddress.substring(2);
    Transaction tx = Transaction.builder().nonce(nonce).gasPrice(gasPrice).gasLimit(gasLimit).destination(Hex.decode(to)).data(args.getData() == null ? null : Hex.decode(args.getData())).chainId(config.getNetworkConstants().getChainId()).value(value).build();
    tx.sign(wallet.getAccount(new RskAddress(addr1)).getEcKey().getPrivKeyBytes());
    String expectedHash = tx.getHash().toJsonString();
    assertEquals("Method is not creating the expected transaction", 0, expectedHash.compareTo(txHash));
}
Also used : BlockStore(org.ethereum.db.BlockStore) 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) BigInteger(java.math.BigInteger) ReceiptStore(org.ethereum.db.ReceiptStore)

Aggregations

HashMapDB (org.ethereum.datasource.HashMapDB)208 Test (org.junit.Test)181 World (co.rsk.test.World)45 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)34 ReceiptStore (org.ethereum.db.ReceiptStore)34 ReceiptStoreImpl (org.ethereum.db.ReceiptStoreImpl)34 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)29 KeyValueDataSource (org.ethereum.datasource.KeyValueDataSource)27 IndexedBlockStore (org.ethereum.db.IndexedBlockStore)27 TrieStore (co.rsk.trie.TrieStore)26 HashMapBlocksIndex (co.rsk.db.HashMapBlocksIndex)23 Trie (co.rsk.trie.Trie)21 Blockchain (org.ethereum.core.Blockchain)17 RskAddress (co.rsk.core.RskAddress)16 Transaction (org.ethereum.core.Transaction)16 DslParser (co.rsk.test.dsl.DslParser)15 WorldDslProcessor (co.rsk.test.dsl.WorldDslProcessor)15 JsonNode (com.fasterxml.jackson.databind.JsonNode)15 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)13 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)12