Skip to content

Commit

Permalink
added get all blocks fucctionality and first block initialization wit…
Browse files Browse the repository at this point in the history
…h dummy hash value
  • Loading branch information
KulwinderSingh07 committed Oct 22, 2023
1 parent 1c84387 commit 20b740c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
47 changes: 39 additions & 8 deletions controllers/block-controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,6 +21,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
controllers.Blockinitalizer()
log.Fatal(run())
}

Expand Down

0 comments on commit 20b740c

Please sign in to comment.