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.

124 lines
2.8 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. if h == "" {
  73. //no genesis block yet
  74. color.Green(urlAPI + "/blocks/genesis")
  75. res, err := http.Get(urlAPI + "/blocks/genesis")
  76. check(err)
  77. body, err := ioutil.ReadAll(res.Body)
  78. check(err)
  79. err = json.Unmarshal(body, &block)
  80. check(err)
  81. color.Yellow("[New Block]: " + block.Hash)
  82. err = blockchain.addBlock(block)
  83. check(err)
  84. } else {
  85. block.NextHash = h
  86. }
  87. for block.NextHash != "" {
  88. res, err := http.Get(urlAPI + "/blocks/next/" + block.Hash)
  89. check(err)
  90. body, err := ioutil.ReadAll(res.Body)
  91. check(err)
  92. err = json.Unmarshal(body, &block)
  93. check(err)
  94. color.Yellow("[New Block]: " + block.Hash)
  95. err = blockchain.addBlock(block)
  96. check(err)
  97. }
  98. blockchain.print()
  99. }
  100. func (bc *Blockchain) print() {
  101. color.Green("Printing Blockchain stored in memory")
  102. color.Green("Genesis Block: " + bc.GenesisBlock)
  103. for _, b := range bc.Blocks {
  104. color.Green("Block height:")
  105. fmt.Println(b.Height)
  106. color.Green("Hash: " + b.Hash)
  107. color.Green("Date: " + b.Date.String())
  108. color.Green("---")
  109. }
  110. }