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.

131 lines
2.9 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "net/http"
  8. "time"
  9. "github.com/fatih/color"
  10. )
  11. type Block struct {
  12. Hash string `json:"hash"`
  13. Height int64 `json:"height"`
  14. Date time.Time `json:"date"`
  15. PreviousHash string `json:"previoushash"`
  16. NextHash string `json:"nexthash"`
  17. Data []Address `json:"data"`
  18. Emitter string `json:"emitter"` //the ID of the peer that has emmited the block
  19. }
  20. type Blockchain struct {
  21. GenesisBlock string `json:"genesisblock"`
  22. LastUpdate time.Time `json:"lastupdate"`
  23. Blocks []Block `json:"blocks"`
  24. }
  25. var blockchain Blockchain
  26. func (bc *Blockchain) getBlockByHash(hash string) (Block, error) {
  27. for _, block := range bc.Blocks {
  28. if block.Hash == hash {
  29. return block, nil
  30. }
  31. }
  32. var b Block
  33. return b, errors.New("Block Hash not found")
  34. }
  35. func (bc *Blockchain) createBlock(address Address) Block {
  36. var b Block
  37. b.Height = int64(len(bc.Blocks))
  38. if len(bc.Blocks) == 0 {
  39. b.Height = 0
  40. } else {
  41. b.PreviousHash = bc.Blocks[len(bc.Blocks)-1].Hash
  42. }
  43. b.Date = time.Now()
  44. b.Data = append(b.Data, address)
  45. b.Emitter = runningPeer.ID
  46. b.Hash = hashBlock(b)
  47. return b
  48. }
  49. func (bc *Blockchain) blockExists(block Block) bool {
  50. for _, b := range bc.Blocks {
  51. if b.Hash == block.Hash {
  52. return true
  53. }
  54. }
  55. return false
  56. }
  57. func (bc *Blockchain) addBlock(block Block) error {
  58. if blockchain.blockExists(block) {
  59. return errors.New("[Error adding Block]: Block already exists in the Blockchain")
  60. }
  61. if len(bc.Blocks) > 0 {
  62. bc.Blocks[len(bc.Blocks)-1].NextHash = block.Hash
  63. } else {
  64. bc.GenesisBlock = block.Hash
  65. }
  66. bc.Blocks = append(bc.Blocks, block)
  67. return nil
  68. }
  69. func reconstructBlockchainFromBlock(urlAPI string, h string) {
  70. color.Yellow("reconstructing the blockchain from last block in memory")
  71. var block Block
  72. var err error
  73. block, err = blockchain.getBlockByHash(h)
  74. check(err)
  75. if h == "" {
  76. //no genesis block yet
  77. color.Green(urlAPI + "/blocks/genesis")
  78. res, err := http.Get(urlAPI + "/blocks/genesis")
  79. check(err)
  80. body, err := ioutil.ReadAll(res.Body)
  81. check(err)
  82. err = json.Unmarshal(body, &block)
  83. check(err)
  84. color.Yellow("[New Block]: " + block.Hash)
  85. err = blockchain.addBlock(block)
  86. check(err)
  87. } else {
  88. block.NextHash = h
  89. }
  90. for block.NextHash != "" && block.Hash != "" {
  91. res, err := http.Get(urlAPI + "/blocks/next/" + block.Hash)
  92. check(err)
  93. body, err := ioutil.ReadAll(res.Body)
  94. check(err)
  95. err = json.Unmarshal(body, &block)
  96. check(err)
  97. if block.Hash != "" {
  98. color.Yellow("[New Block]: " + block.Hash)
  99. err = blockchain.addBlock(block)
  100. check(err)
  101. }
  102. }
  103. blockchain.print()
  104. }
  105. func (bc *Blockchain) print() {
  106. color.Green("Printing Blockchain stored in memory")
  107. color.Green("Genesis Block: " + bc.GenesisBlock)
  108. for _, b := range bc.Blocks {
  109. color.Green("Block height:")
  110. fmt.Println(b.Height)
  111. color.Green("Hash: " + b.Hash)
  112. color.Green("Date: " + b.Date.String())
  113. color.Green("---")
  114. }
  115. }