Search in sources :

Example 71 with Repository

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

the class ProgramTrace method getContractDetails.

private static ContractDetails getContractDetails(ProgramInvoke programInvoke) {
    Repository repository = programInvoke.getRepository();
    if (repository instanceof RepositoryTrack) {
        repository = ((RepositoryTrack) repository).getOriginRepository();
    }
    RskAddress addr = new RskAddress(programInvoke.getOwnerAddress());
    return repository.getContractDetails(addr);
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) RepositoryTrack(org.ethereum.db.RepositoryTrack)

Example 72 with Repository

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

the class BlockTxsValidationRule method isValid.

@Override
public boolean isValid(Block block, Block parent) {
    if (block == null || parent == null) {
        logger.warn("BlockTxsValidationRule - block or parent are null");
        return false;
    }
    List<Transaction> txs = block.getTransactionsList();
    if (txs.isEmpty()) {
        return true;
    }
    Repository parentRepo = repository.getSnapshotTo(parent.getStateRoot());
    Map<RskAddress, BigInteger> curNonce = new HashMap<>();
    for (Transaction tx : txs) {
        try {
            tx.verify();
        } catch (RuntimeException e) {
            logger.warn("Unable to verify transaction", e);
            return false;
        }
        RskAddress sender = tx.getSender();
        BigInteger expectedNonce = curNonce.get(sender);
        if (expectedNonce == null) {
            expectedNonce = parentRepo.getNonce(sender);
        }
        curNonce.put(sender, expectedNonce.add(ONE));
        BigInteger txNonce = new BigInteger(1, tx.getNonce());
        if (!expectedNonce.equals(txNonce)) {
            logger.warn("Invalid transaction: Tx nonce {} != expected nonce {} (parent nonce: {}): {}", txNonce, expectedNonce, parentRepo.getNonce(sender), tx);
            panicProcessor.panic("invalidtransaction", String.format("Invalid transaction: Tx nonce %s != expected nonce %s (parent nonce: %s): %s", txNonce, expectedNonce, parentRepo.getNonce(sender), tx.getHash()));
            return false;
        }
    }
    return true;
}
Also used : Repository(org.ethereum.core.Repository) Transaction(org.ethereum.core.Transaction) HashMap(java.util.HashMap) RskAddress(co.rsk.core.RskAddress) BigInteger(java.math.BigInteger)

Example 73 with Repository

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

the class BridgeStateTest method recreateFromEmptyStorageProvider.

@Test
public void recreateFromEmptyStorageProvider() throws IOException {
    RskSystemProperties config = new RskSystemProperties();
    Repository repository = new RepositoryImpl(config, new TrieStoreImpl(new HashMapDB()));
    BridgeConstants bridgeConstants = config.getBlockchainConfig().getCommonConstants().getBridgeConstants();
    BridgeStorageProvider provider = new BridgeStorageProvider(repository, PrecompiledContracts.BRIDGE_ADDR, bridgeConstants);
    BridgeState state = new BridgeState(42, provider);
    BridgeState clone = BridgeState.create(bridgeConstants, state.getEncoded());
    Assert.assertNotNull(clone);
    Assert.assertEquals(42, clone.getBtcBlockchainBestChainHeight());
    Assert.assertTrue(clone.getBtcTxHashesAlreadyProcessed().isEmpty());
    Assert.assertTrue(clone.getActiveFederationBtcUTXOs().isEmpty());
    Assert.assertTrue(clone.getRskTxsWaitingForSignatures().isEmpty());
}
Also used : TrieStoreImpl(co.rsk.trie.TrieStoreImpl) Repository(org.ethereum.core.Repository) RepositoryImpl(co.rsk.db.RepositoryImpl) HashMapDB(org.ethereum.datasource.HashMapDB) BridgeConstants(co.rsk.config.BridgeConstants) RskSystemProperties(co.rsk.config.RskSystemProperties) Test(org.junit.Test)

Example 74 with Repository

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

the class BridgeStorageProviderTest method getNewFederation_nullBytes.

@Test
public void getNewFederation_nullBytes() throws IOException {
    List<Integer> storageBytesCalls = new ArrayList<>();
    List<Integer> deserializeCalls = new ArrayList<>();
    Context contextMock = mock(Context.class);
    PowerMockito.mockStatic(BridgeSerializationUtils.class);
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    Whitebox.setInternalState(storageProvider, "btcContext", contextMock);
    when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).then((InvocationOnMock invocation) -> {
        storageBytesCalls.add(0);
        RskAddress contractAddress = invocation.getArgumentAt(0, RskAddress.class);
        DataWord address = invocation.getArgumentAt(1, DataWord.class);
        // Make sure the bytes are get 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("newFederation".getBytes(StandardCharsets.UTF_8)), address);
        return null;
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeFederation(any(byte[].class), any(Context.class))).then((InvocationOnMock invocation) -> {
        deserializeCalls.add(0);
        return null;
    });
    Assert.assertEquals(null, storageProvider.getNewFederation());
    Assert.assertEquals(null, storageProvider.getNewFederation());
    // 2 for the calls to getStorageBytes
    Assert.assertEquals(2, storageBytesCalls.size());
    // 2 for the calls to getStorageBytes
    Assert.assertEquals(0, deserializeCalls.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 75 with Repository

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

the class BridgeStorageProviderTest method getFeePerKbElection_emptyVotes.

@Test
public void getFeePerKbElection_emptyVotes() {
    AddressBasedAuthorizer authorizerMock = mock(AddressBasedAuthorizer.class);
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    HashMap<ABICallSpec, List<RskAddress>> electionVotes = new HashMap<>();
    byte[] serializedElection = BridgeSerializationUtils.serializeElection(new ABICallElection(authorizerMock, electionVotes));
    when(repositoryMock.getStorageBytes(any(RskAddress.class), any(DataWord.class))).thenReturn(serializedElection);
    when(authorizerMock.getRequiredAuthorizedKeys()).thenReturn(1);
    ABICallElection result = storageProvider.getFeePerKbElection(authorizerMock);
    assertThat(result.getVotes().isEmpty(), is(true));
    assertThat(result.getWinner(), nullValue());
}
Also used : Repository(org.ethereum.core.Repository) RskAddress(co.rsk.core.RskAddress) DataWord(org.ethereum.vm.DataWord) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) 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