Skip to content

Commit

Permalink
wip: checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed May 29, 2023
1 parent 4dc8486 commit 4b5a5d2
Show file tree
Hide file tree
Showing 14 changed files with 587 additions and 305 deletions.
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pokt-network/go-mockdns v0.0.1 h1:1Kb/kIFH6bNtY9F1bFhJyMRMCc7WyiqfGg0hotBZ1JI=
github.com/pokt-network/go-mockdns v0.0.1/go.mod h1:lgRN6+KxQBawyIghpnl5CezHFGS9VLzvtVlwxvzXTQ4=
github.com/pokt-network/gocuke v0.0.1 h1:qJ/Ryf+hi5L6T9lsOZDNbiAclHkLlDio5/eVKQEYhgE=
github.com/pokt-network/gocuke v0.0.1/go.mod h1:BowLKW4++696gTTU33teodtIhjjyaphEbhQT9D5Refw=
github.com/pokt-network/smt v0.4.0 h1:fodLphes/EtQw+nr6Zc5giSJdnjIkM2BY6cGTOfTaqo=
Expand Down
7 changes: 5 additions & 2 deletions internal/testutil/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ func WithBusEventHandler(
) *mock_modules.MockBus {
t.Helper()

handler := handlerFactory(t, busMock)
busMock.EXPECT().PublishEventToBus(gomock.Any()).Do(handler).AnyTimes()
if handlerFactory != nil {
handler := handlerFactory(t, busMock)
busMock.EXPECT().PublishEventToBus(gomock.Any()).Do(handler).AnyTimes()
}

return busMock
}

Expand Down
135 changes: 76 additions & 59 deletions internal/testutil/constructors/constructors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package constructors

import (
"github.com/foxcpp/go-mockdns"
libp2pHost "github.com/libp2p/go-libp2p/core/host"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/pokt-network/pocket/internal/testutil/bus"
Expand All @@ -25,85 +26,100 @@ type serviceURLStr = string
func NewBusesMocknetAndP2PModules(
t gocuke.TestingT,
count int,
dnsSrv *mockdns.Server,
genesisState *genesis.GenesisState,
busEventHandlerFactory testutil.BusEventHandlerFactory,
) (
buses map[serviceURLStr]*mock_modules.MockBus,
libp2pNetworkMock mocknet.Mocknet,
p2pModules map[serviceURLStr]modules.P2PModule,
) {
// TODO_THIS_COMMIT: refactor
dnsSrv := testutil.MinimalDNSMock(t)

libp2pNetworkMock = mocknet.New()
// destroy mocknet on test cleanup
t.Cleanup(func() {
err := libp2pNetworkMock.Close()
require.NoError(t, err)
})

buses = make(map[serviceURLStr]*mock_modules.MockBus)
p2pModules = make(map[serviceURLStr]modules.P2PModule)
// CONSIDERATION: using an iterator/generator would prevent unintentional
// ID collisions
privKeys := testutil.LoadLocalnetPrivateKeys(t, count)
// CONSIDERATION: using an iterator/generator would prevent unintentional
// serviceURL collisions
serviceURLs := p2p_testutil.SequentialServiceURLs(t, count)
for i, serviceURL := range serviceURLs {
if len(privKeys) <= i {
t.Logf("WARNING: not enough private keys for %d service URLs", len(serviceURLs))
break
}

privKey := privKeys[i]
busMock := bus_testutil.NewBus(t, privKey, serviceURL, genesisState, busEventHandlerFactory)
buses[serviceURL] = busMock

// TODO_THIS_COMMIT: refactor
_ = consensus_testutil.BaseConsensusMock(t, busMock)
_ = persistence_testutil.BasePersistenceMock(t, busMock, genesisState)

// -- option 1
_ = telemetry_testutil.BaseTelemetryMock(t, busMock)

// -- option 2
//_ = telemetry_testutil.WithTimeSeriesAgent(
// t, telemetry_testutil.MinimalTelemetryMock(t, busMock),
//)

// MUST register DNS before instantiating P2PModule
testutil.AddServiceURLZone(t, dnsSrv, serviceURL)

host := p2p_testutil.NewMocknetHost(t, libp2pNetworkMock, privKey)
p2pModules[serviceURL] = NewP2PModuleWithHost(t, busMock, host)
}
libp2pNetworkMock = p2p_testutil.NewLibp2pNetworkMock(t)
serviceURLKeyMap := testutil.SequentialServiceURLPrivKeyMap(t, count)

buses, p2pModules = NewBusesAndP2PModules(
t, busEventHandlerFactory,
dnsSrv,
genesisState,
libp2pNetworkMock,
serviceURLKeyMap,
)
err := libp2pNetworkMock.LinkAll()
require.NoError(t, err)

return buses, libp2pNetworkMock, p2pModules
}

// TODO_THIS_TEST: need this?
func NewP2PModules(
// TODO_THIS_COMMIT: rename / move, if possible
func NewP2PModule(
t gocuke.TestingT,
privKeys []cryptoPocket.PrivateKey,
// TODO_THIS_COMMIT: get these from the bus instead
serviceURL string,
privKey cryptoPocket.PrivateKey,
// --
busMock *mock_modules.MockBus,
genesisState *genesis.GenesisState,
dnsSrv *mockdns.Server,
libp2pNetworkMock mocknet.Mocknet,
// TODO_THIS_COMMIT: consider *p2p.P2PModule instead
) modules.P2PModule {
_ = consensus_testutil.BaseConsensusMock(t, busMock)
_ = persistence_testutil.BasePersistenceMock(t, busMock, genesisState)

// -- option 1
_ = telemetry_testutil.BaseTelemetryMock(t, busMock)

// -- option 2
//_ = telemetry_testutil.WithTimeSeriesAgent(
// t, telemetry_testutil.MinimalTelemetryMock(t, busMock),
//)

// MUST register DNS before instantiating P2PModule
testutil.AddServiceURLZone(t, dnsSrv, serviceURL)

host := testutil.NewMocknetHost(t, libp2pNetworkMock, privKey)
return NewP2PModuleWithHost(t, busMock, host)
}

// TODO_THIS_TEST: need this?
func NewBusesAndP2PModules(
t gocuke.TestingT,
busEventHandlerFactory testutil.BusEventHandlerFactory,
dnsSrv *mockdns.Server,
genesisState *genesis.GenesisState,
libp2pNetworkMock mocknet.Mocknet,
serviceURLKeyMap map[serviceURLStr]cryptoPocket.PrivateKey,
) (
busMocks map[serviceURLStr]*mock_modules.MockBus,
// TODO_THIS_COMMIT: consider *p2p.P2PModule instead
p2pModules map[serviceURLStr]modules.P2PModule,
) {
// CONSIDERATION: using an iterator/generator would prevent unintentional
// serviceURL collisions
serviceURLs := p2p_testutil.SequentialServiceURLs(t, len(privKeys))
_ = p2p_testutil.SetupMockNetPeers(t, libp2pNetworkMock, privKeys, serviceURLs)

for i, serviceURL := range serviceURLs {
host := libp2pNetworkMock.Hosts()[i]
// TECHDEBT: refactor
p2pModules[serviceURL] = NewP2PModuleWithHost(t, busMock, host)
busMocks = make(map[serviceURLStr]*mock_modules.MockBus)
p2pModules = make(map[serviceURLStr]modules.P2PModule)

for serviceURL, privKey := range serviceURLKeyMap {
busMock := bus_testutil.NewBus(
t, privKey,
serviceURL,
genesisState,
busEventHandlerFactory,
)
busMocks[serviceURL] = busMock

p2pModules[serviceURL] = NewP2PModule(
t, serviceURL,
// TODO_THIS_COMMIT: send via busMock instead
privKey,
busMock,
// TODO_THIS_COMMIT: send via busMock instead
genesisState,
// TODO_THIS_COMMIT: send via busMock instead (?)
dnsSrv,
// TODO_THIS_COMMIT: send via busMock instead (?)
libp2pNetworkMock,
)
}
return p2pModules
return busMocks, p2pModules
}

// TODO_THIS_TEST: need this?
Expand All @@ -122,6 +138,7 @@ func NewBusesAndP2PModuleWithHost(
return busMock, NewP2PModuleWithHost(t, busMock, host)
}

// TODO_THIS_COMMIT: rename; consider returning *p2p.P2PModule instead
func NewP2PModuleWithHost(
t gocuke.TestingT,
busMock *mock_modules.MockBus,
Expand Down
41 changes: 41 additions & 0 deletions internal/testutil/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package testutil

import (
"github.com/foxcpp/go-mockdns"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/pokt-network/pocket/runtime/genesis"
"github.com/pokt-network/pocket/shared/modules"
"github.com/pokt-network/pocket/shared/modules/base_modules"
)

// TODO_THIS_COMMIT: is this helpful?
const TestModuleName = "testModule"

var (
_ modules.Module = &TestModule{}
_ modules.ModuleFactoryWithOptions = &TestModule{}
)

type TestModule struct {
base_modules.IntegratableModule
base_modules.InterruptableModule

DNS *mockdns.Server
Genesis *genesis.GenesisState
Libp2pNetworkMock mocknet.Mocknet
}

func (m *TestModule) GetModuleName() string {
return TestModuleName
}

func (m *TestModule) Create(
bus modules.Bus,
opts ...modules.ModuleOption,
) (modules.Module, error) {
panic("implement me")
}

func (m *TestModule) GetDNS() *mockdns.Server {
return m.DNS
}
73 changes: 73 additions & 0 deletions internal/testutil/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package testutil

import (
"fmt"

crypto2 "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/multiformats/go-multiaddr"
"github.com/regen-network/gocuke"
"github.com/stretchr/testify/require"

"github.com/pokt-network/pocket/runtime/defaults"
"github.com/pokt-network/pocket/shared/crypto"
)

const ServiceURLFormat = "node%d.consensus:42069"

func NewMocknetHost(
t gocuke.TestingT,
libp2pNetworkMock mocknet.Mocknet,
privKey crypto.PrivateKey,
) host.Host {
t.Helper()

// TODO_THIS_COMMIT: move to const
addrMock, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/10.0.0.1/tcp/%d", defaults.DefaultP2PPort))
require.NoError(t, err)

libp2pPrivKey, err := crypto2.UnmarshalEd25519PrivateKey(privKey.Bytes())
require.NoError(t, err)

host, err := libp2pNetworkMock.AddPeer(libp2pPrivKey, addrMock)
require.NoError(t, err)

return host
}

func SequentialServiceURLPrivKeyMap(t gocuke.TestingT, count int) map[string]crypto.PrivateKey {
t.Helper()

// CONSIDERATION: using an iterator/generator would prevent unintentional
// ID collisions
privKeys := LoadLocalnetPrivateKeys(t, count)
// CONSIDERATION: using an iterator/generator would prevent unintentional
// serviceURL collisions
serviceURLs := SequentialServiceURLs(t, count)

require.GreaterOrEqualf(t, len(privKeys), len(serviceURLs), "not enough private keys for service URLs")

serviceURLKeysMap := make(map[string]crypto.PrivateKey, len(serviceURLs))

for i, serviceURL := range serviceURLs {
serviceURLKeysMap[serviceURL] = privKeys[i]
}
return serviceURLKeysMap
}

// CONSIDERATION: serviceURLs are only unique within their respective slice;
// consider building an iterator/generator instead.
func SequentialServiceURLs(t gocuke.TestingT, count int) (serviceURLs []string) {
t.Helper()

for i := 0; i < count; i++ {
serviceURLs = append(serviceURLs, NewServiceURL(i+1))
}
return serviceURLs
}

// TECHDEBT: rename `validatorId()` to `serviceURL()`
func NewServiceURL(i int) string {
return fmt.Sprintf(ServiceURLFormat, i)
}
Loading

0 comments on commit 4b5a5d2

Please sign in to comment.