Skip to content

Commit

Permalink
Handle eth_getLogs (with blockHash filter) case when block doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Marccio Silva committed May 11, 2021
1 parent 4cb8621 commit a59d810
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
3 changes: 3 additions & 0 deletions rskj-core/src/main/java/org/ethereum/rpc/LogFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,9 @@ else if ("latest".equalsIgnoreCase(fr.fromBlock)) {
private static void processSingleBlockByHash(String blockHash, Blockchain blockchain, LogFilter filter, BlocksBloomStore blocksBloomStore) {
Keccak256 keccak256BlockHash = new Keccak256(stringHexToByteArray(blockHash));
Block blockByHash = blockchain.getBlockByHash(keccak256BlockHash.getBytes());
if (blockByHash == null) {
return;
}

long blockNumber = blockByHash.getNumber();
processBlocks(blockNumber, blockNumber, filter, blockchain, blocksBloomStore);
Expand Down
37 changes: 27 additions & 10 deletions rskj-core/src/test/java/org/ethereum/rpc/Web3ImplLogsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import co.rsk.core.Wallet;
import co.rsk.core.WalletFactory;
import co.rsk.core.bc.MiningMainchainView;
import co.rsk.crypto.Keccak256;
import co.rsk.db.RepositoryLocator;
import co.rsk.logfilter.BlocksBloomStore;
import co.rsk.peg.BridgeSupportFactory;
Expand Down Expand Up @@ -57,8 +58,8 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.ethereum.rpc.TypeConverter.stringHexToByteArray;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -89,11 +90,13 @@ public class Web3ImplLogsTest {
// Examples:
// Incremented(bool indexed odd, uint x) -> Keccak-256("Incremented(bool,uint256)")
//
private final static String GET_VALUED_EVENT_SIGNATURE = "1ee041944547858a75ebef916083b6d4f5ae04bea9cd809334469dd07dbf441b";
private final static String INC_EVENT_SIGNATURE = "6e61ef44ac2747ff8b84d353a908eb8bd5c3fb118334d57698c5cfc7041196ad";
private final static String ONE_TOPIC = "0000000000000000000000000000000000000000000000000000000000000001";
private final static String INCREMENT_METHOD_SIGNATURE = "371303c0";
private final static String GET_VALUE_METHOD_SIGNATURE = "20965255";
private static final String GET_VALUED_EVENT_SIGNATURE = "1ee041944547858a75ebef916083b6d4f5ae04bea9cd809334469dd07dbf441b";
private static final String INC_EVENT_SIGNATURE = "6e61ef44ac2747ff8b84d353a908eb8bd5c3fb118334d57698c5cfc7041196ad";
private static final String ONE_TOPIC = "0000000000000000000000000000000000000000000000000000000000000001";
private static final String INCREMENT_METHOD_SIGNATURE = "371303c0";
private static final String GET_VALUE_METHOD_SIGNATURE = "20965255";
private static final String TRACKED_TEST_BLOCK_HASH = "0xafb368a4f74e51a3c6b6d72b049c4fc7bc7506251f13a3afa4fee4bece0e85eb";
private static final String UNTRACKED_TEST_BLOCK_HASH = "0xdea168a4f74e51a3eeb6d72b049c4fc7bc750dd51f13a3afa4fee4bece0e85eb";
private final TestSystemProperties config = new TestSystemProperties();
private Blockchain blockChain;
private MiningMainchainView mainchainView;
Expand Down Expand Up @@ -819,7 +822,7 @@ public void getLogsTwiceFromBlockchainWithCallContractAndFilterBySecondTopic() t
public void getLogsFromBlockchainWithEventInContractCreationReturnsAsExpectedWithBlockHashFilter() throws Exception {
addEventInContractCreation();
Web3.FilterRequest fr = new Web3.FilterRequest();
final String blockHash = "0xafb368a4f74e51a3c6b6d72b049c4fc7bc7506251f13a3afa4fee4bece0e85eb";
final String blockHash = TRACKED_TEST_BLOCK_HASH;
fr.blockHash = blockHash;

Object[] logs = web3.eth_getLogs(fr);
Expand All @@ -832,12 +835,26 @@ public void getLogsFromBlockchainWithEventInContractCreationReturnsAsExpectedWit
assertEquals(txdto.getContractAddress(),((LogFilterElement)logs[0]).address);
}

@Test
public void getLogsWithBlockHashFilterForNonexistentBlockThrowsException() throws Exception {
final String blockHash = UNTRACKED_TEST_BLOCK_HASH;
byte[] blockHashBytes = new Keccak256(stringHexToByteArray(blockHash)).getBytes();
assertFalse(blockChain.hasBlockInSomeBlockchain(blockHashBytes));
Web3.FilterRequest fr = new Web3.FilterRequest();
fr.blockHash = blockHash;

Object[] logs = web3.eth_getLogs(fr);

assertNotNull(logs);
assertEquals(0, logs.length);
}

@Test(expected = RskJsonRpcRequestException.class)
public void getLogsThrowsExceptionWhenBlockHashIsUsedCombinedWithFromBlock() throws Exception {
addEventInContractCreation();
Web3.FilterRequest fr = new Web3.FilterRequest();
fr.fromBlock = "earliest";
fr.blockHash = "0xafb368a4f74e51a3c6b6d72b049c4fc7bc7506251f13a3afa4fee4bece0e85eb";
fr.blockHash = TRACKED_TEST_BLOCK_HASH;

web3.eth_getLogs(fr);
}
Expand All @@ -847,7 +864,7 @@ public void getLogsThrowsExceptionWhenBlockHashIsUsedCombinedWithToBlock() throw
addEventInContractCreation();
Web3.FilterRequest fr = new Web3.FilterRequest();
fr.toBlock = "latest";
fr.blockHash = "0xafb368a4f74e51a3c6b6d72b049c4fc7bc7506251f13a3afa4fee4bece0e85eb";
fr.blockHash = TRACKED_TEST_BLOCK_HASH;

web3.eth_getLogs(fr);
}
Expand Down

0 comments on commit a59d810

Please # to comment.