Skip to content

Commit

Permalink
chore: dial back godoc comments 😅
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Nov 3, 2023
1 parent e136136 commit 833968c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 175 deletions.
39 changes: 3 additions & 36 deletions internal/testclient/testblock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,8 @@ import (
"pocket/pkg/observable/channel"
)

// NewLocalnetClient creates and returns a new BlockClient for localnet testing
// environments.
//
// Parameters:
// - ctx: The context for creating the client.
// - t: The testing.T instance for assertions.
//
// The function initializes a localnet query client, ensures its successful creation, and then
// proceeds to set up the dependencies required to instantiate a new BlockClient.
// The final BlockClient instance connects to the CometLocalWebsocketURL endpoint.
//
// Returns:
// - A new instance of client.BlockClient configured for localnet interactions.
// NewLocalnetClient creates and returns a new BlockClient that's configured for
// use with the localnet sequencer.
func NewLocalnetClient(ctx context.Context, t *testing.T) client.BlockClient {
t.Helper()

Expand All @@ -55,13 +44,7 @@ func NewLocalnetClient(ctx context.Context, t *testing.T) client.BlockClient {
// This mock BlockClient will expect a call to CommittedBlocksSequence, and
// when that call is made, it returns a new BlocksObservable that is notified of
// blocks sent on the given blocksPublishCh.
//
// Parameters:
// - t: *testing.T - The test instance.
// - blocksPublishCh: chan client.Block - The channel from which blocks are published to the observable.
//
// Returns:
// - *mockclient.MockBlockClient: The mock block client.
// blocksPublishCh is the channel the caller can use to publish blocks the observable.
func NewOneTimeCommittedBlocksSequenceBlockClient(
t *testing.T,
blocksPublishCh chan client.Block,
Expand Down Expand Up @@ -92,14 +75,6 @@ func NewOneTimeCommittedBlocksSequenceBlockClient(
// NewAnyTimeLatestBlockBlockClient creates a mock BlockClient that expects
// calls to the LatestBlock method any number of times. When the LatestBlock
// method is called, it returns a mock Block with the provided hash and height.
//
// Parameters:
// - t: *testing.T - The test instance.
// - hash: []byte - The expected hash value that the mock Block should return.
// - height: int64 - The expected block height that the mock Block should return.
//
// Returns:
// - *mockclient.MockBlockClient: The mock block client.
func NewAnyTimeLatestBlockBlockClient(
t *testing.T,
hash []byte,
Expand All @@ -121,14 +96,6 @@ func NewAnyTimeLatestBlockBlockClient(
// NewAnyTimesBlock creates a mock Block that expects calls to Height and Hash
// methods any number of times. When the methods are called, they return the
// provided height and hash respectively.
//
// Parameters:
// - t: *testing.T - The test instance.
// - hash: []byte - The expected hash value that the mock Block should return.
// - height: int64 - The expected block height that the mock Block should return.
//
// Returns:
// - *mockclient.MockBlock: The mock block.
func NewAnyTimesBlock(t *testing.T, hash []byte, height int64) *mockclient.MockBlock {
t.Helper()
ctrl := gomock.NewController(t)
Expand Down
45 changes: 15 additions & 30 deletions internal/testclient/testeventsquery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,21 @@ import (
"pocket/pkg/observable/channel"
)

// NewLocalnetClient creates and returns a new events query client that connects to the
// localnet sequencer. It leverages the NewEventsQueryClient from the eventsquery package.
//
// Parameters:
// t: Testing object which is passed for marking helper.
// opts: Any additional options to configure the events query client.
//
// Returns: An instance of EventsQueryClient which connects to the localnet sequencer.
// NewLocalnetClient creates and returns a new events query client that configured
// for use with the localnet sequencer. Any options provided are applied to the client.
func NewLocalnetClient(t *testing.T, opts ...client.EventsQueryClientOption) client.EventsQueryClient {
t.Helper()

return eventsquery.NewEventsQueryClient(testclient.CometLocalWebsocketURL, opts...)
}

// NewOneTimeEventsQuery creates a mock of the EventsQueryClient which expects a single call to the
// EventsBytes method. It returns a mock client that emits event bytes to the provided publish channel.
//
// Parameters:
// ctx: Context object to define the context for the operation.
// t: Testing object.
// query: The query string for which event bytes are fetched.
// publishCh: Channel to which the event bytes are published.
//
// Returns: A mock instance of EventsQueryClient which behaves as described.
// NewOneTimeEventsQuery creates a mock of the EventsQueryClient which expects
// a single call to the EventsBytes method. It returns a mock client whose event
// bytes method always constructs a new observable. query is the query string
// for which event bytes subscription is expected to be for.
// The caller can simulate blockchain events by sending on publishCh, the value
// of which is set to the publish channel of the events bytes observable publish
// channel.
func NewOneTimeEventsQuery(
ctx context.Context,
t *testing.T,
Expand All @@ -63,18 +54,12 @@ func NewOneTimeEventsQuery(
return eventsQueryClient
}

// NewOneTimeTxEventsQueryClient initializes a new MockEventsQueryClient that is set up to
// query for transaction events for a specific message sender. This is useful for tests where
// you want to listen for a one-time event related to a specific sender.
//
// Parameters:
// - ctx: The context to pass to the client.
// - t: The testing.T instance for assertions.
// - key: The keyring record from which the signing address is derived.
// - publishCh: A channel where the events are published.
//
// Returns:
// - A new instance of mockclient.MockEventsQueryClient set up for the specific query.
// NewOneTimeTxEventsQueryClient creates a mock of the Events that expects to to
// a single call to the EventsBytes method where the query is for transaction
// events for sender address matching that of the given key.
// The caller can simulate blockchain events by sending on publishCh, the value
// of which is set to the publish channel of the events bytes observable publish
// channel.
func NewOneTimeTxEventsQueryClient(
ctx context.Context,
t *testing.T,
Expand Down
113 changes: 12 additions & 101 deletions internal/testclient/testtx/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,10 @@ import (
// correlations between these "times" values and the contexts in which the expected
// methods may be called.

// NewOneTimeErrTxTimeoutTxContext creates a mock transaction context designed to simulate a specific
// timeout error scenario during transaction broadcasting.
//
// Parameters:
// - t: The testing.T instance for the current test.
// - keyring: The Cosmos SDK keyring containing the signer's cryptographic keys.
// - signingKeyName: The name of the key within the keyring to use for signing.
// - expectedTx: A pointer whose value will be set to the expected transaction
// bytes (in hexadecimal format).
// - expectedErrMsg: A pointer whose value will be set to the expected error
// message string.
//
// The function performs the following actions:
// 1. It retrieves the signer's cryptographic key from the provided keyring using the signingKeyName.
// 2. It computes the corresponding address of the signer's key.
// 3. It then formats an error message indicating that the fee payer's address does not exist.
// 4. It creates a base mock transaction context using NewBaseTxContext.
// 5. It sets up the mock behavior for the BroadcastTxSync method to return a specific preset response.
// 6. It also sets up the mock behavior for the QueryTx method to return a specific error response.
// NewOneTimeErrTxTimeoutTxContext creates a mock transaction context designed to
// simulate a specific timeout error scenario during transaction broadcasting.
// expectedErrMsg is populated with the same error message which is presented in
// the result from the QueryTx method so that it can be asserted against.
func NewOneTimeErrTxTimeoutTxContext(
t *testing.T,
keyring cosmoskeyring.Keyring,
Expand Down Expand Up @@ -116,24 +101,8 @@ func NewOneTimeErrTxTimeoutTxContext(
// NewOneTimeErrCheckTxTxContext creates a mock transaction context to simulate
// a specific error scenario during the ABCI check-tx phase (i.e., during initial
// validation before the transaction is included in the block).
//
// Parameters:
// - t: The testing.T instance for the current test.
// - keyring: The Cosmos SDK keyring containing the signer's cryptographic keys.
// - signingKeyName: The name of the key within the keyring to be used for signing.
// - expectedTx: A pointer whose value will be set to the expected transaction
// bytes (in hexadecimal format).
// - expectedErrMsg: A pointer whose value will be set to the expected error
// message string.
//
// The function operates as follows:
// 1. Retrieves the signer's cryptographic key from the provided keyring based on
// the signingKeyName.
// 2. Determines the corresponding address of the signer's key.
// 3. Composes an error message suggesting that the fee payer's address is unrecognized.
// 4. Creates a base mock transaction context using the NewBaseTxContext function.
// 5. Sets up the mock behavior for the BroadcastTxSync method to return a specific
// error response related to the check phase of the transaction.
// expectedErrMsg is populated with the same error message which is presented in
// the result from the QueryTx method so that it can be asserted against.
func NewOneTimeErrCheckTxTxContext(
t *testing.T,
keyring cosmoskeyring.Keyring,
Expand Down Expand Up @@ -179,22 +148,7 @@ func NewOneTimeErrCheckTxTxContext(
}

// NewOneTimeTxTxContext creates a mock transaction context primed to respond with
// a single successful transaction response. This function facilitates testing by
// ensuring that the BroadcastTxSync method will return a specific, controlled response
// without actually broadcasting the transaction to the network.
//
// Parameters:
// - t: The testing.T instance used for the current test, typically passed from
// the calling test function.
// - keyring: The Cosmos SDK keyring containing the available cryptographic keys.
// - signingKeyName: The name of the key within the keyring used for transaction signing.
// - expectedTx: A pointer whose value will be set to the expected transaction
// bytes (in hexadecimal format).
//
// The function operates as follows:
// 1. Constructs a base mock transaction context using the NewBaseTxContext function.
// 2. Configures the mock behavior for the BroadcastTxSync method to return a pre-defined
// successful transaction response, ensuring that this behavior will only be triggered once.
// a single successful transaction response.
func NewOneTimeTxTxContext(
t *testing.T,
keyring cosmoskeyring.Keyring,
Expand Down Expand Up @@ -224,30 +178,11 @@ func NewOneTimeTxTxContext(
return txCtxMock
}

// NewBaseTxContext establishes a foundational mock transaction context with
// predefined behaviors suitable for a broad range of testing scenarios. It ensures
// that when interactions like transaction building, signing, and encoding occur
// in the test environment, they produce predictable and controlled outcomes.
//
// Parameters:
// - t: The testing.T instance used for the current test, typically passed from
// the calling test function.
// - signingKeyName: The name of the key within the keyring to be used for
// transaction signing.
// - keyring: The Cosmos SDK keyring containing the available cryptographic keys.
// - expectedTx: A pointer whose value will be set to the expected transaction
// bytes (in hexadecimal format).
// - expectedErrMsg: A pointer whose value will be set to the expected error
// message string.
//
// The function works as follows:
// 1. Invokes the NewAnyTimesTxTxContext to create a base mock transaction context.
// 2. Sets the expectation that NewTxBuilder method will be called exactly once.
// 3. Configures the mock behavior for the SignTx method to utilize the context's
// signing logic.
// 4. Overrides the EncodeTx method's behavior to intercept the encoding operation,
// capture the encoded transaction bytes, compute the transaction hash, and populate
// the expectedTx and expectedTxHash parameters accordingly.
// NewBaseTxContext creates a mock transaction context that's configured to expect
// calls to NewTxBuilder, SignTx, and EncodeTx methods, any number of times.
// EncodeTx is used to intercept the encoded transaction bytes and store them in
// the expectedTx output parameter. Each of these methods proxies to the corresponding
// method on a real transaction context.
func NewBaseTxContext(
t *testing.T,
signingKeyName string,
Expand Down Expand Up @@ -281,30 +216,6 @@ func NewBaseTxContext(
// NewAnyTimesTxTxContext initializes a mock transaction context that's configured to allow
// arbitrary calls to certain predefined interactions, primarily concerning the retrieval
// of account numbers and sequences.
//
// Parameters:
// - t: The testing.T instance used for the current test, typically passed from the calling test function.
// - keyring: The Cosmos SDK keyring containing the available cryptographic keys.
//
// The function operates in the following manner:
// 1. Establishes a new gomock controller for setting up mock expectations and behaviors.
// 2. Prepares a set of flags suitable for localnet testing environments.
// 3. Sets up a mock behavior to intercept the GetAccountNumberSequence method calls,
// ensuring that whenever this method is invoked, it consistently returns an account number
// and sequence of 1, without making real queries to the underlying infrastructure.
// 4. Constructs a client context tailored for localnet testing with the provided keyring
// and the mocked account retriever.
// 5. Initializes a transaction factory from the client context and validates its integrity.
// 6. Injects the transaction factory and client context dependencies to create a new transaction context.
// 7. Creates a mock transaction context that always returns the provided keyring when the GetKeyring method is called.
//
// This setup aids tests by facilitating the creation of mock transaction contexts that have predictable
// and controlled outcomes for account number and sequence retrieval operations.
//
// Returns:
// - A mock transaction context suitable for setting additional expectations in tests.
// - A real transaction context initialized with the supplied dependencies.

func NewAnyTimesTxTxContext(
t *testing.T,
keyring cosmoskeyring.Keyring,
Expand Down
10 changes: 2 additions & 8 deletions pkg/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,8 @@ type Dialer interface {
DialContext(ctx context.Context, urlStr string) (Connection, error)
}

// EventsQueryClientOption is an interface-wide type which can be implemented to use or modify the
// query client during construction. This would likely be done in an
// implementation-specific way; e.g. using a type assertion to assign to an
// implementation struct field(s).
// EventsQueryClientOption defines a function type that modifies the EventsQueryClient.
type EventsQueryClientOption func(EventsQueryClient)

// TxClientOption defines a function type that modifies the TxClient. This pattern
// allows for flexible and optional configurations to be applied to a TxClient instance.
// Such options can be used to customize properties, behaviors, or any other attributes
// of the TxClient without altering its core construction logic.
// TxClientOption defines a function type that modifies the TxClient.
type TxClientOption func(TxClient)

0 comments on commit 833968c

Please sign in to comment.