Search in sources :

Example 6 with Coin

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

the class BridgeSupport method voteFeePerKbChange.

/**
 * Votes for a fee per kb value.
 *
 * @return 1 upon successful vote, -1 when the vote was unsuccessful,
 * FEE_PER_KB_GENERIC_ERROR_CODE when there was an un expected error.
 */
public Integer voteFeePerKbChange(Transaction tx, Coin feePerKb) {
    AddressBasedAuthorizer authorizer = bridgeConstants.getFeePerKbChangeAuthorizer();
    if (!authorizer.isAuthorized(tx)) {
        return FEE_PER_KB_GENERIC_ERROR_CODE;
    }
    if (!feePerKb.isPositive()) {
        return NEGATIVE_FEE_PER_KB_ERROR_CODE;
    }
    if (feePerKb.isGreaterThan(bridgeConstants.getMaxFeePerKb())) {
        return EXCESSIVE_FEE_PER_KB_ERROR_CODE;
    }
    ABICallElection feePerKbElection = provider.getFeePerKbElection(authorizer);
    ABICallSpec feeVote = new ABICallSpec("setFeePerKb", new byte[][] { BridgeSerializationUtils.serializeCoin(feePerKb) });
    boolean successfulVote = feePerKbElection.vote(feeVote, tx.getSender());
    if (!successfulVote) {
        return -1;
    }
    ABICallSpec winner = feePerKbElection.getWinner();
    if (winner == null) {
        logger.info("Successful fee per kb vote for {}", feePerKb);
        return 1;
    }
    Coin winnerFee;
    try {
        winnerFee = BridgeSerializationUtils.deserializeCoin(winner.getArguments()[0]);
    } catch (Exception e) {
        logger.warn("Exception deserializing winner feePerKb", e);
        return FEE_PER_KB_GENERIC_ERROR_CODE;
    }
    if (winnerFee == null) {
        logger.warn("Invalid winner feePerKb: feePerKb can't be null");
        return FEE_PER_KB_GENERIC_ERROR_CODE;
    }
    if (!winnerFee.equals(feePerKb)) {
        logger.debug("Winner fee is different than the last vote: maybe you forgot to clear winners");
    }
    logger.info("Fee per kb changed to {}", winnerFee);
    provider.setFeePerKb(winnerFee);
    feePerKbElection.clear();
    return 1;
}
Also used : Coin(co.rsk.bitcoinj.core.Coin) VMException(org.ethereum.vm.exception.VMException) UTXOProviderException(co.rsk.bitcoinj.core.UTXOProviderException) VerificationException(co.rsk.bitcoinj.core.VerificationException) InsufficientMoneyException(co.rsk.bitcoinj.core.InsufficientMoneyException) AddressFormatException(co.rsk.bitcoinj.core.AddressFormatException) IOException(java.io.IOException) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) PeginInstructionsException(co.rsk.peg.pegininstructions.PeginInstructionsException)

Example 7 with Coin

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

the class BridgeSupport method releaseBtc.

/**
 * Initiates the process of sending coins back to BTC.
 * This is the default contract method.
 * The funds will be sent to the bitcoin address controlled by the private key that signed the rsk tx.
 * The amount sent to the bridge in this tx will be the amount sent in the btc network minus fees.
 * @param rskTx The rsk tx being executed.
 * @throws IOException
 */
public void releaseBtc(Transaction rskTx) throws IOException {
    Coin value = rskTx.getValue().toBitcoin();
    final RskAddress senderAddress = rskTx.getSender();
    // as we can't send btc from contracts we want to send them back to the senderAddressStr
    if (BridgeUtils.isContractTx(rskTx)) {
        logger.trace("Contract {} tried to release funds. Release is just allowed from standard accounts.", rskTx);
        if (activations.isActive(ConsensusRule.RSKIP185)) {
            emitRejectEvent(value, senderAddress.toHexString(), RejectedPegoutReason.CALLER_CONTRACT);
            return;
        } else {
            throw new Program.OutOfGasException("Contract calling releaseBTC");
        }
    }
    Context.propagate(btcContext);
    NetworkParameters btcParams = bridgeConstants.getBtcParams();
    Address btcDestinationAddress = BridgeUtils.recoverBtcAddressFromEthTransaction(rskTx, btcParams);
    requestRelease(btcDestinationAddress, value, rskTx);
}
Also used : Coin(co.rsk.bitcoinj.core.Coin) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) NetworkParameters(co.rsk.bitcoinj.core.NetworkParameters) RskAddress(co.rsk.core.RskAddress)

Example 8 with Coin

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

the class BridgeUtilsTest method testIsValidPegInTx.

@Test
public void testIsValidPegInTx() {
    // Peg-in is for the genesis federation ATM
    Context btcContext = new Context(networkParameters);
    Federation federation = bridgeConstantsRegtest.getGenesisFederation();
    Wallet wallet = new BridgeBtcWallet(btcContext, Collections.singletonList(federation));
    Address federationAddress = federation.getAddress();
    wallet.addWatchedAddress(federationAddress, federation.getCreationTime().toEpochMilli());
    when(activations.isActive(any(ConsensusRule.class))).thenReturn(false);
    // Tx sending less than the minimum allowed, not a peg-in tx
    Coin minimumLockValue = bridgeConstantsRegtest.getLegacyMinimumPeginTxValueInSatoshis();
    BtcTransaction tx = new BtcTransaction(networkParameters);
    tx.addOutput(minimumLockValue.subtract(Coin.CENT), federationAddress);
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertFalse(BridgeUtils.isValidPegInTx(tx, federation, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to the federation, but also spending from the federation address,
    // the typical peg-out tx, not a peg-in tx.
    BtcTransaction tx2 = new BtcTransaction(networkParameters);
    tx2.addOutput(Coin.COIN, federationAddress);
    TransactionInput txIn = new TransactionInput(networkParameters, tx2, new byte[] {}, new TransactionOutPoint(networkParameters, 0, Sha256Hash.ZERO_HASH));
    tx2.addInput(txIn);
    signWithNecessaryKeys(bridgeConstantsRegtest.getGenesisFederation(), BridgeRegTestConstants.REGTEST_FEDERATION_PRIVATE_KEYS, txIn, tx2);
    assertFalse(BridgeUtils.isValidPegInTx(tx2, federation, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 1 btc to the federation, is a peg-in tx
    BtcTransaction tx3 = new BtcTransaction(networkParameters);
    tx3.addOutput(Coin.COIN, federationAddress);
    tx3.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isValidPegInTx(tx3, federation, btcContext, bridgeConstantsRegtest, activations));
    // Tx sending 50 btc to the federation, is a peg-in tx
    BtcTransaction tx4 = new BtcTransaction(networkParameters);
    tx4.addOutput(Coin.FIFTY_COINS, federationAddress);
    tx4.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(BridgeUtils.isValidPegInTx(tx4, federation, btcContext, bridgeConstantsRegtest, activations));
}
Also used : Context(co.rsk.bitcoinj.core.Context) Coin(co.rsk.bitcoinj.core.Coin) Script(co.rsk.bitcoinj.script.Script) Address(co.rsk.bitcoinj.core.Address) RskAddress(co.rsk.core.RskAddress) Wallet(co.rsk.bitcoinj.wallet.Wallet) ConsensusRule(org.ethereum.config.blockchain.upgrades.ConsensusRule) 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)

Example 9 with Coin

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

the class BridgeUtilsTest method testIsValidPegInTx_value_between_old_and_new_after_iris.

@Test
public void testIsValidPegInTx_value_between_old_and_new_after_iris() {
    // Tx sending btc between old and new value, it is a peg-in after iris
    Context btcContext = new Context(networkParameters);
    Federation federation = this.getGenesisFederationForTest(bridgeConstantsRegtest, btcContext);
    when(activations.isActive(ConsensusRule.RSKIP219)).thenReturn(true);
    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.isGreaterThan(minimumPegInValueAfterIris));
    assertTrue(valueLock.isLessThan(minimumPegInValueBeforeIris));
    tx.addOutput(valueLock, federation.getAddress());
    tx.addInput(Sha256Hash.ZERO_HASH, 0, new Script(new byte[] {}));
    assertTrue(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)

Example 10 with Coin

use of co.rsk.bitcoinj.core.Coin 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

Coin (co.rsk.bitcoinj.core.Coin)19 BtcTransaction (co.rsk.bitcoinj.core.BtcTransaction)7 Address (co.rsk.bitcoinj.core.Address)6 Context (co.rsk.bitcoinj.core.Context)6 Script (co.rsk.bitcoinj.script.Script)6 RskAddress (co.rsk.core.RskAddress)6 Test (org.junit.Test)6 AddressFormatException (co.rsk.bitcoinj.core.AddressFormatException)4 NetworkParameters (co.rsk.bitcoinj.core.NetworkParameters)4 TransactionInput (co.rsk.bitcoinj.core.TransactionInput)4 Wallet (co.rsk.bitcoinj.wallet.Wallet)4 IOException (java.io.IOException)4 BigInteger (java.math.BigInteger)4 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)4 ECKey (org.ethereum.crypto.ECKey)4 VMException (org.ethereum.vm.exception.VMException)4 BtcECKey (co.rsk.bitcoinj.core.BtcECKey)3 InsufficientMoneyException (co.rsk.bitcoinj.core.InsufficientMoneyException)3 Sha256Hash (co.rsk.bitcoinj.core.Sha256Hash)3 UTXOProviderException (co.rsk.bitcoinj.core.UTXOProviderException)3