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]);
}
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]);
}
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);
}
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"));
}
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);
}
Aggregations