Skip to content

Instantly share code, notes, and snippets.

@ndinh215
Forked from LordGhostX/blockchain.go
Created February 23, 2023 03:23
Show Gist options
  • Save ndinh215/aa6ebebad875b4190f4d8e694015f84f to your computer and use it in GitHub Desktop.
Save ndinh215/aa6ebebad875b4190f4d8e694015f84f to your computer and use it in GitHub Desktop.

Revisions

  1. @LordGhostX LordGhostX revised this gist Dec 6, 2021. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions blockchain.go
    Original file line number Diff line number Diff line change
    @@ -76,9 +76,15 @@ func (b Blockchain) isValid() bool {
    return true
    }


    func main() {
    blockchain := CreateBlockchain(2)
    blockchain.addBlock("Alice", "Bob", 5)
    blockchain.addBlock("John", "Bob", 2)
    fmt.Println(blockchain.isValid())
    // 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("Alice", "Bob", 5)
    blockchain.addBlock("John", "Bob", 2)

    // check if the blockchain is valid; expecting true
    fmt.Println(blockchain.isValid())
    }
  2. @LordGhostX LordGhostX revised this gist Dec 2, 2021. 1 changed file with 15 additions and 15 deletions.
    30 changes: 15 additions & 15 deletions blockchain.go
    Original file line number Diff line number Diff line change
    @@ -10,17 +10,17 @@ import (
    )

    type Block struct {
    data map[string]string
    hash string
    data map[string]interface{}
    hash string
    previousHash string
    timestamp time.Time
    pow int
    timestamp time.Time
    pow int
    }

    type Blockchain struct {
    genesisBlock Block
    chain []Block
    difficulty int
    chain []Block
    difficulty int
    }

    func (b Block) calculateHash() string {
    @@ -39,7 +39,7 @@ func (b *Block) mine(difficulty int) {

    func CreateBlockchain(difficulty int) Blockchain {
    genesisBlock := Block{
    hash: "0",
    hash: "0",
    timestamp: time.Now(),
    }
    return Blockchain{
    @@ -50,16 +50,16 @@ func CreateBlockchain(difficulty int) Blockchain {
    }

    func (b *Blockchain) addBlock(from, to string, amount float64) {
    blockData := map[string]string{
    "from": from,
    "to": to,
    "amount": fmt.Sprintf("%f", amount),
    blockData := map[string]interface{}{
    "from": from,
    "to": to,
    "amount": amount,
    }
    lastBlock := b.chain[len(b.chain) - 1]
    lastBlock := b.chain[len(b.chain)-1]
    newBlock := Block{
    data: blockData,
    data: blockData,
    previousHash: lastBlock.hash,
    timestamp: time.Now(),
    timestamp: time.Now(),
    }
    newBlock.mine(b.difficulty)
    b.chain = append(b.chain, newBlock)
    @@ -68,7 +68,7 @@ func (b *Blockchain) addBlock(from, to string, amount float64) {
    func (b Blockchain) isValid() bool {
    for i := range b.chain[1:] {
    previousBlock := b.chain[i]
    currentBlock := b.chain[i + 1]
    currentBlock := b.chain[i+1]
    if currentBlock.hash != currentBlock.calculateHash() || currentBlock.previousHash != previousBlock.hash {
    return false
    }
  3. @LordGhostX LordGhostX created this gist Nov 30, 2021.
    84 changes: 84 additions & 0 deletions blockchain.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    package main

    import (
    "crypto/sha256"
    "encoding/json"
    "fmt"
    "strconv"
    "strings"
    "time"
    )

    type Block struct {
    data map[string]string
    hash string
    previousHash string
    timestamp time.Time
    pow int
    }

    type Blockchain struct {
    genesisBlock Block
    chain []Block
    difficulty int
    }

    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)
    }

    func (b *Block) mine(difficulty int) {
    for !strings.HasPrefix(b.hash, strings.Repeat("0", difficulty)) {
    b.pow++
    b.hash = b.calculateHash()
    }
    }

    func CreateBlockchain(difficulty int) Blockchain {
    genesisBlock := Block{
    hash: "0",
    timestamp: time.Now(),
    }
    return Blockchain{
    genesisBlock,
    []Block{genesisBlock},
    difficulty,
    }
    }

    func (b *Blockchain) addBlock(from, to string, amount float64) {
    blockData := map[string]string{
    "from": from,
    "to": to,
    "amount": fmt.Sprintf("%f", 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)
    }

    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() {
    blockchain := CreateBlockchain(2)
    blockchain.addBlock("Alice", "Bob", 5)
    blockchain.addBlock("John", "Bob", 2)
    fmt.Println(blockchain.isValid())
    }