Search in sources :

Example 1 with ExecutionStats

use of co.rsk.peg.performance.ExecutionStats in project rskj by rsksmart.

the class DeriveExtendedPublicKeyPerformanceTestCase method estimateDeriveExtendedPublicKey.

private ExecutionStats estimateDeriveExtendedPublicKey(int times, int pathLength, EnvironmentBuilder environmentBuilder) throws VMException {
    String name = String.format("%s-%d", function.name, pathLength);
    ExecutionStats stats = new ExecutionStats(name);
    Random rnd = new Random();
    byte[] chainCode = new byte[32];
    NetworkParameters networkParameters = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
    ABIEncoder abiEncoder = (int executionIndex) -> {
        rnd.nextBytes(chainCode);
        DeterministicKey key = HDKeyDerivation.createMasterPubKeyFromBytes(new ECKey().getPubKey(true), chainCode);
        int[] pathParts = new int[pathLength];
        for (int i = 0; i < pathLength; i++) {
            pathParts[i] = rnd.nextInt(MAX_CHILD);
        }
        String path = String.join("/", Arrays.stream(pathParts).mapToObj(i -> String.format("%d", i)).collect(Collectors.toList()));
        return function.encode(new Object[] { key.serializePubB58(networkParameters), path });
    };
    executeAndAverage(name, times, environmentBuilder, abiEncoder, Helper.getZeroValueTxBuilder(new ECKey()), Helper.getRandomHeightProvider(10), stats, (EnvironmentBuilder.Environment environment, byte[] result) -> {
        Object[] decodedResult = function.decodeResult(result);
        Assert.assertEquals(String.class, decodedResult[0].getClass());
        String address = (String) decodedResult[0];
        Assert.assertTrue(address.startsWith("xpub"));
    });
    return stats;
}
Also used : PrecompiledContractPerformanceTestCase(co.rsk.peg.performance.PrecompiledContractPerformanceTestCase) HDKeyDerivation(co.rsk.bitcoinj.crypto.HDKeyDerivation) ExecutionStats(co.rsk.peg.performance.ExecutionStats) VMException(org.ethereum.vm.exception.VMException) Arrays(java.util.Arrays) CallTransaction(org.ethereum.core.CallTransaction) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) Test(org.junit.Test) Random(java.util.Random) Collectors(java.util.stream.Collectors) CombinedExecutionStats(co.rsk.peg.performance.CombinedExecutionStats) DeterministicKey(co.rsk.bitcoinj.crypto.DeterministicKey) TestSystemProperties(co.rsk.config.TestSystemProperties) Ignore(org.junit.Ignore) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) Assert(org.junit.Assert) ECKey(org.ethereum.crypto.ECKey) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) ECKey(org.ethereum.crypto.ECKey) Random(java.util.Random) DeterministicKey(co.rsk.bitcoinj.crypto.DeterministicKey) ExecutionStats(co.rsk.peg.performance.ExecutionStats) CombinedExecutionStats(co.rsk.peg.performance.CombinedExecutionStats)

Example 2 with ExecutionStats

use of co.rsk.peg.performance.ExecutionStats in project rskj by rsksmart.

the class ToBase58CheckPerformanceTestCase method estimateToBase58Check.

private ExecutionStats estimateToBase58Check(int times, EnvironmentBuilder environmentBuilder) throws VMException {
    String name = function.name;
    ExecutionStats stats = new ExecutionStats(name);
    Random rnd = new Random();
    int[] versions = new int[] { // See https://en.bitcoin.it/wiki/Base58Check_encoding for details
    0, 5, 111, 196 };
    byte[] hash = new byte[20];
    ABIEncoder abiEncoder = (int executionIndex) -> {
        rnd.nextBytes(hash);
        int version = versions[rnd.nextInt(versions.length)];
        return function.encode(new Object[] { hash, version });
    };
    executeAndAverage(name, times, environmentBuilder, abiEncoder, Helper.getZeroValueTxBuilder(new ECKey()), Helper.getRandomHeightProvider(10), stats, (EnvironmentBuilder.Environment environment, byte[] result) -> {
        Object[] decodedResult = function.decodeResult(result);
        Assert.assertEquals(String.class, decodedResult[0].getClass());
        String address = (String) decodedResult[0];
        Assert.assertTrue(MIN_ADDRESS_LENGTH <= address.length());
        Assert.assertTrue(MAX_ADDRESS_LENGTH >= address.length());
    });
    return stats;
}
Also used : Random(java.util.Random) ECKey(org.ethereum.crypto.ECKey) ExecutionStats(co.rsk.peg.performance.ExecutionStats)

Example 3 with ExecutionStats

use of co.rsk.peg.performance.ExecutionStats in project rskj by rsksmart.

the class ExtractPublicKeyFromExtendedPublicKeyPerformanceTestCase method estimateExtractPublicKeyFromExtendedPublicKey.

private ExecutionStats estimateExtractPublicKeyFromExtendedPublicKey(int times, EnvironmentBuilder environmentBuilder) throws VMException {
    ExecutionStats stats = new ExecutionStats(function.name);
    Random rnd = new Random();
    byte[] chainCode = new byte[32];
    NetworkParameters networkParameters = NetworkParameters.fromID(NetworkParameters.ID_MAINNET);
    byte[] publicKey = new ECKey().getPubKey(true);
    String expectedHexPublicKey = ByteUtil.toHexString(publicKey);
    ABIEncoder abiEncoder = (int executionIndex) -> {
        rnd.nextBytes(chainCode);
        DeterministicKey key = HDKeyDerivation.createMasterPubKeyFromBytes(publicKey, chainCode);
        return function.encode(new Object[] { key.serializePubB58(networkParameters) });
    };
    executeAndAverage(function.name, times, environmentBuilder, abiEncoder, Helper.getZeroValueTxBuilder(new ECKey()), Helper.getRandomHeightProvider(10), stats, (EnvironmentBuilder.Environment environment, byte[] result) -> {
        Object[] decodedResult = function.decodeResult(result);
        Assert.assertEquals(byte[].class, decodedResult[0].getClass());
        String hexPublicKey = ByteUtil.toHexString((byte[]) decodedResult[0]);
        Assert.assertEquals(expectedHexPublicKey, hexPublicKey);
    });
    return stats;
}
Also used : Random(java.util.Random) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) ECKey(org.ethereum.crypto.ECKey) DeterministicKey(co.rsk.bitcoinj.crypto.DeterministicKey) ExecutionStats(co.rsk.peg.performance.ExecutionStats)

Example 4 with ExecutionStats

use of co.rsk.peg.performance.ExecutionStats in project rskj by rsksmart.

the class GetMultisigScriptHashPerformanceTestCase method estimateGetMultisigScriptHash.

private ExecutionStats estimateGetMultisigScriptHash(int times, int numberOfKeys, EnvironmentBuilder environmentBuilder) throws VMException {
    String name = String.format("%s-%d", function.name, numberOfKeys);
    ExecutionStats stats = new ExecutionStats(name);
    Random rnd = new Random();
    int minimumSignatures = rnd.nextInt(numberOfKeys) + 1;
    byte[][] publicKeys = new byte[numberOfKeys][];
    for (int i = 0; i < numberOfKeys; i++) {
        publicKeys[i] = new ECKey().getPubKey(true);
    }
    String expectedHashHex = ByteUtil.toHexString(ScriptBuilder.createP2SHOutputScript(minimumSignatures, Arrays.stream(publicKeys).map(BtcECKey::fromPublicOnly).collect(Collectors.toList())).getPubKeyHash());
    ABIEncoder abiEncoder = (int executionIndex) -> function.encode(new Object[] { minimumSignatures, publicKeys });
    executeAndAverage(name, times, environmentBuilder, abiEncoder, Helper.getZeroValueTxBuilder(new ECKey()), Helper.getRandomHeightProvider(10), stats, (EnvironmentBuilder.Environment environment, byte[] result) -> {
        Object[] decodedResult = function.decodeResult(result);
        Assert.assertEquals(byte[].class, decodedResult[0].getClass());
        String hexHash = ByteUtil.toHexString((byte[]) decodedResult[0]);
        Assert.assertEquals(expectedHashHex, hexHash);
    });
    return stats;
}
Also used : Random(java.util.Random) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) ECKey(org.ethereum.crypto.ECKey) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) ExecutionStats(co.rsk.peg.performance.ExecutionStats) CombinedExecutionStats(co.rsk.peg.performance.CombinedExecutionStats)

Example 5 with ExecutionStats

use of co.rsk.peg.performance.ExecutionStats in project rskj by rsksmart.

the class GetCoinbasePerformanceTestCase method getCoinbase.

@Test
public void getCoinbase() throws IOException, VMException {
    ExecutionStats stats = new ExecutionStats("getCoinbase");
    EnvironmentBuilder environmentBuilder = (int executionIndex, TxBuilder txBuilder, int height) -> {
        World world = buildWorld(6000, 500, 6);
        BlockHeaderContract contract = new BlockHeaderContract(activationConfig, new RskAddress("0000000000000000000000000000000001000010"));
        contract.init(txBuilder.build(executionIndex), world.getBlockChain().getBestBlock(), world.getRepository(), world.getBlockStore(), null, new LinkedList<>());
        return EnvironmentBuilder.Environment.withContract(contract);
    };
    doGetCoinbase(environmentBuilder, stats, 4000);
    BlockHeaderPerformanceTest.addStats(stats);
}
Also used : RskAddress(co.rsk.core.RskAddress) World(co.rsk.test.World) LinkedList(java.util.LinkedList) ExecutionStats(co.rsk.peg.performance.ExecutionStats) Test(org.junit.Test)

Aggregations

ExecutionStats (co.rsk.peg.performance.ExecutionStats)5 Random (java.util.Random)4 ECKey (org.ethereum.crypto.ECKey)4 NetworkParameters (co.rsk.bitcoinj.core.NetworkParameters)2 DeterministicKey (co.rsk.bitcoinj.crypto.DeterministicKey)2 CombinedExecutionStats (co.rsk.peg.performance.CombinedExecutionStats)2 Test (org.junit.Test)2 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)1 HDKeyDerivation (co.rsk.bitcoinj.crypto.HDKeyDerivation)1 TestSystemProperties (co.rsk.config.TestSystemProperties)1 RskAddress (co.rsk.core.RskAddress)1 PrecompiledContractPerformanceTestCase (co.rsk.peg.performance.PrecompiledContractPerformanceTestCase)1 World (co.rsk.test.World)1 Arrays (java.util.Arrays)1 LinkedList (java.util.LinkedList)1 Collectors (java.util.stream.Collectors)1 CallTransaction (org.ethereum.core.CallTransaction)1 PrecompiledContracts (org.ethereum.vm.PrecompiledContracts)1 VMException (org.ethereum.vm.exception.VMException)1 Assert (org.junit.Assert)1