Search in sources :

Example 91 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeSupportTest method assertRefundInProcessPegInVersionLegacy.

private void assertRefundInProcessPegInVersionLegacy(boolean isWhitelisted, boolean mockLockingCap, TxSenderAddressType lockSenderAddressType, @Nullable ConsensusRule consensusRule) throws IOException, RegisterBtcTransactionException {
    // Arrange
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    if (consensusRule != null) {
        when(activations.isActive(consensusRule)).thenReturn(true);
    }
    Repository repository = createRepository();
    BtcECKey srcKey1 = new BtcECKey();
    ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
    Address btcAddress = srcKey1.toAddress(btcParams);
    RskAddress rskAddress = new RskAddress(key.getAddress());
    BtcTransaction btcTx = new BtcTransaction(btcParams);
    btcTx.addOutput(Coin.COIN.multiply(10), bridgeConstants.getGenesisFederation().getAddress());
    btcTx.addInput(PegTestUtils.createHash(1), 0, new Script(new byte[] {}));
    BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
    LockWhitelist lockWhitelist = mock(LockWhitelist.class);
    when(lockWhitelist.isWhitelistedFor(eq(btcAddress), any(Coin.class), any(int.class))).thenReturn(isWhitelisted);
    when(provider.getLockWhitelist()).thenReturn(lockWhitelist);
    ReleaseTransactionSet releaseTransactionSet = new ReleaseTransactionSet(new HashSet<>());
    when(provider.getReleaseTransactionSet()).thenReturn(releaseTransactionSet);
    if (mockLockingCap) {
        when(provider.getLockingCap()).thenReturn(Coin.COIN.multiply(1));
    }
    BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(lockSenderAddressType, btcAddress, rskAddress);
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, new PeginInstructionsProvider(), mock(Block.class), mock(BtcBlockStoreWithCache.Factory.class), activations);
    // Act
    bridgeSupport.processPegIn(btcTx, mock(Transaction.class), 0, mock(Sha256Hash.class));
    // Assert
    Assert.assertEquals(1, releaseTransactionSet.getEntries().size());
    // Check rejection tx input was created from btc tx
    boolean successfulRejection = false;
    for (ReleaseTransactionSet.Entry e : releaseTransactionSet.getEntries()) {
        if (e.getTransaction().getInput(0).getOutpoint().getHash() == btcTx.getHash()) {
            successfulRejection = true;
            break;
        }
    }
    Assert.assertTrue(successfulRejection);
    // Check tx was not marked as processed
    Assert.assertFalse(provider.getHeightIfBtcTxhashIsAlreadyProcessed(btcTx.getHash()).isPresent());
}
Also used : Script(co.rsk.bitcoinj.script.Script) RskAddress(co.rsk.core.RskAddress) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ECKey(org.ethereum.crypto.ECKey) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) Block(org.ethereum.core.Block)

Example 92 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeStorageProviderTest method saveLastRetiredFederationP2SHScript_after_RSKIP186.

@Test
public void saveLastRetiredFederationP2SHScript_after_RSKIP186() {
    Repository repository = mock(Repository.class);
    BridgeStorageProvider provider0 = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getNetworkConstants().getBridgeConstants(), activationsAllForks);
    Script script = new Script(new byte[] {});
    provider0.setLastRetiredFederationP2SHScript(script);
    provider0.saveLastRetiredFederationP2SHScript();
    // Once the network upgrade is active, we will store it in the repository
    verify(repository, times(1)).addStorageBytes(PrecompiledContracts.BRIDGE_ADDR, DataWord.fromString("lastRetiredFedP2SHScript"), BridgeSerializationUtils.serializeScript(script));
}
Also used : Script(co.rsk.bitcoinj.script.Script) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 93 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeSerializationUtilsTest method deserializeScript.

@Test
public void deserializeScript() {
    Script expectedScript = ScriptBuilder.createP2SHOutputScript(2, Lists.newArrayList(new BtcECKey(), new BtcECKey(), new BtcECKey()));
    byte[] data = RLP.encodeList(RLP.encodeElement(expectedScript.getProgram()));
    Script actualScript = BridgeSerializationUtils.deserializeScript(data);
    Assert.assertEquals(expectedScript, actualScript);
}
Also used : Script(co.rsk.bitcoinj.script.Script) Test(org.junit.Test)

Example 94 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeStorageProviderTest method setLastRetiredFederationP2SHScriptAndGetLastRetiredFederationP2SHScript.

@Test
public void setLastRetiredFederationP2SHScriptAndGetLastRetiredFederationP2SHScript() {
    Repository repository = createRepository();
    Repository track = repository.startTracking();
    Script script = new Script(new byte[] {});
    BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getNetworkConstants().getBridgeConstants(), activationsAllForks);
    // We store the value
    provider0.setLastRetiredFederationP2SHScript(script);
    provider0.saveLastRetiredFederationP2SHScript();
    track.commit();
    track = repository.startTracking();
    BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getNetworkConstants().getBridgeConstants(), activationsAllForks);
    // And then we get it back
    assertThat(provider.getLastRetiredFederationP2SHScript(), is(Optional.of(script)));
}
Also used : Script(co.rsk.bitcoinj.script.Script) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 95 with Script

use of co.rsk.bitcoinj.script.Script in project rskj by rsksmart.

the class BridgeUtilsTest method testIsValidPegInTx_value_between_old_and_new_before_iris.

@Test
public void testIsValidPegInTx_value_between_old_and_new_before_iris() {
    // Tx sending btc between old and new value, it is not a peg-in before iris
    Context btcContext = new Context(networkParameters);
    Federation federation = this.getGenesisFederationForTest(bridgeConstantsRegtest, btcContext);
    when(activations.isActive(ConsensusRule.RSKIP219)).thenReturn(false);
    BtcTransaction tx = new BtcTransaction(networkParameters);
    // Get a value in between pre and post iris minimum
    Coin minimumPegInValueBeforeIris = bridgeConstantsRegtest.getLegacyMinimumPeginTxValueInSatoshis();
    Coin minimumPegInValueAfterIris = bridgeConstantsRegtest.getMinimumPeginTxValueInSatoshis();
    Coin valueLock = minimumPegInValueAfterIris.plus((minimumPegInValueBeforeIris.subtract(minimumPegInValueAfterIris)).div(2));
    assertTrue(valueLock.isLessThan(minimumPegInValueBeforeIris));
    assertTrue(valueLock.isGreaterThan(minimumPegInValueAfterIris));
    tx.addOutput(valueLock, federation.getAddress());
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isValidPegInTx(tx, federation, btcContext, bridgeConstantsRegtest, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) Coin(co.rsk.bitcoinj.core.Coin) Script(co.rsk.bitcoinj.script.Script) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Aggregations

Script (co.rsk.bitcoinj.script.Script)123 Test (org.junit.Test)91 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)60 RskAddress (co.rsk.core.RskAddress)47 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)40 Repository (org.ethereum.core.Repository)34 MutableRepository (org.ethereum.db.MutableRepository)33 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)29 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)26 Block (org.ethereum.core.Block)24 Context (co.rsk.bitcoinj.core.Context)21 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)20 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)20 Address (co.rsk.bitcoinj.core.Address)17 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)17 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)17 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)17 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)17 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)17 InternalTransaction (org.ethereum.vm.program.InternalTransaction)17