Browse Source

Calculate hashes when added to blockchain

gRPC
garritfra 6 years ago
parent
commit
4cff76fe66
  1. 26
      core/Block.go
  2. 26
      core/Blockchain.go
  3. 2
      core/Server.go
  4. 23
      core/crypto.go

26
core/Block.go

@ -1,10 +1,6 @@
package main
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
"time"
)
@ -17,11 +13,8 @@ type Block struct {
}
// NewBlock creates a new Block
func NewBlock(previousHash string) Block {
block := Block{Timestamp: time.Now(), PreviousHash: previousHash}
block.calculateHash()
func NewBlock() Block {
block := Block{}
return block
}
@ -30,18 +23,3 @@ func (block *Block) AddTransaction(transaction Transaction) error {
block.Data = append(block.Data, transaction)
return nil
}
func (block *Block) calculateHash() {
var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer)
if err := encoder.Encode(block); err != nil {
panic(err)
}
hasher := sha256.New()
bytes := buffer.Bytes()
hasher.Write(bytes)
sum := hex.EncodeToString(hasher.Sum(nil))
block.Hash = string(sum)
}

26
core/Blockchain.go

@ -1,5 +1,9 @@
package main
import (
"log"
)
// Blockchain struct
type Blockchain struct {
blocks []Block
@ -7,29 +11,41 @@ type Blockchain struct {
// AddBlock adds a block to the chain
func (bc *Blockchain) AddBlock(block Block) {
block.PreviousHash = bc.GetLastHash()
block.Hash = calculateHash(block)
bc.blocks = append(bc.blocks, block)
log.Print("Block added: %o", block)
}
// NewBlockchain creates a new Blockchain
func NewBlockchain() Blockchain {
log.Print("Creating Blockchain...")
blockchain := Blockchain{blocks: make([]Block, 0)}
genesisBlock := generateGenesisBlock()
blockchain.AddBlock(genesisBlock)
return blockchain
}
func generateGenesisBlock() Block {
block := NewBlock("0")
block := Block{PreviousHash: "0"}
transaction := Transaction{Amount: 0, Sender: "0", Receiver: "0", Message: "Genesis"}
block.AddTransaction(transaction)
log.Print("Genesis Block created")
return block
}
// GetLastBlock returns the latest block on the chain
func (bc *Blockchain) GetLastBlock() Block {
return bc.blocks[len(bc.blocks)-1]
// GetLastHash returns the hash of the latest block on the chain
func (bc *Blockchain) GetLastHash() string {
bcLength := len(bc.blocks)
if bcLength == 0 {
return "0"
}
return bc.blocks[len(bc.blocks)-1].Hash
}

2
core/Server.go

@ -16,7 +16,7 @@ func StartServer() {
gob.Register(Blockchain{})
blockchain = NewBlockchain()
block := NewBlock(blockchain.GetLastBlock().Hash)
block := NewBlock()
transaction := Transaction{Sender: "foo", Receiver: "bar", Amount: 100}
block.AddTransaction(transaction)

23
core/crypto.go

@ -0,0 +1,23 @@
package main
import (
"bytes"
"crypto/sha256"
"encoding/gob"
"encoding/hex"
)
func calculateHash(obj interface{}) string {
var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer)
if err := encoder.Encode(obj); err != nil {
panic(err)
}
hasher := sha256.New()
bytes := buffer.Bytes()
hasher.Write(bytes)
sum := hex.EncodeToString(hasher.Sum(nil))
return string(sum)
}
Loading…
Cancel
Save