Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
feat: add ability to use blockHash with eth_getLogs (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
tynes authored Sep 30, 2020
1 parent 0c5b962 commit a594d86
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/statemanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,8 @@ StateManager.prototype.getLogs = function(filter, callback) {
{
fromBlock: this.blockchain.getEffectiveBlockNumber.bind(this.blockchain, filter.fromBlock || "latest"),
toBlock: this.blockchain.getEffectiveBlockNumber.bind(this.blockchain, filter.toBlock || "latest"),
latestBlock: this.blockchain.getEffectiveBlockNumber.bind(this.blockchain, "latest")
latestBlock: this.blockchain.getEffectiveBlockNumber.bind(this.blockchain, "latest"),
block: this.blockchain.getBlock.bind(this.blockchain, filter.blockHash || 0)
},
function(err, results) {
if (err) {
Expand All @@ -788,11 +789,17 @@ StateManager.prototype.getLogs = function(filter, callback) {
var fromBlock = results.fromBlock;
var toBlock = results.toBlock;
var latestBlock = results.latestBlock;
var block = results.block;

if (toBlock > latestBlock) {
toBlock = latestBlock;
}

if (filter.blockHash) {
fromBlock = to.number(block.header.number);
toBlock = to.number(block.header.number);
}

var logs = [];
var current = fromBlock;

Expand Down
52 changes: 52 additions & 0 deletions test/local/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,58 @@ var tests = function(web3, EventTest) {
);
});

// NOTE! This test relies on the events triggered in the tests above.
it("should return logs using blockHash", function(done) {
var provider = web3.currentProvider;

provider.send(
{
jsonrpc: "2.0",
method: "eth_getBlockByNumber",
params: [
"latest",
false
],
id: new Date().getTime()
},
function(err, result) {
if (err) {
return done(err);
}

const hash = result.result.hash;

provider.send(
{
jsonrpc: "2.0",
method: "eth_getLogs",
params: [
{
blockHash: hash,
topics: [
"0xc54307031d9aa93e0568c363be84a9400dce343fef6a2851d55662a6af1a29da",
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000006"
]
}
],
id: new Date().getTime()
},
function(err, result) {
if (err) {
return done(err);
}
const logIndex = result.result[0].logIndex;
const transactionIndex = result.result[0].transactionIndex;
assert.strictEqual(logIndex, "0x0");
assert.strictEqual(transactionIndex, "0x0");
done();
}
);
}
);
});

it("always returns a change for every new block subscription when instamining", function(done) {
var provider = web3.currentProvider;

Expand Down

0 comments on commit a594d86

Please sign in to comment.