Search code examples
blockchainsubstrate

Create a substrate module for logging the block hash


Sorry, I'm still a beginer for substrate blockchain. If that possible to create a substrate module that creates a log file when the block is multiple of four, and the log file will store that block of hash. I just simply saw some simple example of creating custom module, but it is not related to what I want to solve. Can anyone give me some idea or example for me to follow, thanks.


Solution

  • The offchain worker can do this.

    But why you want to make this a pallet?

    Just write the external program and send rpc to get block hash.

    λ curl http://127.0.0.1:9933 -H 'Content-Type:application/json' -d '{"jsonrpc":"2.0","method":"chain_getBlockHash","params":[1],"id":1}' 
    {"jsonrpc":"2.0","result":"0xf7eea779852c06125c51a6c225f9cef3e6c77bcc785337615306981bb4d5eccb","id":1}
    
    let mut f = std::fs::File::open();
    
    for block_number in 0.. {
        f.write(chain_get_block_hash(block_number)).unwrap();
    }
    
    f.sync_all().unwrap();