Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. func findLongestChain(hashMap map[string]*Block) *Block {
  2. tmp := &Block{}
  3. longestBlockSoFar := &Block{}
  4. longestSoFar := 0
  5. tmpLong := 0
  6. blockMap := hashMap
  7. // iterate blockMap
  8. for k, v := range blockMap {
  9. fmt.Println(k)
  10. tmp = v
  11.  
  12. for i := 0; i < len(blockMap); i++ {
  13.  
  14. // Initial check - Is the precedessor genesis block?
  15. // If so ++ tmpCounter and compare counters and return the final counter.
  16. //TODO : Genesis is hardcoded
  17. if tmp.HashOfPredecessor == "genesis" {
  18.  
  19. tmpLong++
  20. if tmpLong >= longestSoFar {
  21. longestSoFar = tmpLong
  22.  
  23. longestBlockSoFar = v
  24.  
  25. //blocks = append(blocks, longestBlock)
  26.  
  27. }
  28. tmpLong = 0
  29. break
  30.  
  31. }
  32. // check that there is a precedessor, ++ counter
  33. // set tmp block to its precedessor and iterate again.
  34. if tmp.HashOfPredecessor != "" {
  35. tmp = blockMap[tmp.HashOfPredecessor]
  36. tmpLong++
  37.  
  38. }
  39.  
  40. }
  41. }
  42.  
  43. return longestBlockSoFar
  44. }
  45.  
  46. func testLongestChain() {
  47. tmpMap := make(map[string]*Block)
  48. block1 := &Block{}
  49. block1.HashOfPredecessor = "sej" //TODO implement
  50. tmpMap["ro"] = block1
  51.  
  52. block2 := &Block{}
  53. block2.HashOfPredecessor = "fed" //TODO implement
  54. tmpMap["sej"] = block2
  55.  
  56. block3 := &Block{}
  57. block3.HashOfPredecessor = "gert" //TODO implement
  58. tmpMap["fed"] = block3
  59.  
  60. block4 := &Block{}
  61. block4.HashOfPredecessor = "genesis" //TODO implement
  62. tmpMap["gert"] = block4
  63.  
  64. blockJegBrancher := &Block{}
  65. blockJegBrancher.HashOfPredecessor = "sej"
  66. tmpMap["branch"] = blockJegBrancher
  67.  
  68. blockJegBrancher1 := &Block{}
  69. blockJegBrancher1.HashOfPredecessor = "branch"
  70. blockJegBrancher1.Signature = "longest"
  71. tmpMap["branchMere"] = blockJegBrancher1
  72.  
  73. block5 := &Block{}
  74. block5.HashOfPredecessor = "ro"
  75. block5.Signature = "ikkeBranch"
  76. tmpMap["klam"] = block5
  77. findLongestChain(tmpMap)
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement