-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.go
67 lines (57 loc) · 1.55 KB
/
blockchain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"crypto/sha256"
"fmt"
)
type Sha256Hash [sha256.Size]byte
type BlockChain struct {
NewestBlockPointer *HashPointer
Blocks map[Sha256Hash]Block
NewestBlockIndex int
}
type HashPointer struct {
Prev *Block
PrevHash Sha256Hash
}
type Block struct {
PrevBlock *HashPointer
Header Sha256Hash
Nonce int
Payload string
}
func main() {
bc := NewBlockChain()
bc.AddBlock("Genesis Block")
fmt.Printf("Current Block %x\n", bc.NewestBlockPointer.PrevHash)
newestBlock := bc.Blocks[bc.NewestBlockPointer.PrevHash]
fmt.Printf("Block Header %x\n", newestBlock.Header)
fmt.Printf("Nonce %d\n", newestBlock.Nonce)
}
func NewBlockChain() *BlockChain {
return &BlockChain{Blocks: make(map[Sha256Hash]Block)}
}
func (bc *BlockChain) AddBlock(payload string) {
candidateBlock := bc.mine(payload, bc.NewestBlockIndex+1)
blockHash := BlockHash(candidateBlock)
bc.Blocks[blockHash] = *candidateBlock
bc.NewestBlockIndex += 1
bc.NewestBlockPointer = &HashPointer{Prev: candidateBlock, PrevHash: blockHash}
}
func (bc *BlockChain) mine(payload string, index int) *Block {
var header Sha256Hash
nonce := 0
for {
trialBlock := Block{Payload: payload, Nonce: nonce, PrevBlock: bc.NewestBlockPointer}
header = BlockHash(&trialBlock)
if header[0] == 0 {
break
} else {
nonce += 1
}
}
return &Block{PrevBlock: bc.NewestBlockPointer, Header: header, Nonce: nonce, Payload: payload}
}
func BlockHash(block *Block) Sha256Hash {
serializedBlock := fmt.Sprintf("%v", block)
return sha256.Sum256([]byte(serializedBlock))
}