Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Jan 15, 2025
1 parent 0b1ff7f commit b975758
Show file tree
Hide file tree
Showing 5 changed files with 476 additions and 81 deletions.
49 changes: 32 additions & 17 deletions testutil/e2e/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"testing"

comettypes "github.com/cometbft/cometbft/types"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/gorilla/websocket"
Expand All @@ -26,14 +27,14 @@ import (
// E2EApp wraps an integration.App and provides both gRPC and WebSocket servers for end-to-end testing
type E2EApp struct {
*integration.App
grpcServer *grpc.Server
grpcListener net.Listener
wsServer *http.Server
wsListener net.Listener
wsUpgrader websocket.Upgrader
wsConnMutex sync.RWMutex
wsConnections map[*websocket.Conn]map[string]struct{} // maps connections to their subscribed event queries
blockEventChan chan *coretypes.ResultEvent
grpcServer *grpc.Server
grpcListener net.Listener
wsServer *http.Server
wsListener net.Listener
wsUpgrader websocket.Upgrader
wsConnMutex sync.RWMutex
wsConnections map[*websocket.Conn]map[string]struct{} // maps connections to their subscribed event queries
resultEventChan chan *coretypes.ResultEvent
}

// NewE2EApp creates a new E2EApp instance with integration.App, gRPC, and WebSocket servers
Expand Down Expand Up @@ -78,13 +79,13 @@ func NewE2EApp(t *testing.T, opts ...integration.IntegrationAppOptionFn) *E2EApp
require.NoError(t, err, "failed to create WebSocket listener")

e2eApp := &E2EApp{
App: app,
grpcListener: grpcListener,
grpcServer: grpcServer,
wsListener: wsListener,
wsConnections: make(map[*websocket.Conn]map[string]struct{}),
wsUpgrader: websocket.Upgrader{},
blockEventChan: make(chan *coretypes.ResultEvent, 1),
App: app,
grpcListener: grpcListener,
grpcServer: grpcServer,
wsListener: wsListener,
wsConnections: make(map[*websocket.Conn]map[string]struct{}),
wsUpgrader: websocket.Upgrader{},
resultEventChan: make(chan *coretypes.ResultEvent),
}

mux.Handle(http.MethodPost, rootPattern, newPostHandler(client, e2eApp))
Expand Down Expand Up @@ -117,7 +118,7 @@ func NewE2EApp(t *testing.T, opts ...integration.IntegrationAppOptionFn) *E2EApp
}()

// Start event handling
go e2eApp.handleBlockEvents(t)
go e2eApp.handleResultEvents(t)

return e2eApp
}
Expand All @@ -129,7 +130,7 @@ func (app *E2EApp) Close() error {
return err
}

close(app.blockEventChan)
close(app.resultEventChan)

return nil
}
Expand All @@ -147,3 +148,17 @@ func (app *E2EApp) GetClientConn(ctx context.Context) (*grpc.ClientConn, error)
func (app *E2EApp) GetWSEndpoint() string {
return "ws://" + app.wsListener.Addr().String() + "/websocket"
}

// TODO_IN_THIS_COMMIT: godoc & move...
func (app *E2EApp) GetCometBlockID() comettypes.BlockID {
lastBlockID := app.GetSdkCtx().BlockHeader().LastBlockId
partSetHeader := lastBlockID.GetPartSetHeader()

return comettypes.BlockID{
Hash: lastBlockID.GetHash(),
PartSetHeader: comettypes.PartSetHeader{
Total: partSetHeader.GetTotal(),
Hash: partSetHeader.GetHash(),
},
}
}
107 changes: 106 additions & 1 deletion testutil/e2e/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestNewE2EApp(t *testing.T) {
_, err = app.RunMsg(t, &banktypes.MsgSend{
FromAddress: app.GetFaucetBech32(),
ToAddress: gateway2Addr.String(),
Amount: cosmostypes.NewCoins(cosmostypes.NewInt64Coin(volatile.DenomuPOKT, 100000000)),
Amount: cosmostypes.NewCoins(cosmostypes.NewInt64Coin(volatile.DenomuPOKT, 10000000000)),
})
require.NoError(t, err)

Expand Down Expand Up @@ -319,3 +319,108 @@ func TestSanity3(t *testing.T) {

t.Logf("result: %s", result)
}

func TestSanity4(t *testing.T) {
ctx := context.Background()
initialHeight := int64(7553)
// TODO_IN_THIS_COMMIT: does this 👆 need to be reconciled with the internal height of app?

//app := NewE2EApp(t)

//registry := codectypes.NewInterfaceRegistry()
//cdc := codec.NewProtoCodec(registry)
keyRing := keyring.NewInMemory(testclient.Marshaler)
_, err := keyRing.NewAccount(
"pnf",
"crumble shrimp south strategy speed kick green topic stool seminar track stand rhythm almost bubble pet knock steel pull flag weekend country major blade",
"",
cosmostypes.FullFundraiserPath,
hd.Secp256k1,
)
require.NoError(t, err)

//// TODO_IN_THIS_COMMOT: fund gateway2 account.
//_, err = app.RunMsg(t, &banktypes.MsgSend{
// FromAddress: app.GetFaucetBech32(),
// ToAddress: pnfAddr.String(),
// Amount: cosmostypes.NewCoins(cosmostypes.NewInt64Coin(volatile.DenomuPOKT, 10000000000)),
//})
//require.NoError(t, err)

ctrl := gomock.NewController(t)
blockQueryClient := mockclient.NewMockBlockQueryClient(ctrl)
blockQueryClient.EXPECT().
Block(gomock.Any(), gomock.Any()).
DoAndReturn(
func(ctx context.Context, height *int64) (*cometrpctypes.ResultBlock, error) {
//time.Sleep(time.Second * 100)
blockResultMock := &cometrpctypes.ResultBlock{
Block: &types.Block{
Header: types.Header{
Height: initialHeight,
},
},
}
return blockResultMock, nil
},
).AnyTimes()
//blockQueryClient, err := sdkclient.NewClientFromNode("tcp://127.0.0.1:42070")
//blockQueryClient, err := sdkclient.NewClientFromNode("tcp://127.0.0.1:26657")
//require.NoError(t, err)

//creds := insecure.NewCredentials()
//grpcConn := testclient.NewLocalnetClientCtx(t, flagSet).GetClient()
//grpcConn, err := grpc.NewClient("127.0.0.1:42069", grpc.WithTransportCredentials(creds))
//require.NoError(t, err)

// TODO_IN_THIS_COMMIT: NOT localnet flagset NOR context, should be
// configured to match the E2E app listeners.
//flagSet := testclient.NewFlagSet(t, "tcp://127.0.0.1:42070")
flagSet := testclient.NewLocalnetFlagSet(t)
clientCtx := testclient.NewLocalnetClientCtx(t, flagSet).WithKeyring(keyRing)
deps := depinject.Supply(clientCtx, blockQueryClient)

sharedQueryClient, err := query.NewSharedQuerier(deps)
require.NoError(t, err)

sharedParams, err := sharedQueryClient.GetParams(ctx)
require.NoError(t, err)

t.Logf("shared params: %+v", sharedParams)

eventsQueryClient := events.NewEventsQueryClient("ws://127.0.0.1:26657/websocket")
deps = depinject.Configs(deps, depinject.Supply(eventsQueryClient))
blockClient, err := block.NewBlockClient(ctx, deps)
require.NoError(t, err)

txFactory, err := cosmostx.NewFactoryCLI(clientCtx, flagSet)
require.NoError(t, err)

deps = depinject.Configs(deps, depinject.Supply(txtypes.Context(clientCtx), txFactory))

//_, txContext := testtx.NewE2ETxContext(t, keyRing, flagSet)
txContext, err := tx.NewTxContext(deps)
require.NoError(t, err)

deps = depinject.Configs(deps, depinject.Supply(blockClient, txContext))
txClient, err := tx.NewTxClient(ctx, deps, tx.WithSigningKeyName("pnf"))
require.NoError(t, err)

time.Sleep(time.Second * 1)

eitherErr := txClient.SignAndBroadcast(
ctx,
&banktypes.MsgSend{
FromAddress: "pokt1eeeksh2tvkh7wzmfrljnhw4wrhs55lcuvmekkw",
ToAddress: "pokt15w3fhfyc0lttv7r585e2ncpf6t2kl9uh8rsnyz",
Amount: cosmostypes.NewCoins(cosmostypes.NewInt64Coin(volatile.DenomuPOKT, 10000000000)),
},
)

// TODO_IN_THIS_COMMIT: signal to the WS server to send another block result event...
//app.NextBlock(t)

err, errCh := eitherErr.SyncOrAsyncError()
require.NoError(t, err)
require.NoError(t, <-errCh)
}
10 changes: 8 additions & 2 deletions testutil/e2e/comet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cometbft/cometbft/abci/types"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
rpctypes "github.com/cometbft/cometbft/rpc/jsonrpc/types"
comettypes "github.com/cometbft/cometbft/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
gogogrpc "github.com/cosmos/gogoproto/grpc"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
Expand Down Expand Up @@ -167,7 +168,12 @@ func newPostHandler(client gogogrpc.ClientConn, app *E2EApp) runtime.HandlerFunc
// Simulate 1 second block production delay.
time.Sleep(time.Second * 1)

app.EmitWSEvents(finalizeBlockRes.GetEvents())
fmt.Println(">>> emitting ws events")
//app.EmitWSEvents(app.GetSdkCtx().EventManager().Events())

// TODO_IMPROVE: If we want/need to support multiple txs per
// block in the future, this will have to be refactored.
app.EmitWSEvents(finalizeBlockRes, txBz)
}()

// DEV_NOTE: There SHOULD ALWAYS be exactly one tx result so long as
Expand All @@ -179,7 +185,7 @@ func newPostHandler(client gogogrpc.ClientConn, app *E2EApp) runtime.HandlerFunc
Data: txRes.GetData(),
Log: txRes.GetLog(),
Codespace: txRes.GetCodespace(),
//Hash:,
Hash: comettypes.Tx(txBz).Hash(),
}

response = rpctypes.NewRPCSuccessResponse(req.ID, bcastTxRes)
Expand Down
Loading

0 comments on commit b975758

Please sign in to comment.