This file contains hidden or 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
//GetBetsForMatch ... | |
func (b *Blockchain) GetBetsForMatch(matchID string) Bets { | |
matchBets := Bets{} | |
i := 0 | |
chainLength := len(b.Chain) | |
for i < chainLength { | |
block := b.Chain[i] | |
betsInBlock := block.Bets | |
j := 0 | |
betsLength := len(betsInBlock) |
This file contains hidden or 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
//GetBetsForMatch GET /match/{matchId} retrieves all bets for a match | |
func (c *Controller) GetBetsForMatch(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
matchID := strings.ToLower(vars["matchId"]) | |
bets := c.blockchain.GetBetsForMatch(matchID) | |
w.WriteHeader(http.StatusOK) | |
data, _ := json.Marshal(bets) | |
w.Write(data) | |
return |
This file contains hidden or 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
// Routes defines the list of routes of our API | |
type Routes []Route | |
var routes = Routes{ | |
//... | |
Route{ | |
"GetBetsForMatch", | |
"GET", | |
"/match/{matchId}", | |
controller.GetBetsForMatch, |
This file contains hidden or 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
//ChainIsValid Used by consensus algorithm | |
func (b *Blockchain) ChainIsValid() bool { | |
i := 1 | |
for i < len(b.Chain) { | |
currentBlock := b.Chain[i] | |
prevBlock := b.Chain[i-1] | |
currentBlockData := BlockData{Index: strconv.Itoa(prevBlock.Index - 1), Bets: currentBlock.Bets} | |
currentBlockDataAsByteArray, _ := json.Marshal(currentBlockData) | |
currentBlockDataAsStr := base64.URLEncoding.EncodeToString(currentBlockDataAsByteArray) | |
blockHash := b.HashBlock(prevBlock.Hash, currentBlockDataAsStr, currentBlock.Nonce) |
This file contains hidden or 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
//Consensus GET /consensus | |
func (c *Controller) Consensus(w http.ResponseWriter, r *http.Request) { | |
maxChainLength := 0 | |
var longestChain *Blockchain | |
var resp ResponseToSend | |
w.Header().Set("Content-Type", "application/json; charset=UTF-8") | |
for _, node := range c.blockchain.NetworkNodes { | |
if node != c.currentNodeURL { | |
// call /blockchain in node | |
// call url in node |
This file contains hidden or 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
// Routes defines the list of routes of our API | |
type Routes []Route | |
var routes = Routes{ | |
//... | |
Route{ | |
"Consensus", | |
"GET", | |
"/consensus", | |
controller.Consensus, |
This file contains hidden or 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
//RegisterNodesBulk POST /register-nodes-bulk | |
func (c *Controller) RegisterNodesBulk(w http.ResponseWriter, r *http.Request) { | |
body, err := ioutil.ReadAll(r.Body) // read the body of the request | |
if err != nil { | |
log.Fatalln("Error RegisterNodesBulk", err) | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
if err := r.Body.Close(); err != nil { | |
log.Fatalln("Error RegisterNodesBulk", err) |
This file contains hidden or 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
//RegisterNode POST /register-node | |
func (c *Controller) RegisterNode(w http.ResponseWriter, r *http.Request) { | |
body, err := ioutil.ReadAll(r.Body) // read the body of the request | |
if err != nil { | |
log.Fatalln("Error RegisterNode", err) | |
w.WriteHeader(http.StatusInternalServerError) | |
return | |
} | |
if err := r.Body.Close(); err != nil { | |
log.Fatalln("Error RegisterNode", err) |
This file contains hidden or 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
//BroadcastNode broadcasting node | |
func BroadcastNode(newNode string, nodes []string) { | |
for _, node := range nodes { | |
if node != newNode { | |
var registerNodesJSON = []byte(`{"newnodeurl":"` + newNode + `"}`) | |
// call /register-node in node | |
MakePostCall(node+"/register-node", registerNodesJSON) | |
} | |
} |
This file contains hidden or 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
// Routes defines the list of routes of our API | |
type Routes []Route | |
var routes = Routes{ | |
//... | |
Route{ | |
"RegisterAndBroadcastNode", | |
"POST", | |
"/register-and-broadcast-node", | |
controller.RegisterAndBroadcastNode, |
NewerOlder