Skip to content

Commit

Permalink
test: initialize upstream registry for network
Browse files Browse the repository at this point in the history
  • Loading branch information
aramalipoor authored and OBlackmon3 committed Sep 13, 2024
1 parent a072254 commit b637ab7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
6 changes: 3 additions & 3 deletions erpc/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
50 changes: 47 additions & 3 deletions erpc/networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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()

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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()

Expand Down
15 changes: 11 additions & 4 deletions upstream/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit b637ab7

Please sign in to comment.