Skip to content

Commit

Permalink
chore: add flag to enable/disable deprecated APIs (#897)
Browse files Browse the repository at this point in the history
* chore: add flag to enable/disable deprecated APIs

* chore: update for PR comments

* chore: update for PR comments

* fix: update e2e commit sha

* fix: update e2e commit sha

* fix: update flag name
  • Loading branch information
darrenvechain authored Dec 2, 2024
1 parent f1711c3 commit a157696
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 72 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test-e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ jobs:
uses: actions/checkout@v4
with:
repository: vechain/thor-e2e-tests
# https://github.com/vechain/thor-e2e-tests/tree/209f6ea9a81a98dc2d5e42bf036d2878c5837036
ref: 209f6ea9a81a98dc2d5e42bf036d2878c5837036
# https://github.com/vechain/thor-e2e-tests/tree/8b72bedff11c9e8873d88b6e2dba356d43b56779
ref: 8b72bedff11c9e8873d88b6e2dba356d43b56779

- name: Download artifact
uses: actions/download-artifact@v4
Expand Down
16 changes: 11 additions & 5 deletions api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ import (
)

type Accounts struct {
repo *chain.Repository
stater *state.Stater
callGasLimit uint64
forkConfig thor.ForkConfig
bft bft.Committer
repo *chain.Repository
stater *state.Stater
callGasLimit uint64
forkConfig thor.ForkConfig
bft bft.Committer
enabledDeprecated bool
}

func New(
Expand All @@ -40,13 +41,15 @@ func New(
callGasLimit uint64,
forkConfig thor.ForkConfig,
bft bft.Committer,
enabledDeprecated bool,
) *Accounts {
return &Accounts{
repo,
stater,
callGasLimit,
forkConfig,
bft,
enabledDeprecated,
}
}

Expand Down Expand Up @@ -168,6 +171,9 @@ func (a *Accounts) handleGetStorage(w http.ResponseWriter, req *http.Request) er
}

func (a *Accounts) handleCallContract(w http.ResponseWriter, req *http.Request) error {
if !a.enabledDeprecated {
return utils.HTTPError(nil, http.StatusGone)
}
callData := &CallData{}
if err := utils.ParseJSON(req.Body, &callData); err != nil {
return utils.BadRequest(errors.WithMessage(err, "body"))
Expand Down
21 changes: 18 additions & 3 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ var (
)

func TestAccount(t *testing.T) {
initAccountServer(t)
initAccountServer(t, true)
defer ts.Close()

tclient = thorclient.New(ts.URL)
Expand All @@ -126,6 +126,21 @@ func TestAccount(t *testing.T) {
}
}

func TestDeprecated(t *testing.T) {
initAccountServer(t, false)
defer ts.Close()

tclient = thorclient.New(ts.URL)

body := &accounts.CallData{}

_, statusCode, _ := tclient.RawHTTPClient().RawHTTPPost("/accounts", body)
assert.Equal(t, http.StatusGone, statusCode, "invalid address")

_, statusCode, _ = tclient.RawHTTPClient().RawHTTPPost("/accounts/"+contractAddr.String(), body)
assert.Equal(t, http.StatusGone, statusCode, "invalid address")
}

func getAccount(t *testing.T) {
_, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/accounts/" + invalidAddr)
require.NoError(t, err)
Expand Down Expand Up @@ -264,7 +279,7 @@ func getStorageWithNonExistingRevision(t *testing.T) {
assert.Equal(t, "revision: leveldb: not found\n", string(res), "revision not found")
}

func initAccountServer(t *testing.T) {
func initAccountServer(t *testing.T, enabledDeprecated bool) {
thorChain, err := testchain.NewIntegrationTestChain()
require.NoError(t, err)

Expand All @@ -291,7 +306,7 @@ func initAccountServer(t *testing.T) {
)

router := mux.NewRouter()
accounts.New(thorChain.Repo(), thorChain.Stater(), uint64(gasLimit), thor.NoFork, thorChain.Engine()).
accounts.New(thorChain.Repo(), thorChain.Stater(), uint64(gasLimit), thor.NoFork, thorChain.Engine(), enabledDeprecated).
Mount(router, "/accounts")

ts = httptest.NewServer(router)
Expand Down
47 changes: 26 additions & 21 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ import (

var logger = log.WithContext("pkg", "api")

type Config struct {
AllowedOrigins string
BacktraceLimit uint32
CallGasLimit uint64
PprofOn bool
SkipLogs bool
AllowCustomTracer bool
EnableReqLogger bool
EnableMetrics bool
LogsLimit uint64
AllowedTracers []string
SoloMode bool
EnableDeprecated bool
}

// New return api router
func New(
repo *chain.Repository,
Expand All @@ -41,19 +56,9 @@ func New(
bft bft.Committer,
nw node.Network,
forkConfig thor.ForkConfig,
allowedOrigins string,
backtraceLimit uint32,
callGasLimit uint64,
pprofOn bool,
skipLogs bool,
allowCustomTracer bool,
enableReqLogger bool,
enableMetrics bool,
logsLimit uint64,
allowedTracers []string,
soloMode bool,
config Config,
) (http.HandlerFunc, func()) {
origins := strings.Split(strings.TrimSpace(allowedOrigins), ",")
origins := strings.Split(strings.TrimSpace(config.AllowedOrigins), ",")
for i, o := range origins {
origins[i] = strings.ToLower(strings.TrimSpace(o))
}
Expand All @@ -71,35 +76,35 @@ func New(
http.Redirect(w, req, "doc/stoplight-ui/", http.StatusTemporaryRedirect)
})

accounts.New(repo, stater, callGasLimit, forkConfig, bft).
accounts.New(repo, stater, config.CallGasLimit, forkConfig, bft, config.EnableDeprecated).
Mount(router, "/accounts")

if !skipLogs {
events.New(repo, logDB, logsLimit).
if !config.SkipLogs {
events.New(repo, logDB, config.LogsLimit).
Mount(router, "/logs/event")
transfers.New(repo, logDB, logsLimit).
transfers.New(repo, logDB, config.LogsLimit).
Mount(router, "/logs/transfer")
}
blocks.New(repo, bft).
Mount(router, "/blocks")
transactions.New(repo, txPool).
Mount(router, "/transactions")
debug.New(repo, stater, forkConfig, callGasLimit, allowCustomTracer, bft, allowedTracers, soloMode).
debug.New(repo, stater, forkConfig, config.CallGasLimit, config.AllowCustomTracer, bft, config.AllowedTracers, config.SoloMode).
Mount(router, "/debug")
node.New(nw).
Mount(router, "/node")
subs := subscriptions.New(repo, origins, backtraceLimit, txPool)
subs := subscriptions.New(repo, origins, config.BacktraceLimit, txPool, config.EnableDeprecated)
subs.Mount(router, "/subscriptions")

if pprofOn {
if config.PprofOn {
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
router.HandleFunc("/debug/pprof/trace", pprof.Trace)
router.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
}

if enableMetrics {
if config.EnableMetrics {
router.Use(metricsMiddleware)
}

Expand All @@ -110,7 +115,7 @@ func New(
handlers.ExposedHeaders([]string{"x-genesis-id", "x-thorest-ver"}),
)(handler)

if enableReqLogger {
if config.EnableReqLogger {
handler = RequestLoggerHandler(handler, logger)
}

Expand Down
4 changes: 2 additions & 2 deletions api/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestMetricsMiddleware(t *testing.T) {
assert.NotNil(t, err)

router := mux.NewRouter()
acc := accounts.New(thorChain.Repo(), thorChain.Stater(), math.MaxUint64, thor.NoFork, thorChain.Engine())
acc := accounts.New(thorChain.Repo(), thorChain.Stater(), math.MaxUint64, thor.NoFork, thorChain.Engine(), true)
acc.Mount(router, "/accounts")
router.PathPrefix("/metrics").Handler(metrics.HTTPHandler())
router.Use(metricsMiddleware)
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestWebsocketMetrics(t *testing.T) {
require.NoError(t, err)

router := mux.NewRouter()
sub := subscriptions.New(thorChain.Repo(), []string{"*"}, 10, txpool.New(thorChain.Repo(), thorChain.Stater(), txpool.Options{}))
sub := subscriptions.New(thorChain.Repo(), []string{"*"}, 10, txpool.New(thorChain.Repo(), thorChain.Stater(), txpool.Options{}), true)
sub.Mount(router, "/subscriptions")
router.PathPrefix("/metrics").Handler(metrics.HTTPHandler())
router.Use(metricsMiddleware)
Expand Down
27 changes: 16 additions & 11 deletions api/subscriptions/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ import (
const txQueueSize = 20

type Subscriptions struct {
backtraceLimit uint32
repo *chain.Repository
upgrader *websocket.Upgrader
pendingTx *pendingTx
done chan struct{}
wg sync.WaitGroup
beat2Cache *messageCache[Beat2Message]
beatCache *messageCache[BeatMessage]
backtraceLimit uint32
enabledDeprecated bool
repo *chain.Repository
upgrader *websocket.Upgrader
pendingTx *pendingTx
done chan struct{}
wg sync.WaitGroup
beat2Cache *messageCache[Beat2Message]
beatCache *messageCache[BeatMessage]
}

type msgReader interface {
Expand All @@ -50,10 +51,11 @@ const (
pingPeriod = (pongWait * 7) / 10
)

func New(repo *chain.Repository, allowedOrigins []string, backtraceLimit uint32, txpool *txpool.TxPool) *Subscriptions {
func New(repo *chain.Repository, allowedOrigins []string, backtraceLimit uint32, txpool *txpool.TxPool, enabledDeprecated bool) *Subscriptions {
sub := &Subscriptions{
backtraceLimit: backtraceLimit,
repo: repo,
backtraceLimit: backtraceLimit,
repo: repo,
enabledDeprecated: enabledDeprecated,
upgrader: &websocket.Upgrader{
EnableCompression: true,
CheckOrigin: func(r *http.Request) bool {
Expand Down Expand Up @@ -195,6 +197,9 @@ func (s *Subscriptions) handleSubject(w http.ResponseWriter, req *http.Request)
return err
}
case "beat":
if !s.enabledDeprecated {
return utils.HTTPError(nil, http.StatusGone)
}
if reader, err = s.handleBeatReader(w, req); err != nil {
return err
}
Expand Down
19 changes: 15 additions & 4 deletions api/subscriptions/subscriptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ var ts *httptest.Server
var blocks []*block.Block

func TestSubscriptions(t *testing.T) {
initSubscriptionsServer(t)
initSubscriptionsServer(t, true)
defer ts.Close()

for name, tt := range map[string]func(*testing.T){
Expand All @@ -51,6 +51,17 @@ func TestSubscriptions(t *testing.T) {
}
}

func TestDeprecatedSubscriptions(t *testing.T) {
initSubscriptionsServer(t, false)
defer ts.Close()

u := url.URL{Scheme: "ws", Host: strings.TrimPrefix(ts.URL, "http://"), Path: "/subscriptions/beat"}

_, resp, err := websocket.DefaultDialer.Dial(u.String(), nil)
assert.Error(t, err)
assert.Equal(t, http.StatusGone, resp.StatusCode)
}

func testHandleSubjectWithBlock(t *testing.T) {
genesisBlock := blocks[0]
queryArg := fmt.Sprintf("pos=%s", genesisBlock.Header().ID().String())
Expand Down Expand Up @@ -216,7 +227,7 @@ func TestParseAddress(t *testing.T) {
assert.Equal(t, expectedAddr, *result)
}

func initSubscriptionsServer(t *testing.T) {
func initSubscriptionsServer(t *testing.T, enabledDeprecated bool) {
thorChain, err := testchain.NewIntegrationTestChain()
require.NoError(t, err)

Expand Down Expand Up @@ -263,7 +274,7 @@ func initSubscriptionsServer(t *testing.T) {
require.NoError(t, err)

router := mux.NewRouter()
New(thorChain.Repo(), []string{}, 5, txPool).
New(thorChain.Repo(), []string{}, 5, txPool, enabledDeprecated).
Mount(router, "/subscriptions")
ts = httptest.NewServer(router)
}
Expand Down Expand Up @@ -319,7 +330,7 @@ func TestSubscriptionsBacktrace(t *testing.T) {
require.NoError(t, err)

router := mux.NewRouter()
New(thorChain.Repo(), []string{}, 5, txPool).Mount(router, "/subscriptions")
New(thorChain.Repo(), []string{}, 5, txPool, true).Mount(router, "/subscriptions")
ts = httptest.NewServer(router)

defer ts.Close()
Expand Down
4 changes: 4 additions & 0 deletions cmd/thor/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ var (
Value: 1000,
Usage: "limit the number of logs returned by /logs API",
}
apiEnableDeprecatedFlag = cli.BoolFlag{
Name: "api-enable-deprecated",
Usage: "enable deprecated API endpoints (POST /accounts/{address}, POST /accounts, WS /subscriptions/beat",
}
enableAPILogsFlag = cli.BoolFlag{
Name: "enable-api-logs",
Usage: "enables API requests logging",
Expand Down
Loading

0 comments on commit a157696

Please sign in to comment.