Building a blockchain in Go from scratch walkthrough part 1
-
go mod init github.com/monkrus/golang-blockchain.git
-
Create a Block struct (Hash, Data, PrevHash)
-
Write a method to create a hash based on the previous hash.
-
We join the 2-dimensional array into one
-
Create an actual hash on info
-
Take created hash and push it into the hash field of the block NOTE! [:] is a slice referencing to the whole array)
-
NOTE! SHA256 is really simple comparing to the real way to calculate a hash in the blockchain
-
Write a function to create an actual block
-
Create a blockchain type
-
Write a method to add block to the chain
- create a previous block
- create a new block
- append new block
-
Write a function to create a genesis block
-
Write a function to initialize blockchain
- create an array of blocks using genesis block
- Put functions into the main function
- intialization, adding blocks etc.
-
Run a for loop to see the blockchain
-
RUN go run main.go
Building a blockchain in Go from scratch walkthrough part 2
- Clean up:
- create
blockchain
folder withblock.go
inside of it - rename
package main
intopackage blockchain
- make
blocks
field public by replacing it withBlocks
- add
github.com/monkrus/golang-blockchain
to import inmain.go
- please note
main.go
will now have less lines of code
-
Create proof.go
-
Set up difficulty
-
Create ProofOfWork struct
-
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"
-
Create a ToHex utility function
-
Modify CreateBlock function
-
Delete DeriveHash function
-
Create Validate method
-
Add POW to main.go