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;
}
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);
}
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));
}
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));
}
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));
}
Aggregations