-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
306 lines (274 loc) · 6.95 KB
/
main.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package main
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/middleware/logger"
"github.com/kataras/iris/middleware/recover"
uuid "github.com/satori/go.uuid"
)
type Blockchain struct {
chain []*Block
currentTransactions []Transaction
nodes map[string]struct{}
}
type Block struct {
Index int
Timestamp int64
Transactions []Transaction
Proof int
Previous_hash string
}
type Transaction struct {
Sender string `json:"sender"`
Recipient string `json:"recipient"`
Amount int `json:"amount"`
}
type NodeRegistration struct {
Nodes []string `json:"nodes"`
}
func NewBlockchain() *Blockchain {
b := Blockchain{}
genesisHash := sha256.New()
genesisHash.Write([]byte("genesis"))
genesisHashString := hex.EncodeToString(genesisHash.Sum(nil))
b.newBlock(genesisHashString, 100)
fmt.Println("-------------------- genesis hash", genesisHashString)
b.nodes = make(map[string]struct{})
return &b
}
func (b *Blockchain) newBlock(previous_hash string, proof int) *Block {
if previous_hash == "" {
previous_hash = HashBlock(b.lastBlock()) // TBD
}
nb := Block{
Index: len(b.chain) + 1,
Timestamp: time.Now().UnixNano(),
Transactions: b.currentTransactions,
Proof: proof,
Previous_hash: previous_hash,
}
b.currentTransactions = nil
b.chain = append(b.chain, &nb)
fmt.Printf("\nnew block :: %v\n", nb)
return &nb
}
func (b *Blockchain) newTransaction(sender string, recipient string, amount int) int {
ct := Transaction{
Sender: sender,
Recipient: recipient,
Amount: amount,
}
b.currentTransactions = append(b.currentTransactions, ct)
l := b.lastBlock()
return l.Index + 1
}
func (b *Blockchain) proofOfWork(lastProof int) int {
proof := 0
for validProof(lastProof, proof) == false {
proof++
}
return proof
}
func validProof(lastProof int, proof int) bool {
answer := []byte{0}
guess := lastProof * proof
guessHash := sha256.Sum256([]byte(fmt.Sprintf("%d", guess)))
return bytes.Equal(answer, guessHash[len(guessHash)-1:])
}
func (b *Blockchain) registerNode(addr string) {
parsedAddr, err := url.Parse(addr)
if err != nil {
fmt.Printf("\n\nUnable to register wrong URL : %s \n\n", addr)
return
}
x := parsedAddr.String()
b.nodes[x] = struct{}{}
}
func (b *Blockchain) resolveConflict() bool {
neighbours := b.nodes
var newChain []*Block
changed := false
maxLen := len(b.chain)
type response struct {
Chain []*Block
Length int
}
for url := range neighbours {
resp, err := http.Get(url + "/chain")
if err != nil {
fmt.Printf("\n Unalbe to read from node : %s \n", url)
continue
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("\n Unalbe to parse body from node : %s %v \n", url, body)
continue
}
var r response
err = json.Unmarshal(body, &r)
if err != nil {
fmt.Printf("\n Unalbe to unmarshal body from node : %s %v \n", url, body)
continue
}
if r.Length > maxLen && ValidChain(r.Chain) {
maxLen = r.Length
newChain = r.Chain
changed = true
fmt.Println("wooooooooo - updated", maxLen)
}
}
if changed {
fmt.Println("Yes updating our chain now...")
b.chain = newChain
return true
}
return false
}
func (b *Blockchain) lastBlock() Block {
x := b.chain[len(b.chain)-1]
return *x
}
func ValidChain(bc []*Block) bool {
l := len(bc)
if l == 0 {
return false
}
lb := bc[0]
index := 1
fmt.Println("validating.....")
for index < l {
bl := bc[index]
fmt.Println("\n------------\n")
fmt.Printf("\nlast block :: %v\n", lb)
fmt.Printf("\n block :: %#v\n", bl)
fmt.Println("\n------------\n")
if bl.Previous_hash != HashBlock(*lb) {
fmt.Println("hash did not match", bl.Previous_hash, "___", HashBlock(*lb))
return false
}
if !validProof(lb.Proof, bl.Proof) {
fmt.Println("Proof is invalid")
return false
}
lb = bl
index++
}
return true
}
func HashBlock(b Block) string {
newHash := sha256.New()
newHash.Write([]byte(fmt.Sprintf("%#v", b)))
newHashString := hex.EncodeToString(newHash.Sum(nil))
return newHashString
}
func main() {
fmt.Println("hello")
// Generate a globally unique address for this node
nodeID := uuid.NewV4()
bc := NewBlockchain()
app := iris.New()
app.Logger().SetLevel("debug")
// Optionally, add two built'n handlers
// that can recover from any http-relative panics
// and log the requests to the terminal.
app.Use(recover.New())
app.Use(logger.New())
// Method: GET
// Resource: http://localhost:5000/hello
app.Get("/hello", func(ctx iris.Context) {
ctx.JSON(iris.Map{"message": "Hello Iris!"})
})
app.Post("/transactions/new", func(ctx iris.Context) {
var trans Transaction
ctx.ReadJSON(&trans)
index := bc.newTransaction(trans.Sender, trans.Recipient, trans.Amount)
msg := fmt.Sprintf("%s is sending %d to %s", trans.Sender, trans.Amount, trans.Recipient)
type Resp struct {
Message string
BlockIndex int
}
resp := Resp{
Message: msg,
BlockIndex: index,
}
ctx.JSON(resp)
})
app.Get("/mine", func(ctx iris.Context) {
// We run the proof of work algorithm to get the next proof...
lb := bc.lastBlock()
lp := lb.Proof
proof := bc.proofOfWork(lp)
// reward for us
bc.newTransaction("0", nodeID.String(), 1)
// previousHash := HashBlock(lb)
block := bc.newBlock("", proof)
type Resp struct {
Message string
BlockIndex int
Transaction []Transaction
Proof int
PreviousHash string
}
response := Resp{
Message: "New Block Forged",
BlockIndex: block.Index,
Transaction: block.Transactions,
Proof: block.Proof,
PreviousHash: block.Previous_hash,
}
ctx.JSON(response)
})
// Method: GET
// Resource: http://localhost:5000/chain
app.Get("/chain", func(ctx iris.Context) {
ctx.JSON(iris.Map{"chain": bc.chain, "length": len(bc.chain)})
})
// Method: POST
// Resource: http://localhost:5000/nodes/register
app.Post("/nodes/register", func(ctx iris.Context) {
var reg NodeRegistration
ctx.ReadJSON(®)
if len(reg.Nodes) == 0 {
ctx.JSON(iris.Map{"error": "Empty list of nodes"})
return
}
for _, url := range reg.Nodes {
bc.registerNode(url)
}
ctx.JSON(iris.Map{"message": "new nodes are registered", "nodes": bc.nodes})
})
// Method: GET
// Resource: http://localhost:5000/nodes/resolve
app.Get("/nodes/resolve", func(ctx iris.Context) {
replaced := bc.resolveConflict()
type Resp struct {
Message string
Chain []*Block
}
var r Resp
r.Chain = bc.chain
if replaced {
r.Message = "our chain was replaced"
} else {
r.Message = "our chain is authoritative"
}
ctx.JSON(r)
})
// http://localhost:5000
// http://localhost:5000/ping
// http://localhost:5000/hello
var port string
flag.StringVar(&port, "port", "5000", "port value in string")
flag.Parse()
app.Run(iris.Addr(":"+port), iris.WithoutServerError(iris.ErrServerClosed))
}