Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into pedro/add_metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Feb 26, 2024
2 parents fae9af5 + 7518b78 commit ac4b87d
Show file tree
Hide file tree
Showing 26 changed files with 1,827 additions and 845 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/close-stale-issues.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
jobs:
stale:
runs-on: ubuntu-latest
# only run this action on `vechain/thor` repository
if: github.repository == 'vechain/thor'

permissions:
contents: write
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/qodana-scan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:

jobs:
scan_qodana:
# only run this action on `vechain/thor` repository
if: github.repository == 'vechain/thor'
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ volumes:
## API
Once `thor` has started, the online *OpenAPI* doc can be accessed in your browser. e.g. [http://localhost:8669/](http://localhost:8669) by default.
Once `thor` has started, the online *OpenAPI* documentation can be accessed in your browser. e.g. [http://localhost:8669/](http://localhost:8669) by default.

[![Thorest](https://raw.githubusercontent.com/vechain/thor/master/thorest.png)](http://localhost:8669/)

Expand All @@ -201,6 +201,7 @@ A special shout out to following projects:

* [Ethereum](https://github.com/ethereum)
* [Swagger](https://github.com/swagger-api)
* [Stoplight Elements](https://github.com/stoplightio/elements)

## Contributing

Expand Down
5 changes: 3 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func New(

router := mux.NewRouter()

// to serve api docs
if enableMetrics {
router.PathPrefix("/metrics").Handler(telemetry.Handler())
}
Expand All @@ -62,10 +63,10 @@ func New(
http.StripPrefix("/doc/", http.FileServer(http.FS(doc.FS))),
)

// redirect swagger-ui
// redirect stoplight-ui
router.Path("/").HandlerFunc(
func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "doc/swagger-ui/", http.StatusTemporaryRedirect)
http.Redirect(w, req, "doc/stoplight-ui/", http.StatusTemporaryRedirect)
})

accounts.New(repo, stater, callGasLimit, forkConfig).
Expand Down
21 changes: 12 additions & 9 deletions api/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func New(repo *chain.Repository, stater *state.Stater, forkConfig thor.ForkConfi
}
}

func (d *Debug) prepareClauseEnv(ctx context.Context, blockID thor.Bytes32, txIndex uint64, clauseIndex uint64) (*runtime.Runtime, *runtime.TransactionExecutor, thor.Bytes32, error) {
func (d *Debug) prepareClauseEnv(ctx context.Context, blockID thor.Bytes32, txIndex uint64, clauseIndex uint32) (*runtime.Runtime, *runtime.TransactionExecutor, thor.Bytes32, error) {
block, err := d.repo.GetBlock(blockID)
if err != nil {
if d.repo.IsNotFound(err) {
Expand All @@ -66,7 +66,7 @@ func (d *Debug) prepareClauseEnv(ctx context.Context, blockID thor.Bytes32, txIn
return nil, nil, thor.Bytes32{}, utils.Forbidden(errors.New("tx index out of range"))
}
txID := txs[txIndex].ID()
if clauseIndex >= uint64(len(txs[txIndex].Clauses())) {
if clauseIndex >= uint32(len(txs[txIndex].Clauses())) {
return nil, nil, thor.Bytes32{}, utils.Forbidden(errors.New("clause index out of range"))
}
skipPoA := d.repo.GenesisBlock().Header().ID() == devNetGenesisID
Expand All @@ -86,7 +86,7 @@ func (d *Debug) prepareClauseEnv(ctx context.Context, blockID thor.Bytes32, txIn
if err != nil {
return nil, nil, thor.Bytes32{}, err
}
clauseCounter := uint64(0)
clauseCounter := uint32(0)
for txExec.HasNextClause() {
if txIndex == uint64(i) && clauseIndex == clauseCounter {
return rt, txExec, txID, nil
Expand All @@ -110,7 +110,7 @@ func (d *Debug) prepareClauseEnv(ctx context.Context, blockID thor.Bytes32, txIn
}

// trace an existed clause
func (d *Debug) traceClause(ctx context.Context, tracer tracers.Tracer, blockID thor.Bytes32, txIndex uint64, clauseIndex uint64) (interface{}, error) {
func (d *Debug) traceClause(ctx context.Context, tracer tracers.Tracer, blockID thor.Bytes32, txIndex uint64, clauseIndex uint32) (interface{}, error) {
rt, txExec, txID, err := d.prepareClauseEnv(ctx, blockID, txIndex, clauseIndex)
if err != nil {
return nil, err
Expand All @@ -120,8 +120,8 @@ func (d *Debug) traceClause(ctx context.Context, tracer tracers.Tracer, blockID
BlockID: blockID,
BlockTime: rt.Context().Time,
TxID: txID,
TxIndex: int(txIndex),
ClauseIndex: int(clauseIndex),
TxIndex: txIndex,
ClauseIndex: clauseIndex,
State: rt.State(),
})
rt.SetVMConfig(vm.Config{Tracer: tracer})
Expand Down Expand Up @@ -259,7 +259,7 @@ func (d *Debug) traceCall(ctx context.Context, tracer tracers.Tracer, summary *c
return tracer.GetResult()
}

func (d *Debug) debugStorage(ctx context.Context, contractAddress thor.Address, blockID thor.Bytes32, txIndex uint64, clauseIndex uint64, keyStart []byte, maxResult int) (*StorageRangeResult, error) {
func (d *Debug) debugStorage(ctx context.Context, contractAddress thor.Address, blockID thor.Bytes32, txIndex uint64, clauseIndex uint32, keyStart []byte, maxResult int) (*StorageRangeResult, error) {
rt, _, _, err := d.prepareClauseEnv(ctx, blockID, txIndex, clauseIndex)
if err != nil {
return nil, err
Expand Down Expand Up @@ -316,7 +316,7 @@ func (d *Debug) handleDebugStorage(w http.ResponseWriter, req *http.Request) err
return utils.WriteJSON(w, res)
}

func (d *Debug) parseTarget(target string) (blockID thor.Bytes32, txIndex uint64, clauseIndex uint64, err error) {
func (d *Debug) parseTarget(target string) (blockID thor.Bytes32, txIndex uint64, clauseIndex uint32, err error) {
parts := strings.Split(target, "/")
if len(parts) != 3 {
return thor.Bytes32{}, 0, 0, utils.BadRequest(errors.New("target:" + target + " unsupported"))
Expand Down Expand Up @@ -346,10 +346,13 @@ func (d *Debug) parseTarget(target string) (blockID thor.Bytes32, txIndex uint64
}
txIndex = i
}
clauseIndex, err = strconv.ParseUint(parts[2], 0, 0)
i, err := strconv.ParseUint(parts[2], 0, 0)
if err != nil {
return thor.Bytes32{}, 0, 0, utils.BadRequest(errors.WithMessage(err, "target[2]"))
} else if i > math.MaxUint32 {
return thor.Bytes32{}, 0, 0, utils.BadRequest(errors.New("invalid target[2]"))
}
clauseIndex = uint32(i)
return
}

Expand Down
20 changes: 19 additions & 1 deletion api/doc/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
swagger-ui from https://github.com/swagger-api/swagger-ui @v3.17.6
## Swagger

swagger-ui from https://github.com/swagger-api/swagger-ui @v5.11.2
- Created [window-observer.js](swagger-ui/window-observer.js) to remove `Try it out` functionality for subscription endpoints

```bash
curl https://unpkg.com/[email protected]/swagger-ui.css > swagger-ui/swagger-ui.css
curl https://unpkg.com/[email protected]/swagger-ui-bundle.js > swagger-ui/swagger-ui-bundle.js
curl https://unpkg.com/[email protected]/swagger-ui-standalone-preset.js > swagger-ui/swagger-ui-standalone-preset.js
```

## Stoplight
Spotlight UI from https://github.com/stoplightio/elements @v8.0.3
- Created [window-observer.js](stoplight-ui/window-observer.js) to remove `Send API Request` functionality for subscription endpoints

```bash
curl https://unpkg.com/@stoplight/[email protected]/styles.min.css > stoplight-ui/styles.min.css
curl https://unpkg.com/@stoplight/[email protected]/web-components.min.js > stoplight-ui/web-components.min.js
```
4 changes: 3 additions & 1 deletion api/doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (
"gopkg.in/yaml.v2"
)

//go:embed swagger-ui thor.yaml
// FS embeds the Open API specs and documentation.
//
//go:embed thor.yaml stoplight-ui swagger-ui icons
var FS embed.FS
var version string

Expand Down
Binary file added api/doc/icons/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/doc/icons/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/doc/icons/favicon-96x96.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions api/doc/stoplight-ui/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
<title>Thor API</title>
<!-- Embed elements Elements via Web Component -->
<script src="./web-components.min.js"></script>
<script charset="UTF-8" src="./window-observer.js"> </script>
<link href="./styles.min.css" rel="stylesheet">
<link href="/doc/icons/favicon-32x32.png" rel="icon" sizes="32x32" type="image/png" />
<link href="/doc/icons/favicon-16x16.png" rel="icon" sizes="16x16" type="image/png" />
<link href="/doc/icons/favicon-96x96.png" rel="icon" sizes="96x96" type="image/png" />

</head>
<body>
<elements-api
apiDescriptionUrl="/doc/thor.yaml"
layout="sidebar"
logo="/doc/icons/favicon-96x96.png"
router="hash"
/>
</body>
</html>
1 change: 1 addition & 0 deletions api/doc/stoplight-ui/styles.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions api/doc/stoplight-ui/web-components.min.js

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions api/doc/stoplight-ui/window-observer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* This script is used to remove the "Send API request" button and the "Response" sections from WebSocket operations.
* @type {MutationObserver}
*/
const mutationObserver = new MutationObserver(() => {
if (window.location.hash?.includes("#/paths/subscriptions")) {
const element = document.querySelector('[data-testid="two-column-right"]');
if (element) {
element.remove();
}
}
})

mutationObserver.observe(document, {attributes: false, childList: true, characterData: false, subtree:true});
Binary file removed api/doc/swagger-ui/favicon-16x16.png
Binary file not shown.
Binary file removed api/doc/swagger-ui/favicon-32x32.png
Binary file not shown.
84 changes: 27 additions & 57 deletions api/doc/swagger-ui/index.html
Original file line number Diff line number Diff line change
@@ -1,60 +1,30 @@
<!-- HTML for static distribution bundle build -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Swagger UI</title>
<link rel="stylesheet" type="text/css" href="./swagger-ui.css" >
<link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
<style>
html
{
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after
{
box-sizing: inherit;
}

body
{
margin:0;
background: #fafafa;
}
</style>
</head>

<body>
<div id="swagger-ui"></div>

<script src="./swagger-ui-bundle.js"> </script>
<script src="./swagger-ui-standalone-preset.js"> </script>
<script>
window.onload = function() {

// Build a system
const ui = SwaggerUIBundle({
url: "../thor.yaml",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
})

window.ui = ui
}
</script>
</body>
<head>
<meta charset="utf-8" />
<title>Thor Swagger</title>
<link href="./swagger-ui.css" rel="stylesheet" />
<link href="/doc/icons/favicon-32x32.png" rel="icon" sizes="32x32" type="image/png" />
<link href="/doc/icons/favicon-16x16.png" rel="icon" sizes="16x16" type="image/png" />
<link href="/doc/icons/favicon-96x96.png" rel="icon" sizes="96x96" type="image/png" />
</head>
<body>
<div id="swagger-ui"></div>
<script charset="UTF-8" src="./window-observer.js"> </script>
<script crossorigin src="./swagger-ui-bundle.js"></script>
<script crossorigin src="./swagger-ui-standalone-preset.js"></script>
<script>
window.onload = () => {
window.ui = SwaggerUIBundle({
url: `${window.location.origin}/doc/thor.yaml`,
dom_id: '#swagger-ui',
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
layout: "StandaloneLayout",
});
};
</script>
</body>
</html>
67 changes: 0 additions & 67 deletions api/doc/swagger-ui/oauth2-redirect.html

This file was deleted.

94 changes: 2 additions & 92 deletions api/doc/swagger-ui/swagger-ui-bundle.js

Large diffs are not rendered by default.

Loading

0 comments on commit ac4b87d

Please sign in to comment.