Search in sources :

Example 46 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class SamplePrecompiledContractTest method samplePrecompiledContractMethod1Ok.

@Test
public void samplePrecompiledContractMethod1Ok() {
    DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
    SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
    String funcJson = "{\n" + "   'constant':false, \n" + "   'inputs':[{'name':'param0','type':'int'}, \n" + "               {'name':'param1','type':'bytes'}, \n" + "               {'name':'param2','type':'int'}], \n" + "    'name':'Method1', \n" + "   'outputs':[{'name':'output0','type':'int'}], \n" + "    'type':'function' \n" + "}\n";
    funcJson = funcJson.replaceAll("'", "\"");
    CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
    byte[] bytes = new byte[] { (byte) 0xab, (byte) 0xcd, (byte) 0xef };
    byte[] data = function.encode(111, bytes, 222);
    contract.init(null, null, new RepositoryImpl(config), null, null, new ArrayList<LogInfo>());
    byte[] result = contract.execute(data);
    Object[] results = function.decodeResult(result);
    assertEquals(new BigInteger("1"), results[0]);
}
Also used : LogInfo(org.ethereum.vm.LogInfo) RepositoryImpl(co.rsk.db.RepositoryImpl) CallTransaction(org.ethereum.core.CallTransaction) BigInteger(java.math.BigInteger) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 47 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class SamplePrecompiledContractTest method samplePrecompiledContractMethod1LargeData.

@Test
public void samplePrecompiledContractMethod1LargeData() {
    DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
    SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
    String funcJson = "{\n" + "   'constant':false, \n" + "   'inputs':[{'name':'param0','type':'int'}, \n" + "               {'name':'param1','type':'string'}], \n" + "    'name':'Method1', \n" + "   'outputs':[{'name':'output0','type':'int'}], \n" + "    'type':'function' \n" + "}\n";
    funcJson = funcJson.replaceAll("'", "\"");
    CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
    byte[] data = function.encode(111, StringUtils.leftPad("foobar", 1000000, '*'));
    contract.init(null, null, new RepositoryImpl(config), null, null, new ArrayList<LogInfo>());
    byte[] result = contract.execute(data);
    Object[] results = function.decodeResult(result);
    assertEquals(new BigInteger("1"), results[0]);
}
Also used : LogInfo(org.ethereum.vm.LogInfo) RepositoryImpl(co.rsk.db.RepositoryImpl) CallTransaction(org.ethereum.core.CallTransaction) BigInteger(java.math.BigInteger) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 48 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class SamplePrecompiledContractTest method samplePrecompiledContractAddBalanceOk.

@Test
public void samplePrecompiledContractAddBalanceOk() {
    DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
    SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
    String funcJson = "{\n" + "   'constant':false, \n" + "   'inputs':[], \n" + "    'name':'AddBalance', \n" + "   'outputs':[], \n" + "    'type':'function' \n" + "}\n";
    funcJson = funcJson.replaceAll("'", "\"");
    CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
    byte[] data = function.encode();
    Repository repository = new RepositoryImpl(config);
    contract.init(null, null, repository, null, null, new ArrayList<LogInfo>());
    contract.execute(data);
    int balance = this.GetBalance(repository);
    assertEquals(50000, balance);
}
Also used : Repository(org.ethereum.core.Repository) LogInfo(org.ethereum.vm.LogInfo) RepositoryImpl(co.rsk.db.RepositoryImpl) CallTransaction(org.ethereum.core.CallTransaction) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 49 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class NetworkStateExporterTest method testNoContracts.

@Test
public void testNoContracts() throws Exception {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    String address1String = "1000000000000000000000000000000000000000";
    RskAddress addr1 = new RskAddress(address1String);
    repository.createAccount(addr1);
    repository.addBalance(addr1, Coin.valueOf(1L));
    repository.increaseNonce(addr1);
    String address2String = "2000000000000000000000000000000000000000";
    RskAddress addr2 = new RskAddress(address2String);
    repository.createAccount(addr2);
    repository.addBalance(addr2, Coin.valueOf(10L));
    repository.increaseNonce(addr2);
    repository.increaseNonce(addr2);
    RskAddress remascSender = RskAddress.nullAddress();
    repository.createAccount(remascSender);
    repository.increaseNonce(remascSender);
    repository.createAccount(PrecompiledContracts.REMASC_ADDR);
    repository.addBalance(PrecompiledContracts.REMASC_ADDR, Coin.valueOf(10L));
    repository.increaseNonce(PrecompiledContracts.REMASC_ADDR);
    Map result = writeAndReadJson(repository);
    Assert.assertEquals(3, result.keySet().size());
    Map address1Value = (Map) result.get(address1String);
    Assert.assertEquals(2, address1Value.keySet().size());
    Assert.assertEquals("1", address1Value.get("balance"));
    Assert.assertEquals("1", address1Value.get("nonce"));
    Map address2Value = (Map) result.get(address2String);
    Assert.assertEquals(2, address2Value.keySet().size());
    Assert.assertEquals("10", address2Value.get("balance"));
    Assert.assertEquals("2", address2Value.get("nonce"));
    Map remascValue = (Map) result.get(PrecompiledContracts.REMASC_ADDR_STR);
    Assert.assertEquals(2, remascValue.keySet().size());
    Assert.assertEquals("10", remascValue.get("balance"));
    Assert.assertEquals("1", remascValue.get("nonce"));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) HashMapDB(org.ethereum.datasource.HashMapDB) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 50 with RepositoryImpl

use of co.rsk.db.RepositoryImpl in project rskj by rsksmart.

the class BlockExecutorTest method executeBlockWithTxThatMakesBlockInvalidSenderHasNoBalance.

@Test
public void executeBlockWithTxThatMakesBlockInvalidSenderHasNoBalance() {
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    Repository track = repository.startTracking();
    Account account = createAccount("acctest1", track, Coin.valueOf(30000));
    Account account2 = createAccount("acctest2", track, Coin.valueOf(10L));
    Account account3 = createAccount("acctest3", track, Coin.ZERO);
    track.commit();
    Assert.assertFalse(Arrays.equals(EMPTY_TRIE_HASH, repository.getRoot()));
    BlockExecutor executor = new BlockExecutor(config, repository, null, null, null);
    Transaction tx = createTransaction(account, account2, BigInteger.TEN, repository.getNonce(account.getAddress()));
    Transaction tx2 = createTransaction(account3, account2, BigInteger.TEN, repository.getNonce(account3.getAddress()));
    List<Transaction> txs = new ArrayList<>();
    txs.add(tx);
    txs.add(tx2);
    List<BlockHeader> uncles = new ArrayList<>();
    BlockGenerator blockGenerator = new BlockGenerator();
    Block genesis = blockGenerator.getGenesisBlock();
    genesis.setStateRoot(repository.getRoot());
    Block block = blockGenerator.createChildBlock(genesis, txs, uncles, 1, null);
    BlockResult result = executor.execute(block, genesis.getStateRoot(), false);
    Assert.assertSame(BlockResult.INTERRUPTED_EXECUTION_BLOCK_RESULT, result);
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) ArrayList(java.util.ArrayList) HashMapDB(org.ethereum.datasource.HashMapDB) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) RepositoryImpl(co.rsk.db.RepositoryImpl) Test(org.junit.Test)

Aggregations

RepositoryImpl (co.rsk.db.RepositoryImpl)62 Test (org.junit.Test)54 Repository (org.ethereum.core.Repository)25 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 RskAddress (co.rsk.core.RskAddress)19 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)16 HashMapDB (org.ethereum.datasource.HashMapDB)15 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)13 org.ethereum.core (org.ethereum.core)13 BlockGenerator (co.rsk.blockchain.utils.BlockGenerator)12 LogInfo (org.ethereum.vm.LogInfo)12 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)11 BridgeConstants (co.rsk.config.BridgeConstants)10 BridgeEventLogger (co.rsk.peg.utils.BridgeEventLogger)10 DataWord (org.ethereum.vm.DataWord)9 ArrayList (java.util.ArrayList)8 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)7 ECKey (org.ethereum.crypto.ECKey)7 Keccak256 (co.rsk.crypto.Keccak256)6 SimpleBlockChain (co.rsk.peg.simples.SimpleBlockChain)6