Search in sources :

Example 6 with Repository

use of org.ethereum.core.Repository 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 7 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class BridgeStorageProviderTest method createSaveAndRecreateInstanceWithTxsWaitingForSignatures.

@Test
public void createSaveAndRecreateInstanceWithTxsWaitingForSignatures() throws IOException {
    BtcTransaction tx1 = createTransaction();
    BtcTransaction tx2 = createTransaction();
    BtcTransaction tx3 = createTransaction();
    Keccak256 hash1 = PegTestUtils.createHash3();
    Keccak256 hash2 = PegTestUtils.createHash3();
    Keccak256 hash3 = PegTestUtils.createHash3();
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    BridgeStorageProvider provider0 = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    provider0.getRskTxsWaitingForSignatures().put(hash1, tx1);
    provider0.getRskTxsWaitingForSignatures().put(hash2, tx2);
    provider0.getRskTxsWaitingForSignatures().put(hash3, tx3);
    provider0.save();
    track.commit();
    track = repository.startTracking();
    BridgeStorageProvider provider = new BridgeStorageProvider(track, PrecompiledContracts.BRIDGE_ADDR, config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    SortedMap<Keccak256, BtcTransaction> signatures = provider.getRskTxsWaitingForSignatures();
    Assert.assertNotNull(signatures);
    Assert.assertTrue(signatures.containsKey(hash1));
    Assert.assertTrue(signatures.containsKey(hash2));
    Assert.assertTrue(signatures.containsKey(hash3));
    Assert.assertEquals(tx1.getHash(), signatures.get(hash1).getHash());
    Assert.assertEquals(tx2.getHash(), signatures.get(hash2).getHash());
    Assert.assertEquals(tx3.getHash(), signatures.get(hash3).getHash());
}
Also used : Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) Keccak256(co.rsk.crypto.Keccak256) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with Repository

use of org.ethereum.core.Repository 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 Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class SamplePrecompiledContractTest method samplePrecompiledContractIncrementResultOk.

@Test
public void samplePrecompiledContractIncrementResultOk() {
    DataWord addr = new DataWord(PrecompiledContracts.SAMPLE_ADDR.getBytes());
    SamplePrecompiledContract contract = (SamplePrecompiledContract) precompiledContracts.getContractForAddress(addr);
    String funcJson = "{\n" + "   'constant':false, \n" + "   'inputs':[], \n" + "    'name':'IncrementResult', \n" + "   'outputs':[], \n" + "    'type':'function' \n" + "}\n";
    funcJson = funcJson.replaceAll("'", "\"");
    CallTransaction.Function function = CallTransaction.Function.fromJsonInterface(funcJson);
    byte[] data = function.encode();
    Repository repository = new RepositoryImpl(config);
    Repository track = repository.startTracking();
    contract.init(null, null, track, null, null, new ArrayList<LogInfo>());
    contract.execute(data);
    track.commit();
    int result = this.GetResult(repository);
    assertEquals(1, result);
}
Also used : Repository(org.ethereum.core.Repository) LogInfo(org.ethereum.vm.LogInfo) RepositoryImpl(co.rsk.db.RepositoryImpl) CallTransaction(org.ethereum.core.CallTransaction) DataWord(org.ethereum.vm.DataWord) Test(org.junit.Test)

Example 10 with Repository

use of org.ethereum.core.Repository in project rskj by rsksmart.

the class MinerUtilsTest method harmfulTransactionTest.

@Test
public void harmfulTransactionTest() {
    Transaction tx = Tx.create(config, 0, 50000, 1, 0, 0, 0);
    List<Transaction> txs = new LinkedList<>();
    txs.add(tx);
    Mockito.when(tx.getGasPrice()).thenReturn(null);
    Map<RskAddress, BigInteger> accountNounces = new HashMap();
    byte[] addressBytes = ByteUtil.leftPadBytes(BigInteger.valueOf(new Random(0).nextLong()).toByteArray(), 20);
    accountNounces.put(new RskAddress(addressBytes), BigInteger.valueOf(0));
    Repository repository = Mockito.mock(Repository.class);
    Coin minGasPrice = Coin.valueOf(2L);
    LinkedList<Transaction> txsToRemove = new LinkedList<>();
    List<Transaction> res = new MinerUtils().filterTransactions(txsToRemove, txs, accountNounces, repository, minGasPrice);
    Assert.assertEquals(0, res.size());
    Assert.assertEquals(1, txsToRemove.size());
}
Also used : Coin(co.rsk.core.Coin) Repository(org.ethereum.core.Repository) Transaction(org.ethereum.core.Transaction) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger) Test(org.junit.Test)

Aggregations

Repository (org.ethereum.core.Repository)136 Test (org.junit.Test)109 RskAddress (co.rsk.core.RskAddress)59 DataWord (org.ethereum.vm.DataWord)43 BigInteger (java.math.BigInteger)31 RepositoryImpl (co.rsk.db.RepositoryImpl)25 Coin (co.rsk.core.Coin)23 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)19 HashMapDB (org.ethereum.datasource.HashMapDB)12 BridgeStorageProvider (co.rsk.peg.BridgeStorageProvider)11 Transaction (org.ethereum.core.Transaction)11 InvocationOnMock (org.mockito.invocation.InvocationOnMock)11 TrieStoreImpl (co.rsk.trie.TrieStoreImpl)10 ArrayList (java.util.ArrayList)10 Program (org.ethereum.vm.program.Program)10 Block (org.ethereum.core.Block)9 ProgramInvokeMockImpl (org.ethereum.vm.program.invoke.ProgramInvokeMockImpl)9 RskSystemProperties (co.rsk.config.RskSystemProperties)8 IOException (java.io.IOException)8 List (java.util.List)8