Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support blockHash filter in eth_getLogs JSON-RPC method #1527

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion rskj-core/src/main/java/org/ethereum/rpc/LogFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
package org.ethereum.rpc;

import co.rsk.core.RskAddress;
import co.rsk.crypto.Keccak256;
import co.rsk.logfilter.BlocksBloom;
import co.rsk.logfilter.BlocksBloomStore;
import org.ethereum.core.*;
import org.ethereum.db.TransactionInfo;
import org.ethereum.rpc.exception.RskJsonRpcRequestException;
import org.ethereum.vm.LogInfo;

import java.util.Collection;
Expand Down Expand Up @@ -162,6 +164,8 @@ public static LogFilter fromFilterRequest(Web3.FilterRequest fr, Blockchain bloc
// TODO review pending transaction processing
// when fromBlock and/or toBlock are "pending"

validateFilterRequestParameters(fr);

// Default from block value
if (fr.fromBlock == null) {
fr.fromBlock = "latest";
Expand All @@ -182,7 +186,22 @@ public static LogFilter fromFilterRequest(Web3.FilterRequest fr, Blockchain bloc
return filter;
}

private static void retrieveHistoricalData(Web3.FilterRequest fr, Blockchain blockchain, LogFilter filter, BlocksBloomStore blocksBloomStore) throws Exception {
/**
* Cannot use both blockHash and fromBlock/toBlock filters, according to EIP-234
*/
private static void validateFilterRequestParameters(Web3.FilterRequest fr) {
if (fr.blockHash != null && (fr.fromBlock != null || fr.toBlock != null)) {
throw RskJsonRpcRequestException.invalidParamError("Cannot specify both blockHash and fromBlock/toBlock");
}
}

private static void retrieveHistoricalData(Web3.FilterRequest fr, Blockchain blockchain, LogFilter filter, BlocksBloomStore blocksBloomStore) {

if (fr.blockHash != null) {
processSingleBlockByHash(fr.blockHash, blockchain, filter, blocksBloomStore);
return;
}

Block blockFrom = isBlockWord(fr.fromBlock) ? null : Web3Impl.getBlockByNumberOrStr(fr.fromBlock, blockchain);
Block blockTo = isBlockWord(fr.toBlock) ? null : Web3Impl.getBlockByNumberOrStr(fr.toBlock, blockchain);

Expand All @@ -201,6 +220,17 @@ 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);
}

private static void processBlocks(long fromBlockNumber, long toBlockNumber, LogFilter filter, Blockchain blockchain, BlocksBloomStore blocksBloomStore) {
BlocksBloom auxiliaryBlocksBloom = null;
long bestBlockNumber = blockchain.getBestBlock().getNumber();
Expand Down
2 changes: 2 additions & 0 deletions rskj-core/src/main/java/org/ethereum/rpc/Web3.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class FilterRequest {
public String toBlock;
public Object address;
public Object[] topics;
public String blockHash;

@Override
public String toString() {
Expand All @@ -77,6 +78,7 @@ public String toString() {
", toBlock='" + toBlock + '\'' +
", address=" + address +
", topics=" + Arrays.toString(topics) +
", blockHash='" + blockHash + '\'' +
'}';
}
}
Expand Down
5 changes: 4 additions & 1 deletion rskj-core/src/main/java/org/ethereum/rpc/Web3Impl.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@
import co.rsk.rpc.modules.txpool.TxPoolModule;
import co.rsk.scoring.*;
import org.ethereum.config.blockchain.upgrades.ConsensusRule;
import org.ethereum.core.*;
import org.ethereum.core.Block;
import org.ethereum.core.BlockHeader;
import org.ethereum.core.Blockchain;
import org.ethereum.core.Transaction;
import org.ethereum.crypto.HashUtil;
import org.ethereum.db.BlockInformation;
import org.ethereum.db.BlockStore;
Expand Down
13 changes: 7 additions & 6 deletions rskj-core/src/test/java/co/rsk/rpc/Web3RskImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@
import org.ethereum.rpc.Web3Mocks;
import org.ethereum.vm.DataWord;
import org.ethereum.vm.LogInfo;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;

public class Web3RskImplTest {
Expand Down Expand Up @@ -136,7 +136,7 @@ public void web3_LogFilterElementNullAddress_toString() {

LogFilterElement logFilterElement = new LogFilterElement(logInfo, block, txIndex, tx, logIdx);

Assert.assertEquals("LogFilterElement{logIndex='0x5', blockNumber='0x1', blockHash='0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2', transactionHash='0x0200000000000000000000000000000000000000000000000000000000000000', transactionIndex='0x1', address='0x', data='0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2', topics=[0x00000000000000000000000000000000000000000000000000000000000000c1, 0x00000000000000000000000000000000000000000000000000000000000000c2]}", logFilterElement.toString());
assertEquals("LogFilterElement{logIndex='0x5', blockNumber='0x1', blockHash='0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2', transactionHash='0x0200000000000000000000000000000000000000000000000000000000000000', transactionIndex='0x1', address='0x', data='0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2', topics=[0x00000000000000000000000000000000000000000000000000000000000000c1, 0x00000000000000000000000000000000000000000000000000000000000000c2]}", logFilterElement.toString());
}

@Test
Expand All @@ -160,7 +160,7 @@ public void web3_LogFilterElementNullData_toString() {

LogFilterElement logFilterElement = new LogFilterElement(logInfo, block, txIndex, tx, logIdx);

Assert.assertEquals("LogFilterElement{logIndex='0x5', blockNumber='0x1', blockHash='0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2', transactionHash='0x0200000000000000000000000000000000000000000000000000000000000000', transactionIndex='0x1', address='0x', data='0x', topics=[0x00000000000000000000000000000000000000000000000000000000000000c1, 0x00000000000000000000000000000000000000000000000000000000000000c2]}", logFilterElement.toString());
assertEquals("LogFilterElement{logIndex='0x5', blockNumber='0x1', blockHash='0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2', transactionHash='0x0200000000000000000000000000000000000000000000000000000000000000', transactionIndex='0x1', address='0x', data='0x', topics=[0x00000000000000000000000000000000000000000000000000000000000000c1, 0x00000000000000000000000000000000000000000000000000000000000000c2]}", logFilterElement.toString());
}

@Test
Expand All @@ -176,7 +176,7 @@ public void web3_CallArguments_toString() {
callArguments.nonce = "0";
callArguments.chainId = "0x00";

Assert.assertEquals("CallArguments{from='0x1', to='0x2', gasLimit='21000', gasPrice='100', value='1', data='data', nonce='0', chainId='0x00'}", callArguments.toString());
assertEquals("CallArguments{from='0x1', to='0x2', gasLimit='21000', gasPrice='100', value='1', data='data', nonce='0', chainId='0x00'}", callArguments.toString());
}

@Test
Expand All @@ -186,8 +186,9 @@ public void web3_FilterRequest_toString() {
filterRequest.fromBlock = "1";
filterRequest.toBlock = "2";
filterRequest.address = "0x0000000001";
filterRequest.topics = new Object[] {"2"};
filterRequest.topics = new Object[]{"2"};
filterRequest.blockHash = "0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2";

Assert.assertEquals(filterRequest.toString(), "FilterRequest{fromBlock='1', toBlock='2', address=0x0000000001, topics=[2]}");
assertEquals(filterRequest.toString(), "FilterRequest{fromBlock='1', toBlock='2', address=0x0000000001, topics=[2], blockHash='0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2'}");
}
}
Loading