Search in sources :

Example 6 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class BridgeStorageProviderTest method saveLockWhitelist.

@Test
public void saveLockWhitelist() throws IOException {
    LockWhitelist whitelistMock = mock(LockWhitelist.class);
    List<Integer> storageBytesCalls = new ArrayList<>();
    List<Integer> serializeCalls = new ArrayList<>();
    PowerMockito.mockStatic(BridgeSerializationUtils.class);
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    PowerMockito.when(BridgeSerializationUtils.serializeLockWhitelist(any(LockWhitelist.class))).then((InvocationOnMock invocation) -> {
        LockWhitelist whitelist = invocation.getArgumentAt(0, LockWhitelist.class);
        Assert.assertSame(whitelistMock, whitelist);
        serializeCalls.add(0);
        return Hex.decode("ccdd");
    });
    Mockito.doAnswer((InvocationOnMock invocation) -> {
        storageBytesCalls.add(0);
        RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
        DataWord address = invocation.getArgumentAt(1, DataWord.class);
        byte[] data = invocation.getArgumentAt(2, byte[].class);
        // Make sure the bytes are set to the correct address in the repo and that what's saved is what was serialized
        Assert.assertTrue(Arrays.equals(Hex.decode("aabbccdd"), contractAddress.getBytes()));
        Assert.assertEquals(new DataWord("lockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
        Assert.assertTrue(Arrays.equals(Hex.decode("ccdd"), data));
        return null;
    }).when(repositoryMock).addStorageBytes(any(RskAddress.class), any(DataWord.class), any(byte[].class));
    storageProvider.saveLockWhitelist();
    // Shouldn't have tried to save nor serialize anything
    Assert.assertEquals(0, storageBytesCalls.size());
    Assert.assertEquals(0, serializeCalls.size());
    Whitebox.setInternalState(storageProvider, "lockWhitelist", whitelistMock);
    storageProvider.saveLockWhitelist();
    Assert.assertEquals(1, storageBytesCalls.size());
    Assert.assertEquals(1, serializeCalls.size());
}
Also used : BigInteger(java.math.BigInteger) Repository(org.ethereum.core.Repository) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskAddress(co.rsk.core.RskAddress) DataWord(org.ethereum.vm.DataWord) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class BridgeStorageProviderTest method getFeePerKbElection_withVotes.

@Test
public void getFeePerKbElection_withVotes() {
    AddressBasedAuthorizer authorizerMock = mock(AddressBasedAuthorizer.class);
    Repository repositoryMock = mock(Repository.class);
    when(authorizerMock.getRequiredAuthorizedKeys()).thenReturn(1);
    when(authorizerMock.isAuthorized(any(RskAddress.class))).thenReturn(true);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    byte[] electionFee = new byte[] { 0x43, 0x19 };
    ABICallSpec expectedWinner = new ABICallSpec("setFeePerKb", new byte[][] { electionFee });
    List<RskAddress> voters = new ArrayList<>();
    voters.add(new RskAddress("0000000000000000000000000000000000001321"));
    voters.add(new RskAddress("0000000000000000000000000000000000004049"));
    HashMap<ABICallSpec, List<RskAddress>> electionVotes = new HashMap<>();
    electionVotes.put(expectedWinner, voters);
    byte[] serializedElection = BridgeSerializationUtils.serializeElection(new ABICallElection(authorizerMock, electionVotes));
    when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).thenReturn(serializedElection);
    ABICallElection result = storageProvider.getFeePerKbElection(authorizerMock);
    assertThat(result.getVotes(), is(electionVotes));
    assertThat(result.getWinner(), is(expectedWinner));
}
Also used : DataWord(org.ethereum.vm.DataWord) Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class BridgeStorageProviderTest method getLockWhitelist_nullBytes.

@Test
public void getLockWhitelist_nullBytes() throws IOException {
    List<Integer> calls = new ArrayList<>();
    PowerMockito.mockStatic(BridgeSerializationUtils.class);
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    Context contextMock = mock(Context.class);
    when(contextMock.getParams()).thenReturn(NetworkParameters.fromID(NetworkParameters.ID_REGTEST));
    Whitebox.setInternalState(storageProvider, "btcContext", contextMock);
    when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
        DataWord address = invocation.getArgumentAt(1, DataWord.class);
        // 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(new DataWord("lockWhitelist".getBytes(StandardCharsets.UTF_8)), address);
        return null;
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeLockWhitelist(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        return null;
    });
    LockWhitelist result = storageProvider.getLockWhitelist();
    Assert.assertNotNull(result);
    Assert.assertEquals(0, result.getSize().intValue());
    // 1 for each call to deserializeFederation & getStorageBytes
    Assert.assertEquals(1, calls.size());
}
Also used : BigInteger(java.math.BigInteger) Repository(org.ethereum.core.Repository) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RskAddress(co.rsk.core.RskAddress) DataWord(org.ethereum.vm.DataWord) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class BridgeSupportTest method registerBtcTransactionLockTxWhitelisted.

@Test
public void registerBtcTransactionLockTxWhitelisted() throws BlockStoreException, AddressFormatException, IOException {
    BridgeConstants bridgeConstants = config.getBlockchainConfig().getCommonConstants().getBridgeConstants();
    NetworkParameters parameters = bridgeConstants.getBtcParams();
    List<BtcECKey> federation1Keys = Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(Hex.decode("fa01")), BtcECKey.fromPrivate(Hex.decode("fa02")) });
    federation1Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation federation1 = new Federation(federation1Keys, Instant.ofEpochMilli(1000L), 0L, parameters);
    List<BtcECKey> federation2Keys = Arrays.asList(new BtcECKey[] { BtcECKey.fromPrivate(Hex.decode("fb01")), BtcECKey.fromPrivate(Hex.decode("fb02")), BtcECKey.fromPrivate(Hex.decode("fb03")) });
    federation2Keys.sort(BtcECKey.PUBKEY_COMPARATOR);
    Federation federation2 = new Federation(federation2Keys, Instant.ofEpochMilli(2000L), 0L, parameters);
    Repository repository = new RepositoryImpl(config);
    repository.addBalance(PrecompiledContracts.BRIDGE_ADDR, LIMIT_MONETARY_BASE);
    Block executionBlock = Mockito.mock(Block.class);
    Mockito.when(executionBlock.getNumber()).thenReturn(10L);
    Repository track = repository.startTracking();
    // First transaction goes only to the first federation
    BtcTransaction tx1 = new BtcTransaction(this.btcParams);
    tx1.addOutput(Coin.COIN.multiply(5), federation1.getAddress());
    BtcECKey srcKey1 = new BtcECKey();
    tx1.addInput(PegTestUtils.createHash(), 0, ScriptBuilder.createInputScript(null, srcKey1));
    // Second transaction goes only to the second federation
    BtcTransaction tx2 = new BtcTransaction(this.btcParams);
    tx2.addOutput(Coin.COIN.multiply(10), federation2.getAddress());
    BtcECKey srcKey2 = new BtcECKey();
    tx2.addInput(PegTestUtils.createHash(), 0, ScriptBuilder.createInputScript(null, srcKey2));
    // Third transaction has one output to each federation
    // Lock is expected to be done accordingly and utxos assigned accordingly as well
    BtcTransaction tx3 = new BtcTransaction(this.btcParams);
    tx3.addOutput(Coin.COIN.multiply(2), federation1.getAddress());
    tx3.addOutput(Coin.COIN.multiply(3), federation2.getAddress());
    BtcECKey srcKey3 = new BtcECKey();
    tx3.addInput(PegTestUtils.createHash(), 0, ScriptBuilder.createInputScript(null, srcKey3));
    Context btcContext = new Context(bridgeConstants.getBtcParams());
    BtcBlockStore btcBlockStore = new RepositoryBlockStore(config, track, PrecompiledContracts.BRIDGE_ADDR);
    BtcBlockChain btcBlockChain = new SimpleBlockChain(btcContext, btcBlockStore);
    BridgeStorageProvider provider = new BridgeStorageProvider(track, contractAddress, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    provider.setNewFederation(federation1);
    provider.setOldFederation(federation2);
    // Whitelist the addresses
    LockWhitelist whitelist = provider.getLockWhitelist();
    Address address1 = srcKey1.toAddress(parameters);
    Address address2 = srcKey2.toAddress(parameters);
    Address address3 = srcKey3.toAddress(parameters);
    whitelist.put(address1, Coin.COIN.multiply(5));
    whitelist.put(address2, Coin.COIN.multiply(10));
    whitelist.put(address3, Coin.COIN.multiply(2).add(Coin.COIN.multiply(3)));
    BridgeSupport bridgeSupport = new BridgeSupport(config, track, null, config.getBlockchainConfig().getCommonConstants().getBridgeConstants(), provider, btcBlockStore, btcBlockChain);
    Whitebox.setInternalState(bridgeSupport, "rskExecutionBlock", executionBlock);
    byte[] bits = new byte[1];
    bits[0] = 0x3f;
    List<Sha256Hash> hashes = new ArrayList<>();
    hashes.add(tx1.getHash());
    hashes.add(tx2.getHash());
    hashes.add(tx3.getHash());
    PartialMerkleTree pmt = new PartialMerkleTree(btcParams, bits, hashes, 3);
    List<Sha256Hash> hashlist = new ArrayList<>();
    Sha256Hash merkleRoot = pmt.getTxnHashAndMerkleRoot(hashlist);
    co.rsk.bitcoinj.core.BtcBlock block = new co.rsk.bitcoinj.core.BtcBlock(btcParams, 1, PegTestUtils.createHash(), merkleRoot, 1, 1, 1, new ArrayList<BtcTransaction>());
    btcBlockChain.add(block);
    ((SimpleBlockChain) btcBlockChain).useHighBlock();
    bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx1, 1, pmt);
    bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx2, 1, pmt);
    bridgeSupport.registerBtcTransaction(mock(Transaction.class), tx3, 1, pmt);
    bridgeSupport.save();
    ((SimpleBlockChain) btcBlockChain).useBlock();
    track.commit();
    Assert.assertThat(whitelist.isWhitelisted(address1), is(false));
    Assert.assertThat(whitelist.isWhitelisted(address2), is(false));
    Assert.assertThat(whitelist.isWhitelisted(address3), is(false));
    co.rsk.core.Coin amountToHaveBeenCreditedToSrc1 = co.rsk.core.Coin.fromBitcoin(Coin.valueOf(5, 0));
    co.rsk.core.Coin amountToHaveBeenCreditedToSrc2 = co.rsk.core.Coin.fromBitcoin(Coin.valueOf(10, 0));
    co.rsk.core.Coin amountToHaveBeenCreditedToSrc3 = co.rsk.core.Coin.fromBitcoin(Coin.valueOf(5, 0));
    co.rsk.core.Coin totalAmountExpectedToHaveBeenLocked = amountToHaveBeenCreditedToSrc1.add(amountToHaveBeenCreditedToSrc2).add(amountToHaveBeenCreditedToSrc3);
    RskAddress srcKey1RskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey1.getPrivKey()).getAddress());
    RskAddress srcKey2RskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey2.getPrivKey()).getAddress());
    RskAddress srcKey3RskAddress = new RskAddress(org.ethereum.crypto.ECKey.fromPrivate(srcKey3.getPrivKey()).getAddress());
    Assert.assertEquals(amountToHaveBeenCreditedToSrc1, repository.getBalance(srcKey1RskAddress));
    Assert.assertEquals(amountToHaveBeenCreditedToSrc2, repository.getBalance(srcKey2RskAddress));
    Assert.assertEquals(amountToHaveBeenCreditedToSrc3, repository.getBalance(srcKey3RskAddress));
    Assert.assertEquals(LIMIT_MONETARY_BASE.subtract(totalAmountExpectedToHaveBeenLocked), repository.getBalance(PrecompiledContracts.BRIDGE_ADDR));
    BridgeStorageProvider provider2 = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    Assert.assertEquals(2, provider2.getNewFederationBtcUTXOs().size());
    Assert.assertEquals(2, provider2.getOldFederationBtcUTXOs().size());
    Assert.assertEquals(Coin.COIN.multiply(5), provider2.getNewFederationBtcUTXOs().get(0).getValue());
    Assert.assertEquals(Coin.COIN.multiply(2), provider2.getNewFederationBtcUTXOs().get(1).getValue());
    Assert.assertEquals(Coin.COIN.multiply(10), provider2.getOldFederationBtcUTXOs().get(0).getValue());
    Assert.assertEquals(Coin.COIN.multiply(3), provider2.getOldFederationBtcUTXOs().get(1).getValue());
    Assert.assertEquals(0, provider2.getReleaseRequestQueue().getEntries().size());
    Assert.assertEquals(0, provider2.getReleaseTransactionSet().getEntries().size());
    Assert.assertTrue(provider2.getRskTxsWaitingForSignatures().isEmpty());
    Assert.assertEquals(3, provider2.getBtcTxHashesAlreadyProcessed().size());
}
Also used : RskAddress(co.rsk.core.RskAddress) SimpleBlockChain(co.rsk.peg.simples.SimpleBlockChain) RskAddress(co.rsk.core.RskAddress) BridgeConstants(co.rsk.config.BridgeConstants) BtcBlockStore(co.rsk.bitcoinj.store.BtcBlockStore) co.rsk.bitcoinj.core(co.rsk.bitcoinj.core) org.ethereum.core(org.ethereum.core) SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) RepositoryImpl(co.rsk.db.RepositoryImpl) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 10 with RskAddress

use of co.rsk.core.RskAddress in project rskj by rsksmart.

the class BridgeSupportTest method addLockWhitelistAddress_addFails.

@Test
public void addLockWhitelistAddress_addFails() 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);
    ArgumentCaptor<Address> argument = ArgumentCaptor.forClass(Address.class);
    when(mockedWhitelist.isWhitelisted(any(Address.class))).thenReturn(true);
    Assert.assertEquals(-1, bridgeSupport.addLockWhitelistAddress(mockedTx, "mwKcYS3H8FUgrPtyGMv3xWvf4jgeZUkCYN", BigInteger.valueOf(Coin.COIN.getValue())).intValue());
    verify(mockedWhitelist).isWhitelisted(argument.capture());
    Assert.assertThat(argument.getValue().toBase58(), is("mwKcYS3H8FUgrPtyGMv3xWvf4jgeZUkCYN"));
}
Also used : SimpleRskTransaction(co.rsk.peg.simples.SimpleRskTransaction) RskAddress(co.rsk.core.RskAddress) RskAddress(co.rsk.core.RskAddress) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

RskAddress (co.rsk.core.RskAddress)174 Test (org.junit.Test)102 Repository (org.ethereum.core.Repository)60 BigInteger (java.math.BigInteger)47 Coin (co.rsk.core.Coin)38 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 DataWord (org.ethereum.vm.DataWord)27 TrieImplHashTest (co.rsk.trie.TrieImplHashTest)24 RepositoryImpl (co.rsk.db.RepositoryImpl)16 SimpleRskTransaction (co.rsk.peg.simples.SimpleRskTransaction)15 Transaction (org.ethereum.core.Transaction)15 Program (org.ethereum.vm.program.Program)15 InvocationOnMock (org.mockito.invocation.InvocationOnMock)14 AccountState (org.ethereum.core.AccountState)12 HashMapDB (org.ethereum.datasource.HashMapDB)11 ArrayList (java.util.ArrayList)10 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)10 BridgeConstants (co.rsk.config.BridgeConstants)8 RskSystemProperties (co.rsk.config.RskSystemProperties)8 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)8