From 20b740ccb0d3e74d8ae5e21c6a008bbb22a30754 Mon Sep 17 00:00:00 2001 From: Kulwinder Date: Sun, 22 Oct 2023 22:53:41 +0530 Subject: [PATCH] added get all blocks fucctionality and first block initialization with dummy hash value --- controllers/block-controllers.go | 47 ++++++++++++++++++++++++++------ go.mod | 2 ++ go.sum | 2 ++ main.go | 2 ++ 4 files changed, 45 insertions(+), 8 deletions(-) diff --git a/controllers/block-controllers.go b/controllers/block-controllers.go index d687b00..d4f1b07 100644 --- a/controllers/block-controllers.go +++ b/controllers/block-controllers.go @@ -4,22 +4,53 @@ import ( "encoding/json" "io" "net/http" + "sync" + "time" + + "github.com/KulwinderSingh07/POW-Blockchain/model" + "github.com/davecgh/go-spew/spew" ) +var NewBlock model.Block + +var Blockchain []model.Block + +var difficulty = 1 + +var mutex = &sync.Mutex{} + +func Blockinitalizer() { + t := time.Now() + genesisBlock := model.Block{} + genesisBlock = model.Block{ + Index: 0, + Timestamp: t.String(), + Data: 0, + Hash: calculateHash(genesisBlock), + PrevHash: "", + Difficulty: difficulty, + Nonce: "", + } + spew.Dump(genesisBlock) + + mutex.Lock() + Blockchain = append(Blockchain, genesisBlock) + mutex.Unlock() +} + +func calculateHash(b model.Block) string { + return "hello" +} + func HandleGetBlockchain(res http.ResponseWriter, req *http.Request) { - data, err := json.Marshal("hello") + bytes, err := json.MarshalIndent(Blockchain, "", " ") if err != nil { http.Error(res, err.Error(), http.StatusInternalServerError) return } - io.WriteString(res, string(data)) + io.WriteString(res, string(bytes)) } func HandleWriteBlock(res http.ResponseWriter, req *http.Request) { - data, err := json.Marshal("hello ji") - if err != nil { - http.Error(res, err.Error(), http.StatusInternalServerError) - return - } - io.WriteString(res, string(data)) + } diff --git a/go.mod b/go.mod index dbc91e0..5faf23a 100644 --- a/go.mod +++ b/go.mod @@ -6,3 +6,5 @@ require ( github.com/gorilla/mux v1.8.0 github.com/joho/godotenv v1.5.1 ) + +require github.com/davecgh/go-spew v1.1.1 diff --git a/go.sum b/go.sum index 176459b..3233bbe 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= diff --git a/main.go b/main.go index fac45af..764dc97 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "os" "time" + "github.com/KulwinderSingh07/POW-Blockchain/controllers" "github.com/KulwinderSingh07/POW-Blockchain/model" "github.com/KulwinderSingh07/POW-Blockchain/routes" "github.com/joho/godotenv" @@ -20,6 +21,7 @@ func main() { if err != nil { log.Fatal(err) } + controllers.Blockinitalizer() log.Fatal(run()) }