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.

71 lines
1.7 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "github.com/fatih/color"
  6. "gopkg.in/mgo.v2/bson"
  7. )
  8. func upTree(address string, network NetworkModel) NetworkModel {
  9. var upNetwork NetworkModel
  10. //add address node to network
  11. node := NodeModel{}
  12. err := nodeCollection.Find(bson.M{"id": address}).One(&node)
  13. check(err)
  14. if nodeInNodes(network.Nodes, node) == false {
  15. network.Nodes = append(network.Nodes, node)
  16. }
  17. //get edges before the address
  18. edges := []EdgeModel{}
  19. err = edgeCollection.Find(bson.M{"to": address}).All(&edges)
  20. check(err)
  21. for _, e := range edges {
  22. if edgeInEdges(network.Edges, e) == false {
  23. network.Edges = append(network.Edges, e)
  24. }
  25. //get the nodes involved in edges
  26. /*
  27. nodes := []NodeModel{}
  28. err := nodeCollection.Find(bson.M{"id": }).All(&edges)
  29. check(err)
  30. */
  31. endBranch := false
  32. edgeUpCheck := EdgeModel{}
  33. err := edgeCollection.Find(bson.M{"to": e.From}).One(&edgeUpCheck)
  34. if err != nil {
  35. log.Println(err)
  36. color.Blue("not found")
  37. endBranch = true
  38. }
  39. //need to be fixed when there is a bucle between the addresses (A-->B, B-->C, C-->A)
  40. fmt.Println(e.From + " - " + e.To)
  41. if edgeInEdges(network.Edges, edgeUpCheck) == false && endBranch == false && edgeUpCheck.BlockHeight <= e.BlockHeight && e.To != edgeUpCheck.From {
  42. upNetwork = upTree(e.From, network)
  43. for _, upN := range upNetwork.Nodes {
  44. if nodeInNodes(network.Nodes, upN) == false {
  45. network.Nodes = append(network.Nodes, upN)
  46. }
  47. }
  48. for _, upE := range upNetwork.Edges {
  49. if edgeInEdges(network.Edges, upE) == false {
  50. network.Edges = append(network.Edges, upE)
  51. }
  52. }
  53. }
  54. }
  55. return network
  56. }
  57. func addressTree(address string) NetworkModel {
  58. var network NetworkModel
  59. network = upTree(address, network)
  60. return network
  61. }