You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
2.6 KiB

6 years ago
6 years ago
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. blockchainlib "./blockchainlib"
  7. p2plib "./p2plib"
  8. "github.com/fatih/color"
  9. "github.com/gorilla/mux"
  10. )
  11. type Routes []Route
  12. var routes = Routes{
  13. Route{
  14. "Index",
  15. "GET",
  16. "/",
  17. Index,
  18. },
  19. Route{
  20. "GetPeers",
  21. "GET",
  22. "/peers",
  23. GetPeers,
  24. },
  25. Route{
  26. "PostUser",
  27. "POST",
  28. "/register",
  29. PostUser,
  30. },
  31. Route{
  32. "GenesisBlock",
  33. "GET",
  34. "/blocks/genesis",
  35. GenesisBlock,
  36. },
  37. Route{
  38. "NextBlock",
  39. "GET",
  40. "/blocks/next/{blockhash}",
  41. NextBlock,
  42. },
  43. Route{
  44. "LastBlock",
  45. "GET",
  46. "/blocks/last",
  47. LastBlock,
  48. },
  49. }
  50. type Address struct {
  51. Address string `json:"address"` //the pubK of the user, to perform logins
  52. }
  53. func Index(w http.ResponseWriter, r *http.Request) {
  54. fmt.Fprintln(w, tp.ID)
  55. }
  56. func GetPeers(w http.ResponseWriter, r *http.Request) {
  57. jResp, err := json.Marshal(tp.PeersConnections.Outcoming)
  58. check(err)
  59. fmt.Fprintln(w, string(jResp))
  60. }
  61. func PostUser(w http.ResponseWriter, r *http.Request) {
  62. decoder := json.NewDecoder(r.Body)
  63. var address string
  64. err := decoder.Decode(&address)
  65. if err != nil {
  66. panic(err)
  67. }
  68. defer r.Body.Close()
  69. fmt.Println(address)
  70. color.Blue(address)
  71. //TODO add the verification of the address, to decide if it's accepted to create a new Block
  72. block := blockchain.CreateBlock(address)
  73. blockchain.AddBlock(block)
  74. go PropagateBlock(block)
  75. jResp, err := json.Marshal(blockchain)
  76. check(err)
  77. fmt.Fprintln(w, string(jResp))
  78. }
  79. func PropagateBlock(b blockchainlib.Block) {
  80. //prepare the msg to send to all connected peers
  81. var msg p2plib.Msg
  82. msg.Construct("Block", "new block")
  83. bJson, err := json.Marshal(b)
  84. check(err)
  85. msg.Data = []byte(bJson)
  86. msgB := msg.ToBytes()
  87. for _, peer := range tp.PeersConnections.Outcoming.Peers {
  88. if peer.Conn != nil {
  89. _, err := peer.Conn.Write(msgB)
  90. check(err)
  91. }
  92. }
  93. }
  94. func GenesisBlock(w http.ResponseWriter, r *http.Request) {
  95. var genesis blockchainlib.Block
  96. if len(blockchain.Blocks) > 0 {
  97. genesis = blockchain.Blocks[0]
  98. }
  99. jResp, err := json.Marshal(genesis)
  100. check(err)
  101. fmt.Fprintln(w, string(jResp))
  102. }
  103. func NextBlock(w http.ResponseWriter, r *http.Request) {
  104. vars := mux.Vars(r)
  105. blockhash := vars["blockhash"]
  106. currBlock, err := blockchain.GetBlockByHash(blockhash)
  107. check(err)
  108. nextBlock, err := blockchain.GetBlockByHash(currBlock.NextHash)
  109. check(err)
  110. jResp, err := json.Marshal(nextBlock)
  111. check(err)
  112. fmt.Fprintln(w, string(jResp))
  113. }
  114. func LastBlock(w http.ResponseWriter, r *http.Request) {
  115. var genesis blockchainlib.Block
  116. if len(blockchain.Blocks) > 0 {
  117. genesis = blockchain.Blocks[len(blockchain.Blocks)-1]
  118. }
  119. jResp, err := json.Marshal(genesis)
  120. check(err)
  121. fmt.Fprintln(w, string(jResp))
  122. }