use of org.ethereum.vm.program.InternalTransaction in project rskj by rsksmart.
the class BridgeSupportTest method registerFastBridgeBtcTransaction_OK.
@Test
public void registerFastBridgeBtcTransaction_OK() throws IOException, BlockStoreException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
Context btcContext = mock(Context.class);
when(btcContext.getParams()).thenReturn(bridgeConstants.getBtcParams());
Repository repository = spy(createRepository());
BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activations);
FederationSupport federationSupportMock = mock(FederationSupport.class);
doReturn(provider.getNewFederationBtcUTXOs()).when(federationSupportMock).getActiveFederationBtcUTXOs();
BridgeSupport bridgeSupport = spy(new BridgeSupport(bridgeConstants, provider, mock(BridgeEventLogger.class), new BtcLockSenderProvider(), new PeginInstructionsProvider(), repository, mock(Block.class), btcContext, federationSupportMock, mock(BtcBlockStoreWithCache.Factory.class), activations));
doReturn(bridgeConstants.getGenesisFederation()).when(bridgeSupport).getActiveFederation();
doReturn(true).when(bridgeSupport).validationsForRegisterBtcTransaction(any(), anyInt(), any(), any());
doReturn(PegTestUtils.createHash3(1)).when(bridgeSupport).getFastBridgeDerivationHash(any(Keccak256.class), any(Address.class), any(Address.class), any(RskAddress.class));
Address btcAddress = Address.fromBase58(btcParams, "n3PLxDiwWqa5uH7fSbHCxS6VAjD9Y7Rwkj");
ECKey key = ECKey.fromPublicOnly(new BtcECKey().getPubKey());
RskAddress lbcAddress = new RskAddress(key.getAddress());
Coin valueToSend = Coin.COIN;
BtcTransaction tx = createBtcTransactionWithOutputToAddress(valueToSend, getFastBridgeFederationAddress());
InternalTransaction rskTx = new InternalTransaction(Keccak256.ZERO_HASH.getBytes(), 0, 0, null, null, null, lbcAddress.getBytes(), null, null, null, null);
co.rsk.core.Coin preCallLbcAddressBalance = repository.getBalance(lbcAddress);
BigInteger result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, Hex.decode("ab"), PegTestUtils.createHash3(0), btcAddress, lbcAddress, btcAddress, true);
Assert.assertEquals(co.rsk.core.Coin.fromBitcoin(valueToSend).asBigInteger(), result);
co.rsk.core.Coin postCallLbcAddressBalance = repository.getBalance(lbcAddress);
Assert.assertEquals(preCallLbcAddressBalance.add(co.rsk.core.Coin.fromBitcoin(Coin.COIN)), postCallLbcAddressBalance);
bridgeSupport.save();
Assert.assertTrue(provider.isFastBridgeFederationDerivationHashUsed(tx.getHash(), bridgeSupport.getFastBridgeDerivationHash(PegTestUtils.createHash3(0), btcAddress, btcAddress, lbcAddress)));
Assert.assertEquals(1, provider.getNewFederationBtcUTXOs().size());
// Trying to register the same transaction again fails
result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, Hex.decode("ab"), PegTestUtils.createHash3(0), btcAddress, lbcAddress, btcAddress, true);
Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_UNPROCESSABLE_TX_ALREADY_PROCESSED_ERROR_CODE), result);
}
use of org.ethereum.vm.program.InternalTransaction in project rskj by rsksmart.
the class BridgeSupportTest method registerFastBridgeBtcTransaction_surpasses_locking_cap_and_shouldTransfer_is_false.
@Test
public void registerFastBridgeBtcTransaction_surpasses_locking_cap_and_shouldTransfer_is_false() throws IOException, BlockStoreException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
when(activations.isActive(ConsensusRule.RSKIP134)).thenReturn(true);
ReleaseTransactionSet releaseTransactionSet = new ReleaseTransactionSet(new HashSet<>());
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
when(provider.getReleaseTransactionSet()).thenReturn(releaseTransactionSet);
when(provider.isFastBridgeFederationDerivationHashUsed(any(), any())).thenReturn(false);
BtcLockSender btcLockSender = mock(BtcLockSender.class);
BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
when(btcLockSenderProvider.tryGetBtcLockSender(any())).thenReturn(Optional.of(btcLockSender));
Repository repository = mock(Repository.class);
when(repository.getBalance(any())).thenReturn(co.rsk.core.Coin.valueOf(1));
Context btcContext = mock(Context.class);
when(btcContext.getParams()).thenReturn(bridgeConstants.getBtcParams());
BridgeSupport bridgeSupport = spy(new BridgeSupport(bridgeConstants, provider, mock(BridgeEventLogger.class), btcLockSenderProvider, new PeginInstructionsProvider(), repository, mock(Block.class), btcContext, mock(FederationSupport.class), mock(BtcBlockStoreWithCache.Factory.class), activations));
doReturn(bridgeConstants.getGenesisFederation()).when(bridgeSupport).getActiveFederation();
doReturn(true).when(bridgeSupport).validationsForRegisterBtcTransaction(any(), anyInt(), any(), any());
doReturn(Coin.COIN).when(bridgeSupport).getLockingCap();
doReturn(PegTestUtils.createHash3(1)).when(bridgeSupport).getFastBridgeDerivationHash(any(Keccak256.class), any(Address.class), any(Address.class), any(RskAddress.class));
Address btcAddress = Address.fromBase58(btcParams, "n3PLxDiwWqa5uH7fSbHCxS6VAjD9Y7Rwkj");
ECKey key = ECKey.fromPublicOnly(new BtcECKey().getPubKey());
RskAddress lbcAddress = new RskAddress(key.getAddress());
BtcTransaction tx = createBtcTransactionWithOutputToAddress(Coin.COIN, getFastBridgeFederationAddress());
byte[] pmtSerialized = Hex.decode("ab");
InternalTransaction rskTx = new InternalTransaction(Keccak256.ZERO_HASH.getBytes(), 0, 0, null, null, null, lbcAddress.getBytes(), null, null, null, null);
BigInteger result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, pmtSerialized, PegTestUtils.createHash3(0), btcAddress, lbcAddress, btcAddress, false);
Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_REFUNDED_USER_ERROR_CODE), result);
}
use of org.ethereum.vm.program.InternalTransaction in project rskj by rsksmart.
the class BridgeSupportTest method registerFastBridgeBtcTransaction_amount_sent_is_0.
@Test
public void registerFastBridgeBtcTransaction_amount_sent_is_0() throws BlockStoreException, IOException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
Context btcContext = mock(Context.class);
when(btcContext.getParams()).thenReturn(bridgeConstants.getBtcParams());
ECKey key = ECKey.fromPublicOnly(new BtcECKey().getPubKey());
RskAddress lbcAddress = new RskAddress(key.getAddress());
BridgeSupport bridgeSupport = spy(new BridgeSupport(bridgeConstants, mock(BridgeStorageProvider.class), mock(BridgeEventLogger.class), new BtcLockSenderProvider(), new PeginInstructionsProvider(), mock(Repository.class), mock(Block.class), btcContext, mock(FederationSupport.class), mock(BtcBlockStoreWithCache.Factory.class), activations));
doReturn(bridgeConstants.getGenesisFederation()).when(bridgeSupport).getActiveFederation();
doReturn(true).when(bridgeSupport).validationsForRegisterBtcTransaction(any(), anyInt(), any(), any());
doReturn(PegTestUtils.createHash3(1)).when(bridgeSupport).getFastBridgeDerivationHash(any(Keccak256.class), any(Address.class), any(Address.class), any(RskAddress.class));
BtcTransaction tx = createBtcTransactionWithOutputToAddress(Coin.COIN, new BtcECKey().toAddress(btcParams));
InternalTransaction rskTx = new InternalTransaction(Keccak256.ZERO_HASH.getBytes(), 0, 0, null, null, null, lbcAddress.getBytes(), null, null, null, null);
BigInteger result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, Hex.decode("ab"), PegTestUtils.createHash3(0), mock(Address.class), lbcAddress, mock(Address.class), false);
Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_UNPROCESSABLE_TX_VALUE_ZERO_ERROR), result);
}
use of org.ethereum.vm.program.InternalTransaction in project rskj by rsksmart.
the class BridgeSupportTest method registerFastBridgeBtcTransaction_surpasses_locking_cap_and_shouldTransfer_is_true.
@Test
public void registerFastBridgeBtcTransaction_surpasses_locking_cap_and_shouldTransfer_is_true() throws IOException, BlockStoreException, BridgeIllegalArgumentException {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP176)).thenReturn(true);
when(activations.isActive(ConsensusRule.RSKIP134)).thenReturn(true);
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
ReleaseTransactionSet releaseTransactionSet = new ReleaseTransactionSet(new HashSet<>());
when(provider.getReleaseTransactionSet()).thenReturn(releaseTransactionSet);
BtcLockSender btcLockSender = mock(BtcLockSender.class);
BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
when(btcLockSenderProvider.tryGetBtcLockSender(any())).thenReturn(Optional.of(btcLockSender));
Repository repository = mock(Repository.class);
when(repository.getBalance(any())).thenReturn(co.rsk.core.Coin.valueOf(1));
Context btcContext = mock(Context.class);
when(btcContext.getParams()).thenReturn(bridgeConstants.getBtcParams());
BridgeSupport bridgeSupport = spy(new BridgeSupport(bridgeConstants, provider, mock(BridgeEventLogger.class), btcLockSenderProvider, new PeginInstructionsProvider(), repository, mock(Block.class), btcContext, mock(FederationSupport.class), mock(BtcBlockStoreWithCache.Factory.class), activations));
doReturn(bridgeConstants.getGenesisFederation()).when(bridgeSupport).getActiveFederation();
doReturn(true).when(bridgeSupport).validationsForRegisterBtcTransaction(any(), anyInt(), any(), any());
doReturn(Coin.COIN).when(bridgeSupport).getLockingCap();
doReturn(PegTestUtils.createHash3(1)).when(bridgeSupport).getFastBridgeDerivationHash(any(Keccak256.class), any(Address.class), any(Address.class), any(RskAddress.class));
Address btcAddress = Address.fromBase58(btcParams, "n3PLxDiwWqa5uH7fSbHCxS6VAjD9Y7Rwkj");
ECKey key = ECKey.fromPublicOnly(new BtcECKey().getPubKey());
RskAddress lbcAddress = new RskAddress(key.getAddress());
BtcTransaction tx = createBtcTransactionWithOutputToAddress(Coin.COIN, getFastBridgeFederationAddress());
byte[] pmtSerialized = Hex.decode("ab");
InternalTransaction rskTx = new InternalTransaction(Keccak256.ZERO_HASH.getBytes(), 0, 0, null, null, null, lbcAddress.getBytes(), null, null, null, null);
BigInteger result = bridgeSupport.registerFastBridgeBtcTransaction(rskTx, tx.bitcoinSerialize(), 100, pmtSerialized, PegTestUtils.createHash3(0), btcAddress, lbcAddress, btcAddress, true);
Assert.assertEquals(BigInteger.valueOf(BridgeSupport.FAST_BRIDGE_REFUNDED_LP_ERROR_CODE), result);
}
use of org.ethereum.vm.program.InternalTransaction in project rskj by rsksmart.
the class BridgeSupportReleaseBtcTest method buildReleaseRskTx_fromContract.
private Transaction buildReleaseRskTx_fromContract(Coin coin) {
Transaction releaseTx = Transaction.builder().nonce(NONCE).gasPrice(GAS_PRICE).gasLimit(GAS_LIMIT).destination(PrecompiledContracts.BRIDGE_ADDR.toHexString()).data(Hex.decode(DATA)).chainId(Constants.REGTEST_CHAIN_ID).value(co.rsk.core.Coin.fromBitcoin(Coin.COIN).asBigInteger()).build();
releaseTx.sign(SENDER.getPrivKeyBytes());
return new InternalTransaction(releaseTx.getHash().getBytes(), 400, 0, NONCE.toByteArray(), DataWord.valueOf(GAS_PRICE.intValue()), DataWord.valueOf(GAS_LIMIT.intValue()), SENDER.getAddress(), PrecompiledContracts.BRIDGE_ADDR.getBytes(), co.rsk.core.Coin.fromBitcoin(Coin.COIN).getBytes(), Hex.decode(DATA), "");
}
Aggregations