Step 1 Complete
Create blockchain - Genesis Block - Additional Blocks
This commit is contained in:
parent
889ca490c0
commit
7ffe75002c
2
.gitignore
vendored
2
.gitignore
vendored
@ -24,3 +24,5 @@ _testmain.go
|
||||
*.test
|
||||
*.prof
|
||||
|
||||
# And the binary
|
||||
blockchain-poc
|
||||
|
31
block.go
Normal file
31
block.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Block struct {
|
||||
Timestamp int64
|
||||
Data []byte
|
||||
PrevBlockHash []byte
|
||||
Hash []byte
|
||||
}
|
||||
|
||||
// NewBlock returns a new block, ready to be added to the chain
|
||||
func NewBlock(data string, prevBlockHash []byte) *Block {
|
||||
block := &Block{time.Now().Unix(), []byte(data), prevBlockHash, []byte{}}
|
||||
block.SetHash()
|
||||
return block
|
||||
}
|
||||
|
||||
// SetHash creates the block hash
|
||||
func (b *Block) SetHash() {
|
||||
timestamp := []byte(strconv.FormatInt(b.Timestamp, 10))
|
||||
headers := bytes.Join([][]byte{b.PrevBlockHash, b.Data, timestamp}, []byte{})
|
||||
hash := sha256.Sum256(headers)
|
||||
|
||||
b.Hash = hash[:]
|
||||
}
|
21
blockchain.go
Normal file
21
blockchain.go
Normal file
@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
type Blockchain struct {
|
||||
blocks []*Block
|
||||
}
|
||||
|
||||
func NewBlockchain() *Blockchain {
|
||||
return &Blockchain{[]*Block{NewGenesisBlock()}}
|
||||
}
|
||||
|
||||
// NewGenesisBlock creates a new Genesis Block to start the blockchain
|
||||
func NewGenesisBlock() *Block {
|
||||
return NewBlock("Genesis Block", []byte{})
|
||||
}
|
||||
|
||||
// AddBlock adds a block to the blockchain
|
||||
func (bc *Blockchain) AddBlock(data string) {
|
||||
prevBlock := bc.blocks[len(bc.blocks)-1]
|
||||
newBlock := NewBlock(data, prevBlock.Hash)
|
||||
bc.blocks = append(bc.blocks, newBlock)
|
||||
}
|
16
main.go
Normal file
16
main.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
bc := NewBlockchain()
|
||||
bc.AddBlock("Data 1")
|
||||
bc.AddBlock("Data 2")
|
||||
|
||||
for _, block := range bc.blocks {
|
||||
fmt.Printf("Prev. hash: %x\n", block.PrevBlockHash)
|
||||
fmt.Printf("Data: %s\n", block.Data)
|
||||
fmt.Printf("Hash: %x\n", block.Hash)
|
||||
fmt.Println()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user