Search in sources :

Example 6 with BridgeStorageProvider

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

the class ActiveFederationTest method buildInitializer.

private BridgeStorageProviderInitializer buildInitializer(boolean genesis) {
    final int minFederators = 10;
    final int maxFederators = 16;
    return (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
        if (!genesis) {
            int numFederators = Helper.randomInRange(minFederators, maxFederators);
            List<BtcECKey> federatorKeys = new ArrayList<>();
            for (int i = 0; i < numFederators; i++) {
                federatorKeys.add(new BtcECKey());
            }
            federation = new Federation(federatorKeys, Instant.ofEpochMilli(new Random().nextLong()), Helper.randomInRange(1, 10), networkParameters);
            provider.setNewFederation(federation);
        } else {
            federation = bridgeConstants.getGenesisFederation();
        }
    };
}
Also used : Repository(org.ethereum.core.Repository) Random(java.util.Random) Federation(co.rsk.peg.Federation) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) ArrayList(java.util.ArrayList) List(java.util.List) BtcECKey(co.rsk.bitcoinj.core.BtcECKey)

Example 7 with BridgeStorageProvider

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

the class BridgePerformanceTestCase method execute.

private ExecutionTracker execute(ABIEncoder abiEncoder, BridgeStorageProviderInitializer storageInitializer, TxBuilder txBuilder, HeightProvider heightProvider, int executionIndex) {
    ExecutionTracker executionInfo = new ExecutionTracker(thread);
    RepositoryImpl repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants);
    storageInitializer.initialize(storageProvider, track, executionIndex);
    try {
        storageProvider.save();
    } catch (Exception e) {
        throw new RuntimeException("Error trying to save the storage after initialization", e);
    }
    track.commit();
    Transaction tx = txBuilder.build(executionIndex);
    List<LogInfo> logs = new ArrayList<>();
    RepositoryTrackWithBenchmarking benchmarkerTrack = new RepositoryTrackWithBenchmarking(config, repository);
    Bridge bridge = new Bridge(config, PrecompiledContracts.BRIDGE_ADDR);
    Blockchain blockchain = BlockChainBuilder.ofSizeWithNoTransactionPoolCleaner(heightProvider.getHeight(executionIndex));
    bridge.init(tx, blockchain.getBestBlock(), benchmarkerTrack, blockchain.getBlockStore(), null, logs);
    // Execute a random method so that bridge support initialization
    // does its initial writes to the repo for e.g. genesis block,
    // federation, etc, etc. and we don't get
    // those recorded in the actual execution.
    bridge.execute(Bridge.GET_FEDERATION_SIZE.encode());
    benchmarkerTrack.getStatistics().clear();
    executionInfo.startTimer();
    bridge.execute(abiEncoder.encode(executionIndex));
    executionInfo.endTimer();
    benchmarkerTrack.commit();
    executionInfo.setRepositoryStatistics(benchmarkerTrack.getStatistics());
    return executionInfo;
}
Also used : LogInfo(org.ethereum.vm.LogInfo) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) Blockchain(org.ethereum.core.Blockchain) ArrayList(java.util.ArrayList) RepositoryTrackWithBenchmarking(co.rsk.db.RepositoryTrackWithBenchmarking) Repository(org.ethereum.core.Repository) Transaction(org.ethereum.core.Transaction) RepositoryImpl(co.rsk.db.RepositoryImpl) Bridge(co.rsk.peg.Bridge)

Example 8 with BridgeStorageProvider

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

the class BtcBlockchainTest method buildInitializer.

private BridgeStorageProviderInitializer buildInitializer() {
    final int minBtcBlocks = 1000;
    final int maxBtcBlocks = 2000;
    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);
    };
}
Also used : Context(co.rsk.bitcoinj.core.Context) Repository(org.ethereum.core.Repository) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) RepositoryBlockStore(co.rsk.peg.RepositoryBlockStore) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) BtcBlockChain(co.rsk.bitcoinj.core.BtcBlockChain) RskSystemProperties(co.rsk.config.RskSystemProperties)

Example 9 with BridgeStorageProvider

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

the class ReceiveHeadersTest method receiveHeaders.

@Test
public void receiveHeaders() throws IOException {
    final int minBtcBlocks = 1000;
    final int maxBtcBlocks = 2000;
    BridgeStorageProviderInitializer storageInitializer = (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);
        BtcBlock lastBlock = Helper.generateAndAddBlocks(btcBlockChain, blocksToGenerate);
        blockToTry = Helper.generateBtcBlock(lastBlock);
    };
    ABIEncoder abiEncoder = (int executionIndex) -> {
        List<BtcBlock> headersToSendToBridge = new ArrayList<>();
        // Send just one header (that's the only case we're interested in measuring atm
        headersToSendToBridge.add(blockToTry);
        Object[] headersEncoded = headersToSendToBridge.stream().map(h -> h.bitcoinSerialize()).toArray();
        return Bridge.RECEIVE_HEADERS.encode(new Object[] { headersEncoded });
    };
    ExecutionStats stats = new ExecutionStats("receiveHeaders");
    executeAndAverage("receiveHeaders", 200, abiEncoder, storageInitializer, Helper.getZeroValueRandomSenderTxBuilder(), Helper.getRandomHeightProvider(10), stats);
    BridgePerformanceTest.addStats(stats);
}
Also used : Context(co.rsk.bitcoinj.core.Context) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) BtcBlockChain(co.rsk.bitcoinj.core.BtcBlockChain) Repository(org.ethereum.core.Repository) RepositoryBlockStore(co.rsk.peg.RepositoryBlockStore) BtcBlock(co.rsk.bitcoinj.core.BtcBlock) ArrayList(java.util.ArrayList) List(java.util.List) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 10 with BridgeStorageProvider

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

the class ReleaseBtcTest method releaseBtc.

@Test
public void releaseBtc() throws IOException {
    int minCentsBtc = 5;
    int maxCentsBtc = 100;
    final NetworkParameters parameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
    BridgeStorageProviderInitializer storageInitializer = (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
        ReleaseRequestQueue queue;
        try {
            queue = provider.getReleaseRequestQueue();
        } catch (Exception e) {
            throw new RuntimeException("Unable to gather release request queue");
        }
        for (int i = 0; i < Helper.randomInRange(10, 100); i++) {
            Coin value = Coin.CENT.multiply(Helper.randomInRange(minCentsBtc, maxCentsBtc));
            queue.add(new BtcECKey().toAddress(parameters), value);
        }
    };
    final byte[] releaseBtcEncoded = Bridge.RELEASE_BTC.encode();
    ABIEncoder abiEncoder = (int executionIndex) -> releaseBtcEncoded;
    TxBuilder txBuilder = (int executionIndex) -> {
        long satoshis = Coin.CENT.multiply(Helper.randomInRange(minCentsBtc, maxCentsBtc)).getValue();
        BigInteger weis = Denomination.satoshisToWeis(BigInteger.valueOf(satoshis));
        ECKey sender = new ECKey();
        return Helper.buildSendValueTx(sender, weis);
    };
    ExecutionStats stats = new ExecutionStats("releaseBtc");
    executeAndAverage("releaseBtc", 1000, abiEncoder, storageInitializer, txBuilder, Helper.getRandomHeightProvider(10), stats);
    BridgePerformanceTest.addStats(stats);
}
Also used : NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) BridgeStorageProvider(co.rsk.peg.BridgeStorageProvider) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) ECKey(org.ethereum.crypto.ECKey) ReleaseRequestQueue(co.rsk.peg.ReleaseRequestQueue) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) IOException(java.io.IOException) Coin(co.rsk.bitcoinj.core.Coin) Repository(org.ethereum.core.Repository) BigInteger(java.math.BigInteger) Test(org.junit.Test)

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