From 271ecdb8eccc307a3d045129fa91a790a480f3c6 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Thu, 9 Sep 2021 12:05:14 -0700 Subject: [PATCH] Fixup Token Requests --- peer/cwtch_peer.go | 1 - protocol/connections/engine.go | 10 +++---- protocol/connections/peerapp.go | 6 ++-- protocol/connections/tokenboardclientapp.go | 33 ++++++++++++--------- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index 34260cc..f9e1d65 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -159,7 +159,6 @@ type ModifyServers interface { // SendMessages enables a caller to sender messages to a contact type SendMessages interface { - SendMessage(handle string, message string) error SendGetValToPeer(string, string, string) diff --git a/protocol/connections/engine.go b/protocol/connections/engine.go index c1f27c9..453df99 100644 --- a/protocol/connections/engine.go +++ b/protocol/connections/engine.go @@ -450,7 +450,7 @@ func (e *engine) sendGetValToPeer(eventID, onion, scope, path string) error { return err } - e.getValRequests.Store(onion + eventID, message) + e.getValRequests.Store(onion+eventID, message) return e.sendPeerMessage(onion, model3.PeerMessage{ID: eventID, Context: event.ContextGetVal, Data: message}) } @@ -519,16 +519,16 @@ func (e *engine) sendMessageToGroup(groupID string, server string, ct []byte, si func (e *engine) handlePeerMessage(hostname string, eventID string, context string, message []byte) { log.Debugf("New message from peer: %v %v", hostname, context) - if context == event.ContextAck { + if context == event.ContextAck { e.peerAck(hostname, eventID) } else if context == event.ContextRetVal { - req, ok := e.getValRequests.Load(hostname + eventID) + req, ok := e.getValRequests.Load(hostname + eventID) if ok { reqStr := req.([]byte) e.handlePeerRetVal(hostname, reqStr, message) - e.getValRequests.Delete(hostname + eventID) + e.getValRequests.Delete(hostname + eventID) } else { - log.Errorf("could not find val request for %v %s",hostname, eventID) + log.Errorf("could not find val request for %v %s", hostname, eventID) } } else if context == event.ContextGetVal { var getVal peerGetVal diff --git a/protocol/connections/peerapp.go b/protocol/connections/peerapp.go index 4f707b8..ec3deb5 100644 --- a/protocol/connections/peerapp.go +++ b/protocol/connections/peerapp.go @@ -79,9 +79,9 @@ func (pa *PeerApp) listen() { var peerMessage model2.PeerMessage err := json.Unmarshal(message, &peerMessage) if err == nil { - if pa.IsAllowed(pa.connection.Hostname()) { - pa.MessageHandler(pa.connection.Hostname(), peerMessage.ID, peerMessage.Context, peerMessage.Data) - } + if pa.IsAllowed(pa.connection.Hostname()) { + pa.MessageHandler(pa.connection.Hostname(), peerMessage.ID, peerMessage.Context, peerMessage.Data) + } } else { log.Errorf("Error unmarshalling PeerMessage package: %x %v", message, err) } diff --git a/protocol/connections/tokenboardclientapp.go b/protocol/connections/tokenboardclientapp.go index cb36cc8..8488ae9 100644 --- a/protocol/connections/tokenboardclientapp.go +++ b/protocol/connections/tokenboardclientapp.go @@ -72,7 +72,6 @@ func (ta *TokenBoardClient) Init(connection tapir.Connection) { go ta.Listen() // Optimistically acquire many tokens for this server... go ta.MakePayment() - go ta.MakePayment() ta.Replay() } else { connection.Close() @@ -179,20 +178,26 @@ func (ta *TokenBoardClient) MakePayment() error { powTokenApp := new(applications.ApplicationChain). ChainApplication(new(applications.ProofOfWorkApplication), applications.SuccessfulProofOfWorkCapability). ChainApplication(tokenApplication, applications.HasTokensCapability) - client.Connect(ta.tokenServiceOnion, powTokenApp) + log.Debugf("Waiting for successful PoW Auth...") - conn, err := client.WaitForCapabilityOrClose(ta.tokenServiceOnion, applications.HasTokensCapability) - if err == nil { - powtapp, _ := conn.App().(*applications.TokenApplication) - // 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 + + connected, err := client.Connect(ta.tokenServiceOnion, powTokenApp) + if connected == true && err == nil { + 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) return err