Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Testing, Code Health] refactor: NewMinedRelay to shared testutil #262

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 1 addition & 36 deletions pkg/relayer/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import (
"github.com/pokt-network/poktroll/pkg/observable/channel"
"github.com/pokt-network/poktroll/pkg/polylog/polyzero"
"github.com/pokt-network/poktroll/pkg/relayer"
"github.com/pokt-network/poktroll/pkg/relayer/miner"
"github.com/pokt-network/poktroll/pkg/relayer/session"
"github.com/pokt-network/poktroll/testutil/testclient/testblock"
"github.com/pokt-network/poktroll/testutil/testclient/testsupplier"
"github.com/pokt-network/poktroll/testutil/testpolylog"
"github.com/pokt-network/poktroll/testutil/testrelayer"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
)

func TestRelayerSessionsManager_Start(t *testing.T) {
Expand Down Expand Up @@ -54,7 +51,7 @@ func TestRelayerSessionsManager_Start(t *testing.T) {
relayerSessionsManager.Start(ctx)

// Publish a mined relay to the minedRelaysPublishCh to insert into the session tree.
minedRelay := newMinedRelay(t, sessionStartHeight, sessionEndHeight)
minedRelay := testrelayer.NewMinedRelay(t, sessionStartHeight, sessionEndHeight)
minedRelaysPublishCh <- minedRelay

// Wait a tick to allow the relayer sessions manager to process asynchronously.
Expand Down Expand Up @@ -82,35 +79,3 @@ func TestRelayerSessionsManager_Start(t *testing.T) {
// Wait a tick to allow the relayer sessions manager to process asynchronously.
time.Sleep(250 * time.Millisecond)
}

// newMinedRelay returns a new mined relay with the given session start and end
// heights on the session header, and the bytes and hash fields populated.
func newMinedRelay(
t *testing.T,
sessionStartHeight int64,
sessionEndHeight int64,
) *relayer.MinedRelay {
relay := servicetypes.Relay{
Req: &servicetypes.RelayRequest{
Meta: &servicetypes.RelayRequestMetadata{
SessionHeader: &sessiontypes.SessionHeader{
SessionStartBlockHeight: sessionStartHeight,
SessionEndBlockHeight: sessionEndHeight,
},
},
},
Res: &servicetypes.RelayResponse{},
}

// TODO_BLOCKER: use canonical codec to serialize the relay
relayBz, err := relay.Marshal()
require.NoError(t, err)

relayHash := testrelayer.HashBytes(t, miner.DefaultRelayHasher, relayBz)

return &relayer.MinedRelay{
Relay: relay,
Bytes: relayBz,
Hash: relayHash,
}
}
44 changes: 44 additions & 0 deletions testutil/testrelayer/relays.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package testrelayer

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/pkg/relayer"
"github.com/pokt-network/poktroll/pkg/relayer/miner"
servicetypes "github.com/pokt-network/poktroll/x/service/types"
sessiontypes "github.com/pokt-network/poktroll/x/session/types"
)

// NewMinedRelay returns a new mined relay with the given session start and end
// heights on the session header, and the bytes and hash fields populated.
func NewMinedRelay(
t *testing.T,
sessionStartHeight int64,
sessionEndHeight int64,
) *relayer.MinedRelay {
relay := servicetypes.Relay{
Req: &servicetypes.RelayRequest{
Meta: &servicetypes.RelayRequestMetadata{
SessionHeader: &sessiontypes.SessionHeader{
SessionStartBlockHeight: sessionStartHeight,
SessionEndBlockHeight: sessionEndHeight,
},
},
},
Res: &servicetypes.RelayResponse{},
}

// TODO_BLOCKER: use canonical codec to serialize the relay
relayBz, err := relay.Marshal()
require.NoError(t, err)

relayHash := HashBytes(t, miner.DefaultRelayHasher, relayBz)

return &relayer.MinedRelay{
Relay: relay,
Bytes: relayBz,
Hash: relayHash,
}
}