github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/consumer/session/session_storage_test.go (about)

     1  /*
     2   * Copyright (C) 2018 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 session
    19  
    20  import (
    21  	"math/big"
    22  	"os"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/mysteriumnetwork/node/core/connection/connectionstate"
    28  	"github.com/mysteriumnetwork/node/core/discovery/proposal"
    29  	"github.com/mysteriumnetwork/node/core/storage/boltdb"
    30  	"github.com/mysteriumnetwork/node/identity"
    31  	"github.com/mysteriumnetwork/node/market"
    32  	session_node "github.com/mysteriumnetwork/node/session"
    33  	session_event "github.com/mysteriumnetwork/node/session/event"
    34  	"github.com/mysteriumnetwork/node/session/pingpong/event"
    35  	"github.com/mysteriumnetwork/payments/crypto"
    36  	"github.com/stretchr/testify/assert"
    37  )
    38  
    39  var (
    40  	serviceSessionMock = session_event.SessionContext{
    41  		ID:         "session1",
    42  		StartedAt:  time.Date(2020, 6, 17, 10, 11, 12, 0, time.UTC),
    43  		ConsumerID: identity.FromAddress("consumer1"),
    44  		HermesID:   common.HexToAddress("0x00000000000000000000000000000000000000AC"),
    45  		Proposal: market.ServiceProposal{
    46  			Location:    stubLocation,
    47  			ServiceType: "serviceType",
    48  			ProviderID:  "providerID",
    49  		},
    50  	}
    51  	connectionSessionMock = connectionstate.Status{
    52  		StartedAt:  time.Date(2020, 4, 1, 10, 11, 12, 0, time.UTC),
    53  		SessionID:  session_node.ID("sessionID"),
    54  		ConsumerID: identity.FromAddress("consumerID"),
    55  		HermesID:   common.HexToAddress("0x00000000000000000000000000000000000000AC"),
    56  		Proposal: proposal.PricedServiceProposal{
    57  			ServiceProposal: market.ServiceProposal{
    58  				Location:    stubLocation,
    59  				ServiceType: "serviceType",
    60  				ProviderID:  "providerID",
    61  			},
    62  		},
    63  	}
    64  	connectionStatsMock   = connectionstate.Statistics{BytesReceived: 100000, BytesSent: 50000}
    65  	connectionInvoiceMock = crypto.Invoice{AgreementID: big.NewInt(10), AgreementTotal: big.NewInt(1000), TransactorFee: big.NewInt(10)}
    66  )
    67  
    68  func TestSessionStorage_GetAll(t *testing.T) {
    69  	// given
    70  	sessionExpected := History{
    71  		SessionID:       session_node.ID("session1"),
    72  		Direction:       "Provided",
    73  		ConsumerID:      identity.FromAddress("consumer1"),
    74  		HermesID:        "0x00000000000000000000000000000000000000AC",
    75  		ProviderID:      identity.FromAddress("providerID"),
    76  		ServiceType:     "serviceType",
    77  		ProviderCountry: "MU",
    78  		Started:         time.Date(2020, 6, 17, 10, 11, 12, 0, time.UTC),
    79  		Status:          "New",
    80  	}
    81  	storage, storageCleanup := newStorageWithSessions(sessionExpected)
    82  	defer storageCleanup()
    83  
    84  	// when
    85  	result, err := storage.GetAll()
    86  	// then
    87  	assert.Nil(t, err)
    88  	assert.Equal(t, []History{sessionExpected}, result)
    89  }
    90  
    91  func TestSessionStorage_List(t *testing.T) {
    92  	// given
    93  	session1Expected := History{
    94  		SessionID: session_node.ID("session1"),
    95  		Started:   time.Date(2020, 6, 17, 0, 0, 1, 0, time.UTC),
    96  	}
    97  	session2Expected := History{
    98  		SessionID: session_node.ID("session2"),
    99  		Started:   time.Date(2020, 6, 17, 0, 0, 2, 0, time.UTC),
   100  	}
   101  	storage, storageCleanup := newStorageWithSessions(session1Expected, session2Expected)
   102  	defer storageCleanup()
   103  
   104  	// when
   105  	result, err := storage.List(NewFilter())
   106  	// then
   107  	assert.Nil(t, err)
   108  	assert.Equal(t, []History{session2Expected, session1Expected}, result)
   109  }
   110  
   111  func TestSessionStorage_ListFiltersDirection(t *testing.T) {
   112  	// given
   113  	sessionExpected := History{
   114  		SessionID: session_node.ID("session1"),
   115  		Direction: DirectionProvided,
   116  	}
   117  	storage, storageCleanup := newStorageWithSessions(sessionExpected)
   118  	defer storageCleanup()
   119  
   120  	// when
   121  	result, err := storage.List(NewFilter())
   122  	// then
   123  	assert.Nil(t, err)
   124  	assert.Equal(t, []History{sessionExpected}, result)
   125  
   126  	// when
   127  	result, err = storage.List(NewFilter().SetDirection(DirectionProvided))
   128  	// then
   129  	assert.Nil(t, err)
   130  	assert.Equal(t, []History{sessionExpected}, result)
   131  
   132  	// when
   133  	result, err = storage.List(NewFilter().SetDirection(DirectionConsumed))
   134  	// then
   135  	assert.Nil(t, err)
   136  	assert.Equal(t, []History{}, result)
   137  }
   138  
   139  func TestSessionStorage_Stats(t *testing.T) {
   140  	// given
   141  	sessionExpected := History{
   142  		SessionID:    session_node.ID("session1"),
   143  		Direction:    "Provided",
   144  		ConsumerID:   identity.FromAddress("consumer1"),
   145  		DataSent:     1234,
   146  		DataReceived: 123,
   147  		Tokens:       big.NewInt(12),
   148  		Started:      time.Date(2020, 6, 17, 10, 11, 12, 0, time.UTC),
   149  		Updated:      time.Date(2020, 6, 17, 10, 11, 32, 0, time.UTC),
   150  		Status:       "New",
   151  	}
   152  	storage, storageCleanup := newStorageWithSessions(sessionExpected)
   153  	defer storageCleanup()
   154  
   155  	// when
   156  	result, err := storage.Stats(NewFilter())
   157  	// then
   158  	assert.Nil(t, err)
   159  	assert.Equal(
   160  		t,
   161  		Stats{
   162  			Count: 1,
   163  			ConsumerCounts: map[identity.Identity]int{
   164  				identity.FromAddress("consumer1"): 1,
   165  			},
   166  			SumDataSent:     1234,
   167  			SumDataReceived: 123,
   168  			SumTokens:       big.NewInt(12),
   169  			SumDuration:     20 * time.Second,
   170  		},
   171  		result,
   172  	)
   173  
   174  	// when
   175  	result, err = storage.Stats(NewFilter().SetDirection(DirectionConsumed))
   176  	// then
   177  	assert.Nil(t, err)
   178  	assert.Equal(t, NewStats(), result)
   179  }
   180  
   181  func TestSessionStorage_StatsByDay(t *testing.T) {
   182  	// given
   183  	sessionExpected := History{
   184  		SessionID:    session_node.ID("session1"),
   185  		Direction:    "Provided",
   186  		ConsumerID:   identity.FromAddress("consumer1"),
   187  		DataSent:     1234,
   188  		DataReceived: 123,
   189  		Tokens:       big.NewInt(12),
   190  		Started:      time.Date(2020, 6, 17, 10, 11, 12, 0, time.UTC),
   191  		Updated:      time.Date(2020, 6, 17, 10, 11, 32, 0, time.UTC),
   192  		Status:       "New",
   193  	}
   194  	storage, storageCleanup := newStorageWithSessions(sessionExpected)
   195  	defer storageCleanup()
   196  
   197  	// when
   198  	filter := NewFilter().
   199  		SetStartedFrom(time.Date(2020, 6, 1, 0, 0, 0, 0, time.UTC)).
   200  		SetStartedTo(time.Date(2020, 6, 1, 0, 0, 0, 0, time.UTC))
   201  	result, err := storage.StatsByDay(filter)
   202  	// then
   203  	assert.Nil(t, err)
   204  	assert.Equal(
   205  		t,
   206  		map[time.Time]Stats{
   207  			time.Date(2020, 6, 1, 0, 0, 0, 0, time.UTC): NewStats(),
   208  		},
   209  		result,
   210  	)
   211  
   212  	// when
   213  	filter = NewFilter().
   214  		SetStartedFrom(time.Date(2020, 6, 17, 0, 0, 0, 0, time.UTC)).
   215  		SetStartedTo(time.Date(2020, 6, 18, 0, 0, 0, 0, time.UTC))
   216  	result, err = storage.StatsByDay(filter)
   217  	// then
   218  	assert.Nil(t, err)
   219  	assert.Equal(
   220  		t,
   221  		map[time.Time]Stats{
   222  			time.Date(2020, 6, 17, 0, 0, 0, 0, time.UTC): {
   223  				Count: 1,
   224  				ConsumerCounts: map[identity.Identity]int{
   225  					identity.FromAddress("consumer1"): 1,
   226  				},
   227  				SumDataSent:     1234,
   228  				SumDataReceived: 123,
   229  				SumTokens:       big.NewInt(12),
   230  				SumDuration:     20 * time.Second,
   231  			},
   232  			time.Date(2020, 6, 18, 0, 0, 0, 0, time.UTC): NewStats(),
   233  		},
   234  		result,
   235  	)
   236  
   237  	// when
   238  	filter = NewFilter().
   239  		SetStartedFrom(time.Date(2020, 6, 17, 0, 0, 0, 0, time.UTC)).
   240  		SetStartedTo(time.Date(2020, 6, 18, 0, 0, 0, 0, time.UTC)).
   241  		SetDirection(DirectionConsumed)
   242  	result, err = storage.StatsByDay(filter)
   243  	// then
   244  	assert.Nil(t, err)
   245  	assert.Equal(
   246  		t,
   247  		map[time.Time]Stats{
   248  			time.Date(2020, 6, 17, 0, 0, 0, 0, time.UTC): NewStats(),
   249  			time.Date(2020, 6, 18, 0, 0, 0, 0, time.UTC): NewStats(),
   250  		},
   251  		result,
   252  	)
   253  }
   254  
   255  func TestSessionStorage_consumeServiceSessionsEvent(t *testing.T) {
   256  	// given
   257  	storage, storageCleanup := newStorage()
   258  	storage.timeGetter = func() time.Time {
   259  		return time.Date(2020, 4, 1, 12, 0, 0, 0, time.UTC)
   260  	}
   261  	defer storageCleanup()
   262  
   263  	// when
   264  	storage.consumeServiceSessionEvent(session_event.AppEventSession{
   265  		Status:  session_event.CreatedStatus,
   266  		Session: serviceSessionMock,
   267  	})
   268  	// then
   269  	sessions, err := storage.GetAll()
   270  	assert.Nil(t, err)
   271  	assert.Equal(
   272  		t,
   273  		[]History{
   274  			{
   275  				SessionID:       session_node.ID("session1"),
   276  				Direction:       "Provided",
   277  				ConsumerID:      identity.FromAddress("consumer1"),
   278  				HermesID:        "0x00000000000000000000000000000000000000AC",
   279  				ProviderID:      identity.FromAddress("providerID"),
   280  				ServiceType:     "serviceType",
   281  				ProviderCountry: "MU",
   282  				Started:         time.Date(2020, 6, 17, 10, 11, 12, 0, time.UTC),
   283  				Status:          "New",
   284  				Tokens:          new(big.Int),
   285  			},
   286  		},
   287  		sessions,
   288  	)
   289  
   290  	// when
   291  	storage.consumeServiceSessionStatisticsEvent(session_event.AppEventDataTransferred{
   292  		ID:   serviceSessionMock.ID,
   293  		Up:   123,
   294  		Down: 1234,
   295  	})
   296  	storage.consumeServiceSessionEarningsEvent(session_event.AppEventTokensEarned{
   297  		SessionID: serviceSessionMock.ID,
   298  		Total:     big.NewInt(12),
   299  	})
   300  	storage.consumeServiceSessionEvent(session_event.AppEventSession{
   301  		Status:  session_event.RemovedStatus,
   302  		Session: serviceSessionMock,
   303  	})
   304  	// then
   305  	sessions, err = storage.GetAll()
   306  	assert.Nil(t, err)
   307  	assert.Equal(
   308  		t,
   309  		[]History{
   310  			{
   311  				SessionID:       session_node.ID("session1"),
   312  				Direction:       "Provided",
   313  				ConsumerID:      identity.FromAddress("consumer1"),
   314  				HermesID:        "0x00000000000000000000000000000000000000AC",
   315  				ProviderID:      identity.FromAddress("providerID"),
   316  				ServiceType:     "serviceType",
   317  				ProviderCountry: "MU",
   318  				Started:         time.Date(2020, 6, 17, 10, 11, 12, 0, time.UTC),
   319  				Status:          "Completed",
   320  				Updated:         time.Date(2020, 4, 1, 12, 0, 0, 0, time.UTC),
   321  				DataSent:        1234,
   322  				DataReceived:    123,
   323  				Tokens:          big.NewInt(12),
   324  			},
   325  		},
   326  		sessions,
   327  	)
   328  }
   329  
   330  func TestSessionStorage_consumeEventEndedOK(t *testing.T) {
   331  	// given
   332  	storage, storageCleanup := newStorage()
   333  	storage.timeGetter = func() time.Time {
   334  		return time.Date(2020, 4, 1, 12, 0, 0, 0, time.UTC)
   335  	}
   336  	defer storageCleanup()
   337  
   338  	// when
   339  	storage.consumeConnectionSessionEvent(connectionstate.AppEventConnectionSession{
   340  		Status:      connectionstate.SessionCreatedStatus,
   341  		SessionInfo: connectionSessionMock,
   342  	})
   343  	storage.consumeConnectionStatisticsEvent(connectionstate.AppEventConnectionStatistics{
   344  		Stats:       connectionStatsMock,
   345  		SessionInfo: connectionSessionMock,
   346  	})
   347  	storage.consumeConnectionSessionEvent(connectionstate.AppEventConnectionSession{
   348  		Status:      connectionstate.SessionEndedStatus,
   349  		SessionInfo: connectionSessionMock,
   350  	})
   351  
   352  	// then
   353  	sessions, err := storage.GetAll()
   354  	assert.Nil(t, err)
   355  	assert.Equal(
   356  		t,
   357  		[]History{
   358  			{
   359  				SessionID:       session_node.ID("sessionID"),
   360  				Direction:       "Consumed",
   361  				ConsumerID:      identity.FromAddress("consumerID"),
   362  				HermesID:        "0x00000000000000000000000000000000000000AC",
   363  				ProviderID:      identity.FromAddress("providerID"),
   364  				ServiceType:     "serviceType",
   365  				ProviderCountry: "MU",
   366  				Started:         time.Date(2020, 4, 1, 10, 11, 12, 0, time.UTC),
   367  				Status:          "Completed",
   368  				Updated:         time.Date(2020, 4, 1, 12, 0, 0, 0, time.UTC),
   369  				DataSent:        connectionStatsMock.BytesSent,
   370  				DataReceived:    connectionStatsMock.BytesReceived,
   371  				Tokens:          big.NewInt(0),
   372  			},
   373  		},
   374  		sessions,
   375  	)
   376  }
   377  
   378  func TestSessionStorage_consumeEventConnectedOK(t *testing.T) {
   379  	// given
   380  	storage, storageCleanup := newStorage()
   381  	defer storageCleanup()
   382  
   383  	// when
   384  	storage.consumeConnectionSessionEvent(connectionstate.AppEventConnectionSession{
   385  		Status:      connectionstate.SessionCreatedStatus,
   386  		SessionInfo: connectionSessionMock,
   387  	})
   388  
   389  	// then
   390  	sessions, err := storage.GetAll()
   391  	assert.Nil(t, err)
   392  	assert.Equal(
   393  		t,
   394  		[]History{
   395  			{
   396  				SessionID:       session_node.ID("sessionID"),
   397  				Direction:       "Consumed",
   398  				ConsumerID:      identity.FromAddress("consumerID"),
   399  				HermesID:        "0x00000000000000000000000000000000000000AC",
   400  				ProviderID:      identity.FromAddress("providerID"),
   401  				ServiceType:     "serviceType",
   402  				ProviderCountry: "MU",
   403  				Started:         time.Date(2020, 4, 1, 10, 11, 12, 0, time.UTC),
   404  				Status:          "New",
   405  				Updated:         time.Time{},
   406  				Tokens:          big.NewInt(0),
   407  			},
   408  		},
   409  		sessions,
   410  	)
   411  }
   412  
   413  func TestSessionStorage_consumeSessionSpendingEvent(t *testing.T) {
   414  	// given
   415  	storage, storageCleanup := newStorage()
   416  	storage.timeGetter = func() time.Time {
   417  		return time.Date(2020, 4, 1, 12, 0, 0, 0, time.UTC)
   418  	}
   419  	defer storageCleanup()
   420  
   421  	// when
   422  	storage.consumeConnectionSessionEvent(connectionstate.AppEventConnectionSession{
   423  		Status:      connectionstate.SessionCreatedStatus,
   424  		SessionInfo: connectionSessionMock,
   425  	})
   426  	storage.consumeConnectionSpendingEvent(event.AppEventInvoicePaid{
   427  		ConsumerID: identity.FromAddress("me"),
   428  		SessionID:  "unknown",
   429  		Invoice:    connectionInvoiceMock,
   430  	})
   431  	// then
   432  	sessions, err := storage.GetAll()
   433  	assert.Nil(t, err)
   434  	assert.Equal(
   435  		t,
   436  		[]History{
   437  			{
   438  				SessionID:       session_node.ID("sessionID"),
   439  				Direction:       "Consumed",
   440  				ConsumerID:      identity.FromAddress("consumerID"),
   441  				HermesID:        "0x00000000000000000000000000000000000000AC",
   442  				ProviderID:      identity.FromAddress("providerID"),
   443  				ServiceType:     "serviceType",
   444  				ProviderCountry: "MU",
   445  				Started:         time.Date(2020, 4, 1, 10, 11, 12, 0, time.UTC),
   446  				Status:          "New",
   447  				Updated:         time.Time{},
   448  				Tokens:          big.NewInt(0),
   449  			},
   450  		},
   451  		sessions,
   452  	)
   453  
   454  	// when
   455  	storage.consumeConnectionSpendingEvent(event.AppEventInvoicePaid{
   456  		ConsumerID: identity.FromAddress("me"),
   457  		SessionID:  "sessionID",
   458  		Invoice:    connectionInvoiceMock,
   459  	})
   460  	// then
   461  	sessions, err = storage.GetAll()
   462  	assert.Nil(t, err)
   463  	assert.Equal(
   464  		t,
   465  		[]History{
   466  			{
   467  				SessionID:       session_node.ID("sessionID"),
   468  				Direction:       "Consumed",
   469  				ConsumerID:      identity.FromAddress("consumerID"),
   470  				HermesID:        "0x00000000000000000000000000000000000000AC",
   471  				ProviderID:      identity.FromAddress("providerID"),
   472  				ServiceType:     "serviceType",
   473  				ProviderCountry: "MU",
   474  				Started:         time.Date(2020, 4, 1, 10, 11, 12, 0, time.UTC),
   475  				Status:          "New",
   476  				Updated:         time.Date(2020, 4, 1, 12, 0, 0, 0, time.UTC),
   477  				Tokens:          connectionInvoiceMock.AgreementTotal,
   478  			},
   479  		},
   480  		sessions,
   481  	)
   482  }
   483  
   484  func newStorage() (*Storage, func()) {
   485  	dir, err := os.MkdirTemp("", "sessionStorageTest")
   486  	if err != nil {
   487  		panic(err)
   488  	}
   489  
   490  	db, err := boltdb.NewStorage(dir)
   491  	if err != nil {
   492  		panic(err)
   493  	}
   494  
   495  	return NewSessionStorage(db), func() {
   496  		err := db.Close()
   497  		if err != nil {
   498  			panic(err)
   499  		}
   500  
   501  		err = os.RemoveAll(dir)
   502  		if err != nil {
   503  			panic(err)
   504  		}
   505  	}
   506  }
   507  
   508  func newStorageWithSessions(sessions ...History) (*Storage, func()) {
   509  	storage, storageCleanup := newStorage()
   510  	for _, session := range sessions {
   511  		err := storage.storage.Store(sessionStorageBucketName, &session)
   512  		if err != nil {
   513  			panic(err)
   514  		}
   515  	}
   516  	return storage, storageCleanup
   517  }
   518  
   519  var stubLocation = market.Location{Country: "MU"}