use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTest method eventLoggerLogPeginRejectionEvents_after_rskip_181_activation.
@Test
public void eventLoggerLogPeginRejectionEvents_after_rskip_181_activation() throws Exception {
ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
when(activations.isActive(ConsensusRule.RSKIP170)).thenReturn(true);
when(activations.isActive(ConsensusRule.RSKIP181)).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);
BtcLockSenderProvider btcLockSenderProvider = mock(BtcLockSenderProvider.class);
when(btcLockSenderProvider.tryGetBtcLockSender(any(BtcTransaction.class))).thenReturn(Optional.empty());
PeginInstructionsProvider peginInstructionsProvider = mock(PeginInstructionsProvider.class);
when(peginInstructionsProvider.buildPeginInstructions(any(BtcTransaction.class))).thenReturn(Optional.empty());
BridgeSupport bridgeSupport = bridgeSupportBuilder.withBridgeConstants(bridgeConstants).withProvider(mockBridgeStorageProvider).withEventLogger(mockedEventLogger).withBtcLockSenderProvider(btcLockSenderProvider).withPeginInstructionsProvider(new PeginInstructionsProvider()).withExecutionBlock(executionBlock).withBtcBlockStoreFactory(btcBlockStoreFactory).withActivations(activations).build();
bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx.bitcoinSerialize(), height, pmt.bitcoinSerialize());
verify(mockedEventLogger, atLeastOnce()).logRejectedPegin(any(BtcTransaction.class), any(RejectedPeginReason.class));
verify(mockedEventLogger, atLeastOnce()).logUnrefundablePegin(any(BtcTransaction.class), any(UnrefundablePeginReason.class));
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method removeLockWhitelistAddress_ok.
@Test
public void removeLockWhitelistAddress_ok() {
Transaction mockedTx = mock(Transaction.class);
byte[] senderBytes = ECKey.fromPublicOnly(Hex.decode(// Public key hex of the authorized whitelist admin in regtest, taken from BridgeRegTestConstants
"04641fb250d7ca7a1cb4f530588e978013038ec4294d084d248869dd54d98873e45c61d00ceeaeeb9e35eab19fa5fbd8f07cb8a5f0ddba26b4d4b18349c09199ad")).getAddress();
RskAddress sender = new RskAddress(senderBytes);
when(mockedTx.getSender()).thenReturn(sender);
LockWhitelist mockedWhitelist = mock(LockWhitelist.class);
BridgeSupport bridgeSupport = getBridgeSupportWithMocksForWhitelistTests(mockedWhitelist);
when(mockedWhitelist.remove(any(Address.class))).then((InvocationOnMock m) -> {
Address address = m.<Address>getArgument(0);
Assert.assertEquals("mwKcYS3H8FUgrPtyGMv3xWvf4jgeZUkCYN", address.toBase58());
return true;
});
Assert.assertEquals(1, bridgeSupport.removeLockWhitelistAddress(mockedTx, "mwKcYS3H8FUgrPtyGMv3xWvf4jgeZUkCYN").intValue());
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method removeLockWhitelistAddress_removeFails.
@Test
public void removeLockWhitelistAddress_removeFails() {
Transaction mockedTx = mock(Transaction.class);
byte[] senderBytes = ECKey.fromPublicOnly(Hex.decode(// Public key hex of the authorized whitelist admin in regtest, taken from BridgeRegTestConstants
"04641fb250d7ca7a1cb4f530588e978013038ec4294d084d248869dd54d98873e45c61d00ceeaeeb9e35eab19fa5fbd8f07cb8a5f0ddba26b4d4b18349c09199ad")).getAddress();
RskAddress sender = new RskAddress(senderBytes);
when(mockedTx.getSender()).thenReturn(sender);
LockWhitelist mockedWhitelist = mock(LockWhitelist.class);
BridgeSupport bridgeSupport = getBridgeSupportWithMocksForWhitelistTests(mockedWhitelist);
when(mockedWhitelist.remove(any(Address.class))).then((InvocationOnMock m) -> {
Address address = m.<Address>getArgument(0);
Assert.assertEquals("mwKcYS3H8FUgrPtyGMv3xWvf4jgeZUkCYN", address.toBase58());
return false;
});
Assert.assertEquals(-1, bridgeSupport.removeLockWhitelistAddress(mockedTx, "mwKcYS3H8FUgrPtyGMv3xWvf4jgeZUkCYN").intValue());
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method removeLockWhitelistAddress_invalidAddress.
@Test
public void removeLockWhitelistAddress_invalidAddress() {
Transaction mockedTx = mock(Transaction.class);
byte[] senderBytes = ECKey.fromPublicOnly(Hex.decode(// Public key hex of the authorized whitelist admin in regtest, taken from BridgeRegTestConstants
"04641fb250d7ca7a1cb4f530588e978013038ec4294d084d248869dd54d98873e45c61d00ceeaeeb9e35eab19fa5fbd8f07cb8a5f0ddba26b4d4b18349c09199ad")).getAddress();
RskAddress sender = new RskAddress(senderBytes);
when(mockedTx.getSender()).thenReturn(sender);
LockWhitelist mockedWhitelist = mock(LockWhitelist.class);
BridgeSupport bridgeSupport = getBridgeSupportWithMocksForWhitelistTests(mockedWhitelist);
Assert.assertEquals(-2, bridgeSupport.removeLockWhitelistAddress(mockedTx, "i-am-invalid").intValue());
verify(mockedWhitelist, never()).remove(any());
}
use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.
the class BridgeSupportTestPowerMock method getLockWhitelistMethods.
@Test
public void getLockWhitelistMethods() {
NetworkParameters parameters = NetworkParameters.fromID(NetworkParameters.ID_REGTEST);
LockWhitelist mockedWhitelist = mock(LockWhitelist.class);
when(mockedWhitelist.getSize()).thenReturn(4);
List<LockWhitelistEntry> entries = Arrays.stream(new Integer[] { 2, 3, 4, 5 }).map(i -> new UnlimitedWhiteListEntry(new Address(parameters, BtcECKey.fromPrivate(BigInteger.valueOf(i)).getPubKeyHash()))).collect(Collectors.toList());
when(mockedWhitelist.getAll()).thenReturn(entries);
for (int i = 0; i < 4; i++) {
when(mockedWhitelist.get(entries.get(i).address())).thenReturn(entries.get(i));
}
BridgeSupport bridgeSupport = getBridgeSupportWithMocksForWhitelistTests(mockedWhitelist);
Assert.assertEquals(4, bridgeSupport.getLockWhitelistSize().intValue());
Assert.assertNull(bridgeSupport.getLockWhitelistEntryByIndex(-1));
Assert.assertNull(bridgeSupport.getLockWhitelistEntryByIndex(4));
Assert.assertNull(bridgeSupport.getLockWhitelistEntryByIndex(5));
Assert.assertNull(bridgeSupport.getLockWhitelistEntryByAddress(new Address(parameters, BtcECKey.fromPrivate(BigInteger.valueOf(-1)).getPubKeyHash()).toBase58()));
for (int i = 0; i < 4; i++) {
Assert.assertEquals(entries.get(i), bridgeSupport.getLockWhitelistEntryByIndex(i));
Assert.assertEquals(entries.get(i), bridgeSupport.getLockWhitelistEntryByAddress(entries.get(i).address().toBase58()));
}
}
Aggregations