Skip to content

Commit

Permalink
Fixed another unit test and created #388
Browse files Browse the repository at this point in the history
  • Loading branch information
Olshansk committed Dec 9, 2022
1 parent 148c19d commit 689f271
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
4 changes: 3 additions & 1 deletion shared/modules/utility_module.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package modules

import "google.golang.org/protobuf/types/known/anypb"
import (
"google.golang.org/protobuf/types/known/anypb"
)

//go:generate mockgen -source=$GOFILE -destination=./mocks/utility_module_mock.go -aux_files=github.com/pokt-network/pocket/shared/modules=module.go

Expand Down
1 change: 1 addition & 0 deletions utility/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
typesUtil "github.com/pokt-network/pocket/utility/types"
)

// TODO: The implementation of `UtilityContext` should not be exposed.
type UtilityContext struct {
Height int64
Mempool typesUtil.Mempool // IMPROVE: Look into accessing this directly from the module without needing to pass and save another pointer (e.g. access via bus)
Expand Down
51 changes: 42 additions & 9 deletions utility/test/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import (
"encoding/hex"
"log"
"os"
"reflect"
"testing"

"github.com/golang/mock/gomock"
"github.com/pokt-network/pocket/persistence"
"github.com/pokt-network/pocket/persistence/types"
"github.com/pokt-network/pocket/runtime"
"github.com/pokt-network/pocket/runtime/defaults"
"github.com/pokt-network/pocket/runtime/test_artifacts"
"github.com/pokt-network/pocket/shared/messaging"
"github.com/pokt-network/pocket/shared/modules"
modulesMock "github.com/pokt-network/pocket/shared/modules/mocks"
"github.com/pokt-network/pocket/utility"
utilTypes "github.com/pokt-network/pocket/utility/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -66,6 +69,9 @@ func NewTestingUtilityContext(t *testing.T, height int64) utility.UtilityContext
persistenceContext, err := testPersistenceMod.NewRWContext(height)
require.NoError(t, err)

// TODO(#388): Expose a `GetMempool` function in `utility_module` so we can remove this reflection.
mempool := reflect.ValueOf(testUtilityMod).Elem().FieldByName("Mempool").Interface().(utilTypes.Mempool)

// TECHDEBT: Move the internal of cleanup into a separate function and call this in the
// beginning of every test. This (the current implementation) is an issue because if we call
// `NewTestingUtilityContext` more than once in a single test, we create unnecessary calls to clean.
Expand All @@ -75,11 +81,12 @@ func NewTestingUtilityContext(t *testing.T, height int64) utility.UtilityContext
Action: messaging.DebugMessageAction_DEBUG_PERSISTENCE_RESET_TO_GENESIS,
Message: nil,
}))
mempool.Clear()
})

return utility.UtilityContext{
Height: height,
Mempool: NewTestingMempool(t),
Mempool: mempool,
Context: &utility.Context{
PersistenceRWContext: persistenceContext,
SavePointsM: make(map[string]struct{}),
Expand All @@ -89,13 +96,19 @@ func NewTestingUtilityContext(t *testing.T, height int64) utility.UtilityContext
}

func newTestRuntimeConfig(databaseUrl string) *runtime.Manager {
cfg := runtime.NewConfig(&runtime.BaseConfig{}, runtime.WithPersistenceConfig(&types.PersistenceConfig{
PostgresUrl: databaseUrl,
NodeSchema: testSchema,
BlockStorePath: "",
TxIndexerPath: "",
TreesStoreDir: "",
}))
cfg := runtime.NewConfig(
&runtime.BaseConfig{},
runtime.WithPersistenceConfig(&types.PersistenceConfig{
PostgresUrl: databaseUrl,
NodeSchema: testSchema,
BlockStorePath: "",
TxIndexerPath: "",
TreesStoreDir: "",
}),
runtime.WithUtilityConfig(&utilTypes.UtilityConfig{
MaxMempoolTransactionBytes: 1000000,
MaxMempoolTransactions: 1000,
}))
genesisState, _ := test_artifacts.NewGenesisState(5, 1, 1, 1)
runtimeCfg := runtime.NewManager(cfg, genesisState)
return runtimeCfg
Expand All @@ -109,7 +122,27 @@ func newTestUtilityModule(runtimeCfg *runtime.Manager) modules.UtilityModule {
return utilityMod.(modules.UtilityModule)
}

// TODO: Eventually, we want to fully mock the persistence module within the context of utility tests
// IMPROVE: Not part of `TestMain` because a mock requires `testing.T` to be initialized.
// We are trying to only initialize one `testPersistenceModule` in all the tests, so when the
// utility module tests are no longer dependant on the persistence module explicitly, this
// can be improved.
func mockBusInTestModules(t *testing.T) {
ctrl := gomock.NewController(t)

busMock := modulesMock.NewMockBus(ctrl)
busMock.EXPECT().GetPersistenceModule().Return(testPersistenceMod).AnyTimes()
busMock.EXPECT().GetUtilityModule().Return(testUtilityMod).AnyTimes()

testPersistenceMod.SetBus(busMock)
testUtilityMod.SetBus(busMock)

t.Cleanup(func() {
testPersistenceMod.SetBus(nil)
testUtilityMod.SetBus(nil)
})
}

// TODO(#290): Mock the persistence module so the utility module is not dependant on it.
func newTestPersistenceModule(runtimeCfg *runtime.Manager) modules.PersistenceModule {
persistenceMod, err := persistence.Create(runtimeCfg)
if err != nil {
Expand Down
12 changes: 9 additions & 3 deletions utility/test/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func TestUtilityContext_ApplyTransaction(t *testing.T) {
}

func TestUtilityContext_CheckTransaction(t *testing.T) {
mockBusInTestModules(t)

ctx := NewTestingUtilityContext(t, 0)
tx, _, _, _ := newTestingTransaction(t, ctx)

Expand All @@ -67,7 +69,7 @@ func TestUtilityContext_CheckTransaction(t *testing.T) {

hash, err := tx.Hash()
require.NoError(t, err)
require.True(t, ctx.Mempool.Contains(hash))
require.True(t, ctx.Mempool.Contains(hash)) // IMPROVE: Access the mempool from the `testUtilityMod` directly
require.Equal(t, testUtilityMod.CheckTransaction(txBz).Error(), typesUtil.ErrDuplicateTransaction().Error())

test_artifacts.CleanupTest(ctx)
Expand All @@ -94,17 +96,21 @@ func TestUtilityContext_GetSignerCandidates(t *testing.T) {
}

func TestUtilityContext_CreateAndApplyBlock(t *testing.T) {
mockBusInTestModules(t)

ctx := NewTestingUtilityContext(t, 0)
tx, _, _, _ := newTestingTransaction(t, ctx)
proposer := getAllTestingValidators(t, ctx)[0]

proposer := getFirstActor(t, ctx, typesUtil.ActorType_Validator)
txBz, err := tx.Bytes()
require.NoError(t, err)
require.NoError(t, testUtilityMod.CheckTransaction(txBz))

appHash, txs, er := ctx.CreateAndApplyProposalBlock([]byte(proposer.GetAddress()), 10000)
require.NoError(t, er)
require.NotEmpty(t, appHash)
require.Equal(t, 1, len(txs))
require.Equal(t, txs[0], txBz)
require.NotEmpty(t, appHash)

test_artifacts.CleanupTest(ctx)
}
Expand Down

0 comments on commit 689f271

Please sign in to comment.