/
gopherchain.go
163 lines (115 loc) · 3.61 KB
/
gopherchain.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"crypto/sha256"
"time"
"encoding/hex"
"strconv"
"container/list"
"fmt"
"os"
"math/rand"
"encoding/binary"
)
type Block struct {
index int
timestamp time.Time
data string
pow string
previousHash string
thisHash string
}
func genesis() Block {
genesisPrevHash := "0000000000000000000000000000000000000000000000000000000000000000"
genesisHash := "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
genesisBlock := Block {0, time.Now(), "This is the Genesis block", "Genesis", genesisPrevHash, genesisHash}
return genesisBlock
}
func nextBlock(lastBlock Block) Block {
blockIndex := lastBlock.index + 1
// append the blockchain height for this block
blockData := "This is block " + strconv.Itoa(blockIndex)
// just some data (should be json)
previousHash := lastBlock.thisHash
// string that contains the hash of the previous block
rand.Seed(time.Now().UnixNano())
// seeding the randomizer based on current time
nonce := make([]byte, 4)
// create a 4 byte long nonce variable
rand.Read(nonce)
// populate nonce with a random value
newnonce := binary.LittleEndian.Uint32(nonce)
// get the 4 byte integer representation of the nonce
var blockStringSum string
// variabel to store a sum of the string to be hashed
target := "00000100000000070BFB00000000000000000000000000000000000000000000"
// ---------------------- <Proof of work> ------------------------------ //
for (previousHash != "") {
// to keep an infinite loop while we do work
var sha = sha256.New()
// new SHA256 "object"
str := fmt.Sprint(newnonce)
// get the unsigned integer into a string
blockStringSum = str + previousHash
// append the nonce to the hash of the previous block.
// - normally this should contain the version, previous hash,
// - nonce, block header, etc.
sha.Write([]byte(blockStringSum))
// pass the "blockStringSum" through SHA256
blockStringSum = hex.EncodeToString(sha.Sum(nil))
// get the digest of the SHA256 function on above string
fmt.Printf(blockStringSum)
fmt.Printf("\n")
if (blockStringSum < target) {
// check if difficulty meets target
break
// if target is met, break the loop
} else {
newnonce++
// if difficulty is not met, append the nonce and try again
sha.Reset()
}
}
t, err := os.Create("height")
check(err)
t.WriteString(strconv.Itoa(blockIndex))
fmt.Printf("\nWritten to file:\n")
t.Sync()
proof := hex.EncodeToString(nonce)
// ---------------------- </Proof of work> ------------------------------ //
return Block {blockIndex, time.Now(), blockData, proof, previousHash, blockStringSum }
}
func check(e error) {
if e != nil {
panic(e)
}
}
func main() {
var blockchain = list.New()
var genesisBlock = genesis()
blockchain.PushBack(genesisBlock)
var previousBlock = genesisBlock
f, err := os.OpenFile("dat", os.O_APPEND|os.O_WRONLY|
os.O_CREATE, 0600)
check(err)
for e:= blockchain.Front(); e != nil; e = e.Next() {
newBlock := nextBlock(previousBlock)
blockchain.PushBack(newBlock)
fmt.Printf("[HEIGHT]: ")
fmt.Printf(strconv.Itoa(newBlock.index))
fmt.Printf("\n")
fmt.Printf("Block ")
fmt.Printf(newBlock.thisHash)
fmt.Printf(" has been added to the blockchain!\n")
heightLabel := "[HEIGHT]: "
height := strconv.Itoa(newBlock.index)
blockLabel := "Block: "
block := newBlock.thisHash
blockInfo := heightLabel + height + " \n" + blockLabel + block + "\n"
n, err := f.WriteString(blockInfo)
check(err)
fmt.Printf("wrote %d bytes\n\n", n)
f.Sync()
previousBlock = newBlock
time.Sleep(3000 * time.Millisecond) //simulate block creation by delaying output
}
}