hey folks in this video I'm going to talk about how to block handlers work
keenly to find out more well stick around let's first of all jump into the documentation here
we're on the left-hand side we'll expand create a project let's jump into mappings
and it's here where you can see that the mapping functions define how chain data is transformed
into optimized graphql entities there are three types of handlers there's a block handler
event and also a call handler in this video we'll talk about what a block handler is
now you use a block handler to capture information within a block each time a new block is attached
to the substrate chain you can grab information such as block number and a whole lot more which
we'll look into to achieve this you define a block handler which will be called once for every block
so that's well and fine but let's jump into the code to really understand how this works
so the first thing I'm going to do is create a standard startup project
now the next three commands I'm going to run will be yarn install
yarn kojin and yarn build and I'm going to run these
really quickly because you really should be familiar with these very easy initial steps
okay let's now go and take a look at our mappings file and you can see here we've got the
standard three handlers the block the event and the call these all map to our manifest file
the handle block handle event and handle calls as well let's go ahead and tidy this
up because we really only want to focus on the first one here the handle block
so I can delete these just to make it simpler and then down here I'll go ahead and delete these two
as well I can also delete the various imports up above because I'm not using them anymore
and it's at this point where you can see that I'm grabbing out the block header number assigning it
to the record dot field number one so this should be very familiar with you and what I'll do is I'm
going to add in a logger because I want to see the output of record dot field one so let's go
logger dot info and I'm going to put a new line here and say block number
and I'll echo out record dot field one now one thing I want to do is I don't want to start from
the genesis block or block number one so I'm going to find out what is the latest block at the moment
so let me go into poker scan and you can see here that we've got the latest block so I click on this
and I'm going to grab the block height here and I'm going to paste it here
let me save this let me go ahead and rebuild the code before I run the docker-compose commands now
let's run a docker-compose pull to bring down the images and we'll spin up the docker image itself
okay so now we've got the container up and running you can see we're also echoing or
logging out the block number what other information can we grab from the block handler
I'll go ahead and stop this node and let's jump back in and the first thing we'll do
is let's examine the substrate block import so we'll go to our definition
and we'll split this to the right and you can see that our interface of substrate
block we can also extract spec version and timestamp along with events as well I'll
ignore events for the moment because we'll explore that further in the substrate event
interface so how do you pull out spec event and timestamp well
very easily what we can do is let's add some more information here let's go
spec version
and then to access this I need to go block which is the argument that I'm bringing in
and in fact let me hit underscore here so you can actually see the difference
I'll go underscore block I go dot and if I go sp I should see spec conversion available to me
if I go and add another line here
include a timestamp as well
let's save this and compile it or build it and see how this goes
there we go the blocks are getting fetched and we can see we've got the block number the
spec version and the timestamp now how do we cross-check this well we can always jump into
the polka dot scanner and we'll compare the latest block on this to the
console you can see here I've got block 740 let me cancel that and I'll go across here 740
and we'll do a quick comparison our spec version here is 1990 and it's the same up here as well
9090 what's our timestamp UTC you can see it's 8 24 in the morning and the timestamp here is also
8 24 24. so all is good we've managed to extract the block number the timestamp and also the spec
version now as you can probably imagine we can also extract the likes of the hash the
parent hash the state route extrinsic etc so the question is how do we do this well let's go back
into the code and we'll understand what other methods or properties we can extract from the
various interfaces substrate block extends sign block so let's go to the sign block definition
sign block extends sign block with a justification let's dig in deeper sign blocks justification
includes something called block and justification as well so this block here if you haven't noticed
is the reason for this second block here to access header information so if I go and
dive into the block interface this extends generic block
and inside generic block here you can see we've got content hash and we've got header as well
so let's dive into the header to see what's available inside header you can see that we've
got parent hash we've got number which is the actual block number we've got the state route
extrinsic route and the digest as well so let's go ahead and see what these look like
for my parent hash remember it's blocked dot block dot header
because it was hidden in the header dot parent like so
let's continue and we'll pull out the state route so this will be blocked
again header dot state route so you should be seeing a pattern here then I've got extrinsic root
and this time I'm just going to type in extrinsic route
and why don't we go digest just for good luck
like so let's now go and build this code and we'll run it in docker again
okay there we have it you can see the node is trying to catch up to the latest block
but we've got the timestamp the parent hash the state route all this information coming out as
well as the digest so if we go ahead now and do a quick comparison let's go and jump into sub scan
and we'll try and find the latest block here where you can see we're at 800. we're 801 here
and in the browser we've got 803 just jump to five so I'll go ahead and cancel the note here on 803
and I'll open up 803 in sub scan as well to do a quick comparison the parent hash is 0x
ef 795 and up here we've got of 795 so that's a match the state route 697 let's check out in the
console here the state route here is again 697. so you can see that the information that we're
pulling out matches what's in the effectively the header of the substrate blockchain in the logs
you can see here we've got the digest logs called pre-run time and also the seal so if you want
access to the log information here you can do so using the digest method so there you have it
if you wanted to do this correctly what you would do is you would go and modify your schema graphql
file and you'd give these proper names with proper types you would then go ahead and run
codegen and then you could go in your mapping handler and instead of logging this out to the
console you could log it to values to variables in order to use in your front-end application
so hopefully this has given you an insight into how you can grab the various bits of
information from the substrate block header but please do note that when using a block
handler function your project has to inspect every single block so you can't take advantage of
filters for example in your project thanks for watching and catch you on the next video
you