Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
red-0ne committed Dec 6, 2023
2 parents f309253 + 86eae6f commit d462807
Show file tree
Hide file tree
Showing 64 changed files with 2,034 additions and 180 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ localnet_regenesis: ## Regenerate the localnet genesis file

.PHONY: go_lint
go_lint: ## Run all go linters
golangci-lint run --timeout 5m
golangci-lint run --timeout 5m --build-tags test

go_imports: check_go_version ## Run goimports on all go files
go run ./tools/scripts/goimports
Expand All @@ -139,7 +139,7 @@ test_e2e: ## Run all E2E tests
export POCKET_NODE=$(POCKET_NODE) && \
export APPGATE_SERVER=$(APPGATE_SERVER) && \
POKTROLLD_HOME=../../$(POKTROLLD_HOME) && \
go test -v ./e2e/tests/... -tags=e2e
go test -v ./e2e/tests/... -tags=e2e,test

.PHONY: go_test_verbose
go_test_verbose: check_go_version ## Run all go tests verbosely
Expand Down Expand Up @@ -173,10 +173,14 @@ go_mockgen: ## Use `mockgen` to generate mocks used for testing purposes of all
go generate ./pkg/relayer/interface.go
go generate ./pkg/crypto/rings/interface.go

.PHONY: go_fixturegen
go_fixturegen: ## Generate fixture data for unit tests
.PHONY: go_testgen_fixtures
go_testgen_fixtures: ## Generate fixture data for unit tests
go generate ./pkg/relayer/miner/miner_test.go

.PHONY: go_testgen_accounts
go_testgen_accounts: ## Generate test accounts for usage in test environments
go generate ./testutil/testkeyring/keyring.go

.PHONY: go_develop
go_develop: proto_regen go_mockgen ## Generate protos and mocks

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/noot/ring-go v0.0.0-20231019173746-6c4b33bcf03f
github.com/pokt-network/smt v0.7.1
github.com/regen-network/gocuke v0.6.2
github.com/rs/zerolog v1.29.1
github.com/spf13/cast v1.5.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
Expand Down Expand Up @@ -246,7 +247,6 @@ require (
github.com/rollkit/celestia-openrpc v0.3.0 // indirect
github.com/rollkit/rollkit v0.10.6 // indirect
github.com/rs/cors v1.10.1 // indirect
github.com/rs/zerolog v1.29.1 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
Expand Down
25 changes: 21 additions & 4 deletions pkg/appgateserver/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ import (
"context"
"errors"
"fmt"
"log"
"net/http"
"net/url"
"os"

"cosmossdk.io/depinject"
cosmosflags "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/rs/zerolog"
"github.com/spf13/cobra"

"github.com/pokt-network/poktroll/cmd/signals"
"github.com/pokt-network/poktroll/pkg/appgateserver"
appgateconfig "github.com/pokt-network/poktroll/pkg/appgateserver/config"
"github.com/pokt-network/poktroll/pkg/deps/config"
"github.com/pokt-network/poktroll/pkg/polylog"
"github.com/pokt-network/poktroll/pkg/polylog/polyzero"
)

// We're `explicitly omitting default` so that the appgateserver crashes if these aren't specified.
Expand Down Expand Up @@ -83,18 +85,30 @@ func runAppGateServer(cmd *cobra.Command, _ []string) error {
return err
}

// TODO_TECHDEBT: add logger level and output options to the config.
appGateConfigs, err := appgateconfig.ParseAppGateServerConfigs(configContent)
if err != nil {
return err
}

// TODO_TECHDEBT: populate logger from the config (ideally, from viper).
loggerOpts := []polylog.LoggerOption{
polyzero.WithLevel(zerolog.DebugLevel),
polyzero.WithOutput(os.Stderr),
}

// Construct a logger and associate it with the command context.
logger := polyzero.NewLogger(loggerOpts...)
ctx = logger.WithContext(ctx)
cmd.SetContext(ctx)

// Setup the AppGate server dependencies.
appGateServerDeps, err := setupAppGateServerDependencies(ctx, cmd, appGateConfigs)
if err != nil {
return fmt.Errorf("failed to setup AppGate server dependencies: %w", err)
}

log.Println("INFO: Creating AppGate server...")
logger.Info().Msg("Creating AppGate server...")

// Create the AppGate server.
appGateServer, err := appgateserver.NewAppGateServer(
Expand All @@ -112,13 +126,15 @@ func runAppGateServer(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("failed to create AppGate server: %w", err)
}

log.Printf("INFO: Starting AppGate server, listening on %s...", appGateConfigs.ListeningEndpoint.String())
logger.Info().
Str("listening_endpoint", appGateConfigs.ListeningEndpoint.String()).
Msg("Starting AppGate server...")

// Start the AppGate server.
if err := appGateServer.Start(ctx); err != nil && !errors.Is(err, http.ErrServerClosed) {
return fmt.Errorf("failed to start app gate server: %w", err)
} else if errors.Is(err, http.ErrServerClosed) {
log.Println("INFO: AppGate server stopped")
logger.Info().Msg("AppGate server stopped")
}

return nil
Expand All @@ -140,6 +156,7 @@ func setupAppGateServerDependencies(
}

supplierFuncs := []config.SupplierFn{
config.NewSupplyLoggerFromCtx(ctx),
config.NewSupplyEventsQueryClientFn(queryNodeURL.Host), // leaf
config.NewSupplyBlockClientFn(queryNodeURL.Host), // leaf
config.NewSupplyQueryClientContextFn(queryNodeURL.String()), // leaf
Expand Down
15 changes: 10 additions & 5 deletions pkg/appgateserver/error_reply.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package appgateserver

import (
"log"
"context"
"net/http"

"github.com/pokt-network/poktroll/pkg/partials"
Expand All @@ -11,15 +11,20 @@ import (
// it to the writer provided.
// NOTE: This method is used to reply with an "internal" error that is related
// to the appgateserver itself and not to the relay request.
func (app *appGateServer) replyWithError(payloadBz []byte, writer http.ResponseWriter, err error) {
responseBz, err := partials.GetErrorReply(payloadBz, err)
func (app *appGateServer) replyWithError(
ctx context.Context,
payloadBz []byte,
writer http.ResponseWriter,
err error,
) {
responseBz, err := partials.GetErrorReply(ctx, payloadBz, err)
if err != nil {
log.Printf("ERROR: failed getting error reply: %s", err)
app.logger.Error().Err(err).Msg("failed getting error reply")
return
}

if _, err = writer.Write(responseBz); err != nil {
log.Printf("ERROR: failed writing relay response: %s", err)
app.logger.Error().Err(err).Msg("failed writing relay response")
return
}
}
36 changes: 27 additions & 9 deletions pkg/appgateserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import (
"context"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"

"cosmossdk.io/depinject"

querytypes "github.com/pokt-network/poktroll/pkg/client/query/types"
"github.com/pokt-network/poktroll/pkg/polylog"
"github.com/pokt-network/poktroll/pkg/sdk"
)

Expand All @@ -39,6 +39,8 @@ type SigningInformation struct {
// is running their own instance of the appGateServer or they are sending requests to a gateway running an
// instance of the appGateServer, they will need to either include the application address in the request or not.
type appGateServer struct {
logger polylog.Logger

// signing information holds the signing key and application address for the server
signingInformation *SigningInformation

Expand All @@ -60,6 +62,13 @@ type appGateServer struct {
}

// NewAppGateServer creates a new appGateServer instance with the given dependencies.
//
// Required dependencies:
// - polylog.Logger
// - sdkclient.Context
// - client.BlockClient
// - client.AccountQueryClient
// - crypto.RingCache
func NewAppGateServer(
deps depinject.Config,
opts ...appGateServerOption,
Expand All @@ -68,6 +77,7 @@ func NewAppGateServer(

if err := depinject.Inject(
deps,
&app.logger,
&app.clientCtx,
&app.sdk,
); err != nil {
Expand Down Expand Up @@ -134,7 +144,7 @@ func (app *appGateServer) Stop(ctx context.Context) error {
// and the other (possible) path segments are the JSON RPC request path.
// TODO_TECHDEBT: Revisit the requestPath above based on the SDK that'll be exposed in the future.
func (app *appGateServer) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
ctx := request.Context()
ctx := app.logger.WithContext(request.Context())

// Extract the serviceId from the request path.
path := request.URL.Path
Expand All @@ -144,23 +154,29 @@ func (app *appGateServer) ServeHTTP(writer http.ResponseWriter, request *http.Re
requestPayloadBz, err := io.ReadAll(request.Body)
if err != nil {
app.replyWithError(
ctx,
requestPayloadBz,
writer,
ErrAppGateHandleRelay.Wrapf("reading relay request body: %s", err),
)
log.Printf("ERROR: failed reading relay request body: %s", err)
// TODO_TECHDEBT: log additional info?
app.logger.Error().Err(err).Msg("failed reading relay request body")
return
}
log.Printf("DEBUG: relay request body: %s", string(requestPayloadBz))
app.logger.Debug().
Str("service_id", serviceId).
Str("payload", string(requestPayloadBz)).
Msg("handling relay")

// Determine the application address.
appAddress := app.signingInformation.AppAddress
if appAddress == "" {
appAddress = request.URL.Query().Get("senderAddr")
}
if appAddress == "" {
app.replyWithError(requestPayloadBz, writer, ErrAppGateMissingAppAddress)
log.Print("ERROR: no application address provided")
app.replyWithError(ctx, requestPayloadBz, writer, ErrAppGateMissingAppAddress)
// TODO_TECHDEBT: log additional info?
app.logger.Error().Msg("no application address provided")
return
}

Expand All @@ -173,12 +189,14 @@ func (app *appGateServer) ServeHTTP(writer http.ResponseWriter, request *http.Re
// concurrent requests from numerous applications?
if err := app.handleSynchronousRelay(ctx, appAddress, serviceId, requestPayloadBz, request, writer); err != nil {
// Reply with an error response if there was an error handling the relay.
app.replyWithError(requestPayloadBz, writer, err)
log.Printf("ERROR: failed handling relay: %s", err)
app.replyWithError(ctx, requestPayloadBz, writer, err)
// TODO_TECHDEBT: log additional info?
app.logger.Error().Err(err).Msg("failed handling relay")
return
}

log.Print("INFO: request serviced successfully")
// TODO_TECHDEBT: log additional info?
app.logger.Info().Msg("request serviced successfully")
}

// validateConfig validates the appGateServer configuration.
Expand Down
17 changes: 13 additions & 4 deletions pkg/appgateserver/synchronous.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package appgateserver

import (
"context"
"log"
"net/http"

"github.com/pokt-network/poktroll/pkg/partials"
Expand All @@ -19,13 +18,20 @@ func (app *appGateServer) handleSynchronousRelay(
request *http.Request,
writer http.ResponseWriter,
) error {
// TODO_TECHDEBT: log additional info?
app.logger.Debug().Msg("determining request type")

// Get the type of the request by doing a partial unmarshal of the payload
log.Printf("DEBUG: Determining request type...")
requestType, err := partials.GetRequestType(payloadBz)
requestType, err := partials.GetRequestType(ctx, payloadBz)
if err != nil {
return ErrAppGateHandleRelay.Wrapf("getting request type: %s", err)
}

// TODO_TECHDEBT: log additional info?
app.logger.Debug().
Str("request_type", requestType.String()).
Msg("got request type")

sessionSuppliers, err := app.sdk.GetSessionSupplierEndpoints(ctx, appAddress, serviceId)
if err != nil {
return ErrAppGateHandleRelay.Wrapf("getting current session: %s", err)
Expand All @@ -42,8 +48,11 @@ func (app *appGateServer) handleSynchronousRelay(
return err
}

app.logger.Debug().
Str("relay_response_payload", string(relayResponse.Payload)).
Msg("writing relay response payload")

// Reply with the RelayResponse payload.
log.Printf("DEBUG: Writing relay response payload: %s", string(relayResponse.Payload))
if _, err := writer.Write(relayResponse.Payload); err != nil {
return ErrAppGateHandleRelay.Wrapf("writing relay response payload: %s", err)
}
Expand Down
Loading

0 comments on commit d462807

Please sign in to comment.