Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func findLongestChain(hashMap map[string]*Block) *Block {
- tmp := &Block{}
- longestBlockSoFar := &Block{}
- longestSoFar := 0
- tmpLong := 0
- blockMap := hashMap
- // iterate blockMap
- for k, v := range blockMap {
- fmt.Println(k)
- tmp = v
- for i := 0; i < len(blockMap); i++ {
- // Initial check - Is the precedessor genesis block?
- // If so ++ tmpCounter and compare counters and return the final counter.
- //TODO : Genesis is hardcoded
- if tmp.HashOfPredecessor == "genesis" {
- tmpLong++
- if tmpLong >= longestSoFar {
- longestSoFar = tmpLong
- longestBlockSoFar = v
- //blocks = append(blocks, longestBlock)
- }
- tmpLong = 0
- break
- }
- // check that there is a precedessor, ++ counter
- // set tmp block to its precedessor and iterate again.
- if tmp.HashOfPredecessor != "" {
- tmp = blockMap[tmp.HashOfPredecessor]
- tmpLong++
- }
- }
- }
- return longestBlockSoFar
- }
- func testLongestChain() {
- tmpMap := make(map[string]*Block)
- block1 := &Block{}
- block1.HashOfPredecessor = "sej" //TODO implement
- tmpMap["ro"] = block1
- block2 := &Block{}
- block2.HashOfPredecessor = "fed" //TODO implement
- tmpMap["sej"] = block2
- block3 := &Block{}
- block3.HashOfPredecessor = "gert" //TODO implement
- tmpMap["fed"] = block3
- block4 := &Block{}
- block4.HashOfPredecessor = "genesis" //TODO implement
- tmpMap["gert"] = block4
- blockJegBrancher := &Block{}
- blockJegBrancher.HashOfPredecessor = "sej"
- tmpMap["branch"] = blockJegBrancher
- blockJegBrancher1 := &Block{}
- blockJegBrancher1.HashOfPredecessor = "branch"
- blockJegBrancher1.Signature = "longest"
- tmpMap["branchMere"] = blockJegBrancher1
- block5 := &Block{}
- block5.HashOfPredecessor = "ro"
- block5.Signature = "ikkeBranch"
- tmpMap["klam"] = block5
- findLongestChain(tmpMap)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement