Search in sources :

Example 6 with ContractDetails

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.");
    }
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) HashMapDB(org.ethereum.datasource.HashMapDB) CountDownLatch(java.util.concurrent.CountDownLatch) TrieStore(co.rsk.trie.TrieStore) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Example 7 with ContractDetails

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);
}
Also used : Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Example 8 with ContractDetails

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);
}
Also used : ProgramInvoke(org.ethereum.vm.program.invoke.ProgramInvoke) Coin(co.rsk.core.Coin) BlockchainConfig(org.ethereum.config.BlockchainConfig) Program(org.ethereum.vm.program.Program) RskAddress(co.rsk.core.RskAddress) ContractDetails(org.ethereum.db.ContractDetails)

Example 9 with ContractDetails

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())));
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) AccountState(org.ethereum.core.AccountState) HashMapDB(org.ethereum.datasource.HashMapDB) HashMap(java.util.HashMap) Map(java.util.Map) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Example 10 with ContractDetails

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()));
}
Also used : TestContract(co.rsk.util.TestContract) ProgramResult(org.ethereum.vm.program.ProgramResult) CallTransaction(org.ethereum.core.CallTransaction) Block(org.ethereum.core.Block) ContractDetails(org.ethereum.db.ContractDetails) Test(org.junit.Test)

Aggregations

ContractDetails (org.ethereum.db.ContractDetails)24 Test (org.junit.Test)13 DataWord (org.ethereum.vm.DataWord)9 Repository (org.ethereum.core.Repository)8 RskAddress (co.rsk.core.RskAddress)7 AccountState (org.ethereum.core.AccountState)7 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)6 HashMapDB (org.ethereum.datasource.HashMapDB)6 ProgramResult (org.ethereum.vm.program.ProgramResult)5 TestContract (co.rsk.util.TestContract)4 BigInteger (java.math.BigInteger)4 Block (org.ethereum.core.Block)4 CallTransaction (org.ethereum.core.CallTransaction)4 Coin (co.rsk.core.Coin)3 TrieStore (co.rsk.trie.TrieStore)3 HashMap (java.util.HashMap)3 RskSystemProperties (co.rsk.config.RskSystemProperties)2 VmConfig (co.rsk.config.VmConfig)2 RepositoryImpl (co.rsk.db.RepositoryImpl)2 ArrayList (java.util.ArrayList)2