AddThis Social Bookmark Button

EVM Logging is a feature that allows developers to log their data, and this can be used as the traditional logging as well as proof of transaction that happened in the block chain, for example you have a  contract does an transfer from one account to another, and we generally want to keep track of such transaction in audit logs

. Audit logs can be another contract which has the data structure to hold the details , in our case here , from address , to address , the time and the amount of transaction.This contract will take some storage space in block chain and would cost us some ether. Instead , we can make use of the EVM Logging feature, which is cheaper and a native way of achieving logging.

Events are the means by which we achieve Logging. Events solidity language has the following characterestics

  1. All Events has names, and the event definition looks as below.
    event EventName(datatype <indexed> parameterName,..);

    event:
    Is a keyword in the solidity language 
    EventName:
    is the name of the Event the developer wants to choose.
    datatype:
    is the type of data the parameterName
    indexed :
    is optional and upto three parameters can be marked as indexed.
  2. Contracts cannot Access the logs or even event data,But you can search them using other tools like json rpc eth_getLogs() or even javascript watchers. More on it later
  3. All Events are inheritable,that means if a contract has an event declared it can be called from within its sub contracts ( inheritance ) 

 

A sample contract with event.


Searching event logs: 

Searching event logs can be done in different clients. The following method uses web3.js. Lets have a look at the smart contract of a blood bank simulation.

 

contract BloodBank{
	uint totalAmount;
	event BloodDonated(uint time,string indexed donorName,uint amount);
    function BloodBank(){
      totalAmount=0;
    }

	function  addBlood(uint amount) private{
		totalAmount+=amount;
	}
	function addBloodByUser(string userName,uint amount){
		addBlood(amount);
		BloodDonated(now,userName,amount);
	}
	function  queryBloodRemaining() constant returns (uint) {
		return totalAmount;
	}
}

It has two functions which lets the add blood to the blood bank , with one of them lets you to add by the name of the donor,note the method addBlood is private. The queryBloodRemaining is a constant function which returns the amount of blood left or available in the blood bank. What is interesting here is that the name of the donor is not stored as a field in the contract,but as an parameter to the event log. Apart from the donorName , there are two other fields which we are tracking in the log; the time and the amount. So every time the addBloodByUser is called , the event BloodDonated is invoked with data; current time ( unix timestamp) now , the user name or the donor and the amount of blood. So essentially we are not storing the time,donorname and the amount the donor donated,but only the totalAmount