github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/session/pingpong/exchange_messaging.go (about) 1 /* 2 * Copyright (C) 2019 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package pingpong 19 20 import ( 21 "context" 22 "time" 23 24 "github.com/mysteriumnetwork/node/p2p" 25 "github.com/mysteriumnetwork/node/pb" 26 "github.com/mysteriumnetwork/payments/crypto" 27 "github.com/rs/zerolog/log" 28 ) 29 30 const bigIntBase int = 10 31 32 // ExchangeRequest structure represents message from service consumer to send a an exchange message. 33 type ExchangeRequest struct { 34 Message crypto.ExchangeMessage `json:"exchangeMessage"` 35 } 36 37 // ExchangeSender is responsible for sending the exchange messages. 38 type ExchangeSender struct { 39 ch p2p.ChannelSender 40 } 41 42 // NewExchangeSender returns a new instance of exchange message sender. 43 func NewExchangeSender(ch p2p.ChannelSender) *ExchangeSender { 44 return &ExchangeSender{ 45 ch: ch, 46 } 47 } 48 49 // Send sends the given exchange message. 50 func (es *ExchangeSender) Send(em crypto.ExchangeMessage) error { 51 pMessage := &pb.ExchangeMessage{ 52 Promise: &pb.Promise{ 53 ChannelID: em.Promise.ChannelID, 54 Amount: em.Promise.Amount.Text(bigIntBase), 55 Fee: em.Promise.Fee.Text(bigIntBase), 56 Hashlock: em.Promise.Hashlock, 57 R: em.Promise.R, 58 Signature: em.Promise.Signature, 59 ChainID: em.Promise.ChainID, 60 }, 61 AgreementID: em.AgreementID.Text(bigIntBase), 62 AgreementTotal: em.AgreementTotal.Text(bigIntBase), 63 Provider: em.Provider, 64 Signature: em.Signature, 65 HermesID: em.HermesID, 66 ChainID: em.ChainID, 67 } 68 log.Debug().Msgf("Sending P2P message to %q: %s", p2p.TopicPaymentMessage, pMessage.String()) 69 70 ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) 71 defer cancel() 72 _, err := es.ch.Send(ctx, p2p.TopicPaymentMessage, p2p.ProtoMessage(pMessage)) 73 return err 74 }