Search in sources :

Example 16 with VMException

use of org.ethereum.vm.exception.VMException in project rskj by rsksmart.

the class ProgramTest method testCallToPrecompiledAddress_throwPrecompiledConstractException.

@Test
public void testCallToPrecompiledAddress_throwPrecompiledConstractException() throws VMException {
    when(precompiledContract.execute(any())).thenThrow(new VMException("Revert exception"));
    program.callToPrecompiledAddress(msg, precompiledContract);
    assertStack(STACK_STATE_ERROR);
    assertEquals(gasCost, program.getResult().getGasUsed());
}
Also used : VMException(org.ethereum.vm.exception.VMException) Test(org.junit.Test) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)

Example 17 with VMException

use of org.ethereum.vm.exception.VMException in project rskj by rsksmart.

the class RemascContract method execute.

@Override
public byte[] execute(byte[] data) throws VMException {
    try {
        CallTransaction.Function function = this.getFunction(data);
        Method m = this.getClass().getMethod(function.name);
        Object result = this.invoke(m);
        remasc.save();
        return this.encodeResult(function, result);
    } catch (RemascInvalidInvocationException ex) {
        // Remasc contract was invoked with invalid parameters / out of place, throw VMException to avoid funds being transferred
        logger.warn(ex.getMessage(), ex);
        throw new VMException(ex.getMessage());
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
        panicProcessor.panic("remascExecute", ex.getMessage());
        throw new VMException("Exception executing remasc", ex);
    }
}
Also used : VMException(org.ethereum.vm.exception.VMException) CallTransaction(org.ethereum.core.CallTransaction) Method(java.lang.reflect.Method) VMException(org.ethereum.vm.exception.VMException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 18 with VMException

use of org.ethereum.vm.exception.VMException in project rskj by rsksmart.

the class BridgeTestPowerMock method executeWithInexistentFunctionAfterRskip88.

@Test
public void executeWithInexistentFunctionAfterRskip88() {
    doReturn(false).when(activationConfig).isActive(eq(RSKIP87), anyLong());
    doReturn(true).when(activationConfig).isActive(eq(RSKIP88), anyLong());
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams()), bridgeConstants, activationConfig);
    Bridge bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory);
    Transaction mockedTx = mock(Transaction.class);
    try {
        bridge.init(mockedTx, getGenesisBlock(), createRepository().startTracking(), null, null, null);
        bridge.execute(new byte[4]);
        Assert.fail();
    } catch (VMException e) {
        Assert.assertTrue(e.getMessage().contains("Invalid data given"));
    }
}
Also used : SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) VMException(org.ethereum.vm.exception.VMException) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with VMException

use of org.ethereum.vm.exception.VMException in project rskj by rsksmart.

the class BridgeTestPowerMock method getBtcTransactionConfirmationsAfterWasabi_merkleBranchConstructionError.

@Test
public void getBtcTransactionConfirmationsAfterWasabi_merkleBranchConstructionError() throws Exception {
    Transaction txMock = mock(Transaction.class);
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams()), bridgeConstants, activationConfig);
    Bridge bridge = PowerMockito.spy(new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory));
    bridge.init(txMock, getGenesisBlock(), createRepository().startTracking(), null, null, null);
    BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
    Whitebox.setInternalState(bridge, "bridgeSupport", bridgeSupportMock);
    byte[] btcTxHash = Sha256Hash.of(Hex.decode("aabbcc")).getBytes();
    byte[] btcBlockHash = Sha256Hash.of(Hex.decode("ddeeff")).getBytes();
    byte[][] merkleBranchHashes = new byte[][] { Sha256Hash.of(Hex.decode("11")).getBytes(), Sha256Hash.of(Hex.decode("22")).getBytes(), Sha256Hash.of(Hex.decode("33")).getBytes() };
    BigInteger merkleBranchBits = BigInteger.valueOf(123);
    MerkleBranch merkleBranch = mock(MerkleBranch.class);
    PowerMockito.whenNew(MerkleBranch.class).withArguments(any(List.class), any(Integer.class)).then((Answer<MerkleBranch>) invocation -> {
        List<Sha256Hash> hashes = invocation.getArgument(0);
        Assert.assertArrayEquals(merkleBranchHashes[0], hashes.get(0).getBytes());
        Assert.assertArrayEquals(merkleBranchHashes[1], hashes.get(1).getBytes());
        Assert.assertArrayEquals(merkleBranchHashes[2], hashes.get(2).getBytes());
        Integer bits = invocation.getArgument(1);
        Assert.assertEquals(123, bits.intValue());
        throw new IllegalArgumentException("blabla");
    });
    try {
        bridge.getBtcTransactionConfirmations(new Object[] { btcTxHash, btcBlockHash, merkleBranchBits, merkleBranchHashes });
        Assert.fail();
    } catch (VMException e) {
        Assert.assertTrue(e.getMessage().contains("in getBtcTransactionConfirmations"));
        Assert.assertEquals(IllegalArgumentException.class, e.getCause().getClass());
        verify(bridgeSupportMock, never()).getBtcTransactionConfirmations(any(), any(), any());
    }
}
Also used : OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) CoreMatchers.is(org.hamcrest.CoreMatchers.is) VMException(org.ethereum.vm.exception.VMException) HashMapDB(org.ethereum.datasource.HashMapDB) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) RskAddress(co.rsk.core.RskAddress) Utils.uint32ToByteStreamLE(co.rsk.bitcoinj.core.Utils.uint32ToByteStreamLE) Keccak256(co.rsk.crypto.Keccak256) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) MutableTrieCache(co.rsk.db.MutableTrieCache) RegTestParams(co.rsk.bitcoinj.params.RegTestParams) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) ProgramInvoke(org.ethereum.vm.program.invoke.ProgramInvoke) EVMAssembler(co.rsk.asm.EVMAssembler) MutableTrieImpl(co.rsk.db.MutableTrieImpl) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Instant(java.time.Instant) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) MutableRepository(org.ethereum.db.MutableRepository) StandardCharsets(java.nio.charset.StandardCharsets) ScriptBuilder(co.rsk.bitcoinj.script.ScriptBuilder) TestSystemProperties(co.rsk.config.TestSystemProperties) Modifier(java.lang.reflect.Modifier) RskSystemProperties(co.rsk.config.RskSystemProperties) org.ethereum.core(org.ethereum.core) BlockDifficulty(co.rsk.core.BlockDifficulty) Whitebox(org.powermock.reflect.Whitebox) java.util(java.util) BeforeClass(org.junit.BeforeClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunWith(org.junit.runner.RunWith) Hex(org.bouncycastle.util.encoders.Hex) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) BridgeConstants(co.rsk.config.BridgeConstants) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) TrieStore(co.rsk.trie.TrieStore) Before(org.junit.Before) OutputStream(java.io.OutputStream) TypeConverter(org.ethereum.rpc.TypeConverter) Logger(org.slf4j.Logger) Files(java.nio.file.Files) HashUtil(org.ethereum.crypto.HashUtil) Test(org.junit.Test) IOException(java.io.IOException) Field(java.lang.reflect.Field) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) World(co.rsk.test.World) Program(org.ethereum.vm.program.Program) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) Mockito(org.mockito.Mockito) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) ByteUtil(org.ethereum.util.ByteUtil) Paths(java.nio.file.Paths) TrieStoreImpl(co.rsk.trie.TrieStoreImpl) VM(org.ethereum.vm.VM) Assert(org.junit.Assert) Trie(co.rsk.trie.Trie) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) BigInteger(java.math.BigInteger) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) VMException(org.ethereum.vm.exception.VMException) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with VMException

use of org.ethereum.vm.exception.VMException in project rskj by rsksmart.

the class BridgeTestPowerMock method getBtcTransactionConfirmationsAfterWasabi_errorInBridgeSupport.

@Test
public void getBtcTransactionConfirmationsAfterWasabi_errorInBridgeSupport() throws Exception {
    Transaction txMock = mock(Transaction.class);
    BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(bridgeConstants.getBtcParams()), bridgeConstants, activationConfig);
    Bridge bridge = PowerMockito.spy(new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory));
    bridge.init(txMock, getGenesisBlock(), createRepository().startTracking(), null, null, null);
    BridgeSupport bridgeSupportMock = mock(BridgeSupport.class);
    Whitebox.setInternalState(bridge, "bridgeSupport", bridgeSupportMock);
    byte[] btcTxHash = Sha256Hash.of(Hex.decode("aabbcc")).getBytes();
    byte[] btcBlockHash = Sha256Hash.of(Hex.decode("ddeeff")).getBytes();
    byte[][] merkleBranchHashes = new byte[][] { Sha256Hash.of(Hex.decode("11")).getBytes(), Sha256Hash.of(Hex.decode("22")).getBytes(), Sha256Hash.of(Hex.decode("33")).getBytes() };
    BigInteger merkleBranchBits = BigInteger.valueOf(123);
    MerkleBranch merkleBranch = mock(MerkleBranch.class);
    PowerMockito.whenNew(MerkleBranch.class).withArguments(any(List.class), any(Integer.class)).then((Answer<MerkleBranch>) invocation -> {
        List<Sha256Hash> hashes = invocation.getArgument(0);
        Assert.assertArrayEquals(merkleBranchHashes[0], hashes.get(0).getBytes());
        Assert.assertArrayEquals(merkleBranchHashes[1], hashes.get(1).getBytes());
        Assert.assertArrayEquals(merkleBranchHashes[2], hashes.get(2).getBytes());
        Integer bits = invocation.getArgument(1);
        Assert.assertEquals(123, bits.intValue());
        return merkleBranch;
    });
    when(bridgeSupportMock.getBtcTransactionConfirmations(any(Sha256Hash.class), any(Sha256Hash.class), any(MerkleBranch.class))).then((Answer<Integer>) invocation -> {
        Sha256Hash txHash = invocation.getArgument(0);
        Assert.assertArrayEquals(btcTxHash, txHash.getBytes());
        Sha256Hash blockHash = invocation.getArgument(1);
        Assert.assertArrayEquals(btcBlockHash, blockHash.getBytes());
        MerkleBranch merkleBranchArg = invocation.getArgument(2);
        Assert.assertEquals(merkleBranch, merkleBranchArg);
        throw new VMException("bla bla bla");
    });
    try {
        bridge.getBtcTransactionConfirmations(new Object[] { btcTxHash, btcBlockHash, merkleBranchBits, merkleBranchHashes });
        Assert.fail();
    } catch (VMException e) {
        Assert.assertTrue(e.getMessage().contains("in getBtcTransactionConfirmations"));
        Assert.assertEquals(VMException.class, e.getCause().getClass());
        Assert.assertTrue(e.getCause().getMessage().contains("bla bla bla"));
    }
}
Also used : OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) CoreMatchers.is(org.hamcrest.CoreMatchers.is) VMException(org.ethereum.vm.exception.VMException) HashMapDB(org.ethereum.datasource.HashMapDB) TestGenesisLoader(co.rsk.core.genesis.TestGenesisLoader) RskAddress(co.rsk.core.RskAddress) Utils.uint32ToByteStreamLE(co.rsk.bitcoinj.core.Utils.uint32ToByteStreamLE) Keccak256(co.rsk.crypto.Keccak256) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) MutableTrieCache(co.rsk.db.MutableTrieCache) RegTestParams(co.rsk.bitcoinj.params.RegTestParams) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) ProgramInvoke(org.ethereum.vm.program.invoke.ProgramInvoke) EVMAssembler(co.rsk.asm.EVMAssembler) MutableTrieImpl(co.rsk.db.MutableTrieImpl) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Instant(java.time.Instant) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) MutableRepository(org.ethereum.db.MutableRepository) StandardCharsets(java.nio.charset.StandardCharsets) ScriptBuilder(co.rsk.bitcoinj.script.ScriptBuilder) TestSystemProperties(co.rsk.config.TestSystemProperties) Modifier(java.lang.reflect.Modifier) RskSystemProperties(co.rsk.config.RskSystemProperties) org.ethereum.core(org.ethereum.core) BlockDifficulty(co.rsk.core.BlockDifficulty) Whitebox(org.powermock.reflect.Whitebox) java.util(java.util) BeforeClass(org.junit.BeforeClass) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RunWith(org.junit.runner.RunWith) Hex(org.bouncycastle.util.encoders.Hex) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) BridgeConstants(co.rsk.config.BridgeConstants) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) TrieStore(co.rsk.trie.TrieStore) Before(org.junit.Before) OutputStream(java.io.OutputStream) TypeConverter(org.ethereum.rpc.TypeConverter) Logger(org.slf4j.Logger) Files(java.nio.file.Files) HashUtil(org.ethereum.crypto.HashUtil) Test(org.junit.Test) IOException(java.io.IOException) Field(java.lang.reflect.Field) ProgramInvokeMockImpl(org.ethereum.vm.program.invoke.ProgramInvokeMockImpl) World(co.rsk.test.World) Program(org.ethereum.vm.program.Program) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) Mockito(org.mockito.Mockito) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) ByteUtil(org.ethereum.util.ByteUtil) Paths(java.nio.file.Paths) TrieStoreImpl(co.rsk.trie.TrieStoreImpl) VM(org.ethereum.vm.VM) Assert(org.junit.Assert) Trie(co.rsk.trie.Trie) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) BigInteger(java.math.BigInteger) SimpleBtcTransaction(co.rsk.peg.bitcoin.SimpleBtcTransaction) VMException(org.ethereum.vm.exception.VMException) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

VMException (org.ethereum.vm.exception.VMException)29 IOException (java.io.IOException)16 BlockStoreException (co.rsk.bitcoinj.store.BlockStoreException)14 Test (org.junit.Test)11 BigInteger (java.math.BigInteger)9 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)9 SimpleBtcTransaction (co.rsk.peg.bitcoin.SimpleBtcTransaction)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 RskAddress (co.rsk.core.RskAddress)6 PrecompiledContracts (org.ethereum.vm.PrecompiledContracts)5 Keccak256 (co.rsk.crypto.Keccak256)4 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)3 BtcBlockStore (co.rsk.bitcoinj.store.BtcBlockStore)3 BridgeConstants (co.rsk.config.BridgeConstants)3 BridgeRegTestConstants (co.rsk.config.BridgeRegTestConstants)3 TestSystemProperties (co.rsk.config.TestSystemProperties)3 MerkleBranch (co.rsk.peg.bitcoin.MerkleBranch)3 OneOffWhiteListEntry (co.rsk.peg.whitelist.OneOffWhiteListEntry)3 StandardCharsets (java.nio.charset.StandardCharsets)3 Instant (java.time.Instant)3