github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/state/event/event.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 event 19 20 import ( 21 "fmt" 22 "math/big" 23 24 "github.com/ethereum/go-ethereum/common" 25 26 "github.com/mysteriumnetwork/node/consumer/bandwidth" 27 "github.com/mysteriumnetwork/node/consumer/session" 28 "github.com/mysteriumnetwork/node/core/connection/connectionstate" 29 "github.com/mysteriumnetwork/node/datasize" 30 "github.com/mysteriumnetwork/node/identity/registry" 31 "github.com/mysteriumnetwork/node/money" 32 "github.com/mysteriumnetwork/node/session/pingpong" 33 pingpongEvent "github.com/mysteriumnetwork/node/session/pingpong/event" 34 "github.com/mysteriumnetwork/node/tequilapi/contract" 35 "github.com/mysteriumnetwork/payments/crypto" 36 ) 37 38 // AppTopicState is the topic that we use to announce state changes to via the event bus 39 const AppTopicState = "State change" 40 41 // State represents the node state at the current moment. It's a read only object, used only to display data. 42 type State struct { 43 Services []contract.ServiceInfoDTO 44 Sessions []session.History 45 Connections map[string]Connection 46 Identities []Identity 47 ProviderChannels []pingpong.HermesChannel 48 } 49 50 // Identity represents identity and its status. 51 type Identity struct { 52 Address string 53 RegistrationStatus registry.RegistrationStatus 54 ChannelAddress common.Address 55 56 Balance *big.Int 57 Earnings *big.Int 58 EarningsTotal *big.Int 59 EarningsPerHermes map[common.Address]pingpongEvent.Earnings 60 61 HermesID common.Address 62 } 63 64 // Connection represents consumer connection state. 65 type Connection struct { 66 Session connectionstate.Status 67 Statistics connectionstate.Statistics 68 Throughput bandwidth.Throughput 69 Invoice crypto.Invoice 70 } 71 72 func (c Connection) String() string { 73 spent := money.New(big.NewInt(0)) 74 if c.Invoice.AgreementTotal != nil { 75 spent = money.New(c.Invoice.AgreementTotal) 76 } 77 78 return fmt.Sprintf( 79 "ID: %s, state: %s, duration: %s data: %s/%s, throughput: %s/%s, spent: %s", 80 c.Session.SessionID, 81 c.Session.State, 82 c.Session.Duration(), 83 datasize.FromBytes(c.Statistics.BytesReceived), 84 datasize.FromBytes(c.Statistics.BytesSent), 85 c.Throughput.Down, 86 c.Throughput.Up, 87 spent, 88 ) 89 }