Search in sources :

Example 41 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class RepositoryTest method test16_4.

@Test
public void test16_4() {
    Repository repository = new RepositoryImpl(config);
    byte[] cow = Hex.decode("CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826");
    byte[] horse = Hex.decode("13978AEE95F38490E9769C39B2773ED763D9CD5F");
    byte[] cowKey1 = "key-c-1".getBytes();
    byte[] cowValue1 = "val-c-1".getBytes();
    byte[] horseKey1 = "key-h-1".getBytes();
    byte[] horseValue1 = "val-h-1".getBytes();
    byte[] cowKey2 = "key-c-2".getBytes();
    byte[] cowValue2 = "val-c-2".getBytes();
    byte[] horseKey2 = "key-h-2".getBytes();
    byte[] horseValue2 = "val-h-2".getBytes();
    Repository track = repository.startTracking();
    track.addStorageBytes(COW, new DataWord(cowKey1), cowValue1);
    track.commit();
    // changes level_1
    Repository track1 = repository.startTracking();
    // changes level_2
    Repository track2 = track1.startTracking();
    track2.addStorageBytes(COW, new DataWord(cowKey2), cowValue2);
    track2.commit();
    // leaving level_2
    track1.commit();
    // leaving level_1
    assertArrayEquals(cowValue1, track1.getStorageBytes(COW, new DataWord(cowKey1)));
    assertArrayEquals(cowValue2, track1.getStorageBytes(COW, new DataWord(cowKey2)));
    repository.close();
}
Also used : Repository(org.ethereum.core.Repository) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 42 with Repository

use of org.ethereum.core.Repository 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 43 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class LockTest method buildInitializer.

private BridgeStorageProviderInitializer buildInitializer() {
    final int minHashes = 100;
    final int maxHashes = 10000;
    final int minHeight = 1000;
    final int maxHeight = 2000;
    return (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
        int hashesToGenerate = Helper.randomInRange(minHashes, maxHashes);
        int randomHashIndex = Helper.randomInRange(0, hashesToGenerate - 1);
        Random rnd = new Random();
        Map<Sha256Hash, Long> hashesAlreadyProcessed;
        try {
            hashesAlreadyProcessed = provider.getBtcTxHashesAlreadyProcessed();
        } catch (IOException e) {
            throw new RuntimeException("Exception trying to gather hashes already processed for benchmarking");
        }
        for (int i = 0; i < hashesToGenerate; i++) {
            Sha256Hash hash = Sha256Hash.of(BigInteger.valueOf(rnd.nextLong()).toByteArray());
            long height = Helper.randomInRange(minHeight, maxHeight);
            hashesAlreadyProcessed.put(hash, height);
            if (i == randomHashIndex) {
                randomHashInMap = hash;
            }
        }
    };
}
Also used : Repository(org.ethereum.core.Repository) Random(java.util.Random) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) Sha256Hash(co.rsk.bitcoinj.core.Sha256Hash) IOException(java.io.IOException) Map(java.util.Map)

Example 44 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class LockWhitelistTest method buildInitializer.

private BridgeStorageProviderInitializer buildInitializer() {
    final int minSize = 10;
    final int maxSize = 100;
    final int minBtcBlocks = 500;
    final int maxBtcBlocks = 1000;
    return (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
        BtcBlockStore btcBlockStore = new RepositoryBlockStore(new RskSystemProperties(), repository, PrecompiledContracts.BRIDGE_ADDR);
        Context btcContext = new Context(networkParameters);
        BtcBlockChain btcBlockChain;
        try {
            btcBlockChain = new BtcBlockChain(btcContext, btcBlockStore);
        } catch (BlockStoreException e) {
            throw new RuntimeException("Error initializing btc blockchain for tests");
        }
        int blocksToGenerate = Helper.randomInRange(minBtcBlocks, maxBtcBlocks);
        Helper.generateAndAddBlocks(btcBlockChain, blocksToGenerate);
        lockWhitelist = provider.getLockWhitelist();
        int size = Helper.randomInRange(minSize, maxSize);
        for (int i = 0; i < size; i++) {
            Address address = new BtcECKey().toAddress(networkParameters);
            Coin value = Helper.randomCoin(Coin.COIN, 1, 30);
            lockWhitelist.put(address, value);
        }
    };
}
Also used : BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) Repository(org.ethereum.core.Repository) RepositoryBlockStore(co.rsk.peg.RepositoryBlockStore) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 45 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class StateForBtcReleaseClientTest method getInitializer.

private BridgeStorageProviderInitializer getInitializer() {
    final int minNumTxs = 1;
    final int maxNumTxs = 100;
    final int minNumInputs = 1;
    final int maxNumInputs = 10;
    return (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
        Map<Keccak256, BtcTransaction> txsWaitingForSignatures;
        try {
            txsWaitingForSignatures = provider.getRskTxsWaitingForSignatures();
        } catch (IOException e) {
            throw new RuntimeException("Exception while trying to gather txs waiting for signatures for storage initialization");
        }
        int numTxs = Helper.randomInRange(minNumTxs, maxNumTxs);
        for (int i = 0; i < numTxs; i++) {
            BtcTransaction releaseTx = new BtcTransaction(networkParameters);
            Federation federation = bridgeConstants.getGenesisFederation();
            // Receiver and amounts
            Address toAddress = new BtcECKey().toAddress(networkParameters);
            Coin releaseAmount = Coin.CENT.multiply(Helper.randomInRange(10, 100));
            releaseTx.addOutput(releaseAmount, toAddress);
            // Input generation
            int numInputs = Helper.randomInRange(minNumInputs, maxNumInputs);
            for (int j = 0; j < numInputs; j++) {
                Coin inputAmount = releaseAmount.divide(numInputs);
                BtcTransaction inputTx = new BtcTransaction(networkParameters);
                inputTx.addOutput(inputAmount, federation.getAddress());
                releaseTx.addInput(inputTx.getOutput(0)).setScriptSig(PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(federation));
            }
            Keccak256 rskTxHash = new Keccak256(HashUtil.keccak256(BigInteger.valueOf(new Random().nextLong()).toByteArray()));
            txsWaitingForSignatures.put(rskTxHash, releaseTx);
        }
    };
}
Also used : BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) IOException(java.io.IOException) Keccak256(co.rsk.crypto.Keccak256) Repository(org.ethereum.core.Repository) Federation(co.rsk.peg.Federation)

Aggregations

Repository (org.ethereum.core.Repository)136 Test (org.junit.Test)109 RskAddress (co.rsk.core.RskAddress)59 DataWord (org.ethereum.vm.DataWord)43 BigInteger (java.math.BigInteger)31 RepositoryImpl (co.rsk.db.RepositoryImpl)25 Coin (co.rsk.core.Coin)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 HashMapDB (org.ethereum.datasource.HashMapDB)12 BridgeStorageProvider (co.rsk.peg.BridgeStorageProvider)11 Transaction (org.ethereum.core.Transaction)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)10 ArrayList (java.util.ArrayList)10 Program (org.ethereum.vm.program.Program)10 Block (org.ethereum.core.Block)9 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)9 RskSystemProperties (co.rsk.config.RskSystemProperties)8 IOException (java.io.IOException)8 List (java.util.List)8