use of org.ethereum.db.ContractDetails in project rskj by rsksmart.
the class RepositoryTest method testMultiThread.
// testing for snapshot
@Test
public void testMultiThread() throws InterruptedException {
TrieStore store = new TrieStoreImpl(new HashMapDB());
final Repository repository = new RepositoryImpl(config, store);
final byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
final DataWord cowKey1 = new DataWord("c1");
final DataWord cowKey2 = new DataWord("c2");
final byte[] cowVal0 = Hex.decode("c0a0");
// track
Repository track2 = repository.startTracking();
track2.addStorageBytes(COW, cowKey2, cowVal0);
track2.commit();
repository.flush();
ContractDetails cowDetails = repository.getContractDetails(COW);
assertArrayEquals(cowVal0, cowDetails.getBytes(cowKey2));
final CountDownLatch failSema = new CountDownLatch(1);
new Thread(() -> {
try {
int cnt = 1;
while (true) {
// To Review, not needed?
repository.flush();
Repository snap = repository.getSnapshotTo(repository.getRoot()).startTracking();
byte[] vcnr = new byte[1];
vcnr[0] = (byte) (cnt % 128);
snap.addStorageBytes(COW, cowKey1, vcnr);
cnt++;
}
} catch (Throwable e) {
e.printStackTrace();
failSema.countDown();
}
}).start();
new Thread(() -> {
int cnt = 1;
try {
while (true) {
// track
Repository track21 = repository.startTracking();
byte[] cVal = new byte[1];
cVal[0] = (byte) (cnt % 128);
track21.addStorageBytes(COW, cowKey1, cVal);
track21.commit();
repository.flush();
assertArrayEquals(cVal, repository.getStorageBytes(COW, cowKey1));
assertArrayEquals(cowVal0, repository.getStorageBytes(COW, cowKey2));
cnt++;
}
} catch (Throwable e) {
e.printStackTrace();
failSema.countDown();
}
}).start();
failSema.await(10, TimeUnit.SECONDS);
if (failSema.getCount() == 0) {
throw new RuntimeException("Test failed.");
}
}
use of org.ethereum.db.ContractDetails in project rskj by rsksmart.
the class RepositoryTest method test19.
@Test
public void test19() {
Repository repository = new RepositoryImpl(config);
Repository track = repository.startTracking();
byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
DataWord cowKey1 = new DataWord("c1");
byte[] cowVal1 = Hex.decode("c0a1");
byte[] cowVal0 = Hex.decode("c0a0");
DataWord horseKey1 = new DataWord("e1");
byte[] horseVal1 = Hex.decode("c0a1");
byte[] horseVal0 = Hex.decode("c0a0");
track.addStorageBytes(COW, cowKey1, cowVal0);
track.addStorageBytes(HORSE, horseKey1, horseVal0);
track.commit();
// track
Repository track2 = repository.startTracking();
track2.addStorageBytes(HORSE, horseKey1, horseVal0);
Repository track3 = track2.startTracking();
ContractDetails cowDetails = track3.getContractDetails(COW);
cowDetails.putBytes(cowKey1, cowVal1);
ContractDetails horseDetails = track3.getContractDetails(HORSE);
horseDetails.putBytes(horseKey1, horseVal1);
track3.commit();
track2.rollback();
ContractDetails cowDetailsOrigin = repository.getContractDetails(COW);
byte[] cowValOrin = cowDetailsOrigin.getBytes(cowKey1);
ContractDetails horseDetailsOrigin = repository.getContractDetails(HORSE);
byte[] horseValOrin = horseDetailsOrigin.getBytes(horseKey1);
assertArrayEquals(cowVal0, cowValOrin);
assertArrayEquals(horseVal0, horseValOrin);
}
use of org.ethereum.db.ContractDetails in project rskj by rsksmart.
the class TransactionExecutor method create.
private void create() {
RskAddress newContractAddress = tx.getContractAddress();
if (isEmpty(tx.getData())) {
mEndGas = toBI(tx.getGasLimit()).subtract(BigInteger.valueOf(basicTxCost));
cacheTrack.createAccount(newContractAddress);
} else {
ProgramInvoke programInvoke = programInvokeFactory.createProgramInvoke(tx, txindex, executionBlock, cacheTrack, blockStore);
this.vm = new VM(vmConfig, precompiledContracts);
BlockchainConfig configForBlock = config.getBlockchainConfig().getConfigForBlock(executionBlock.getNumber());
this.program = new Program(vmConfig, precompiledContracts, configForBlock, tx.getData(), programInvoke, tx);
// reset storage if the contract with the same address already exists
// TCK test case only - normally this is near-impossible situation in the real network
ContractDetails contractDetails = program.getStorage().getContractDetails(newContractAddress);
for (DataWord key : contractDetails.getStorageKeys()) {
program.storageSave(key, DataWord.ZERO);
}
}
Coin endowment = tx.getValue();
cacheTrack.transfer(tx.getSender(), newContractAddress, endowment);
}
use of org.ethereum.db.ContractDetails in project rskj by rsksmart.
the class NetworkStateExporterTest method testContracts.
@Test
public void testContracts() 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);
ContractDetails contractDetails = new co.rsk.db.ContractDetailsImpl(config);
contractDetails.setCode(new byte[] { 1, 2, 3, 4 });
contractDetails.put(DataWord.ZERO, DataWord.ONE);
contractDetails.putBytes(DataWord.ONE, new byte[] { 5, 6, 7, 8 });
repository.updateContractDetails(addr1, contractDetails);
AccountState accountState = repository.getAccountState(addr1);
accountState.setStateRoot(contractDetails.getStorageHash());
repository.updateAccountState(addr1, accountState);
Map result = writeAndReadJson(repository);
Assert.assertEquals(1, result.keySet().size());
Map address1Value = (Map) result.get(address1String);
Assert.assertEquals(3, address1Value.keySet().size());
Assert.assertEquals("1", address1Value.get("balance"));
Assert.assertEquals("1", address1Value.get("nonce"));
Map contract = (Map) address1Value.get("contract");
Assert.assertEquals(2, contract.keySet().size());
Assert.assertEquals("01020304", contract.get("code"));
Map data = (Map) contract.get("data");
Assert.assertEquals(2, data.keySet().size());
Assert.assertEquals("01", data.get(Hex.toHexString(DataWord.ZERO.getData())));
Assert.assertEquals("05060708", data.get(Hex.toHexString(DataWord.ONE.getData())));
}
use of org.ethereum.db.ContractDetails in project rskj by rsksmart.
the class ReversibleTransactionExecutorTest method executeTransactionHello.
@Test
public void executeTransactionHello() {
TestContract hello = TestContract.hello();
CallTransaction.Function helloFn = hello.functions.get("hello");
ContractDetails contract = contractRunner.addContract(hello.runtimeBytecode);
RskAddress from = RskAddress.nullAddress();
byte[] gasPrice = Hex.decode("00");
byte[] value = Hex.decode("00");
byte[] gasLimit = Hex.decode("f424");
Block bestBlock = factory.getBlockchain().getBestBlock();
ProgramResult result = reversibleTransactionExecutor.executeTransaction(bestBlock, bestBlock.getCoinbase(), gasPrice, gasLimit, contract.getAddress(), value, helloFn.encode(), from.getBytes());
Assert.assertNull(result.getException());
Assert.assertArrayEquals(new String[] { "chinchilla" }, helloFn.decodeResult(result.getHReturn()));
}
Aggregations