Skip to content

Latest commit

 

History

History
70 lines (43 loc) · 1.78 KB

README1.md

File metadata and controls

70 lines (43 loc) · 1.78 KB

golang-blockchain

Building a blockchain in Go from scratch walkthrough part 1

  1. go mod init github.com/monkrus/golang-blockchain.git

  2. Create a Block struct (Hash, Data, PrevHash)

  3. Write a method to create a hash based on the previous hash.

  4. We join the 2-dimensional array into one

  5. Create an actual hash on info

  6. Take created hash and push it into the hash field of the block NOTE! [:] is a slice referencing to the whole array)

  7. NOTE! SHA256 is really simple comparing to the real way to calculate a hash in the blockchain

  8. Write a function to create an actual block

  9. Create a blockchain type

  10. Write a method to add block to the chain

  • create a previous block
  • create a new block
  • append new block
  1. Write a function to create a genesis block

  2. Write a function to initialize blockchain

  • create an array of blocks using genesis block
  1. Put functions into the main function
  • intialization, adding blocks etc.
  1. Run a for loop to see the blockchain

  2. RUN go run main.go

Building a blockchain in Go from scratch walkthrough part 2

  1. Clean up:
  • create blockchain folder with block.go inside of it
  • rename package main into package blockchain
  • make blocks field public by replacing it with Blocks
  • add github.com/monkrus/golang-blockchain to import in main.go
  • please note main.go will now have less lines of code
  1. Create proof.go

  2. Set up difficulty

  3. Create ProofOfWork struct

  4. Create function NewProof

  • create a target
  • NOTE! One of our hashes has 256 bytes
  • NOTE! left shift is the same as multiplication by 2
  • << is used for "times two" and >> is for "divided by two"
  1. Create a ToHex utility function

  2. Modify CreateBlock function

  3. Delete DeriveHash function

  4. Create Validate method

  5. Add POW to main.go