-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
147 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// cmd/myblockchain/main.go | ||
package main | ||
|
||
import ( | ||
"blockchain/internal/api" | ||
"blockchain/internal/blockchain" | ||
"blockchain/internal/consensus" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
bc := blockchain.NewBlockchain() | ||
|
||
cons := consensus.NewPoWConsensus(&bc) | ||
|
||
router := api.NewRouter(&bc, cons) | ||
|
||
log.Println("Listening on :8080") | ||
log.Fatal(http.ListenAndServe(":8080", router)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module blockchain | ||
|
||
go 1.23.1 | ||
|
||
require github.com/gorilla/mux v1.8.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY= | ||
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"strconv" | ||
|
||
"blockchain/internal/blockchain" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func GetBlockHandler(bc *blockchain.Blockchain) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
vars := mux.Vars(r) | ||
indexStr, ok := vars["index"] | ||
if !ok { | ||
http.Error(w, "Index is required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
index, err := strconv.Atoi(indexStr) | ||
if err != nil { | ||
http.Error(w, "Invalid index format", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
block, err := bc.GetBlock(index) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusNotFound) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(block) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
"blockchain/internal/blockchain" | ||
) | ||
|
||
func GetChainHandler(bc *blockchain.Blockchain) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
response := map[string]interface{}{ | ||
"length": len(bc.Blocks()), | ||
"blocks": bc.Blocks(), | ||
} | ||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// internal/api/router.go | ||
package api | ||
|
||
import ( | ||
"blockchain/internal/blockchain" | ||
"blockchain/internal/consensus" | ||
|
||
"blockchain/internal/api/handlers" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
func NewRouter(bc *blockchain.Blockchain, cons *consensus.PoWConsensus) *mux.Router { | ||
router := mux.NewRouter() | ||
|
||
router.HandleFunc("/chain", handlers.GetChainHandler(bc)).Methods("GET") | ||
router.HandleFunc("/blocks/{index}", handlers.GetBlockHandler(bc)).Methods("GET") | ||
|
||
return router | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,58 @@ | ||
package blockchain | ||
|
||
import ( | ||
"errors" | ||
"sync" | ||
) | ||
|
||
type Blockchain struct { | ||
Blocks []Block | ||
blocks []Block | ||
Difficulty int | ||
mutex sync.Mutex | ||
} | ||
|
||
func NewBlockchain() Blockchain { | ||
return Blockchain{ | ||
Blocks: []Block{GenesisBlock(2)}, | ||
blocks: []Block{GenesisBlock(2)}, | ||
Difficulty: 2, | ||
mutex: sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (bc *Blockchain) Blocks() []Block { | ||
bc.mutex.Lock() | ||
defer bc.mutex.Unlock() | ||
|
||
return bc.blocks | ||
} | ||
|
||
func (bc *Blockchain) AddBlock(block Block) { | ||
bc.Blocks = append(bc.Blocks, block) | ||
bc.mutex.Lock() | ||
defer bc.mutex.Unlock() | ||
|
||
bc.blocks = append(bc.blocks, block) | ||
} | ||
|
||
func (bc *Blockchain) FirstBlock() Block { | ||
return bc.Blocks[0] | ||
bc.mutex.Lock() | ||
defer bc.mutex.Unlock() | ||
|
||
return bc.blocks[0] | ||
} | ||
|
||
func (bc *Blockchain) LastBlock() Block { | ||
return bc.Blocks[len(bc.Blocks)-1] | ||
bc.mutex.Lock() | ||
defer bc.mutex.Unlock() | ||
|
||
return bc.blocks[len(bc.blocks)-1] | ||
} | ||
|
||
func (bc *Blockchain) GetBlock(index int) (Block, error) { | ||
bc.mutex.Lock() | ||
defer bc.mutex.Unlock() | ||
|
||
if index < 0 || index >= len(bc.blocks) { | ||
return Block{}, errors.New("invalid index") | ||
} | ||
return bc.blocks[index], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters