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, Tooling] feat: implement in-memory networks #290

Closed
wants to merge 94 commits into from
Closed
Show file tree
Hide file tree
Changes from 45 commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
4bcd8de
refactor: `NewMinedRelay` to shared testutil
bryanchriswhite Dec 12, 2023
673911c
refactor: claim & proof protobuf types
bryanchriswhite Dec 12, 2023
fd55896
refactor: rename supplier keeper `UpsertClaim` & `UpsertProof`
bryanchriswhite Dec 7, 2023
43cbf0e
refactor: misc. claim-side improvements
bryanchriswhite Dec 12, 2023
ef3cd99
chore: add TODOs
bryanchriswhite Dec 13, 2023
f509fbf
refactor: supplier module keys
bryanchriswhite Dec 12, 2023
6817364
refactor: supplier module errors
bryanchriswhite Dec 12, 2023
6e60952
chore: review feedback improvements
bryanchriswhite Dec 22, 2023
edc4e41
chore: review feedback improvements
bryanchriswhite Dec 22, 2023
7994ca2
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/cla…
bryanchriswhite Dec 22, 2023
6a5b3e6
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Dec 22, 2023
dfb067d
chore: review feedback improvements
bryanchriswhite Dec 22, 2023
0351cc7
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Dec 22, 2023
b82e49a
fix: pre-generated keyring accounts
bryanchriswhite Dec 19, 2023
2d52838
fix: session hydrator
bryanchriswhite Dec 21, 2023
6a9d6ec
feat: add InMemoryCosmosNetwork interface
bryanchriswhite Dec 21, 2023
5566e5c
chore: add InMemoryNetworkConfig
bryanchriswhite Dec 21, 2023
923907b
chore: add TODO comment
bryanchriswhite Dec 21, 2023
8b57b22
Revert "fix: pre-generated keyring accounts"
bryanchriswhite Dec 21, 2023
eee5dcf
chore: add BaseInMemoryCosmosNetwork abstract impl.
bryanchriswhite Dec 21, 2023
4af4132
feat: add inMemoryNetworkWithSessions impl.
bryanchriswhite Dec 21, 2023
ae7de37
feat: add inMemoryNetworkWithGateways
bryanchriswhite Dec 21, 2023
a2e53c1
chore: add GetGenesis test helper
bryanchriswhite Dec 21, 2023
93125b0
chore: add godoc comments & simplify
bryanchriswhite Dec 22, 2023
6737cb7
fixup! feat: add inMemoryNetworkWithSessions impl.
bryanchriswhite Dec 22, 2023
35cd36a
chore: self-review
bryanchriswhite Dec 22, 2023
41ee458
Merge branch 'issues/141/prep/in-memory-network' into issues/141/feat…
bryanchriswhite Dec 22, 2023
86f281d
fix: linter errors
bryanchriswhite Jan 2, 2024
d3a21e3
chore: self-review improvements
bryanchriswhite Jan 2, 2024
1fbf000
fixup! fix: linter errors
bryanchriswhite Jan 2, 2024
b11bbe3
chore: add TODOs
bryanchriswhite Jan 8, 2024
f3e7866
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/cla…
bryanchriswhite Jan 8, 2024
df9e3c2
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 8, 2024
bca059c
trigger CI
bryanchriswhite Jan 8, 2024
8ab13f0
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 8, 2024
c21071b
chore: review feedback improvements
bryanchriswhite Jan 8, 2024
6515c64
chore: update comment
bryanchriswhite Jan 8, 2024
b4ed47c
Merge branch 'issues/141/refactor/supplier-errors' into issues/141/pr…
bryanchriswhite Jan 8, 2024
7987e14
chore: rename InMemoryCosmosNetwork to InMemoryNetwork
bryanchriswhite Jan 8, 2024
cdcfb7d
chore: add #GetConfig()
bryanchriswhite Jan 8, 2024
191c956
Merge branch 'issues/141/prep/in-memory-network' into issues/141/feat…
bryanchriswhite Jan 8, 2024
92c0a19
chore: fix comment
bryanchriswhite Jan 8, 2024
0a6b950
Merge branch 'issues/141/prep/in-memory-network' into issues/141/feat…
bryanchriswhite Jan 8, 2024
86548cd
chore: post-merge refactor
bryanchriswhite Jan 8, 2024
ff09a42
chore: fix comment typo
bryanchriswhite Jan 8, 2024
8e84c43
Merge branch 'issues/141/prep/in-memory-network' into issues/141/feat…
bryanchriswhite Jan 8, 2024
9f472f6
trigger CI
bryanchriswhite Jan 8, 2024
3de1971
chore: add TODO
bryanchriswhite Jan 9, 2024
800b53b
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/cla…
bryanchriswhite Jan 9, 2024
8e0f4e9
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 9, 2024
2b32545
chore: generalize ErrSupplierInvalidAddress and simplify
bryanchriswhite Dec 13, 2023
f41547e
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 9, 2024
a527f23
Merge branch 'issues/141/refactor/supplier-errors' into issues/141/pr…
bryanchriswhite Jan 9, 2024
27b662c
Merge branch 'issues/141/prep/in-memory-network' into issues/141/feat…
bryanchriswhite Jan 9, 2024
03bb983
rename: BaseInMemoryNetwork#CreateOnChainAccounts to #FundOnChainAcco…
bryanchriswhite Jan 9, 2024
a9c2a78
refactor: rename & simplify
bryanchriswhite Jan 9, 2024
a92ad13
chore: review feedback improvements
bryanchriswhite Jan 9, 2024
5905d20
Merge branch 'main' into issues/141/refactor/claim-proof
bryanchriswhite Jan 10, 2024
4248b0b
chore: review feedback improvements
bryanchriswhite Jan 10, 2024
f064432
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 10, 2024
7bacad9
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 10, 2024
6d5826f
fix: usage raw string literal
bryanchriswhite Jan 10, 2024
92970b9
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 10, 2024
ab3fd1e
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 10, 2024
a57273c
chore: review feedback improvements
bryanchriswhite Jan 10, 2024
8486a8c
Merge branch 'issues/141/refactor/supplier-errors' into issues/141/pr…
bryanchriswhite Jan 10, 2024
53faaac
Merge branch 'main' into issues/141/refactor/claim-proof
bryanchriswhite Jan 10, 2024
0ada011
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 10, 2024
6df8994
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 10, 2024
2f1232c
chore: review feedback improvements
bryanchriswhite Jan 10, 2024
bd60220
Merge branch 'issues/141/refactor/supplier-errors' into issues/141/pr…
bryanchriswhite Jan 10, 2024
c648d4f
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/cla…
bryanchriswhite Jan 10, 2024
dd53efc
Merge branch 'issues/141/refactor/claim-proof' into issues/141/refact…
bryanchriswhite Jan 10, 2024
fea3d27
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/sup…
bryanchriswhite Jan 10, 2024
c00e1b2
Merge branch 'issues/141/refactor/supplier-keys' into issues/141/refa…
bryanchriswhite Jan 10, 2024
7d1b1e0
Merge remote-tracking branch 'pokt/main' into issues/141/refactor/sup…
bryanchriswhite Jan 10, 2024
dd5ea83
Merge branch 'issues/141/refactor/supplier-errors' into issues/141/pr…
bryanchriswhite Jan 10, 2024
afbefe7
trigger CI
bryanchriswhite Jan 10, 2024
24afe56
chore: review feedback improvements
bryanchriswhite Jan 10, 2024
fe5e743
chore: review feedback improvements
bryanchriswhite Jan 10, 2024
6c35227
Merge remote-tracking branch 'pokt/main' into issues/141/prep/in-memo…
bryanchriswhite Jan 10, 2024
b0b0436
Merge branch 'issues/141/prep/in-memory-network' into issues/141/feat…
bryanchriswhite Jan 10, 2024
dc07730
fixup! chore: review feedback improvements
bryanchriswhite Jan 10, 2024
a906d45
fix: linter errors
bryanchriswhite Jan 10, 2024
cd19c7e
Merge branch 'main' into issues/141/feat/in-memory-network
bryanchriswhite Jan 11, 2024
12dbc57
chore: review feedback improvements
bryanchriswhite Jan 12, 2024
c1774bf
chore: review feedback improvements
bryanchriswhite Jan 22, 2024
985a817
chore: review feedback improvements
bryanchriswhite Jan 22, 2024
e905b12
chore: review feedback improvements
bryanchriswhite Jan 22, 2024
413f80e
ch
bryanchriswhite Jan 22, 2024
911c423
Merge branch 'main' into issues/141/feat/in-memory-network
bryanchriswhite Jan 22, 2024
3f03074
chore: review feedback improvements
bryanchriswhite Jan 22, 2024
4516ff5
chore: review feedback improvements
bryanchriswhite Jan 22, 2024
86c7133
fix: linter errors
bryanchriswhite Jan 22, 2024
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
8 changes: 5 additions & 3 deletions proto/pocket/supplier/claim.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ package pocket.supplier;
option go_package = "github.com/pokt-network/poktroll/x/supplier/types";

import "cosmos_proto/cosmos.proto";
import "pocket/session/session.proto";

// Claim is the serialized object stored on-chain for claims pending to be proven
message Claim {
string supplier_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // the address of the supplier that submitted this claim
string session_id = 2; // session id from the SessionHeader
uint64 session_end_block_height = 3; // session end block height from the SessionHeader
bytes root_hash = 4; // smt.SMST#Root()
// The session header of the session that this claim is for.
session.SessionHeader session_header = 2;
// Root hash returned from smt.SMST#Root().
bytes root_hash = 3;
}
14 changes: 8 additions & 6 deletions proto/pocket/supplier/proof.proto
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
syntax = "proto3";
package pocket.supplier;

import "cosmos_proto/cosmos.proto";
import "pocket/session/session.proto";

option go_package = "github.com/pokt-network/poktroll/x/supplier/types";

// TODO_UPNEXT(@Olshansk): The structure below is the default (untouched) scaffolded type. Update
// and productionize it for our use case.
message Proof {
string index = 1;
string supplier_address = 2;
string session_id = 3;
string merkle_proof = 4;
string supplier_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// The session header of the session that this claim is for.
session.SessionHeader session_header = 2;
// The serialized SMST proof from the `#ClosestProof()` method.
bytes closest_merkle_proof = 3;
}

5 changes: 3 additions & 2 deletions proto/pocket/supplier/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ syntax = "proto3";

package pocket.supplier;

import "cosmos_proto/cosmos.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "pocket/supplier/params.proto";
import "pocket/shared/supplier.proto";
import "pocket/supplier/claim.proto";
Expand Down Expand Up @@ -76,7 +77,7 @@ message QueryAllSupplierResponse {

message QueryGetClaimRequest {
string session_id = 1;
string supplier_address = 2;
string supplier_address = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

message QueryGetClaimResponse {
Expand Down
194 changes: 194 additions & 0 deletions testutil/network/basenet/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package basenet

import (
"encoding/json"
"fmt"
"testing"

"cosmossdk.io/math"
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
testcli "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/testutil/network"
"github.com/pokt-network/poktroll/testutil/testkeyring"
apptypes "github.com/pokt-network/poktroll/x/application/types"
gatewaytypes "github.com/pokt-network/poktroll/x/gateway/types"
sharedtypes "github.com/pokt-network/poktroll/x/shared/types"
suppliertypes "github.com/pokt-network/poktroll/x/supplier/types"
)

const warnNoModuleGenesisFmt = "WARN: no %s module genesis state found, if this is unexpected, ensure that genesis is populated before creating on-chain accounts"

// CreateKeyringAccounts populates the Keyring associated with the in-memory
// network with memnet.numKeyringAccounts() number of pre-generated accounts.
func (memnet *BaseInMemoryNetwork) CreateKeyringAccounts(t *testing.T) {
t.Helper()

// Keyring MAY be provided setting InMemoryNetworkConfig#Keyring.
if memnet.Config.Keyring == nil {
t.Log("Keyring not initialized, using new in-memory keyring")

// Construct an in-memory keyring so that it can be populated and used prior
// to network start.
memnet.Config.Keyring = keyring.NewInMemory(memnet.Config.CosmosCfg.Codec)
} else {
t.Log("Keyring already initialized, using existing keyring")
}

// Create memnet.NumKeyringAccounts() number of accounts in the configured keyring.
accts := testkeyring.CreatePreGeneratedKeyringAccounts(
t, memnet.Config.Keyring, memnet.Config.GetNumKeyringAccounts(t),
)

// Assign the memnet's pre-generated accounts to a new pre-generated
// accounts iterator containing only the accounts which were also created
// in the keyring.
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
memnet.PreGeneratedAccountIterator = testkeyring.NewPreGeneratedAccountIterator(accts...)
}

// CreateOnChainAccounts creates on-chain accounts (i.e. auth module) for the sum of
// the configured number of suppliers, applications, and gateways.
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
func (memnet *BaseInMemoryNetwork) CreateOnChainAccounts(t *testing.T) {
t.Helper()

// NB: while it may initially seem like the memnet#Init<actor>AccountsWithSequence() methods
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

giphy

bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
// can be refactored into a generic function, this is not possible so long as the genesis
// state lists are passed directly & remain a slice of concrete types (as opposed to pointers).
// Under these conditions, a generic function would not be able to unmarshal the genesis state
// stored in the in-memory network because it is unmarshalling uses reflection, and it is not
// possible to reflect over a nil generic type value.

// Retrieve the supplier module's genesis state from cosmos-sdk in-memory network.
supplierGenesisState := network.GetGenesisState[*suppliertypes.GenesisState](t, suppliertypes.ModuleName, memnet)
if supplierGenesisState == nil {
t.Logf(warnNoModuleGenesisFmt, "supplier")
} else {
// Initialize on-chain accounts for genesis suppliers.
memnet.InitSupplierAccountsWithSequence(t, supplierGenesisState.SupplierList...)

bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
}

// Retrieve the application module's genesis state from cosmos-sdk in-memory network.
appGenesisState := network.GetGenesisState[*apptypes.GenesisState](t, apptypes.ModuleName, memnet)
if appGenesisState == nil {
t.Logf(warnNoModuleGenesisFmt, "application")
} else {
// Initialize on-chain accounts for genesis applications.
memnet.InitAppAccountsWithSequence(t, appGenesisState.ApplicationList...)
}

// Retrieve the gateway module's genesis state from cosmos-sdk in-memory network.
gatewayGenesisState := network.GetGenesisState[*gatewaytypes.GenesisState](t, gatewaytypes.ModuleName, memnet)
if gatewayGenesisState == nil {
t.Logf(warnNoModuleGenesisFmt, "gateway")
} else {
// Initialize on-chain accounts for genesis gateways.
memnet.InitGatewayAccountsWithSequence(t, gatewayGenesisState.GatewayList...)
}

// need to wait for the account to be initialized in the next block
require.NoError(t, memnet.GetNetwork(t).WaitForNextBlock())
}

// InitAccount initializes an Account by sending it some funds from the validator
// in the network to the address provided
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
func (memnet *BaseInMemoryNetwork) InitAccount(
t *testing.T,
addr types.AccAddress,
) {
t.Helper()

signerAccountNumber := 0
// Validator's client context MUST be used for this CLI command because its keyring includes the validator's key
clientCtx := memnet.Network.Validators[0].ClientCtx
// MUST NOT use memnet.GetClientCtx(t) as its keyring does not include the validator's account
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
// TODO_UPNEXT(@bryanchriswhite): Ensure validator key is always available via the in-memory network's keyring.
net := memnet.GetNetwork(t)
val := net.Validators[0]

args := []string{
fmt.Sprintf("--%s=true", flags.FlagOffline),
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
fmt.Sprintf("--%s=%d", flags.FlagAccountNumber, signerAccountNumber),
fmt.Sprintf("--%s=%d", flags.FlagSequence, memnet.NextAccountSequenceNumber(t)),

fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync),
fmt.Sprintf("--%s=%s", flags.FlagFees, types.NewCoins(types.NewCoin(net.Config.BondDenom, math.NewInt(10))).String()),
}
amount := types.NewCoins(types.NewCoin("stake", math.NewInt(200)))
responseRaw, err := testcli.MsgSendExec(clientCtx, val.Address, addr, amount, args...)
require.NoError(t, err)
var responseJSON map[string]interface{}
err = json.Unmarshal(responseRaw.Bytes(), &responseJSON)
require.NoError(t, err)
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, float64(0), responseJSON["code"], "code is not 0 in the response: %v", responseJSON)
}

// GetPreGeneratedAccountIterator returns the pre-generated account iterator associated
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
// with the in-memory network; i.e. used to populate genesis and the keyring. Calling
// #Next() will return the next (unused) pre-generated account in the iterator.
func (memnet *BaseInMemoryNetwork) CreateNewOnChainAccount(t *testing.T) *testkeyring.PreGeneratedAccount {
t.Helper()

preGeneratedAcct, ok := testkeyring.PreGeneratedAccounts().Next()
require.Truef(t, ok, "no pre-generated accounts available")

// Create a supplier on-chain account.
memnet.InitAccount(t, preGeneratedAcct.Address)

testkeyring.CreatePreGeneratedKeyringAccounts(t, memnet.GetClientCtx(t).Keyring, 1)

return preGeneratedAcct
}

// InitSupplierAccountsWithSequence uses the testCLI to create on-chain accounts
// (i.e. auth module) for the addresses of the given suppliers.
func (memnet *BaseInMemoryNetwork) InitSupplierAccountsWithSequence(
t *testing.T,
supplierList ...sharedtypes.Supplier,
) {
t.Helper()

net := memnet.GetNetwork(t)
require.NotNil(t, net, "in-memory cosmos testutil network not initialized yet, call #Start() first")

for _, supplier := range supplierList {
supplierAddr, err := types.AccAddressFromBech32(supplier.GetAddress())
require.NoError(t, err)
memnet.InitAccount(t, supplierAddr)
}
}

// InitAppAccountsWithSequence uses the testCLI to create on-chain accounts
// (i.e. auth module) for the addresses of the given Applications.
func (memnet *BaseInMemoryNetwork) InitAppAccountsWithSequence(
t *testing.T,
appList ...apptypes.Application,
) {
t.Helper()

for _, application := range appList {
appAddr, err := types.AccAddressFromBech32(application.GetAddress())
require.NoError(t, err)
memnet.InitAccount(t, appAddr)
}
}

// InitGatewayAccountsWithSequence uses the testCLI to create on-chain accounts
// (i.e. auth module) for the addresses of the given Gateways.
func (memnet *BaseInMemoryNetwork) InitGatewayAccountsWithSequence(
t *testing.T,
gatewayList ...gatewaytypes.Gateway,
) {
t.Helper()

for _, gateway := range gatewayList {
gatewayAddr, err := types.AccAddressFromBech32(gateway.GetAddress())
require.NoError(t, err)
memnet.InitAccount(t, gatewayAddr)
}
}
125 changes: 125 additions & 0 deletions testutil/network/basenet/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package basenet

import (
"context"
"sync/atomic"
"testing"

"github.com/cosmos/cosmos-sdk/client"
sdknetwork "github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/stretchr/testify/require"

"github.com/pokt-network/poktroll/testutil/network"
"github.com/pokt-network/poktroll/testutil/testkeyring"
)

var _ network.InMemoryNetwork = (*BaseInMemoryNetwork)(nil)

// BaseInMemoryNetwork is an "abstract" (i.e. partial) implementation, intended
// to be embedded by other ("concrete") InMemoryNetwork implementations.
type BaseInMemoryNetwork struct {
Config network.InMemoryNetworkConfig
PreGeneratedAccountIterator *testkeyring.PreGeneratedAccountIterator
Network *sdknetwork.Network
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved

// lastAccountSeqNumber stores the last (most recently generated) account sequence number.
// NB: explicitly NOT using atomic.Int32 as it's usage doesn't compose well with anonymous
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
// literal declarations.
lastAccountSeqNumber int32
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
}

// NewBaseInMemoryNetwork creates a new BaseInMemoryNetwork with the given
// configuration and pre-generated accounts. Intended to be used in constructor
// functions of structs that embed BaseInMemoryNetwork.
func NewBaseInMemoryNetwork(
t *testing.T,
cfg *network.InMemoryNetworkConfig,
preGeneratedAccounts *testkeyring.PreGeneratedAccountIterator,
) *BaseInMemoryNetwork {
t.Helper()

return &BaseInMemoryNetwork{
Config: *cfg,
PreGeneratedAccountIterator: preGeneratedAccounts,

// First functional account sequence number is 1. Starting at 0 so that
// callers can always use NextAccountSequenceNumber() (no boundary condition).
lastAccountSeqNumber: int32(0),
}
}

// InitializeDefaults sets the underlying cosmos-sdk testutil network config to
// a reasonable default in case one was not provided with the InMemoryNetworkConfig.
func (memnet *BaseInMemoryNetwork) InitializeDefaults(t *testing.T) {
if memnet.Config.CosmosCfg == nil {
t.Log("Cosmos config not initialized, using default config")

// Initialize a network config.
cfg := network.DefaultConfig()
memnet.Config.CosmosCfg = &cfg
} else {
t.Log("Cosmos config already initialized, using existing config")
}
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
}

// GetClientCtx returns the underlying cosmos-sdk testutil network's client context.
func (memnet *BaseInMemoryNetwork) GetClientCtx(t *testing.T) client.Context {
t.Helper()

// Only the first validator's client context is populated.
// (see: https://pkg.go.dev/github.com/cosmos/cosmos-sdk/testutil/network#pkg-overview)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for linking to it!

ctx := memnet.GetNetwork(t).Validators[0].ClientCtx

// TODO_UPNEXT(@bryanchriswhite): Ensure validator key is always available.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ticket or PR #?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ctx := memnet.GetNetwork(t).Validators[0].ClientCtx
// TODO_UPNEXT(@bryanchriswhite): Ensure validator key is always available.
// TODO_UPNEXT(@bryanchriswhite): Ensure validator key is always available.
ctx := memnet.GetNetwork(t).Validators[0].ClientCtx

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more related to the line below it that the one above. I didn't want to crowd either comment.


// Overwrite the client context's Keyring with the in-memory one that contains
// our pre-generated accounts.
return ctx.WithKeyring(memnet.Config.Keyring)
}

// GetConfig returns the InMemoryNetworkConfig which associated with a given
// InMemoryNetwork instance.
func (memnet *BaseInMemoryNetwork) GetConfig(t *testing.T) *network.InMemoryNetworkConfig {
t.Helper()

return &memnet.Config
}

// GetCosmosNetworkConfig returns the underlying cosmos-sdk testutil network config.
// It requires that the config has been set, failing the test if not.
func (memnet *BaseInMemoryNetwork) GetCosmosNetworkConfig(t *testing.T) *sdknetwork.Config {
t.Helper()

require.NotEmptyf(t, memnet.Config, "in-memory network config not set")
return memnet.Config.CosmosCfg
}

// GetNetwork returns the underlying cosmos-sdk testutil network instance.
// It requires that the cosmos-sdk in-memory network has been set, failing the test if not.
func (memnet *BaseInMemoryNetwork) GetNetwork(t *testing.T) *sdknetwork.Network {
t.Helper()

require.NotEmptyf(t, memnet.Network, "in-memory cosmos network not set")
return memnet.Network
}

// GetLastAccountSequenceNumber returns the last (most recently generated) account sequence number.
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
// It is safe for concurrent use.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

func (memnet *BaseInMemoryNetwork) GetLastAccountSequenceNumber(t *testing.T) int {
t.Helper()

return int(atomic.LoadInt32(&memnet.lastAccountSeqNumber))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL!

I know this might seem like overkill, but this is making me almost feel like we need it everywhere. Going to start keeping an eye out.

}

// NextAccountSequenceNumber increments the account sequence number and returns the new value.
// It is safe for concurrent use.
func (memnet *BaseInMemoryNetwork) NextAccountSequenceNumber(t *testing.T) int {
t.Helper()

return int(atomic.AddInt32(&memnet.lastAccountSeqNumber, 1))
}

// Start is a stub which is expected to be implemented by embedders. It panics when called.
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
func (memnet *BaseInMemoryNetwork) Start(_ context.Context, t *testing.T) {
panic("not implemented")
bryanchriswhite marked this conversation as resolved.
Show resolved Hide resolved
}
Loading