Created
December 30, 2021 01:42
-
-
Save SoftwareDevPro/42aa6d1eac340f7895c11b133a9c654b to your computer and use it in GitHub Desktop.
Basic Blockchain with Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/sha256" | |
"encoding/json" | |
"fmt" | |
"strconv" | |
"strings" | |
"time" | |
) | |
type Block struct { | |
data map[string]interface{} | |
hash string | |
previousHash string | |
timestamp time.Time | |
pow int | |
} | |
type Blockchain struct { | |
genesisBlock Block | |
chain []Block | |
difficulty int | |
} | |
// Calculates a hash for the block | |
func (b Block) calculateHash() string { | |
data, _ := json.Marshal(b.data) | |
blockData := b.previousHash + string(data) + b.timestamp.String() + strconv.Itoa(b.pow) | |
blockHash := sha256.Sum256([]byte(blockData)) | |
return fmt.Sprintf("%x", blockHash) | |
} | |
// Mines a block. | |
func (b *Block) mine(difficulty int) { | |
for !strings.HasPrefix(b.hash, strings.Repeat("0", difficulty)) { | |
b.pow++ | |
b.hash = b.calculateHash() | |
} | |
} | |
// Creates a blockchain with an initial difficulty | |
func CreateBlockchain(difficulty int) Blockchain { | |
genesisBlock := Block{ | |
hash: "0", | |
timestamp: time.Now(), | |
} | |
return Blockchain{ | |
genesisBlock, | |
[]Block{genesisBlock}, | |
difficulty, | |
} | |
} | |
// Adds a block to the blockchain | |
func (b *Blockchain) addBlock(from, to string, amount float64) { | |
blockData := map[string]interface{}{ | |
"from": from, | |
"to": to, | |
"amount": amount, | |
} | |
lastBlock := b.chain[len(b.chain)-1] | |
newBlock := Block{ | |
data: blockData, | |
previousHash: lastBlock.hash, | |
timestamp: time.Now(), | |
} | |
newBlock.mine(b.difficulty) | |
b.chain = append(b.chain, newBlock) | |
} | |
// Validate the blockchain, where a valid blockchain has all blocks with | |
// the current blocks previous hash equal to the previous blocks hash, | |
// and each block produces a valid hash value. | |
func (b Blockchain) isValid() bool { | |
for i := range b.chain[1:] { | |
previousBlock := b.chain[i] | |
currentBlock := b.chain[i+1] | |
if currentBlock.hash != currentBlock.calculateHash() || currentBlock.previousHash != previousBlock.hash { | |
return false | |
} | |
} | |
return true | |
} | |
func main() { | |
// create a new blockchain instance with a mining difficulty of 2 | |
blockchain := CreateBlockchain(2) | |
// record transactions on the blockchain for Alice, Bob, and John | |
blockchain.addBlock("Joe", "Alice", 5) | |
blockchain.addBlock("John", "Jim", 2) | |
// check if the blockchain is valid; expecting true | |
fmt.Println(blockchain.isValid()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment