Friday 2 November 2018

Blockchain with R

block_example <- list(index = 1,
                     timestamp = "2018-11-02 08.32 IST",
                     data = "Infoway",
                     previous_hash = 0,
                     proof = 9,
                     new_hash = NULL)
_________________________________________________________________
library("digest")
digest("Manish" ,"sha256")
________________________________________________________________

    hash_block <- function(block){
      block$new_hash <- digest(c(block$index,
                                 block$timestamp,
                                 block$data,
                                 block$previous_hash), "sha256")
      return(block)
    }
____________________________________________________________
      # Increment the proof number until a number is found that is divisable by 99 and by the proof of the previous block
   
proof_of_work <- function(last_proof){
      proof <- last_proof + 1
      while (!(proof %% 99 == 0 & proof %% last_proof == 0 )){
        proof <- proof + 1}
 return(proof) }
_________________________________________________________________
    gen_new_block <- function(previous_block){
      new_proof <- proof_of_work(previous_block$proof)
      new_block <- list(index = previous_block$index + 1,
                        timestamp = Sys.time(),
                        data = paste0("this is block ", previous_block$index +1),
                        previous_hash = previous_block$new_hash,
                        proof = new_proof)
      new_block_hashed <- hash_block(new_block)
      return(new_block_hashed) }
_____________________________________________________________________

    block_genesis <-  list(index = 1,
                           timestamp = Sys.time(),
                           data = "Genesis Block",
                           previous_hash = "0",
                           proof = 1)
_____________________________________________________________________
blockchain <- list(block_genesis)
    previous_block <- blockchain[[1]]

      # How many blocks should we add to the chain after the genesis block
      num_of_blocks_to_add <- 5

      # Add blocks to the chain
      for (i in 1: num_of_blocks_to_add){
        block_to_add <- gen_new_block(previous_block)
        blockchain[i+1] <- list(block_to_add)
        previous_block <- block_to_add

        print(cat(paste0("Block ", block_to_add$index, " has been added", "\n",
                   "\t", "Proof: ", block_to_add$proof, "\n",
                   "\t", "Hash: ", block_to_add$new_hash))) }





















No comments:

Post a Comment