use of co.rsk.bitcoinj.core.Coin in project rskj by rsksmart.
the class BridgeSupport method adjustBalancesIfChangeOutputWasDust.
/**
* If federation change output value had to be increased to be non-dust, the federation now has
* more BTC than it should. So, we burn some sBTC to make balances match.
*
* @param btcTx The btc tx that was just completed
* @param sentByUser The number of sBTC originaly sent by the user
*/
private void adjustBalancesIfChangeOutputWasDust(BtcTransaction btcTx, Coin sentByUser) {
if (btcTx.getOutputs().size() <= 1) {
// If there is no change, do-nothing
return;
}
Coin sumInputs = Coin.ZERO;
for (TransactionInput transactionInput : btcTx.getInputs()) {
sumInputs = sumInputs.add(transactionInput.getValue());
}
Coin change = btcTx.getOutput(1).getValue();
Coin spentByFederation = sumInputs.subtract(change);
if (spentByFederation.isLessThan(sentByUser)) {
Coin coinsToBurn = sentByUser.subtract(spentByFederation);
this.transferTo(BURN_ADDRESS, co.rsk.core.Coin.fromBitcoin(coinsToBurn));
}
}
use of co.rsk.bitcoinj.core.Coin in project rskj by rsksmart.
the class BridgeSupport method createMigrationTransaction.
private Pair<BtcTransaction, List<UTXO>> createMigrationTransaction(Wallet originWallet, Address destinationAddress) {
Coin expectedMigrationValue = originWallet.getBalance();
logger.debug("[createMigrationTransaction] Balance to migrate: {}", expectedMigrationValue);
for (; ; ) {
BtcTransaction migrationBtcTx = new BtcTransaction(originWallet.getParams());
migrationBtcTx.addOutput(expectedMigrationValue, destinationAddress);
SendRequest sr = SendRequest.forTx(migrationBtcTx);
sr.changeAddress = destinationAddress;
sr.feePerKb = getFeePerKb();
sr.missingSigsMode = Wallet.MissingSigsMode.USE_OP_ZERO;
sr.recipientsPayFees = true;
try {
originWallet.completeTx(sr);
for (TransactionInput transactionInput : migrationBtcTx.getInputs()) {
transactionInput.disconnect();
}
List<UTXO> selectedUTXOs = originWallet.getUTXOProvider().getOpenTransactionOutputs(originWallet.getWatchedAddresses()).stream().filter(utxo -> migrationBtcTx.getInputs().stream().anyMatch(input -> input.getOutpoint().getHash().equals(utxo.getHash()) && input.getOutpoint().getIndex() == utxo.getIndex())).collect(Collectors.toList());
return Pair.of(migrationBtcTx, selectedUTXOs);
} catch (InsufficientMoneyException | Wallet.ExceededMaxTransactionSize | Wallet.CouldNotAdjustDownwards e) {
expectedMigrationValue = expectedMigrationValue.divide(2);
} catch (Wallet.DustySendRequested e) {
throw new IllegalStateException("Retiring federation wallet cannot be emptied", e);
} catch (UTXOProviderException e) {
throw new RuntimeException("Unexpected UTXO provider error", e);
}
}
}
use of co.rsk.bitcoinj.core.Coin in project rskj by rsksmart.
the class VoteFeePerKbChangeTest method voteFeePerKbChange_unauthorized.
@Test
public void voteFeePerKbChange_unauthorized() throws VMException {
BridgeStorageProviderInitializer storageInitializer = Helper.buildNoopInitializer();
Coin genesisFeePerKB = BridgeRegTestConstants.getInstance().getGenesisFeePerKb();
ABIEncoder abiEncoder = (int executionIndex) -> Bridge.VOTE_FEE_PER_KB.encode(BigInteger.valueOf(Helper.randomCoin(Coin.MILLICOIN, 1, 100).getValue()));
TxBuilder txBuilder = (int executionIndex) -> {
String generator = "unauthorized";
ECKey sender = ECKey.fromPrivate(HashUtil.keccak256(generator.getBytes(StandardCharsets.UTF_8)));
return Helper.buildTx(sender);
};
ExecutionStats stats = new ExecutionStats("voteFeePerKbChange_unauthorized");
executeAndAverage("voteFeePerKbChange_unauthorized", 1000, abiEncoder, storageInitializer, txBuilder, Helper.getRandomHeightProvider(10), stats, ((environment, callResult) -> {
Assert.assertEquals(genesisFeePerKB.getValue(), ((Bridge) environment.getContract()).getFeePerKb(null));
}));
BridgePerformanceTest.addStats(stats);
}
use of co.rsk.bitcoinj.core.Coin in project rskj by rsksmart.
the class ReleaseBtcTest method releaseBtc.
@Test
public void releaseBtc() throws IOException {
int minCentsBtc = 5;
int maxCentsBtc = 100;
final NetworkParameters parameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
BridgeStorageProviderInitializer storageInitializer = (BridgeStorageProvider provider, Repository repository, int executionIndex) -> {
ReleaseRequestQueue queue;
try {
queue = provider.getReleaseRequestQueue();
} catch (Exception e) {
throw new RuntimeException("Unable to gather release request queue");
}
for (int i = 0; i < Helper.randomInRange(10, 100); i++) {
Coin value = Coin.CENT.multiply(Helper.randomInRange(minCentsBtc, maxCentsBtc));
queue.add(new BtcECKey().toAddress(parameters), value);
}
};
final byte[] releaseBtcEncoded = Bridge.RELEASE_BTC.encode();
ABIEncoder abiEncoder = (int executionIndex) -> releaseBtcEncoded;
TxBuilder txBuilder = (int executionIndex) -> {
long satoshis = Coin.CENT.multiply(Helper.randomInRange(minCentsBtc, maxCentsBtc)).getValue();
BigInteger weis = Denomination.satoshisToWeis(BigInteger.valueOf(satoshis));
ECKey sender = new ECKey();
return Helper.buildSendValueTx(sender, weis);
};
ExecutionStats stats = new ExecutionStats("releaseBtc");
executeAndAverage("releaseBtc", 1000, abiEncoder, storageInitializer, txBuilder, Helper.getRandomHeightProvider(10), stats);
BridgePerformanceTest.addStats(stats);
}
Aggregations