diff --git a/protocol/connections/tokenboardclientapp.go b/protocol/connections/tokenboardclientapp.go index df85e2b..36e120a 100644 --- a/protocol/connections/tokenboardclientapp.go +++ b/protocol/connections/tokenboardclientapp.go @@ -71,7 +71,7 @@ func (ta *TokenBoardClient) Init(connection tapir.Connection) { log.Debugf("Successfully Initialized Connection") go ta.Listen() // Optimistically acquire many tokens for this server... - ta.MakePayment() + go ta.MakePayment() ta.Replay() } else { connection.Close() @@ -181,25 +181,29 @@ func (ta *TokenBoardClient) MakePayment() error { log.Debugf("Waiting for successful PoW Auth...") - connected, err := client.Connect(ta.tokenServiceOnion, powTokenApp) - if connected == true && err == nil { - conn, err := client.WaitForCapabilityOrClose(ta.tokenServiceOnion, applications.HasTokensCapability) - if err == nil { - powtapp := conn.App().(*applications.TokenApplication) - if powtapp != nil { - // Update tokens...we need a lock here to prevent SpendToken from modifying the tokens - // during this process.. - log.Debugf("Transcript: %v", powtapp.Transcript().OutputTranscriptToAudit()) - conn.Close() - log.Debugf("Updating Tokens") - ta.tokenLock.Lock() - ta.tokens = append(ta.tokens, powtapp.Tokens...) - if len(ta.tokens) < 5 { - go ta.MakePayment() - } - ta.tokenLock.Unlock() - return nil - } + // We can't have duplicate outbounds... + conn, _ := client.GetConnection(ta.tokenServiceOnion) + for { + if conn == nil { + break + } + conn, _ = client.GetConnection(ta.tokenServiceOnion) + } + + client.Connect(ta.tokenServiceOnion, powTokenApp) + conn, err := client.WaitForCapabilityOrClose(ta.tokenServiceOnion, applications.HasTokensCapability) + if err == nil { + powtapp, ok := conn.App().(*applications.TokenApplication) + if ok { + // Update tokens...we need a lock here to prevent SpendToken from modifying the tokens + // during this process.. + log.Debugf("Updating Tokens") + ta.tokenLock.Lock() + ta.tokens = append(ta.tokens, powtapp.Tokens...) + ta.tokenLock.Unlock() + log.Debugf("Transcript: %v", powtapp.Transcript().OutputTranscriptToAudit()) + conn.Close() + return nil } } log.Debugf("Error making payment: to %v %v", ta.tokenServiceOnion, err)