In general, I assume events traverse from smaller to larger block number.
Example:
contractDeployedBlockNumber = 10000
fromBlock: contractDeployedBlockNumber, toBlock: 'latest'
[Q] Could event watch()
traverse from 'latest'
block number through 0th block number (or smaller block number)? this is basically reverse traverse operation in order to catch the latest event
first and break, this will help to retrieve the latest emitted event as fast as possible.
Smart contract's function keeps a registered users' information and updates it using registerUser()
function.
contract Example {
mapping(address => uint) userLatestEmittedBlocNum;
function registerUser(string memory userEmail,
string memory name,
string memory githubUserName) public
returns (bool success)
{
userLatestEmittedBlocNum[msg.sender] = block.number;
emit LogUser(msg.sender, userEmail, name, githubUserName);
return true;
}
function getUserLatestEmittedBlocNum(address userAddress) public view
returns(uint)
{
if (userLatestEmittedBlocNum[userAddress] != 0)
return (userLatestEmittedBlocNum[userAddress]);
}
event LogUser(address userAddress, string userEmail, string name, string githubUserName);
}
I can store the block number where the user's information is updated under userLatestEmittedBlocNum[msg.sender]
and retrieve the emitted log directly from that block number, but this will cost additional storage and gas usage.
blockReadFrom = Example.functions. getUserLatestEmittedBlocNum(userAddress).call()
my_filter = Example.eventFilter('LogUser',{'fromBlock':int(blockReadFrom),'toBlock':int(blockReadFrom) + 1})
As a results we need to retrieve the latest event for each users for the up-to-date information.
It may be inefficient to iterate from smaller block number to latest block number (that will also get the previous updated events as well), and span range might be very large. Instead, if we could able to scan from latest block number to small block number and break the iteration when we catch the event, it might be faster to find the latest emitted log.
On a smart contract, some information is logged and over time it may updated, hence the latest
event is valid. So I want to retrieve the latest event on my smart contract, without start scanning from the much smaller block number, which is time consuming and inefficient. So basically starting from 'latest' block number to smaller block, and break this iteration if I catch the event. Otherwise if I start from smaller block number, no matter what, I have to iterate till the latest block number.
Goal: fromBlock: latest, toBlock: 0
blockNum = 0;
var contract = myContract.LogJob({}, { fromBlock: blockNum, toBlock: 'latest' });
var i = 0;
contract.watch(function (error, result) {
console.log(i++ + ": " + JSON.stringify(result));
});
latest
to thatmindedBlockNumber
. Hope I guess I was able to give you some idea. Please let me know if my understanding is incorrect.fromBlock: latest, toBlock: 0
@ Rajesh