-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
305 lines (263 loc) · 8.1 KB
/
main.go
File metadata and controls
305 lines (263 loc) · 8.1 KB
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
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"go-spew/spew"
"io"
"log"
"net/http"
_ "net/http/pprof"
// "net/http/pprof"
"os"
"time"
"github.com/pkg/profile"
"github.com/gorilla/mux"
"godotenv"
)
// type Account struct {
// Taud int //total audience
// Tjournals int
// }
// type Journal struct {
// Jtopic string
// Jarea string
// Jwriter string
// country string
// }
// type WriterDetails struct {
// Wname string
// Country string
// Profession string
// PastWorksInShort string
// }
// type Book struct {
// Bname string
// Writer string
// WriterDetails
// Journal
// Audience
// Account
// }
// type Audience struct{
// name string
// country string
// }
type hypotenuse struct {
trianglePlaneAngle float32 // +ve represent Asset's Hypotenuse & -ve represent Currency's hypotenuse
Hash string
length int
MerkelRoot string
Index int
Timestamp string
height //PrevHash string including genesis block and it contains unique describtor of an organisation
}
type base struct {
currency int
}
type height struct {
CommHash string //Community Hash which stays unique
//OtherSide string
op int
MerkelRoot string
}
type triangle struct { //triangle = Block
hypotenuse //Hash string
base //currency
OtherSide
}
type OtherSide struct {
trianglePlaneAngle float32 // +ve represent Asset's Hypotenuse & -ve represent Currency's hypotenuse
Hash string
length int
MerkelRoot string
Index int
Timestamp string
height
}
var trianglechain []triangle
func calculatehash(triangle triangle) string {
record := string(triangle.hypotenuse.Index) + triangle.hypotenuse.Timestamp + string(triangle.base.currency) + triangle.hypotenuse.MerkelRoot + string(triangle.hypotenuse.length) + fmt.Sprintf("%f", triangle.hypotenuse.trianglePlaneAngle) + triangle.hypotenuse.height.CommHash + string(triangle.hypotenuse.height.op) + triangle.hypotenuse.height.MerkelRoot
h := sha256.New()
h.Write([]byte(record))
hashed := h.Sum(nil)
fmt.Println(hashed)
//triangle.hypotenuse.hash = hashed , we will be doing in caller function
return hex.EncodeToString(hashed)
}
func generatetriangle( /*oldtriangle triangle */ oldtriangle triangle, b int) (triangle, error) {
var newtriangle triangle
t := time.Now()
newtriangle.hypotenuse.Index = oldtriangle.hypotenuse.Index + 1
newtriangle.hypotenuse.Timestamp = t.String()
newtriangle.hypotenuse.length = oldtriangle.hypotenuse.length + 6
newtriangle.hypotenuse.MerkelRoot = oldtriangle.hypotenuse.MerkelRoot
newtriangle.OtherSide.Hash = oldtriangle.hypotenuse.Hash
newtriangle.OtherSide.Index = oldtriangle.hypotenuse.Index
newtriangle.OtherSide.length = oldtriangle.hypotenuse.length
newtriangle.OtherSide.MerkelRoot = oldtriangle.hypotenuse.MerkelRoot
newtriangle.OtherSide.Timestamp = oldtriangle.hypotenuse.Timestamp
newtriangle.OtherSide.trianglePlaneAngle = oldtriangle.hypotenuse.trianglePlaneAngle
newtriangle.base.currency = b
newtriangle.hypotenuse.Hash = calculatehash(newtriangle)
fmt.Println(newtriangle.hypotenuse.Hash)
return newtriangle, nil
}
func istriangleValid(newtriangle, oldtriangle triangle) bool {
if oldtriangle.hypotenuse.Index+1 != newtriangle.hypotenuse.Index {
return false
}
if oldtriangle.hypotenuse.Hash != newtriangle.OtherSide.Hash {
return false
}
if calculatehash(newtriangle) != newtriangle.hypotenuse.Hash {
return false
}
return true
}
func replaceChain(newtriangles []triangle) {
if len(newtriangles) > len(trianglechain) {
trianglechain = newtriangles
}
}
func run() error {
mux := makeMuxRouter()
httpAddr := os.Getenv("ADDR")
log.Println("Listening on ", os.Getenv("ADDR"))
s := &http.Server{
Addr: ":" + httpAddr,
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
err := s.ListenAndServe()
if err != nil {
return err
}
return nil
}
func makeMuxRouter() http.Handler {
muxRouter := mux.NewRouter()
muxRouter.HandleFunc("/", handleGettrianglechain).Methods("GET")
muxRouter.HandleFunc("/", handleWritetriangle).Methods("POST")
return muxRouter
}
func handleGettrianglechain(w http.ResponseWriter, r *http.Request) {
bytes, err := json.MarshalIndent(trianglechain, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
io.WriteString(w, string(bytes))
}
type message struct {
Currency int
}
func handleWritetriangle(w http.ResponseWriter, r *http.Request) {
var m message
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&m); err != nil {
respondWithJson(w, r, http.StatusBadRequest, r.Body)
return
}
defer r.Body.Close()
newtriangle, err := generatetriangle(trianglechain[len(trianglechain)-1], m.Currency)
if err != nil {
respondWithJson(w, r, http.StatusInternalServerError, m)
return
}
if istriangleValid(newtriangle, trianglechain[len(trianglechain)-1]) {
newtrianglechain := append(trianglechain, newtriangle)
replaceChain(newtrianglechain)
spew.Dump(trianglechain)
}
respondWithJson(w, r, http.StatusCreated, newtriangle)
}
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)
}
// var cpuprofile = flag.String("cpuprofile", "", "./cpu.prof")
// var memprofile = flag.String("memprofile", "", "./mem.prof")
func main() {
// flag.Parse()
// if *cpuprofile != "" {
// f, err := os.Create(*cpuprofile)
// if err != nil {
// log.Fatal("could not create CPU profile: ", err)
// }
// if err := pprof.StartCPUProfile(f); err != nil {
// log.Fatal("could not start CPU profile: ", err)
// }
// defer pprof.StopCPUProfile()
// }
// // ... rest of the program ...
// if *memprofile != "" {
// f, err := os.Create(*memprofile)
// if err != nil {
// log.Fatal("could not create memory profile: ", err)
// }
// runtime.GC() // get up-to-date statistics
// if err := pprof.WriteHeapProfile(f); err != nil {
// log.Fatal("could not write memory profile: ", err)
// }
// f.Close()
// }
defer profile.Start().Stop()
err := godotenv.Load()
if err != nil {
log.Fatal(err)
}
// bookGenesis:= new(Book)
// bookGenesis.Bname= "Ramayan"
// bookGenesis.WriterDetails.Country="India"
// bookGenesis.WriterDetails.Wname="Valimiki"
// bookGenesis.WriterDetails.Profession="Sage"
// bookGenesis.WriterDetails.PastWorksInShort="Always was a devotee of Lord Rama"
// bookGenesis.Journal.Jtopic=""
// bookGenesis.Journal.Jarea=""
// bookGenesis.Journal.Jwriter=""
// bookGenesis.Journal.country=""
// bookGenesis.Audience.country=""
// bookGenesis.Audience.name=""
// bookGenesis.Account.Taud=0
// bookGenesis.Account.Tjournals=0 // abhi is arg ko lena hai batch banane ke samay
// fmt.Println(bookGenesis)
// //fmt.Println(calculatehash(triangle{ 1, time.Now().String(), *bookGenesis , "", "" }))
// newtriangle.hypotenuse.Index = oldtriangle.hypotenuse.Index +1
// newtriangle.hypotenuse.Timestamp= t.String()
// newtriangle.hypotenuse.length= oldtriangle.hypotenuse.length + 6
// newtriangle.hypotenuse.MerkelRoot= oldtriangle.hypotenuse.MerkelRoot
go func() {
t := time.Now()
var genesisT triangle
genesisT.hypotenuse.Index = 0
genesisT.hypotenuse.Timestamp = t.String()
genesisT.hypotenuse.length = 3
genesisT.hypotenuse.MerkelRoot = "kumar sandeep"
genesisT.OtherSide.Hash = "Shiva"
genesisT.OtherSide.Index = 0
genesisT.OtherSide.length = 0
genesisT.OtherSide.MerkelRoot = "shiva"
genesisT.OtherSide.Timestamp = t.String()
genesisT.OtherSide.trianglePlaneAngle = 90
genesisT.base.currency = 3
genesisT.hypotenuse.Hash = calculatehash(genesisT)
// genesistriangle , err:= generatetriangle(triangle{ 1, t.String(), *bookGenesis , "", "" }, )
// if err!= nil{
// log.Fatal(err)
// }
trianglechain = append(trianglechain, genesisT)
fmt.Println(trianglechain[0])
//
}()
log.Fatal(run())
}