From b637ab7e9f2601c2416057764b463d6d7492e20c Mon Sep 17 00:00:00 2001 From: aramalipoor Date: Sun, 8 Sep 2024 18:54:12 +0200 Subject: [PATCH] test: initialize upstream registry for network --- erpc/http_server.go | 6 +++--- erpc/networks_test.go | 50 ++++++++++++++++++++++++++++++++++++++++--- upstream/registry.go | 15 +++++++++---- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/erpc/http_server.go b/erpc/http_server.go index 9468b90d..3313114d 100644 --- a/erpc/http_server.go +++ b/erpc/http_server.go @@ -400,12 +400,12 @@ func processErrorBody(logger *zerolog.Logger, nq *common.NormalizedRequest, err nq.Mu.RLock() } if common.HasErrorCode(err, common.ErrCodeEndpointClientSideException) { - logger.Debug().Object("request", nq).Err(err).Msgf("forward request errored with client-side exception") + logger.Debug().Err(err).Object("request", nq).Msgf("forward request errored with client-side exception") } else { if e, ok := err.(common.StandardError); ok { - logger.Error().Object("request", nq).Err(err).Msgf("failed to forward request: %s", e.DeepestMessage()) + logger.Error().Err(err).Object("request", nq).Msgf("failed to forward request: %s", e.DeepestMessage()) } else { - logger.Error().Object("request", nq).Err(err).Msgf("failed to forward request: %s", err.Error()) + logger.Error().Err(err).Object("request", nq).Msgf("failed to forward request: %s", err.Error()) } } if nq != nil { diff --git a/erpc/networks_test.go b/erpc/networks_test.go index 8c600ae0..6d2dbfea 100644 --- a/erpc/networks_test.go +++ b/erpc/networks_test.go @@ -30,7 +30,7 @@ import ( var TRUE = true func init() { - log.Logger = log.Level(zerolog.TraceLevel).Output(zerolog.ConsoleWriter{Out: os.Stderr}) + log.Logger = log.Level(zerolog.ErrorLevel).Output(zerolog.ConsoleWriter{Out: os.Stderr}) } func TestNetwork_Forward(t *testing.T) { @@ -64,10 +64,20 @@ func TestNetwork_Forward(t *testing.T) { } mt := health.NewTracker("prjA", 2*time.Second) + up1 := &common.UpstreamConfig{ + Type: common.UpstreamTypeEvm, + Id: "test", + Endpoint: "http://rpc1.localhost", + Evm: &common.EvmUpstreamConfig{ + ChainId: 123, + }, + } upsReg := upstream.NewUpstreamsRegistry( &log.Logger, "prjA", - []*common.UpstreamConfig{}, + []*common.UpstreamConfig{ + up1, + }, rateLimitersRegistry, vendors.NewVendorsRegistry(), mt, @@ -77,6 +87,10 @@ func TestNetwork_Forward(t *testing.T) { &log.Logger, "prjA", &common.NetworkConfig{ + Architecture: common.ArchitectureEvm, + Evm: &common.EvmNetworkConfig{ + ChainId: 123, + }, RateLimitBudget: "MyLimiterBudget_Test1", }, rateLimitersRegistry, @@ -86,6 +100,14 @@ func TestNetwork_Forward(t *testing.T) { if err != nil { t.Fatal(err) } + err = upsReg.Bootstrap(context.TODO()) + if err != nil { + t.Fatal(err) + } + err = upsReg.PrepareUpstreamsForNetwork(util.EvmNetworkId(123)) + if err != nil { + t.Fatal(err) + } ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -133,10 +155,20 @@ func TestNetwork_Forward(t *testing.T) { } mt := health.NewTracker("prjA", 2*time.Second) + up1 := &common.UpstreamConfig{ + Type: common.UpstreamTypeEvm, + Id: "test", + Endpoint: "http://rpc1.localhost", + Evm: &common.EvmUpstreamConfig{ + ChainId: 123, + }, + } upsReg := upstream.NewUpstreamsRegistry( &log.Logger, "prjA", - []*common.UpstreamConfig{}, + []*common.UpstreamConfig{ + up1, + }, rateLimitersRegistry, vendors.NewVendorsRegistry(), mt, @@ -146,6 +178,10 @@ func TestNetwork_Forward(t *testing.T) { &log.Logger, "prjA", &common.NetworkConfig{ + Architecture: common.ArchitectureEvm, + Evm: &common.EvmNetworkConfig{ + ChainId: 123, + }, RateLimitBudget: "MyLimiterBudget_Test2", }, rateLimitersRegistry, @@ -155,6 +191,14 @@ func TestNetwork_Forward(t *testing.T) { if err != nil { t.Fatal(err) } + err = upsReg.Bootstrap(context.TODO()) + if err != nil { + t.Fatal(err) + } + err = upsReg.PrepareUpstreamsForNetwork(util.EvmNetworkId(123)) + if err != nil { + t.Fatal(err) + } ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/upstream/registry.go b/upstream/registry.go index 823ae264..ee7abe8a 100644 --- a/upstream/registry.go +++ b/upstream/registry.go @@ -168,11 +168,18 @@ func (u *UpstreamsRegistry) GetSortedUpstreams(networkId, method string) ([]*Ups // Initialize scores for this method on this network and "any" network for _, ups := range methodUpsList { - if _, ok := u.upstreamScores[ups.Config().Id][networkId][method]; !ok { - u.upstreamScores[ups.Config().Id][networkId][method] = 0 + upid := ups.Config().Id + if _, ok := u.upstreamScores[upid]; !ok { + u.upstreamScores[upid] = make(map[string]map[string]float64) } - if _, ok := u.upstreamScores[ups.Config().Id]["*"][method]; !ok { - u.upstreamScores[ups.Config().Id]["*"][method] = 0 + if _, ok := u.upstreamScores[upid][networkId]; !ok { + u.upstreamScores[upid][networkId] = make(map[string]float64) + } + if _, ok := u.upstreamScores[upid][networkId][method]; !ok { + u.upstreamScores[upid][networkId][method] = 0 + } + if _, ok := u.upstreamScores[upid]["*"][method]; !ok { + u.upstreamScores[upid]["*"][method] = 0 } }