Skip to content

Instantly share code, notes, and snippets.

View morris-ribs's full-sized avatar

Mauricio Ribeiro morris-ribs

View GitHub Profile
//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)
//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
// Routes defines the list of routes of our API
type Routes []Route
var routes = Routes{
//...
Route{
"GetBetsForMatch",
"GET",
"/match/{matchId}",
controller.GetBetsForMatch,
//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)
//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
// Routes defines the list of routes of our API
type Routes []Route
var routes = Routes{
//...
Route{
"Consensus",
"GET",
"/consensus",
controller.Consensus,
//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)
//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)
//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)
}
}
// Routes defines the list of routes of our API
type Routes []Route
var routes = Routes{
//...
Route{
"RegisterAndBroadcastNode",
"POST",
"/register-and-broadcast-node",
controller.RegisterAndBroadcastNode,