Search in sources :

Example 6 with TransactionOutPoint

use of co.rsk.bitcoinj.core.TransactionOutPoint in project rskj by rsksmart.

the class BridgeUtilsTest method testIsMigrationTx.

@Test
public void testIsMigrationTx() {
    Context btcContext = new Context(networkParameters);
    List<BtcECKey> activeFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
    Federation activeFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(2000L), 2L, networkParameters);
    List<BtcECKey> retiringFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
    Federation retiringFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(retiringFederationKeys), Instant.ofEpochMilli(1000L), 1L, networkParameters);
    List<BtcECKey> retiredFederationKeys = Stream.of(BtcECKey.fromPrivate(Hex.decode("fc01")), BtcECKey.fromPrivate(Hex.decode("fc02"))).sorted(BtcECKey.PUBKEY_COMPARATOR).collect(Collectors.toList());
    Federation retiredFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 1L, networkParameters);
    Address activeFederationAddress = activeFederation.getAddress();
    BtcTransaction migrationTx = new BtcTransaction(networkParameters);
    migrationTx.addOutput(Coin.COIN, activeFederationAddress);
    TransactionInput migrationTxInput = new TransactionInput(networkParameters, migrationTx, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    migrationTx.addInput(migrationTxInput);
    signWithNecessaryKeys(retiringFederation, retiringFederationKeys, migrationTxInput, migrationTx);
    assertTrue(BridgeUtils.isMigrationTx(migrationTx, activeFederation, retiringFederation, null, btcContext, bridgeConstantsRegtest, activations));
    BtcTransaction toActiveFederationTx = new BtcTransaction(networkParameters);
    toActiveFederationTx.addOutput(Coin.COIN, activeFederationAddress);
    toActiveFederationTx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isMigrationTx(toActiveFederationTx, activeFederation, retiringFederation, null, btcContext, bridgeConstantsRegtest, activations));
    Address randomAddress = PegTestUtils.createRandomBtcAddress();
    BtcTransaction fromRetiringFederationTx = new BtcTransaction(networkParameters);
    fromRetiringFederationTx.addOutput(Coin.COIN, randomAddress);
    TransactionInput fromRetiringFederationTxInput = new TransactionInput(networkParameters, fromRetiringFederationTx, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    fromRetiringFederationTx.addInput(fromRetiringFederationTxInput);
    signWithNecessaryKeys(retiringFederation, retiringFederationKeys, fromRetiringFederationTxInput, fromRetiringFederationTx);
    assertFalse(BridgeUtils.isMigrationTx(fromRetiringFederationTx, activeFederation, retiringFederation, null, btcContext, bridgeConstantsRegtest, activations));
    assertFalse(BridgeUtils.isMigrationTx(migrationTx, activeFederation, null, null, btcContext, bridgeConstantsRegtest, activations));
    BtcTransaction retiredMigrationTx = new BtcTransaction(networkParameters);
    retiredMigrationTx.addOutput(Coin.COIN, activeFederationAddress);
    TransactionInput retiredMigrationTxInput = new TransactionInput(networkParameters, retiredMigrationTx, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    retiredMigrationTx.addInput(retiredMigrationTxInput);
    signWithNecessaryKeys(retiredFederation, retiredFederationKeys, retiredMigrationTxInput, retiredMigrationTx);
    Script p2SHScript = retiredFederation.getP2SHScript();
    assertTrue(BridgeUtils.isMigrationTx(retiredMigrationTx, activeFederation, null, p2SHScript, btcContext, bridgeConstantsRegtest, activations));
    assertTrue(BridgeUtils.isMigrationTx(retiredMigrationTx, activeFederation, retiringFederation, p2SHScript, btcContext, bridgeConstantsRegtest, activations));
    assertFalse(BridgeUtils.isMigrationTx(toActiveFederationTx, activeFederation, null, p2SHScript, btcContext, bridgeConstantsRegtest, activations));
    assertFalse(BridgeUtils.isMigrationTx(toActiveFederationTx, activeFederation, retiringFederation, p2SHScript, btcContext, bridgeConstantsRegtest, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) Script(co.rsk.bitcoinj.script.Script) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 7 with TransactionOutPoint

use of co.rsk.bitcoinj.core.TransactionOutPoint in project rskj by rsksmart.

the class BridgeUtilsTest method testIsPegOutTx_fromFastBridgeErpFederation.

@Test
public void testIsPegOutTx_fromFastBridgeErpFederation() {
    when(activations.isActive(ConsensusRule.RSKIP284)).thenReturn(true);
    List<BtcECKey> defaultFederationKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")), BtcECKey.fromPrivate(Hex.decode("fa03")));
    defaultFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation defaultFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(defaultFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters);
    List<BtcECKey> erpFederationPublicKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa03")), BtcECKey.fromPrivate(Hex.decode("fa04")), BtcECKey.fromPrivate(Hex.decode("fa05")));
    erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation erpFederation = new ErpFederation(FederationTestUtils.getFederationMembersWithBtcKeys(defaultFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters, erpFederationPublicKeys, 500L, activations);
    Federation standardFederation = bridgeConstantsRegtest.getGenesisFederation();
    // Create a tx from the fast bridge erp fed to a random address
    Address randomAddress = PegTestUtils.createRandomBtcAddress();
    BtcTransaction pegOutTx1 = new BtcTransaction(networkParameters);
    pegOutTx1.addOutput(Coin.COIN, randomAddress);
    TransactionInput pegOutInput1 = new TransactionInput(networkParameters, pegOutTx1, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    pegOutTx1.addInput(pegOutInput1);
    Script fastBridgeErpRedeemScript = FastBridgeErpRedeemScriptParser.createFastBridgeErpRedeemScript(erpFederation.getRedeemScript(), PegTestUtils.createHash(2));
    signWithNecessaryKeys(erpFederation, fastBridgeErpRedeemScript, defaultFederationKeys, pegOutInput1, pegOutTx1);
    // Before RSKIP 201 activation
    when(activations.isActive(ConsensusRule.RSKIP201)).thenReturn(false);
    assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(defaultFederation), activations));
    assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Arrays.asList(defaultFederation, standardFederation), activations));
    assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(standardFederation), activations));
    assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, activations, defaultFederation.getP2SHScript()));
    assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, activations, defaultFederation.getP2SHScript(), standardFederation.getP2SHScript()));
    assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, activations, standardFederation.getP2SHScript()));
    // After RSKIP 201 activation
    when(activations.isActive(ConsensusRule.RSKIP201)).thenReturn(true);
    assertTrue(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(defaultFederation), activations));
    assertTrue(BridgeUtils.isPegOutTx(pegOutTx1, Arrays.asList(defaultFederation, standardFederation), activations));
    assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(standardFederation), activations));
    assertTrue(BridgeUtils.isPegOutTx(pegOutTx1, activations, defaultFederation.getP2SHScript()));
    assertTrue(BridgeUtils.isPegOutTx(pegOutTx1, activations, defaultFederation.getP2SHScript(), standardFederation.getP2SHScript()));
    assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, activations, standardFederation.getP2SHScript()));
}
Also used : Script(co.rsk.bitcoinj.script.Script) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 8 with TransactionOutPoint

use of co.rsk.bitcoinj.core.TransactionOutPoint in project rskj by rsksmart.

the class BridgeUtilsTest method testIsValidPegInTx_hasChangeUtxoFromFastBridgeErpRetiredFederation_afterRskip201_notPegin.

@Test
public void testIsValidPegInTx_hasChangeUtxoFromFastBridgeErpRetiredFederation_afterRskip201_notPegin() {
    Context btcContext = new Context(networkParameters);
    Federation activeFederation = bridgeConstantsRegtest.getGenesisFederation();
    when(activations.isActive(ConsensusRule.RSKIP201)).thenReturn(true);
    when(activations.isActive(ConsensusRule.RSKIP284)).thenReturn(true);
    List<BtcECKey> retiredFederationKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
    retiredFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation retiredFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters);
    List<BtcECKey> erpFederationPublicKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa03")), BtcECKey.fromPrivate(Hex.decode("fa04")));
    erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation erpFederation = new ErpFederation(FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters, erpFederationPublicKeys, 500L, activations);
    // Create a tx from the retired fast bridge fed to the active fed
    BtcTransaction tx = new BtcTransaction(networkParameters);
    tx.addOutput(Coin.COIN, activeFederation.getAddress());
    TransactionInput txInput = new TransactionInput(networkParameters, tx, new byte[0], new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx.addInput(txInput);
    Script fastBridgeErpRedeemScript = FastBridgeErpRedeemScriptParser.createFastBridgeErpRedeemScript(erpFederation.getRedeemScript(), PegTestUtils.createHash(2));
    signWithNecessaryKeys(erpFederation, fastBridgeErpRedeemScript, retiredFederationKeys, txInput, tx);
    assertFalse(BridgeUtils.isValidPegInTx(tx, Collections.singletonList(activeFederation), retiredFederation.getP2SHScript(), btcContext, bridgeConstantsRegtest, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) Script(co.rsk.bitcoinj.script.Script) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 9 with TransactionOutPoint

use of co.rsk.bitcoinj.core.TransactionOutPoint in project rskj by rsksmart.

the class BridgeUtilsTest method testIsValidPegInTx_hasChangeUtxoFromErpRetiredFederation_beforeRskip201_isPegin.

@Test
public void testIsValidPegInTx_hasChangeUtxoFromErpRetiredFederation_beforeRskip201_isPegin() {
    Context btcContext = new Context(networkParameters);
    Federation activeFederation = bridgeConstantsRegtest.getGenesisFederation();
    when(activations.isActive(ConsensusRule.RSKIP284)).thenReturn(true);
    List<BtcECKey> retiredFederationKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
    retiredFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation retiredFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters);
    List<BtcECKey> erpFederationPublicKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa03")), BtcECKey.fromPrivate(Hex.decode("fa04")));
    erpFederationPublicKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation erpFederation = new ErpFederation(FederationTestUtils.getFederationMembersWithBtcKeys(retiredFederationKeys), Instant.ofEpochMilli(1000L), 0L, networkParameters, erpFederationPublicKeys, 500L, activations);
    // Create a tx from the retired erp fed to the active fed
    BtcTransaction tx = new BtcTransaction(networkParameters);
    tx.addOutput(Coin.COIN, activeFederation.getAddress());
    TransactionInput txInput = new TransactionInput(networkParameters, tx, new byte[0], new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx.addInput(txInput);
    signWithErpFederation(erpFederation, retiredFederationKeys, txInput, tx);
    assertTrue(BridgeUtils.isValidPegInTx(tx, Collections.singletonList(activeFederation), retiredFederation.getP2SHScript(), btcContext, bridgeConstantsRegtest, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) BtcECKey(co.rsk.bitcoinj.core.BtcECKey) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 10 with TransactionOutPoint

use of co.rsk.bitcoinj.core.TransactionOutPoint in project rskj by rsksmart.

the class BridgeUtilsTest method testIsPegOutTx_noRedeemScript.

@Test
public void testIsPegOutTx_noRedeemScript() {
    Federation federation = bridgeConstantsRegtest.getGenesisFederation();
    Address randomAddress = PegTestUtils.createRandomBtcAddress();
    BtcTransaction pegOutTx1 = new BtcTransaction(networkParameters);
    pegOutTx1.addOutput(Coin.COIN, randomAddress);
    TransactionInput pegOutInput1 = new TransactionInput(networkParameters, pegOutTx1, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    pegOutTx1.addInput(pegOutInput1);
    assertFalse(BridgeUtils.isPegOutTx(pegOutTx1, Collections.singletonList(federation), activations));
}
Also used : Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) BtcTransaction(co.rsk.bitcoinj.core.BtcTransaction) TransactionInput(co.rsk.bitcoinj.core.TransactionInput) TransactionOutPoint(co.rsk.bitcoinj.core.TransactionOutPoint) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Aggregations

BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)22 TransactionInput (co.rsk.bitcoinj.core.TransactionInput)22 TransactionOutPoint (co.rsk.bitcoinj.core.TransactionOutPoint)22 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)22 Test (org.junit.Test)22 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)17 Script (co.rsk.bitcoinj.script.Script)14 Address (co.rsk.bitcoinj.core.Address)12 RskAddress (co.rsk.core.RskAddress)12 Context (co.rsk.bitcoinj.core.Context)11 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)3 ScriptChunk (co.rsk.bitcoinj.script.ScriptChunk)3 TransactionSignature (co.rsk.bitcoinj.crypto.TransactionSignature)2 ConsensusRule (org.ethereum.config.blockchain.upgrades.ConsensusRule)2 Coin (co.rsk.bitcoinj.core.Coin)1 ScriptBuilder (co.rsk.bitcoinj.script.ScriptBuilder)1 Wallet (co.rsk.bitcoinj.wallet.Wallet)1 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)1