|
| 1 | +package rpcsmartrouter |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "sync" |
| 8 | + "testing" |
| 9 | + |
| 10 | + commonlib "github.com/magma-Devs/smart-router/protocol/common" |
| 11 | + "github.com/magma-Devs/smart-router/protocol/holdoff" |
| 12 | + "github.com/magma-Devs/smart-router/protocol/routersession" |
| 13 | + spectypes "github.com/magma-Devs/smart-router/types/spec" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +// newLimitedTon is a TON node that answers 429 to every path, counting the requests by path. |
| 18 | +func newLimitedTon(t *testing.T) (*httptest.Server, func(path string) int) { |
| 19 | + t.Helper() |
| 20 | + var mu sync.Mutex |
| 21 | + hits := map[string]int{} |
| 22 | + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 23 | + mu.Lock() |
| 24 | + hits[r.URL.Path]++ |
| 25 | + mu.Unlock() |
| 26 | + w.Header().Set("Content-Type", "application/json") |
| 27 | + w.Header().Set("Retry-After", "60") |
| 28 | + w.WriteHeader(http.StatusTooManyRequests) |
| 29 | + fmt.Fprint(w, `{"error":"rate limited"}`) |
| 30 | + })) |
| 31 | + t.Cleanup(srv.Close) |
| 32 | + return srv, func(path string) int { |
| 33 | + mu.Lock() |
| 34 | + defer mu.Unlock() |
| 35 | + return hits[path] |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func tontReverifyInputs(t *testing.T, provider *routersession.RPCStaticProviderEndpoint, holdoffs *holdoff.Registry) (*chainReverifyInputs, *admissionRegistry) { |
| 40 | + t.Helper() |
| 41 | + admissions := newAdmissionRegistry() |
| 42 | + return &chainReverifyInputs{ |
| 43 | + chainParser: tontParser(t), |
| 44 | + rpcEndpoint: &routersession.RPCEndpoint{ChainID: "TONT", ApiInterface: spectypes.APIInterfaceRest}, |
| 45 | + convertProvidersToSessions: fakeConvert, |
| 46 | + configuredStatic: []*routersession.RPCStaticProviderEndpoint{provider}, |
| 47 | + recordAdmission: admissions.record, |
| 48 | + recordReverifiedAdmission: admissions.recordReverified, |
| 49 | + admissionFor: admissions.admissionFor, |
| 50 | + rateLimitHoldoff: holdoffs, |
| 51 | + }, admissions |
| 52 | +} |
| 53 | + |
| 54 | +// Since MAG-4214 a re-verify pass verifies the urls the router dials: on a chain with internal |
| 55 | +// paths a bare url is one url per path. The hold-off reads and writes the same list, so the |
| 56 | +// url a 429 came from, the url a relay hold is on, and the url the pass skips are one url |
| 57 | +// (found in review, PR #618). On TONT a bare base url A dials A/v2 and A/v3. |
| 58 | +func TestReverifyHoldoff_UsesTheDialedURLs(t *testing.T) { |
| 59 | + t.Run("a 429 from a generated url is held on that url", func(t *testing.T) { |
| 60 | + a := newFakeTon(t, tontRootHash) |
| 61 | + c, _ := newLimitedTon(t) |
| 62 | + provider := tontProvider(commonlib.NodeUrl{Url: a.srv.URL}, commonlib.NodeUrl{Url: c.URL}) |
| 63 | + key := holdoff.ProviderKey("TONT", spectypes.APIInterfaceRest, provider.Name) |
| 64 | + holdoffs := holdoff.NewRegistry() |
| 65 | + inputs, admissions := tontReverifyInputs(t, provider, holdoffs) |
| 66 | + active := map[uint64]*routersession.ConsumerSessionsWithProvider{0: makeSession(provider.Name)} |
| 67 | + |
| 68 | + applyReverification(t.Context(), inputs, active, reverifyTierStatic, 100) |
| 69 | + require.NotNil(t, admissions.admissionFor(provider).URLRateLimit(2), "the pass met the 429 on dialed index 2 (C/v2)") |
| 70 | + require.True(t, holdoffs.HeldOff(key, c.URL+"/v2"), "the url that answered 429 is held") |
| 71 | + require.False(t, holdoffs.HeldOff(key, a.srv.URL+"/v2"), "A/v2 answered") |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("a relay hold on a generated url is honoured", func(t *testing.T) { |
| 75 | + a := newFakeTon(t, tontRootHash) |
| 76 | + c, hits := newLimitedTon(t) |
| 77 | + provider := tontProvider(commonlib.NodeUrl{Url: a.srv.URL}, commonlib.NodeUrl{Url: c.URL}) |
| 78 | + key := holdoff.ProviderKey("TONT", spectypes.APIInterfaceRest, provider.Name) |
| 79 | + holdoffs := holdoff.NewRegistry() |
| 80 | + holdoffs.RecordRateLimit(key, c.URL+"/v2", 0) // what the relay path records for C/v2 |
| 81 | + inputs, _ := tontReverifyInputs(t, provider, holdoffs) |
| 82 | + active := map[uint64]*routersession.ConsumerSessionsWithProvider{0: makeSession(provider.Name)} |
| 83 | + |
| 84 | + applyReverification(t.Context(), inputs, active, reverifyTierStatic, 100) |
| 85 | + require.Zero(t, hits("/v2/getMasterchainInfo"), "the held url is not asked") |
| 86 | + require.NotZero(t, a.asked("/v2/getMasterchainInfo"), "the free url is") |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("a held pinned url is skipped, not a free sibling", func(t *testing.T) { |
| 90 | + a := newFakeTon(t, tontRootHash) |
| 91 | + b, hits := newLimitedTon(t) |
| 92 | + pinned := commonlib.NodeUrl{Url: b.URL + "/v2", InternalPath: "/v2"} |
| 93 | + provider := tontProvider(commonlib.NodeUrl{Url: a.srv.URL}, pinned) |
| 94 | + key := holdoff.ProviderKey("TONT", spectypes.APIInterfaceRest, provider.Name) |
| 95 | + holdoffs := holdoff.NewRegistry() |
| 96 | + holdoffs.RecordRateLimit(key, pinned.Url, 0) |
| 97 | + inputs, _ := tontReverifyInputs(t, provider, holdoffs) |
| 98 | + active := map[uint64]*routersession.ConsumerSessionsWithProvider{0: makeSession(provider.Name)} |
| 99 | + |
| 100 | + applyReverification(t.Context(), inputs, active, reverifyTierStatic, 100) |
| 101 | + require.Zero(t, hits("/v2/getMasterchainInfo"), "the held url B/v2 is not asked") |
| 102 | + require.NotZero(t, a.asked("/v2/getMasterchainInfo"), "A/v2 is asked") |
| 103 | + }) |
| 104 | + |
| 105 | + t.Run("the retry wait reads the dialed urls", func(t *testing.T) { |
| 106 | + const base = "https://ton.example" |
| 107 | + provider := tontProvider(commonlib.NodeUrl{Url: base}) |
| 108 | + parser := tontParser(t) |
| 109 | + key := holdoff.ProviderKey("TONT", spectypes.APIInterfaceRest, provider.Name) |
| 110 | + require.Equal(t, []holdoffEntry{{provider: key, url: base + "/v2"}, {provider: key, url: base + "/v3"}}, |
| 111 | + holdoffEntries(key, provider, parser), "heldOffUntil and the pass read the same entries") |
| 112 | + reg := holdoff.NewRegistry() |
| 113 | + reg.RecordRateLimit(key, base+"/v2", 0) |
| 114 | + require.True(t, heldOffUntil(reg, provider, parser).IsZero(), "/v3 is free, so a retry need not wait") |
| 115 | + }) |
| 116 | +} |
0 commit comments