Search in sources :

Example 86 with RskAddress

use of co.rsk.core.RskAddress 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 87 with RskAddress

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

the class Transaction method getSender.

public synchronized RskAddress getSender() {
    if (sender != null) {
        return sender;
    }
    try {
        ECKey key = ECKey.signatureToKey(getRawHash().getBytes(), getSignature().toBase64());
        sender = new RskAddress(key.getAddress());
    } catch (SignatureException e) {
        logger.error(e.getMessage(), e);
        panicProcessor.panic("transaction", e.getMessage());
        sender = RskAddress.nullAddress();
    }
    return sender;
}
Also used : RskAddress(co.rsk.core.RskAddress) ECKey(org.ethereum.crypto.ECKey) SignatureException(java.security.SignatureException)

Example 88 with RskAddress

use of co.rsk.core.RskAddress 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 89 with RskAddress

use of co.rsk.core.RskAddress 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)

Example 90 with RskAddress

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

the class BridgeStorageProviderTest method getReleaseRequestQueue.

@Test
public void getReleaseRequestQueue() throws IOException {
    List<Integer> calls = new ArrayList<>();
    ReleaseRequestQueue requestQueueMock = mock(ReleaseRequestQueue.class);
    PowerMockito.mockStatic(BridgeSerializationUtils.class);
    Repository repositoryMock = mock(Repository.class);
    BridgeStorageProvider storageProvider = new BridgeStorageProvider(repositoryMock, mockAddress("aabbccdd"), config.getBlockchainConfig().getCommonConstants().getBridgeConstants());
    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("releaseRequestQueue".getBytes(StandardCharsets.UTF_8)), address);
        return new byte[] { (byte) 0xaa };
    });
    PowerMockito.when(BridgeSerializationUtils.deserializeReleaseRequestQueue(any(byte[].class), any(NetworkParameters.class))).then((InvocationOnMock invocation) -> {
        calls.add(0);
        byte[] data = invocation.getArgumentAt(0, byte[].class);
        NetworkParameters parameters = invocation.getArgumentAt(1, NetworkParameters.class);
        Assert.assertEquals(NetworkParameters.fromID(NetworkParameters.ID_REGTEST), parameters);
        // Make sure we're deserializing what just came from the repo
        Assert.assertTrue(Arrays.equals(new byte[] { (byte) 0xaa }, data));
        return requestQueueMock;
    });
    Assert.assertSame(requestQueueMock, storageProvider.getReleaseRequestQueue());
    // 1 for each call to deserializeFederation & getStorageBytes
    Assert.assertEquals(2, 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)

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