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.

115 lines
2.6 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) blockExists(block Block) bool {
  36. for _, b := range bc.Blocks {
  37. if b.Hash == block.Hash {
  38. return true
  39. }
  40. }
  41. return false
  42. }
  43. func (bc *Blockchain) addBlock(block Block) error {
  44. if blockchain.blockExists(block) {
  45. return errors.New("[Error adding Block]: Block already exists in the Blockchain")
  46. }
  47. if len(bc.Blocks) > 0 {
  48. bc.Blocks[len(bc.Blocks)-1].NextHash = block.Hash
  49. } else {
  50. bc.GenesisBlock = block.Hash
  51. }
  52. bc.Blocks = append(bc.Blocks, block)
  53. return nil
  54. }
  55. func reconstructBlockchainFromBlock(urlAPI string, h string) {
  56. color.Yellow("reconstructing the blockchain from last block in memory")
  57. var block Block
  58. var err error
  59. block, err = blockchain.getBlockByHash(h)
  60. check(err)
  61. if h == "" {
  62. //no genesis block yet
  63. color.Green(urlAPI + "/blocks/genesis")
  64. res, err := http.Get(urlAPI + "/blocks/genesis")
  65. check(err)
  66. body, err := ioutil.ReadAll(res.Body)
  67. check(err)
  68. err = json.Unmarshal(body, &block)
  69. check(err)
  70. color.Yellow("[New Block]: " + block.Hash)
  71. err = blockchain.addBlock(block)
  72. check(err)
  73. } else {
  74. block.NextHash = h
  75. }
  76. for block.NextHash != "" && block.Hash != "" {
  77. res, err := http.Get(urlAPI + "/blocks/next/" + block.Hash)
  78. check(err)
  79. body, err := ioutil.ReadAll(res.Body)
  80. check(err)
  81. err = json.Unmarshal(body, &block)
  82. check(err)
  83. if block.Hash != "" {
  84. color.Yellow("[New Block]: " + block.Hash)
  85. err = blockchain.addBlock(block)
  86. check(err)
  87. }
  88. }
  89. blockchain.print()
  90. }
  91. func (bc *Blockchain) print() {
  92. color.Green("Printing Blockchain stored in memory")
  93. color.Green("Genesis Block: " + bc.GenesisBlock)
  94. for _, b := range bc.Blocks {
  95. color.Green("Block height:")
  96. fmt.Println(b.Height)
  97. color.Green("Hash: " + b.Hash)
  98. color.Green("Date: " + b.Date.String())
  99. color.Green("---")
  100. }
  101. }