Extract Block and Blockchain into separate files

This commit is contained in:
Ivan Kuznetsov 2017-08-15 14:29:52 +07:00
parent 23982003a4
commit b97fb423a7
3 changed files with 65 additions and 58 deletions

44
block.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"crypto/sha256"
"log"
"strconv"
"time"
)
// Block keeps block headers
type Block struct {
Timestamp int64
Data []byte
PrevBlock []byte
hash []byte
}
// SetHash calculates and sets block hash
func (b *Block) SetHash() {
h := sha256.New()
timestamp := []byte(strconv.FormatInt(b.Timestamp, 10))
var data []byte
data = append(data, b.PrevBlock...)
data = append(data, b.Data...)
data = append(data, timestamp...)
_, err := h.Write(data)
if err != nil {
log.Panic(err)
}
b.hash = h.Sum(nil)
}
// NewBlock creates and returns Block
func NewBlock(data string, prevBlock []byte) *Block {
block := &Block{time.Now().Unix(), []byte(data), prevBlock, []byte("")}
block.SetHash()
return block
}
// NewGenesisBlock creates and returns genesis Block
func NewGenesisBlock() *Block {
return NewBlock("Genesis Block", []byte("0"))
}

21
blockchain.go Normal file
View File

@ -0,0 +1,21 @@
package main
import "time"
// Blockchain keeps a sequence of Blocks
type Blockchain struct {
blocks []*Block
}
// AddBlock saves provided data as a block in the blockchain
func (bc *Blockchain) AddBlock(data string) {
prevBlock := bc.blocks[len(bc.blocks)-1]
newBlock := &Block{time.Now().Unix(), []byte(data), prevBlock.hash, []byte("")}
newBlock.SetHash()
bc.blocks = append(bc.blocks, newBlock)
}
// NewBlockchain creates a new Blockchain with genesis Block
func NewBlockchain() *Blockchain {
return &Blockchain{[]*Block{NewGenesisBlock()}}
}

58
main.go
View File

@ -1,67 +1,9 @@
package main
import (
"crypto/sha256"
"fmt"
"log"
"strconv"
"time"
)
// Block keeps block headers
type Block struct {
Timestamp int64
Data []byte
PrevBlock []byte
hash []byte
}
// SetHash calculates and sets block hash
func (b *Block) SetHash() {
h := sha256.New()
timestamp := []byte(strconv.FormatInt(b.Timestamp, 10))
var data []byte
data = append(data, b.PrevBlock...)
data = append(data, b.Data...)
data = append(data, timestamp...)
_, err := h.Write(data)
if err != nil {
log.Panic(err)
}
b.hash = h.Sum(nil)
}
// NewBlock creates and returns Block
func NewBlock(data string, prevBlock []byte) *Block {
block := &Block{time.Now().Unix(), []byte(data), prevBlock, []byte("")}
block.SetHash()
return block
}
// NewGenesisBlock creates and returns genesis Block
func NewGenesisBlock() *Block {
return NewBlock("Genesis Block", []byte("0"))
}
// Blockchain keeps a sequence of Blocks
type Blockchain struct {
blocks []*Block
}
// AddBlock saves provided data as a block in the blockchain
func (bc *Blockchain) AddBlock(data string) {
prevBlock := bc.blocks[len(bc.blocks)-1]
newBlock := &Block{time.Now().Unix(), []byte(data), prevBlock.hash, []byte("")}
newBlock.SetHash()
bc.blocks = append(bc.blocks, newBlock)
}
// NewBlockchain creates a new Blockchain with genesis Block
func NewBlockchain() *Blockchain {
return &Blockchain{[]*Block{NewGenesisBlock()}}
}
func main() {
bc := NewBlockchain()