Search in sources :

Example 11 with OneOffWhiteListEntry

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

the class BridgeSupportTest method when_registerBtcTransaction_usesLegacyType_afterFork_lock_and_no_refund.

@Test
public void when_registerBtcTransaction_usesLegacyType_afterFork_lock_and_no_refund() throws Exception {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
    Federation federation1 = getFederation(bridgeConstants);
    Repository repository = createRepository();
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
    Block executionBlock = mock(Block.class);
    when(executionBlock.getNumber()).thenReturn(10L);
    BtcECKey srcKey1 = new BtcECKey();
    ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
    Address btcAddress = srcKey1.toAddress(btcParams);
    RskAddress rskAddress = new RskAddress(key.getAddress());
    // First transaction goes only to the first federation
    BtcTransaction tx1 = new BtcTransaction(btcParams);
    tx1.addOutput(Coin.COIN.multiply(5), federation1.getAddress());
    tx1.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey1));
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, contractAddress, bridgeConstants, activations);
    provider.setNewFederation(federation1);
    // Whitelist the addresses
    LockWhitelist whitelist = provider.getLockWhitelist();
    whitelist.put(btcAddress, new OneOffWhiteListEntry(btcAddress, Coin.COIN.multiply(5)));
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(repository, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
    BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(TxSenderAddressType.P2PKH, btcAddress, rskAddress);
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, new PeginInstructionsProvider(), executionBlock, mockFactory, activations);
    byte[] bits = new byte[1];
    bits[0] = 0x3f;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(tx1.getHash());
    PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 1);
    List<Sha256Hash> hashlist = new ArrayList<>();
    Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
    co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), merkleRoot, 1, 1, 1, new ArrayList<>());
    int height = 30;
    mockChainOfStoredBlocks(btcBlockStore, registerHeader, 35, height);
    bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmt.bitcoinSerialize());
    co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(Coin.valueOf(5, 0));
    Assert.assertThat(whitelist.isWhitelisted(btcAddress), is(false));
    Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskAddress));
    Assert.assertEquals(LIMIT_MONETARY_BASE.subtract(totalAmountExpectedToHaveBeenLocked), repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
    Assert.assertEquals(1, provider.getNewFederationBtcUTXOs().size());
    Assert.assertEquals(Coin.COIN.multiply(5), provider.getNewFederationBtcUTXOs().get(0).getValue());
    Assert.assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
    Assert.assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
    Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
    Assert.assertTrue(provider.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash()).isPresent());
}
Also used : RskAddress(co.rsk.core.RskAddress) ECKey(org.ethereum.crypto.ECKey) 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) 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) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 12 with OneOffWhiteListEntry

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

the class BridgeSupportTest method registerBtcTransaction_accepts_lock_tx_with_witness_after_rskip_143_activation.

@Test
public void registerBtcTransaction_accepts_lock_tx_with_witness_after_rskip_143_activation() throws BlockStoreException, IOException, BridgeIllegalArgumentException {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
    Federation federation1 = getFederation(bridgeConstants);
    Repository repository = createRepository();
    BtcTransaction tx1 = new BtcTransaction(btcParams);
    BtcECKey srcKey1 = new BtcECKey();
    ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
    Address btcAddress = srcKey1.toAddress(btcParams);
    RskAddress rskAddress = new RskAddress(key.getAddress());
    Coin amountToLock = Coin.COIN.multiply(10);
    tx1.addOutput(amountToLock, federation1.getAddress());
    tx1.addInput(PegTestUtils.createHash(1), 0, new Script(new byte[] {}));
    TransactionWitness txWit = new TransactionWitness(1);
    txWit.setPush(0, new byte[] {});
    tx1.setWitness(0, txWit);
    byte[] bits = new byte[1];
    bits[0] = 0x3f;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(tx1.getHash());
    PartialMerkleTree pmtWithoutWitness = new PartialMerkleTree(btcParams, bits, hashes, 1);
    List<Sha256Hash> hashlist = new ArrayList<>();
    Sha256Hash blockMerkleRoot = pmtWithoutWitness.getTxnHashAndMerkleRoot(hashlist);
    co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), blockMerkleRoot, 1, 1, 1, new ArrayList<>());
    List<Sha256Hash> hashes2 = new ArrayList<>();
    hashes2.add(tx1.getHash(true));
    PartialMerkleTree pmtWithWitness = new PartialMerkleTree(btcParams, bits, hashes2, 1);
    List<Sha256Hash> hashlist2 = new ArrayList<>();
    Sha256Hash witnessMerkleRoot = pmtWithWitness.getTxnHashAndMerkleRoot(hashlist2);
    int height = 50;
    StoredBlock block = new StoredBlock(registerHeader, new BigInteger("0"), height);
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    co.rsk.bitcoinj.core.BtcBlock headBlock = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(2), Sha256Hash.of(new byte[] { 1 }), 1, 1, 1, new ArrayList<>());
    StoredBlock chainHead = new StoredBlock(headBlock, new BigInteger("0"), 132);
    when(btcBlockStore.getChainHead()).thenReturn(chainHead);
    when(btcBlockStore.getStoredBlockAtMainChainHeight(block.getHeight())).thenReturn(block);
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(any(), any(), any(), any())).thenReturn(btcBlockStore);
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, contractAddress, bridgeConstants, activations);
    provider.setNewFederation(federation1);
    // Whitelist the addresses
    LockWhitelist whitelist = provider.getLockWhitelist();
    whitelist.put(btcAddress, new OneOffWhiteListEntry(btcAddress, Coin.COIN.multiply(10)));
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, getBtcLockSenderProvider(TxSenderAddressType.P2SHP2WPKH, btcAddress, rskAddress), new PeginInstructionsProvider(), mock(Block.class), mockFactory, activations);
    CoinbaseInformation coinbaseInformation = new CoinbaseInformation(witnessMerkleRoot);
    provider.setCoinbaseInformation(registerHeader.getHash(), coinbaseInformation);
    bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmtWithWitness.bitcoinSerialize());
    co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(amountToLock);
    Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskAddress));
    Assert.assertEquals(1, provider.getNewFederationBtcUTXOs().size());
    Assert.assertEquals(amountToLock, provider.getNewFederationBtcUTXOs().get(0).getValue());
    Assert.assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
    Assert.assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
    Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
    Assert.assertTrue(provider.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash(false)).isPresent());
}
Also used : RskAddress(co.rsk.core.RskAddress) ECKey(org.ethereum.crypto.ECKey) 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) Script(co.rsk.bitcoinj.script.Script) PeginInstructionsProvider(co.rsk.peg.pegininstructions.PeginInstructionsProvider) CoinbaseInformation(co.rsk.peg.bitcoin.CoinbaseInformation) 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) BigInteger(java.math.BigInteger) Block(org.ethereum.core.Block) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 13 with OneOffWhiteListEntry

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

the class BridgeSupportTest method when_registerBtcTransaction_usesLegacyType_beforeFork_lock_and_no_refund.

@Test
public void when_registerBtcTransaction_usesLegacyType_beforeFork_lock_and_no_refund() throws Exception {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(false);
    Federation federation1 = getFederation(bridgeConstants);
    Repository repository = createRepository();
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
    Block executionBlock = mock(Block.class);
    when(executionBlock.getNumber()).thenReturn(10L);
    BtcECKey srcKey1 = new BtcECKey();
    ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
    Address btcAddress = srcKey1.toAddress(btcParams);
    RskAddress rskAddress = new RskAddress(key.getAddress());
    Coin amountToLock = Coin.COIN.multiply(5);
    // First transaction goes only to the first federation
    BtcTransaction tx1 = new BtcTransaction(btcParams);
    tx1.addOutput(amountToLock, federation1.getAddress());
    tx1.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey1));
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, contractAddress, bridgeConstants, activations);
    provider.setNewFederation(federation1);
    // Whitelist the addresses
    LockWhitelist whitelist = provider.getLockWhitelist();
    whitelist.put(btcAddress, new OneOffWhiteListEntry(btcAddress, Coin.COIN.multiply(5)));
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(repository, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
    BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(TxSenderAddressType.P2PKH, btcAddress, rskAddress);
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, new PeginInstructionsProvider(), executionBlock, mockFactory, activations);
    byte[] bits = new byte[1];
    bits[0] = 0x3f;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(tx1.getHash());
    PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 1);
    List<Sha256Hash> hashlist = new ArrayList<>();
    Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
    co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), merkleRoot, 1, 1, 1, new ArrayList<>());
    int height = 30;
    mockChainOfStoredBlocks(btcBlockStore, registerHeader, 35, height);
    bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmt.bitcoinSerialize());
    // Assert
    co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(amountToLock);
    Assert.assertThat(whitelist.isWhitelisted(btcAddress), is(false));
    Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskAddress));
    Assert.assertEquals(LIMIT_MONETARY_BASE.subtract(totalAmountExpectedToHaveBeenLocked), repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
    Assert.assertEquals(1, provider.getNewFederationBtcUTXOs().size());
    Assert.assertEquals(amountToLock, provider.getNewFederationBtcUTXOs().get(0).getValue());
    Assert.assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
    Assert.assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
    Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
    Assert.assertTrue(provider.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash()).isPresent());
}
Also used : RskAddress(co.rsk.core.RskAddress) ECKey(org.ethereum.crypto.ECKey) 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) 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) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 14 with OneOffWhiteListEntry

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

the class BridgeSupportTest method when_registerBtcTransaction_usesSegCompatibilityType_afterFork_lock_and_no_refund.

@Test
public void when_registerBtcTransaction_usesSegCompatibilityType_afterFork_lock_and_no_refund() throws Exception {
    ActivationConfig.ForBlock activations = mock(ActivationConfig.ForBlock.class);
    when(activations.isActive(ConsensusRule.RSKIP143)).thenReturn(true);
    Federation federation1 = getFederation(bridgeConstants);
    Repository repository = createRepository();
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
    Block executionBlock = mock(Block.class);
    when(executionBlock.getNumber()).thenReturn(10L);
    // First transaction goes only to the first federation
    BtcTransaction tx1 = new BtcTransaction(btcParams);
    BtcECKey srcKey1 = new BtcECKey();
    ECKey key = ECKey.fromPublicOnly(srcKey1.getPubKey());
    Address btcAddress = srcKey1.toAddress(btcParams);
    RskAddress rskAddress = new RskAddress(key.getAddress());
    Coin amountToLock = Coin.COIN.multiply(5);
    tx1.addOutput(amountToLock, federation1.getAddress());
    tx1.addInput(PegTestUtils.createHash(1), 0, ScriptBuilder.createInputScript(null, srcKey1));
    BtcBlockStoreWithCache btcBlockStore = mock(BtcBlockStoreWithCache.class);
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, contractAddress, bridgeConstants, activations);
    provider.setNewFederation(federation1);
    // Whitelist the addresses
    LockWhitelist whitelist = provider.getLockWhitelist();
    whitelist.put(btcAddress, new OneOffWhiteListEntry(btcAddress, amountToLock));
    BtcBlockStoreWithCache.Factory mockFactory = mock(BtcBlockStoreWithCache.Factory.class);
    when(mockFactory.newInstance(repository, bridgeConstants, provider, activations)).thenReturn(btcBlockStore);
    BtcLockSenderProvider btcLockSenderProvider = getBtcLockSenderProvider(TxSenderAddressType.P2SHP2WPKH, btcAddress, rskAddress);
    BridgeSupport bridgeSupport = getBridgeSupport(bridgeConstants, provider, repository, btcLockSenderProvider, new PeginInstructionsProvider(), executionBlock, mockFactory, activations);
    byte[] bits = new byte[1];
    bits[0] = 0x3f;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(tx1.getHash());
    PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 1);
    List<Sha256Hash> hashlist = new ArrayList<>();
    Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
    co.rsk.bitcoinj.core.BtcBlock registerHeader = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(1), merkleRoot, 1, 1, 1, new ArrayList<>());
    int height = 30;
    mockChainOfStoredBlocks(btcBlockStore, registerHeader, 35, height);
    bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1.bitcoinSerialize(), height, pmt.bitcoinSerialize());
    co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = co.rsk.core.Coin.fromBitcoin(Coin.valueOf(5, 0));
    Assert.assertThat(whitelist.isWhitelisted(btcAddress), is(false));
    Assert.assertEquals(totalAmountExpectedToHaveBeenLocked, repository.getBalance(rskAddress));
    Assert.assertEquals(LIMIT_MONETARY_BASE.subtract(totalAmountExpectedToHaveBeenLocked), repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
    Assert.assertEquals(1, provider.getNewFederationBtcUTXOs().size());
    Assert.assertEquals(amountToLock, provider.getNewFederationBtcUTXOs().get(0).getValue());
    Assert.assertEquals(0, provider.getReleaseRequestQueue().getEntries().size());
    Assert.assertEquals(0, provider.getReleaseTransactionSet().getEntries().size());
    Assert.assertTrue(provider.getRskTxsWaitingForSignatures().isEmpty());
    Assert.assertTrue(provider.getHeightIfBtcTxhashIsAlreadyProcessed(tx1.getHash()).isPresent());
}
Also used : RskAddress(co.rsk.core.RskAddress) ECKey(org.ethereum.crypto.ECKey) 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) 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) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) Test(org.junit.Test)

Example 15 with OneOffWhiteListEntry

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

the class BridgeStorageProviderTest method getLockWhitelist_nonNullBytes.

@Test
public void getLockWhitelist_nonNullBytes() {
    List<Integer> calls = new ArrayList<>();
    LockWhitelist whitelistMock = new LockWhitelist(new HashMap<>());
    LockWhitelistEntry oneOffEntry = new OneOffWhiteListEntry(getBtcAddress("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), Coin.COIN);
    LockWhitelistEntry unlimitedEntry = new UnlimitedWhiteListEntry(getBtcAddress("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"));
    whitelistMock.put(oneOffEntry.address(), oneOffEntry);
    whitelistMock.put(unlimitedEntry.address(), unlimitedEntry);
    PowerMockito.mockStatic(BridgeSerializationUtils.class);
    Repository repositoryMock = mock(Repository.class);
    // Overriding Activation to make sure it serializes the unlimited whitelist data
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getNetworkConstants().getBridgeConstants(), activationsAllForks);
    when(repositoryMock.getStorageBytes(any(RskAddress.class), eq(DataWord.valueOf("lockWhitelist".getBytes(StandardCharsets.UTF_8))))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        RskAddress contractAddress = invocation.getArgument(0);
        DataWord address = invocation.getArgument(1);
        // Make sure the bytes are got from the correct address in the repo
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
        Assert.assertEquals(DataWord.valueOf("lockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
        return new byte[] { (byte) 0xaa };
    });
    when(repositoryMock.getStorageBytes(any(RskAddress.class), eq(DataWord.valueOf("unlimitedLockWhitelist".getBytes(StandardCharsets.UTF_8))))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        RskAddress contractAddress = invocation.getArgument(0);
        DataWord address = invocation.getArgument(1);
        // Make sure the bytes are got from the correct address in the repo
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd }, contractAddress.getBytes()));
        Assert.assertEquals(DataWord.valueOf("unlimitedLockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
        return new byte[] { (byte) 0xbb };
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeOneOffLockWhitelistAndDisableBlockHeight(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        byte[] data = invocation.getArgument(0);
        NetworkParameters parameters = invocation.getArgument(1);
        Assert.assertEquals(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), parameters);
        // Make sure we're deserializing what just came from the repo with the correct AddressBasedAuthorizer
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
        HashMap<Address, LockWhitelistEntry> map = new HashMap<>();
        map.put(oneOffEntry.address(), oneOffEntry);
        return Pair.of(map, 0);
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeUnlimitedLockWhitelistEntries(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        byte[] unlimitedData = invocation.getArgument(0);
        NetworkParameters parameters = invocation.getArgument(1);
        Assert.assertEquals(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), parameters);
        // Make sure we're deserializing what just came from the repo with the correct AddressBasedAuthorizer
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xbb }, unlimitedData));
        HashMap<Address, LockWhitelistEntry> map = new HashMap<>();
        map.put(unlimitedEntry.address(), unlimitedEntry);
        return map;
    });
    Assert.assertEquals(whitelistMock.getAll(), storageProvider.getLockWhitelist().getAll());
    // 1 for each call to deserializeFederationOnlyBtcKeys & getStorageBytes (we call getStorageBytes twice)
    Assert.assertEquals(4, calls.size());
}
Also used : UnlimitedWhiteListEntry(co.rsk.peg.whitelist.UnlimitedWhiteListEntry) RskAddress(co.rsk.core.RskAddress) LockWhitelistEntry(co.rsk.peg.whitelist.LockWhitelistEntry) DataWord(org.ethereum.vm.DataWord) BigInteger(java.math.BigInteger) LockWhitelist(co.rsk.peg.whitelist.LockWhitelist) Repository(org.ethereum.core.Repository) MutableRepository(org.ethereum.db.MutableRepository) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskAddress(co.rsk.core.RskAddress) OneOffWhiteListEntry(co.rsk.peg.whitelist.OneOffWhiteListEntry) ActivationConfigsForTest(org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

OneOffWhiteListEntry (co.rsk.peg.whitelist.OneOffWhiteListEntry)20 RskAddress (co.rsk.core.RskAddress)17 Test (org.junit.Test)14 LockWhitelist (co.rsk.peg.whitelist.LockWhitelist)13 ActivationConfigsForTest (org.ethereum.config.blockchain.upgrades.ActivationConfigsForTest)12 MutableRepository (org.ethereum.db.MutableRepository)12 Repository (org.ethereum.core.Repository)11 co.rsk.bitcoinj.core (co.rsk.bitcoinj.core)9 ActivationConfig (org.ethereum.config.blockchain.upgrades.ActivationConfig)9 BigInteger (java.math.BigInteger)8 InternalTransaction (org.ethereum.vm.program.InternalTransaction)8 PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseInputScriptThatSpendsFromTheFederation)7 PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation (co.rsk.peg.PegTestUtils.createBaseRedeemScriptThatSpendsFromTheFederation)7 PeginInstructionsProvider (co.rsk.peg.pegininstructions.PeginInstructionsProvider)7 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)7 Block (org.ethereum.core.Block)7 Transaction (org.ethereum.core.Transaction)7 BtcLockSenderProvider (co.rsk.peg.btcLockSender.BtcLockSenderProvider)6 ECKey (org.ethereum.crypto.ECKey)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6