Search in sources :

Example 1 with BridgeStorageProvider

use of co.rsk.peg.BridgeStorageProvider 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 2 with BridgeStorageProvider

use of co.rsk.peg.BridgeStorageProvider 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 3 with BridgeStorageProvider

use of co.rsk.peg.BridgeStorageProvider 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)

Example 4 with BridgeStorageProvider

use of co.rsk.peg.BridgeStorageProvider in project rskj by rsksmart.

the class UpdateCollectionsTest method updateCollections_buildReleaseTxs.

private void updateCollections_buildReleaseTxs(ExecutionStats stats, int numCases) throws IOException {
    final int minUTXOs = 1;
    final int maxUTXOs = 1000;
    final int minMilliBtc = 1;
    final int maxMilliBtc = 1000;
    final int minReleaseRequests = 1;
    final int maxReleaseRequests = 100;
    final int minMilliReleaseBtc = 10;
    final int maxMilliReleaseBtc = 2000;
    final NetworkParameters parameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
    BridgeStorageProviderInitializer storageInitializer = (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
        Random rnd = new Random();
        List<UTXO> utxos;
        ReleaseRequestQueue queue;
        try {
            utxos = provider.getNewFederationBtcUTXOs();
        } catch (Exception e) {
            throw new RuntimeException("Unable to gather active federation btc utxos");
        }
        try {
            queue = provider.getReleaseRequestQueue();
        } catch (Exception e) {
            throw new RuntimeException("Unable to gather release request queue");
        }
        // Generate some utxos
        int numUTXOs = Helper.randomInRange(minUTXOs, maxUTXOs);
        Script federationScript = BridgeRegTestConstants.getInstance().getGenesisFederation().getP2SHScript();
        for (int i = 0; i < numUTXOs; i++) {
            Sha256Hash hash = Sha256Hash.wrap(HashUtil.sha256(BigInteger.valueOf(rnd.nextLong()).toByteArray()));
            Coin value = Coin.MILLICOIN.multiply(Helper.randomInRange(minMilliBtc, maxMilliBtc));
            utxos.add(new UTXO(hash, 0, value, 1, false, federationScript));
        }
        // Generate some release requests to process
        for (int i = 0; i < Helper.randomInRange(minReleaseRequests, maxReleaseRequests); i++) {
            Coin value = Coin.MILLICOIN.multiply(Helper.randomInRange(minMilliReleaseBtc, maxMilliReleaseBtc));
            queue.add(new BtcECKey().toAddress(parameters), value);
        }
    };
    final byte[] updateCollectionsEncoded = Bridge.UPDATE_COLLECTIONS.encode();
    ABIEncoder abiEncoder = (int executionIndex) -> updateCollectionsEncoded;
    executeAndAverage("updateCollections-releaseRequests", numCases, abiEncoder, storageInitializer, Helper.getZeroValueRandomSenderTxBuilder(), Helper.getRandomHeightProvider(10), stats);
}
Also used : Script(co.rsk.bitcoinj.script.Script) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) ReleaseRequestQueue(co.rsk.peg.ReleaseRequestQueue) IOException(java.io.IOException) Repository(org.ethereum.core.Repository) Random(java.util.Random) List(java.util.List)

Example 5 with BridgeStorageProvider

use of co.rsk.peg.BridgeStorageProvider in project rskj by rsksmart.

the class UpdateCollectionsTest method updateCollections_confirmTxs.

private void updateCollections_confirmTxs(ExecutionStats stats, int numCases) throws IOException {
    final int minTxsWaitingForSigs = 0;
    final int maxTxsWaitingForSigs = 10;
    final int minReleaseTxs = 1;
    final int maxReleaseTxs = 100;
    final int minBlockNumber = 10;
    final int maxBlockNumber = 100;
    final int minHeight = 50;
    final int maxHeight = 150;
    final int minCentOutput = 1;
    final int maxCentOutput = 100;
    final NetworkParameters parameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
    BridgeStorageProviderInitializer storageInitializer = (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
        Random rnd = new Random();
        SortedMap<Keccak256, BtcTransaction> txsWaitingForSignatures;
        ReleaseTransactionSet txSet;
        try {
            txsWaitingForSignatures = provider.getRskTxsWaitingForSignatures();
        } catch (Exception e) {
            throw new RuntimeException("Unable to gather txs waiting for signatures");
        }
        try {
            txSet = provider.getReleaseTransactionSet();
        } catch (Exception e) {
            throw new RuntimeException("Unable to gather release tx set");
        }
        // Generate some txs waiting for signatures
        Script genesisFederationScript = bridgeConstants.getGenesisFederation().getP2SHScript();
        for (int i = 0; i < Helper.randomInRange(minTxsWaitingForSigs, maxTxsWaitingForSigs); i++) {
            Keccak256 rskHash = new Keccak256(HashUtil.keccak256(BigInteger.valueOf(rnd.nextLong()).toByteArray()));
            BtcTransaction btcTx = new BtcTransaction(networkParameters);
            Sha256Hash inputHash = Sha256Hash.wrap(HashUtil.sha256(BigInteger.valueOf(rnd.nextLong()).toByteArray()));
            btcTx.addInput(inputHash, 0, genesisFederationScript);
            btcTx.addOutput(Helper.randomCoin(Coin.CENT, minCentOutput, maxCentOutput), new BtcECKey());
            txsWaitingForSignatures.put(rskHash, btcTx);
        }
        // Generate some txs waiting for confirmations
        for (int i = 0; i < Helper.randomInRange(minReleaseTxs, maxReleaseTxs); i++) {
            BtcTransaction btcTx = new BtcTransaction(networkParameters);
            Sha256Hash inputHash = Sha256Hash.wrap(HashUtil.sha256(BigInteger.valueOf(rnd.nextLong()).toByteArray()));
            btcTx.addInput(inputHash, 0, genesisFederationScript);
            btcTx.addOutput(Helper.randomCoin(Coin.CENT, minCentOutput, maxCentOutput), new BtcECKey());
            long blockNumber = Helper.randomInRange(minBlockNumber, maxBlockNumber);
            txSet.add(btcTx, blockNumber);
        }
    };
    final byte[] updateCollectionsEncoded = Bridge.UPDATE_COLLECTIONS.encode();
    ABIEncoder abiEncoder = (int executionIndex) -> updateCollectionsEncoded;
    HeightProvider heightProvider = (int executionIndex) -> Helper.randomInRange(minHeight, maxHeight);
    executeAndAverage("updateCollections-releaseTxs", numCases, abiEncoder, storageInitializer, Helper.getZeroValueRandomSenderTxBuilder(), heightProvider, stats);
}
Also used : Script(co.rsk.bitcoinj.script.Script) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) Keccak256(co.rsk.crypto.Keccak256) IOException(java.io.IOException) Repository(org.ethereum.core.Repository) Random(java.util.Random) ReleaseTransactionSet(co.rsk.peg.ReleaseTransactionSet) SortedMap(java.util.SortedMap)

Aggregations

BridgeStorageProvider (co.rsk.peg.BridgeStorageProvider)11 Repository (org.ethereum.core.Repository)11 IOException (java.io.IOException)5 Random (java.util.Random)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)3 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)3 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)3 RskSystemProperties (co.rsk.config.RskSystemProperties)3 Federation (co.rsk.peg.Federation)3 RepositoryBlockStore (co.rsk.peg.RepositoryBlockStore)3 BtcBlockChain (co.rsk.bitcoinj.core.BtcBlockChain)2 Context (co.rsk.bitcoinj.core.Context)2 Script (co.rsk.bitcoinj.script.Script)2 Keccak256 (co.rsk.crypto.Keccak256)2 ReleaseRequestQueue (co.rsk.peg.ReleaseRequestQueue)2 Test (org.junit.Test)2 BtcBlock (co.rsk.bitcoinj.core.BtcBlock)1 Coin (co.rsk.bitcoinj.core.Coin)1