From 3e85db8d95925b70b18e1c813d73d070e31e1345 Mon Sep 17 00:00:00 2001 From: migwi Date: Tue, 1 Jan 2019 02:44:21 +0300 Subject: [PATCH] Add stack trace implementation to track the current Hub --- analytics/chains.go | 60 +++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/analytics/chains.go b/analytics/chains.go index f426d5d..afd55e6 100644 --- a/analytics/chains.go +++ b/analytics/chains.go @@ -54,10 +54,11 @@ func ChainDiscovery(client *rpcclient.Client, txHash string) ([]*Hub, error) { // back in time when the source for each path can be identified. var hubsChain []*Hub - var depth = 3 + var depth = 2 - for _, val := range tx.Outpoints { - var setCount, hubCount, newDepth int + for _, val := range tx.Outpoints[3:] { + var setCount, hubCount int + var stackTrace []*Hub count := 1 entry := &Hub{ @@ -67,9 +68,9 @@ func ChainDiscovery(client *rpcclient.Client, txHash string) ([]*Hub, error) { } hubCopy := entry - origHub := entry + // origHub := entry - err = handleDepths(entry, hubCopy, origHub, client, count, depth, setCount, newDepth, hubCount) + err = handleDepths(entry, hubCopy, stackTrace, client, count, depth, setCount, hubCount) if err != nil { return nil, err } @@ -82,32 +83,43 @@ func ChainDiscovery(client *rpcclient.Client, txHash string) ([]*Hub, error) { return hubsChain, nil } -func handleDepths(prevHub, curHub, origHub *Hub, client *rpcclient.Client, count, depth, setCount, newDepth, hubCount int) error { - err := curHub.getDepth(client) - if err != nil || count == depth { - return err - } +func handleDepths(curHub, prevHub *Hub, stack []*Hub, client *rpcclient.Client, count, depth, setCount, hubCount int) error { + // Adds items to the stack. + stack = append(stack, curHub) - if hubCount == len(prevHub.Matched[setCount].Inputs) { - hubCount = 0 - count++ + if count == depth || curHub.TxHash == "" { + // backtrack till we find an unvisited Hub. + for { + if len(stack) == 0 { + return nil + } - if setCount+1 < len(prevHub.Matched) { - setCount++ - } else { - setCount = 0 - prevHub = origHub.Matched[setCount].Inputs[hubCount] - } + curHub = stack[len(stack)-1] - } + if hubCount+1 < len(prevHub.Matched[setCount].Inputs) { + hubCount++ + count-- + break + } else if setCount+1 < len(prevHub.Matched) && hubCount == len(prevHub.Matched[setCount].Inputs) { + setCount++ + count-- + hubCount = 0 + break + } - curHub = prevHub.Matched[setCount].Inputs[hubCount] + stack = stack[len(stack)-1:] + } + } - if len(prevHub.Matched) > 0 && hubCount < len(prevHub.Matched[setCount].Inputs) { - hubCount++ + err := curHub.getDepth(client) + if err != nil { + return err } - return handleDepths(prevHub, curHub, origHub, client, count, depth, setCount, newDepth, hubCount) + fmt.Printf(" >>>>>> setCount %v, len %v >>>>> hubCount %v, len %v \n", setCount, len(curHub.Matched), hubCount, len(curHub.Matched[setCount].Inputs)) + curHub = curHub.Matched[setCount].Inputs[hubCount] + + return handleDepths(curHub, prevHub, stack, client, count+1, depth, setCount, hubCount) } // getDepth appends all the sets linked to a given output after a given amount