use of co.rsk.peg.pegininstructions.PeginInstructionsProvider 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 co.rsk.peg.pegininstructions.PeginInstructionsProvider in project rskj by rsksmart.
the class BridgeSupportTest method eventLoggerLogPeginBtc_before_rskip_170_activation.
@Test
public void eventLoggerLogPeginBtc_before_rskip_170_activation() throws Exception {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP146)).thenReturn(true);
when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(false);
BridgeEventLogger mockedEventLogger = mock(BridgeEventLogger.class);
BridgeStorageProvider mockBridgeStorageProvider = mock(BridgeStorageProvider.class);
when(mockBridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(any(Sha256Hash.class))).thenReturn(Optional.empty());
LockWhitelist lockWhitelist = mock(LockWhitelist.class);
when(lockWhitelist.isWhitelistedFor(any(Address.class), any(Coin.class), any(int.class))).thenReturn(true);
when(mockBridgeStorageProvider.getLockWhitelist()).thenReturn(lockWhitelist);
when(mockBridgeStorageProvider.getNewFederation()).thenReturn(bridgeConstants.getGenesisFederation());
Block executionBlock = mock(Block.class);
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = mock(BtcBlockStoreWithCache.Factory.class);
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
when(btcBlockStoreFactory.newInstance(any(Repository.class), any(), any(), any())).thenReturn(btcBlockStore);
// Create transaction
Coin lockValue = Coin.COIN;
BtcTransaction tx = new BtcTransaction(bridgeConstants.getBtcParams());
tx.addOutput(lockValue, mockBridgeStorageProvider.getNewFederation().getAddress());
BtcECKey srcKey = new BtcECKey();
tx.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey));
// Create header and PMT
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(bridgeConstants.getBtcParams(), bits, hashes, 1);
Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(new ArrayList<>());
co.rsk.bitcoinj.core.BtcBlock btcBlock = new co.rsk.bitcoinj.core.BtcBlock(bridgeConstants.getBtcParams(), 1, PegTestUtils.createHash(), merkleRoot, 1, 1, 1, new ArrayList<>());
int height = 1;
mockChainOfStoredBlocks(btcBlockStore, btcBlock, height + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(), height);
BridgeSupport bridgeSupport = bridgeSupportBuilder.withBridgeConstants(bridgeConstants).withProvider(mockBridgeStorageProvider).withEventLogger(mockedEventLogger).withBtcLockSenderProvider(new BtcLockSenderProvider()).withPeginInstructionsProvider(new PeginInstructionsProvider()).withExecutionBlock(executionBlock).withBtcBlockStoreFactory(btcBlockStoreFactory).withActivations(activations).build();
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx.bitcoinSerialize(), height, pmt.bitcoinSerialize());
verify(mockedEventLogger, atLeastOnce()).logLockBtc(any(RskAddress.class), any(BtcTransaction.class), any(Address.class), any(Coin.class));
verify(mockedEventLogger, never()).logPeginBtc(any(RskAddress.class), any(BtcTransaction.class), any(Coin.class), anyInt());
}
use of co.rsk.peg.pegininstructions.PeginInstructionsProvider in project rskj by rsksmart.
the class BridgeSupportTest method addSignature_fedPubKey_belongs_to_retiring_federation.
@Test
public void addSignature_fedPubKey_belongs_to_retiring_federation() throws Exception {
// Setup
FederationSupport mockFederationSupport = mock(FederationSupport.class);
BridgeStorageProvider provider = mock(BridgeStorageProvider.class);
BridgeSupport bridgeSupport = new BridgeSupport(bridgeConstants, provider, mock(BridgeEventLogger.class), new BtcLockSenderProvider(), new PeginInstructionsProvider(), mock(Repository.class), mock(Block.class), new Context(bridgeConstants.getBtcParams()), mockFederationSupport, null, null);
// Creates retiring federation
List<BtcECKey> federation1Keys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")));
federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
Federation retiringFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(federation1Keys), Instant.ofEpochMilli(1000L), 0L, btcParams);
// Creates active federation
List<BtcECKey> activeFederationKeys = Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fa03")), BtcECKey.fromPrivate(Hex.decode("fa04")));
activeFederationKeys.sort(BtcECKey.PUBKEY_COMPARATOR);
Federation activeFederation = new Federation(FederationTestUtils.getFederationMembersWithBtcKeys(activeFederationKeys), Instant.ofEpochMilli(1000L), 0L, btcParams);
when(mockFederationSupport.getActiveFederation()).thenReturn(activeFederation);
when(mockFederationSupport.getRetiringFederation()).thenReturn(retiringFederation);
when(provider.getRskTxsWaitingForSignatures()).thenReturn(new TreeMap<>());
bridgeSupport.addSignature(BtcECKey.fromPrivate(Hex.decode("fa01")), null, PegTestUtils.createHash3(1).getBytes());
verify(provider, times(1)).getRskTxsWaitingForSignatures();
}
use of co.rsk.peg.pegininstructions.PeginInstructionsProvider 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 co.rsk.peg.pegininstructions.PeginInstructionsProvider in project rskj by rsksmart.
the class BridgeSupportTest method eventLoggerLogPeginBtc_after_rskip_170_activation.
@Test
public void eventLoggerLogPeginBtc_after_rskip_170_activation() throws Exception {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP146)).thenReturn(true);
when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(true);
BridgeEventLogger mockedEventLogger = mock(BridgeEventLogger.class);
BridgeStorageProvider mockBridgeStorageProvider = mock(BridgeStorageProvider.class);
when(mockBridgeStorageProvider.getHeightIfBtcTxhashIsAlreadyProcessed(any(Sha256Hash.class))).thenReturn(Optional.empty());
LockWhitelist lockWhitelist = mock(LockWhitelist.class);
when(lockWhitelist.isWhitelistedFor(any(Address.class), any(Coin.class), any(int.class))).thenReturn(true);
when(mockBridgeStorageProvider.getLockWhitelist()).thenReturn(lockWhitelist);
when(mockBridgeStorageProvider.getNewFederation()).thenReturn(bridgeConstants.getGenesisFederation());
Block executionBlock = mock(Block.class);
BtcBlockStoreWithCache.Factory btcBlockStoreFactory = mock(BtcBlockStoreWithCache.Factory.class);
BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
when(btcBlockStoreFactory.newInstance(any(Repository.class), any(), any(), any())).thenReturn(btcBlockStore);
// Create transaction
Coin lockValue = Coin.COIN;
BtcTransaction tx = new BtcTransaction(bridgeConstants.getBtcParams());
tx.addOutput(lockValue, mockBridgeStorageProvider.getNewFederation().getAddress());
BtcECKey srcKey = new BtcECKey();
tx.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey));
// Create header and PMT
byte[] bits = new byte[1];
bits[0] = 0x3f;
List<Sha256Hash> hashes = new ArrayList<>();
hashes.add(tx.getHash());
PartialMerkleTree pmt = new PartialMerkleTree(bridgeConstants.getBtcParams(), bits, hashes, 1);
Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(new ArrayList<>());
co.rsk.bitcoinj.core.BtcBlock btcBlock = new co.rsk.bitcoinj.core.BtcBlock(bridgeConstants.getBtcParams(), 1, PegTestUtils.createHash(), merkleRoot, 1, 1, 1, new ArrayList<>());
int height = 1;
mockChainOfStoredBlocks(btcBlockStore, btcBlock, height + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(), height);
BridgeSupport bridgeSupport = bridgeSupportBuilder.withBridgeConstants(bridgeConstants).withProvider(mockBridgeStorageProvider).withEventLogger(mockedEventLogger).withBtcLockSenderProvider(new BtcLockSenderProvider()).withPeginInstructionsProvider(new PeginInstructionsProvider()).withExecutionBlock(executionBlock).withBtcBlockStoreFactory(btcBlockStoreFactory).withActivations(activations).build();
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx.bitcoinSerialize(), height, pmt.bitcoinSerialize());
verify(mockedEventLogger, never()).logLockBtc(any(RskAddress.class), any(BtcTransaction.class), any(Address.class), any(Coin.class));
verify(mockedEventLogger, atLeastOnce()).logPeginBtc(any(RskAddress.class), any(BtcTransaction.class), any(Coin.class), anyInt());
}
Aggregations