diff --git a/addressTree.go b/addressTree.go new file mode 100644 index 0000000..dd639bd --- /dev/null +++ b/addressTree.go @@ -0,0 +1,57 @@ +package main + +import ( + "gopkg.in/mgo.v2/bson" +) + +func upTree(address string, network NetworkModel) NetworkModel { + var upNetwork NetworkModel + + //add address node to network + node := NodeModel{} + err := nodeCollection.Find(bson.M{"id": address}).One(&node) + check(err) + if nodeInNodes(network.Nodes, node) == false { + network.Nodes = append(network.Nodes, node) + } + + //get edges before the address + edges := []EdgeModel{} + err = edgeCollection.Find(bson.M{"to": address}).All(&edges) + check(err) + + for _, e := range edges { + if edgeInEdges(network.Edges, e) == false { + network.Edges = append(network.Edges, e) + } + //get the nodes involved in edges + /* + nodes := []NodeModel{} + err := nodeCollection.Find(bson.M{"id": }).All(&edges) + check(err) + */ + + if e.From != e.To { + upNetwork = upTree(e.From, network) + for _, upN := range upNetwork.Nodes { + if nodeInNodes(network.Nodes, upN) == false { + network.Nodes = append(network.Nodes, upN) + } + } + for _, upE := range upNetwork.Edges { + if edgeInEdges(network.Edges, upE) == false { + network.Edges = append(network.Edges, upE) + } + } + } + } + + return network +} +func addressTree(address string) NetworkModel { + var network NetworkModel + + network = upTree(address, network) + return network + +} diff --git a/exploreBlockchain.go b/exploreBlockchain.go index 74ec621..7caf7cc 100644 --- a/exploreBlockchain.go +++ b/exploreBlockchain.go @@ -4,11 +4,8 @@ import ( "fmt" "strconv" - "gopkg.in/mgo.v2/bson" - "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcrpcclient" - "github.com/fatih/color" ) func explore(client *btcrpcclient.Client, blockHash string) { @@ -111,40 +108,3 @@ func explore(client *btcrpcclient.Client, blockHash string) { fmt.Println(realBlocks) fmt.Println("reached the end of blockchain") } -func addressTree(address string) NetworkModel { - var network NetworkModel - - var currentEdge EdgeModel - currentEdge.From = "a" - currentEdge.To = "b" - for currentEdge.From != currentEdge.To { - color.Green("for") - fmt.Println(address) - //get edges before the address - edges := []EdgeModel{} - err := edgeCollection.Find(bson.M{"to": address}).All(&edges) - check(err) - for _, edge := range edges { - network.Edges = append(network.Edges, edge) - fmt.Println(edge) - } - //get all nodes from edges - for _, edge := range edges { - node := NodeModel{} - err := nodeCollection.Find(bson.M{"id": edge.From}).One(&node) - check(err) - if nodeInNodes(network.Nodes, node) == false { - network.Nodes = append(network.Nodes, node) - } - - err = nodeCollection.Find(bson.M{"id": edge.To}).One(&node) - check(err) - if nodeInNodes(network.Nodes, node) == false { - network.Nodes = append(network.Nodes, node) - } - } - address = edges[0].From - currentEdge = edges[0] - } - return network -} diff --git a/mongoOperations.go b/mongoOperations.go index 8004093..ce27324 100644 --- a/mongoOperations.go +++ b/mongoOperations.go @@ -118,3 +118,11 @@ func nodeInNodes(nodes []NodeModel, node NodeModel) bool { } return false } +func edgeInEdges(edges []EdgeModel, edge EdgeModel) bool { + for _, e := range edges { + if e.From == edge.From && e.To == edge.To { + return true + } + } + return false +}