|
| 1 | +// Copyright (c) 2026 The VeChainThor developers |
| 2 | + |
| 3 | +// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying |
| 4 | +// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> |
| 5 | + |
| 6 | +package admin_test |
| 7 | + |
| 8 | +import ( |
| 9 | + "bytes" |
| 10 | + "encoding/json" |
| 11 | + "io" |
| 12 | + "log/slog" |
| 13 | + "net/http" |
| 14 | + "net/http/httptest" |
| 15 | + "sync/atomic" |
| 16 | + "testing" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/ethereum/go-ethereum/crypto" |
| 20 | + "github.com/gorilla/mux" |
| 21 | + "github.com/stretchr/testify/assert" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | + |
| 24 | + "github.com/vechain/thor/v2/api" |
| 25 | + "github.com/vechain/thor/v2/api/admin" |
| 26 | + healthAPI "github.com/vechain/thor/v2/api/admin/health" |
| 27 | + apinode "github.com/vechain/thor/v2/api/node" |
| 28 | + "github.com/vechain/thor/v2/cmd/thor/node" |
| 29 | + "github.com/vechain/thor/v2/comm" |
| 30 | + "github.com/vechain/thor/v2/test/testchain" |
| 31 | + "github.com/vechain/thor/v2/thor" |
| 32 | + "github.com/vechain/thor/v2/txpool" |
| 33 | +) |
| 34 | + |
| 35 | +// TestAdminToggleAffectsNodeAPI is the e2e contract test: flipping |
| 36 | +// /admin/features/txpool-api via the admin server must immediately gate |
| 37 | +// /node/txpool on the business API server, via the shared atomic.Bool. |
| 38 | +func TestAdminToggleAffectsNodeAPI(t *testing.T) { |
| 39 | + chain, err := testchain.NewDefault() |
| 40 | + require.NoError(t, err) |
| 41 | + pool := txpool.New(chain.Repo(), chain.Stater(), txpool.Options{ |
| 42 | + Limit: 100, LimitPerAccount: 16, MaxLifetime: time.Minute, |
| 43 | + }, &thor.NoFork) |
| 44 | + defer pool.Close() |
| 45 | + |
| 46 | + enableTxPool := &atomic.Bool{} |
| 47 | + enableTxPool.Store(true) |
| 48 | + txpoolGate := admin.NewGate("txpool-api", enableTxPool) |
| 49 | + apiLogsGate := admin.NewGate("apilogs", &atomic.Bool{}) |
| 50 | + pprofGate := admin.NewGate("pprof", &atomic.Bool{}) |
| 51 | + |
| 52 | + // Admin server |
| 53 | + privKey, _ := crypto.HexToECDSA("99f0500549792796c14fed62011a51081dc5b5e68fe8bd8a13b86be829c4fd36") |
| 54 | + master := &node.Master{PrivateKey: privKey} |
| 55 | + adminHandler := admin.NewHTTPHandler( |
| 56 | + &slog.LevelVar{}, |
| 57 | + healthAPI.New(chain.Repo(), comm.New(chain.Repo(), pool)), |
| 58 | + apiLogsGate, txpoolGate, pprofGate, |
| 59 | + master, |
| 60 | + ) |
| 61 | + adminTS := httptest.NewServer(adminHandler) |
| 62 | + defer adminTS.Close() |
| 63 | + |
| 64 | + // Business API server, sharing enableTxPool with the admin gate |
| 65 | + nodeRouter := mux.NewRouter() |
| 66 | + apinode.New(comm.New(chain.Repo(), pool), pool, enableTxPool).Mount(nodeRouter, "/node") |
| 67 | + nodeTS := httptest.NewServer(nodeRouter) |
| 68 | + defer nodeTS.Close() |
| 69 | + |
| 70 | + // Sanity: initially enabled |
| 71 | + require.Equal(t, http.StatusOK, getStatus(t, nodeTS.URL+"/node/txpool")) |
| 72 | + |
| 73 | + // Toggle off via admin |
| 74 | + body, _ := json.Marshal(api.ToggleStatus{Enabled: false}) |
| 75 | + resp, err := http.Post(adminTS.URL+"/admin/features/txpool-api", "application/json", bytes.NewReader(body)) |
| 76 | + require.NoError(t, err) |
| 77 | + defer resp.Body.Close() |
| 78 | + require.Equal(t, http.StatusOK, resp.StatusCode) |
| 79 | + |
| 80 | + // Business endpoint now 503 |
| 81 | + assert.Equal(t, http.StatusServiceUnavailable, getStatus(t, nodeTS.URL+"/node/txpool")) |
| 82 | + |
| 83 | + // Toggle back on via admin |
| 84 | + body, _ = json.Marshal(api.ToggleStatus{Enabled: true}) |
| 85 | + resp, err = http.Post(adminTS.URL+"/admin/features/txpool-api", "application/json", bytes.NewReader(body)) |
| 86 | + require.NoError(t, err) |
| 87 | + defer resp.Body.Close() |
| 88 | + require.Equal(t, http.StatusOK, resp.StatusCode) |
| 89 | + |
| 90 | + assert.Equal(t, http.StatusOK, getStatus(t, nodeTS.URL+"/node/txpool")) |
| 91 | +} |
| 92 | + |
| 93 | +func getStatus(t *testing.T, url string) int { |
| 94 | + t.Helper() |
| 95 | + res, err := http.Get(url) //#nosec G107 |
| 96 | + require.NoError(t, err) |
| 97 | + defer res.Body.Close() |
| 98 | + _, _ = io.Copy(io.Discard, res.Body) |
| 99 | + return res.StatusCode |
| 100 | +} |
0 commit comments