Search in sources :

Example 31 with LockWhitelist

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));
}
Also used : RskAddress(co.rsk.core.RskAddress) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) InternalTransaction(org.ethereum.vm.program.InternalTransaction) Transaction(org.ethereum.core.Transaction) Block(org.ethereum.core.Block) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 32 with LockWhitelist

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());
}
Also used : LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) InternalTransaction(org.ethereum.vm.program.InternalTransaction) RskAddress(co.rsk.core.RskAddress) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskAddress(co.rsk.core.RskAddress) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 33 with LockWhitelist

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());
}
Also used : LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) InternalTransaction(org.ethereum.vm.program.InternalTransaction) RskAddress(co.rsk.core.RskAddress) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskAddress(co.rsk.core.RskAddress) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 34 with LockWhitelist

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());
}
Also used : LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) InternalTransaction(org.ethereum.vm.program.InternalTransaction) RskAddress(co.rsk.core.RskAddress) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 35 with LockWhitelist

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()));
    }
}
Also used : OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) RskAddress(co.rsk.core.RskAddress) TxSenderAddressType(co.rsk.peg.btcLockSender.BtcLockSender.TxSenderAddressType) Keccak256(co.rsk.crypto.Keccak256) ByteBuffer(java.nio.ByteBuffer) SecureRandom(java.security.SecureRandom) Assert.assertThat(org.junit.Assert.assertThat) ActivationConfig(org.ethereum.config.blockchain.upgrades.ActivationConfig) TransactionSignature(co.rsk.bitcoinj.crypto.TransactionSignature) MutableTrieCache(co.rsk.db.MutableTrieCache) RegTestParams(co.rsk.bitcoinj.params.RegTestParams) Is.is(org.hamcrest.core.Is.is) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) PrecompiledContracts(org.ethereum.vm.PrecompiledContracts) BigInteger(java.math.BigInteger) MerkleBranch(co.rsk.peg.bitcoin.MerkleBranch) Wallet(co.rsk.bitcoinj.wallet.Wallet) BridgeTestNetConstants(co.rsk.config.BridgeTestNetConstants) MutableTrieImpl(co.rsk.db.MutableTrieImpl) BlockGenerator(co.rsk.blockchain.utils.BlockGenerator) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) MutableRepository(org.ethereum.db.MutableRepository) SimpleBlockChain(co.rsk.peg.simples.SimpleBlockChain) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) ScriptBuilder(co.rsk.bitcoinj.script.ScriptBuilder) Script(co.rsk.bitcoinj.script.Script) DigestOutputStream(java.security.DigestOutputStream) Stream(java.util.stream.Stream) BridgeEventLogger(co.rsk.peg.utils.BridgeEventLogger) org.ethereum.core(org.ethereum.core) BlockDifficulty(co.rsk.core.BlockDifficulty) Whitebox(org.powermock.reflect.Whitebox) java.util(java.util) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry) MessageDigest(java.security.MessageDigest) RunWith(org.junit.runner.RunWith) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) BigIntegers(org.bouncycastle.util.BigIntegers) Hex(org.bouncycastle.util.encoders.Hex) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) BridgeConstants(co.rsk.config.BridgeConstants) BridgeRegTestConstants(co.rsk.config.BridgeRegTestConstants) PowerMockRunner(org.powermock.modules.junit4.PowerMockRunner) PowerMockito(org.powermock.api.mockito.PowerMockito) Before(org.junit.Before) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) BlockChainBuilder(co.rsk.test.builders.BlockChainBuilder) HashUtil(org.ethereum.crypto.HashUtil) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) Program(org.ethereum.vm.program.Program) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) Mockito(org.mockito.Mockito) BlockStoreException(co.rsk.bitcoinj.store.BlockStoreException) Matchers.hasItem(org.hamcrest.Matchers.hasItem) java.io(java.io) InternalTransaction(org.ethereum.vm.program.InternalTransaction) BtcLockSender(co.rsk.peg.btcLockSender.BtcLockSender) SimpleWallet(co.rsk.peg.simples.SimpleWallet) Assert(org.junit.Assert) Trie(co.rsk.trie.Trie) Constants(org.ethereum.config.Constants) ECKey(org.ethereum.crypto.ECKey) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) RskAddress(co.rsk.core.RskAddress) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

LockWhitelist (co.rsk.peg.whitelist.LockWhitelist)41 RskAddress (co.rsk.core.RskAddress)38 Test (org.junit.Test)36 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)34 InternalTransaction (org.ethereum.vm.program.InternalTransaction)33 MutableRepository (org.ethereum.db.MutableRepository)20 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)20 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)19 Repository (org.ethereum.core.Repository)19 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)18 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)17 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)16 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)16 OneOffWhiteListEntry (co.rsk.peg.whitelist.OneOffWhiteListEntry)16 Block (org.ethereum.core.Block)16 Transaction (org.ethereum.core.Transaction)16 BigInteger (java.math.BigInteger)15 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)9 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)9 ECKey (org.ethereum.crypto.ECKey)9