Search in sources :

Example 6 with LockWhitelist

use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.

the class BridgeSupportTestPowerMock method setLockWhitelistDisableBlockDelay_overflow.

@Test
public void setLockWhitelistDisableBlockDelay_overflow() throws IOException, BlockStoreException {
    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);
    when(mockedWhitelist.isDisableBlockSet()).thenReturn(false);
    int bestChainHeight = (Integer.MAX_VALUE / 2) + 2;
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    StoredBlock storedBlock = mock(StoredBlock.class);
    when(storedBlock.getHeight()).thenReturn(bestChainHeight);
    when(btcBlockStore.getChainHead()).thenReturn(storedBlock);
    BtcBlock btcBlock = mock(BtcBlock.class);
    doReturn(Sha256Hash.of(Hex.decode("aa"))).when(btcBlock).getHash();
    doReturn(btcBlock).when(storedBlock).getHeader();
    BridgeSupport bridgeSupport = getBridgeSupportWithMocksAndBtcBlockstoreForWhitelistTests(mockedWhitelist, btcBlockStore);
    BigInteger disableBlockDelayBI = BigInteger.valueOf(Integer.MAX_VALUE / 2);
    Assert.assertEquals(-2, bridgeSupport.setLockWhitelistDisableBlockDelay(mockedTx, disableBlockDelayBI).intValue());
    verify(mockedWhitelist, never()).put(any(), any());
}
Also used : LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) InternalTransaction(org.ethereum.vm.program.InternalTransaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with LockWhitelist

use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.

the class BridgeSupportTestPowerMock method setLockWhitelistDisableBlockDelay_ok.

@Test
public void setLockWhitelistDisableBlockDelay_ok() throws IOException, BlockStoreException {
    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);
    when(mockedWhitelist.isDisableBlockSet()).thenReturn(false);
    int bestChainHeight = 10;
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    StoredBlock storedBlock = mock(StoredBlock.class);
    when(storedBlock.getHeight()).thenReturn(bestChainHeight);
    BtcBlock btcBlock = mock(BtcBlock.class);
    doReturn(Sha256Hash.of(Hex.decode("aa"))).when(btcBlock).getHash();
    doReturn(btcBlock).when(storedBlock).getHeader();
    when(btcBlockStore.getChainHead()).thenReturn(storedBlock);
    BridgeSupport bridgeSupport = getBridgeSupportWithMocksAndBtcBlockstoreForWhitelistTests(mockedWhitelist, btcBlockStore);
    BigInteger disableBlockDelayBI = BigInteger.valueOf(100);
    Assert.assertEquals(1, bridgeSupport.setLockWhitelistDisableBlockDelay(mockedTx, disableBlockDelayBI).intValue());
    verify(mockedWhitelist, times(1)).setDisableBlockHeight(disableBlockDelayBI.intValue() + bestChainHeight);
}
Also used : LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) InternalTransaction(org.ethereum.vm.program.InternalTransaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with LockWhitelist

use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.

the class BridgeSupportTestPowerMock method setLockWhitelistDisableBlockDelay_disabled.

@Test
public void setLockWhitelistDisableBlockDelay_disabled() throws IOException, BlockStoreException {
    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);
    when(mockedWhitelist.isDisableBlockSet()).thenReturn(true);
    BridgeSupport bridgeSupport = getBridgeSupportWithMocksForWhitelistTests(mockedWhitelist);
    BigInteger disableBlockDelayBI = BigInteger.valueOf(100);
    Assert.assertEquals(-1, bridgeSupport.setLockWhitelistDisableBlockDelay(mockedTx, disableBlockDelayBI).intValue());
    verify(mockedWhitelist, never()).put(any(), any());
}
Also used : LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) InternalTransaction(org.ethereum.vm.program.InternalTransaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with LockWhitelist

use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.

the class BridgeSupportTestPowerMock method addLockWhitelistAddress_invalidAddress.

@Test
public void addLockWhitelistAddress_invalidAddress() throws IOException {
    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.addOneOffLockWhitelistAddress(mockedTx, "i-am-invalid", BigInteger.valueOf(Coin.COIN.getValue())).intValue());
    verify(mockedWhitelist, never()).put(any(), 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 10 with LockWhitelist

use of co.rsk.peg.whitelist.LockWhitelist in project rskj by rsksmart.

the class BridgeSupportTest method assertLockingCap.

private void assertLockingCap(boolean shouldLock, boolean isLockingCapEnabled, Coin lockingCap, Coin amountSentToNewFed, Coin amountSentToOldFed, Coin amountInNewFed, Coin amountInOldFed) throws BlockStoreException, IOException, BridgeIllegalArgumentException {
    // Configure if locking cap should be evaluated
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP134)).thenReturn(isLockingCapEnabled);
    BridgeConstants bridgeConstants = mock(BridgeConstants.class);
    when(bridgeConstants.getLegacyMinimumPeginTxValueInSatoshis()).thenReturn(Coin.SATOSHI);
    when(bridgeConstants.getBtcParams()).thenReturn(BridgeRegTestConstants.getInstance().getBtcParams());
    when(bridgeConstants.getBtc2RskMinimumAcceptableConfirmations()).thenReturn(1);
    when(bridgeConstants.getGenesisFeePerKb()).thenReturn(BridgeRegTestConstants.getInstance().getGenesisFeePerKb());
    when(bridgeConstants.getMaxRbtc()).thenReturn(BridgeRegTestConstants.getInstance().getMaxRbtc());
    // Configure locking cap
    when(bridgeConstants.getInitialLockingCap()).thenReturn(lockingCap);
    Repository repository = createRepository();
    Federation oldFederation = this.getFederation(bridgeConstants);
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants, activations);
    // We need a random new fed
    provider.setNewFederation(this.getFederation(bridgeConstants, Arrays.asList(BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03")))));
    // Use genesis fed as old
    provider.setOldFederation(oldFederation);
    Coin currentFunds = Coin.ZERO;
    // Configure existing utxos in both federations
    if (amountInOldFed != null) {
        UTXO utxo = new UTXO(Sha256Hash.wrap(HashUtil.randomHash()), 0, amountInOldFed, 1, false, new Script(new byte[] {}));
        provider.getOldFederationBtcUTXOs().add(utxo);
        currentFunds = currentFunds.add(amountInOldFed);
    }
    if (amountInNewFed != null) {
        UTXO utxo = new UTXO(Sha256Hash.wrap(HashUtil.randomHash()), 0, amountInNewFed, 1, false, new Script(new byte[] {}));
        provider.getNewFederationBtcUTXOs().add(utxo);
        currentFunds = currentFunds.add(amountInNewFed);
    }
    // Fund bridge in RBTC, substracting the funds that we are indicating were locked in the federations (which means a user locked)
    co.rsk.core.Coin bridgeBalance = LIMIT_MONETARY_BASE.subtract(co.rsk.core.Coin.fromBitcoin(currentFunds));
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, bridgeBalance);
    // The locking cap shouldn't be surpassed by the initial configuration
    Assert.assertFalse(isLockingCapEnabled && currentFunds.isGreaterThan(lockingCap));
    Coin lockValue = Coin.ZERO;
    int newUtxos = 0;
    // Create transaction setting the outputs to the configured values
    BtcTransaction tx = new BtcTransaction(bridgeConstants.getBtcParams());
    if (amountSentToOldFed != null) {
        tx.addOutput(amountSentToOldFed, provider.getOldFederation().getAddress());
        lockValue = lockValue.add(amountSentToOldFed);
        newUtxos++;
    }
    if (amountSentToNewFed != null) {
        tx.addOutput(amountSentToNewFed, provider.getNewFederation().getAddress());
        lockValue = lockValue.add(amountSentToNewFed);
        newUtxos++;
    }
    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 registerHeader = new co.rsk.bitcoinj.core.BtcBlock(bridgeConstants.getBtcParams(), 1, PegTestUtils.createHash(1), merkleRoot, 1, 1, 1, new ArrayList<BtcTransaction>());
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(repository, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
    Block executionBlock = Mockito.mock(Block.class);
    when(executionBlock.getNumber()).thenReturn(10L);
    // Get the tx sender public key
    byte[] data = tx.getInput(0).getScriptSig().getChunks().get(1).data;
    BtcECKey senderBtcKey = BtcECKey.fromPublicOnly(data);
    // Whitelist the addresses
    LockWhitelist whitelist = provider.getLockWhitelist();
    Address address = senderBtcKey.toAddress(bridgeConstants.getBtcParams());
    whitelist.put(address, new OneOffWhiteListEntry(address, lockValue));
    // The address is whitelisted
    Assert.assertThat(whitelist.isWhitelisted(address), is(true));
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, new BtcLockSenderProvider(), new PeginInstructionsProvider(), executionBlock, mockFactory, activations);
    // Simulate blockchain
    int height = 1;
    mockChainOfStoredBlocks(btcBlockStore, registerHeader, height + bridgeConstants.getBtc2RskMinimumAcceptableConfirmations(), height);
    Transaction rskTx = mock(Transaction.class);
    Keccak256 hash = new Keccak256(HashUtil.keccak256(new byte[] {}));
    when(rskTx.getHash()).thenReturn(hash);
    // Try to register tx
    bridgeSupport.registerBtcTransaction(rskTx, tx.bitcoinSerialize(), height, pmt.bitcoinSerialize());
    bridgeSupport.save();
    // If the address is no longer whitelisted, it means it was consumed, whether the lock was rejected by lockingCap or not
    Assert.assertThat(whitelist.isWhitelisted(address), is(false));
    // The Btc transaction should have been processed
    assertTrue(bridgeSupport.isBtcTxHashAlreadyProcessed(tx.getHash()));
    co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(shouldLock ? lockValue : Coin.ZERO);
    RskAddress srcKeyRskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey.getPrivKey()).getAddress());
    // Verify amount was locked
    Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(srcKeyRskAddress));
    Assert.assertEquals(bridgeBalance.subtract(totalAmountExpectedToHaveBeenLocked), repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
    if (!shouldLock) {
        // Release tx should have been created directly to the signatures stack
        BtcTransaction releaseTx = provider.getReleaseTransactionSet().getEntries().iterator().next().getTransaction();
        Assert.assertNotNull(releaseTx);
        // returns the funds to the sender
        Assert.assertEquals(1, releaseTx.getOutputs().size());
        Assert.assertEquals(address, releaseTx.getOutputs().get(0).getAddressFromP2PKHScript(bridgeConstants.getBtcParams()));
        Assert.assertEquals(lockValue, releaseTx.getOutputs().get(0).getValue().add(releaseTx.getFee()));
        // Uses the same UTXO(s)
        Assert.assertEquals(newUtxos, releaseTx.getInputs().size());
        for (int i = 0; i < newUtxos; i++) {
            TransactionInput input = releaseTx.getInput(i);
            Assert.assertEquals(tx.getHash(), input.getOutpoint().getHash());
            Assert.assertEquals(i, input.getOutpoint().getIndex());
        }
    }
}
Also used : RskAddress(co.rsk.core.RskAddress) BtcLockSenderProvider(co.rsk.peg.btcLockSender.BtcLockSenderProvider) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation) PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation(co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation) RskAddress(co.rsk.core.RskAddress) BridgeConstants(co.rsk.config.BridgeConstants) Script(co.rsk.bitcoinj.script.Script) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) Keccak256(co.rsk.crypto.Keccak256) 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) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry)

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