-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
248 lines (204 loc) · 5.41 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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
)
type createUser struct {
name string
}
type Block struct {
Index int `json:"index"`
Timestamp string `json:"timestamp"`
Uid string `json:"uid"`
Name string `json:"name"`
Amount int `json:"amount"`
Is_owner bool `json:"is_owner"`
Hash string `json:"hash"`
PrevHash string `json:"prevHash"`
}
type Trasaction struct {
From string `json:"from"`
To string `json:"to"`
Amount int `json:"amount"`
}
type TransactionResponse struct {
Success bool `json:"success"`
Message string `json:"message"`
}
var Blockchain []Block
func calculateHash(block Block) string {
record := fmt.Sprintf("%v", block.Index) + block.Timestamp + fmt.Sprintf("%v", block.Uid) + block.PrevHash
h := sha256.New()
h.Write([]byte(record))
hashed := h.Sum(nil)
return hex.EncodeToString(hashed)
}
// Create a new user block
func createUserBlock(oldBlock Block, user createUser) (Block, error) {
var newBlock Block
t := time.Now()
name := user.name
id := uuid.New()
newBlock.Index = oldBlock.Index + 1
newBlock.Timestamp = t.String()
newBlock.Uid = id.String()
newBlock.Name = name
newBlock.Amount = 0
newBlock.Is_owner = false
newBlock.PrevHash = oldBlock.Hash
newBlock.Hash = calculateHash(newBlock)
return newBlock, nil
}
func createGensisUserBlock() (Block, error) {
var newBlock Block
t := time.Now()
name := "farhan"
id := uuid.New()
newBlock.Index = 0
newBlock.Timestamp = t.String()
newBlock.Uid = id.String()
newBlock.Name = name
newBlock.Amount = 1000
newBlock.Is_owner = true
newBlock.PrevHash = ""
newBlock.Hash = ""
return newBlock, nil
}
func isBlockValid(newBlock, oldBlock Block) bool {
if oldBlock.Index+1 != newBlock.Index {
return false
}
if oldBlock.Hash != newBlock.PrevHash {
return false
}
if calculateHash(newBlock) != newBlock.Hash {
return false
}
return true
}
func replaceChain(newBlocks []Block) {
if len(newBlocks) > len(Blockchain) {
Blockchain = newBlocks
}
}
func handleGetBlockchain(w http.ResponseWriter, r *http.Request) {
bytes, err := json.Marshal(Blockchain)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// w.WriteHeader(http.StatusAccepted)
w.Header().Set("Content-Type", "application/json")
w.Write(bytes)
}
func respondWithJSON(w http.ResponseWriter, r *http.Request, code int, payload interface{}) {
response, err := json.MarshalIndent(payload, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("HTTP 500: Internal Server Error"))
return
}
w.WriteHeader(code)
w.Write(response)
}
func computeTransaction(t Trasaction) TransactionResponse {
fromIndex := -1
toIndex := -1
for i := 0; i < len(Blockchain); i++ {
if Blockchain[i].Uid == t.From {
if Blockchain[i].Amount < t.Amount {
return TransactionResponse{false, "Insufficient balance"}
} else {
fromIndex = i
}
}
if Blockchain[i].Uid == t.To {
toIndex = i
}
}
if fromIndex != -1 && toIndex != -1 {
Blockchain[fromIndex].Amount = Blockchain[fromIndex].Amount - t.Amount
Blockchain[toIndex].Amount = Blockchain[toIndex].Amount + t.Amount
return TransactionResponse{true, "Transaction successfull"}
}
return TransactionResponse{false, "Transaction failed"}
}
func handleTransaction(w http.ResponseWriter, r *http.Request) {
var t Trasaction
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&t); err != nil {
respondWithJSON(w, r, http.StatusBadRequest, r.Body)
return
}
defer r.Body.Close()
fmt.Println(t, "coming data")
transaction := computeTransaction(t)
fmt.Println(transaction, "transaction")
w.Header().Set("Content-Type", "application/json")
respondWithJSON(w, r, http.StatusAccepted, transaction)
}
func handleCreateUser(w http.ResponseWriter, r *http.Request) {
var u createUser
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&u); err != nil {
respondWithJSON(w, r, http.StatusBadRequest, r.Body)
return
}
defer r.Body.Close()
newBlock, err := createUserBlock(Blockchain[len(Blockchain)-1], u)
if err != nil {
respondWithJSON(w, r, http.StatusInternalServerError, u)
return
}
if isBlockValid(newBlock, Blockchain[len(Blockchain)-1]) {
newBlockchain := append(Blockchain, newBlock)
replaceChain(newBlockchain)
spew.Dump(Blockchain)
}
respondWithJSON(w, r, http.StatusCreated, newBlock)
}
func makeMuxRouter() http.Handler {
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", handleGetBlockchain).Methods("GET")
muxRouter.HandleFunc("/create/", handleCreateUser).Methods("POST")
muxRouter.HandleFunc("/transact/", handleTransaction).Methods("POST")
return muxRouter
}
func run() error {
mux := makeMuxRouter()
httpAddr := os.Getenv("ADDR")
log.Println("Listening on ", os.Getenv("ADDR"))
s := &http.Server{
Addr: "localhost:" + httpAddr,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
if err := s.ListenAndServe(); err != nil {
return err
}
return nil
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal(err)
}
go func() {
genesisBlock, _ := createGensisUserBlock()
spew.Dump(genesisBlock)
Blockchain = append(Blockchain, genesisBlock)
}()
log.Fatal(run())
}