Skip to content

Commit

Permalink
Merge branch 'tony/maindb-v4' into darren/maindb-v4-with-logs-changes
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Dec 3, 2024
2 parents c7141b9 + edd3101 commit f1f7cd7
Show file tree
Hide file tree
Showing 24 changed files with 677 additions and 103 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/lint-go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ jobs:
with:
go-version: '1.22'
cache: false

- name: Check `builtins` directory
# if it has any changes in the 'builtins' dir after running `go generate`, echo an error and fail the workflow
run: |
go generate ./builtin/gen
git diff --exit-code builtin/gen || (echo "\n\n\nbuiltin/gen directory is not up to date, run 'go generate ./...' to update it" && exit 1)
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<p align="center">
<a href="https://www.vechain.org/vechainthor/">
<picture style="padding: 80px;">
<source srcset="docs/assets/banner-dark-mode.png" media="(prefers-color-scheme: dark)" >
<img src="docs/assets/banner-light-mode.png" style="padding: 20px;">
<source srcset="https://github.com/vechain/thor/blob/master/docs/assets/banner-dark-mode.png" media="(prefers-color-scheme: dark)" >
<img src="https://github.com/vechain/thor/blob/master/docs/assets/banner-light-mode.png" style="padding: 20px;">
</picture>
</a>
</p>
Expand Down Expand Up @@ -44,9 +44,9 @@ ___

## Documentation

- [Build](./docs/build.md) - How to build the `thor` binary.
- [Usage](./docs/usage.md) - How to run thor with different configurations.
- [Hosting a Node](./docs/hosting-a-node.md) - Considerations and requirements for hosting a node.
- [Build](https://github.com/vechain/thor/blob/master/docs/build.md) - How to build the `thor` binary.
- [Usage](https://github.com/vechain/thor/blob/master/docs/usage.md) - How to run thor with different configurations.
- [Hosting a Node](https://github.com/vechain/thor/blob/master/docs/hosting-a-node.md) - Considerations and requirements for hosting a node.
- [Core Concepts](https://docs.vechain.org/core-concepts) - Core concepts of the VeChainThor blockchain.
- [API Reference](https://mainnet.vechain.org) - The API reference for the VeChainThor blockchain.

Expand All @@ -67,15 +67,15 @@ To chat with other community members you can join:
<a href="https://www.reddit.com/r/Vechain"><img src="https://img.shields.io/badge/Reddit-FF4500?style=for-the-badge&logo=reddit&logoColor=white"/></a>
</p>

Do note that our [Code of Conduct](./docs/CODE_OF_CONDUCT.md) applies to all VeChain community channels. Users are
Do note that our [Code of Conduct](https://github.com/vechain/thor/blob/master/docs/CODE_OF_CONDUCT.md) applies to all VeChain community channels. Users are
**highly encouraged** to read and adhere to them to avoid repercussions.

---

## Contributing

Contributions to VeChainThor are welcome and highly appreciated. However, before you jump right into it, we would like
you to review our [Contribution Guidelines](./docs/CONTRIBUTING.md) to make sure you have a smooth experience
you to review our [Contribution Guidelines](https://github.com/vechain/thor/blob/master/docs/CONTRIBUTING.md) to make sure you have a smooth experience
contributing to VeChainThor.

---
Expand Down
12 changes: 6 additions & 6 deletions api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,27 +358,27 @@ func (a *Accounts) Mount(root *mux.Router, pathPrefix string) {

sub.Path("/*").
Methods(http.MethodPost).
Name("accounts_call_batch_code").
Name("POST /accounts/*").
HandlerFunc(utils.WrapHandlerFunc(a.handleCallBatchCode))
sub.Path("/{address}").
Methods(http.MethodGet).
Name("accounts_get_account").
Name("GET /accounts/{address}").
HandlerFunc(utils.WrapHandlerFunc(a.handleGetAccount))
sub.Path("/{address}/code").
Methods(http.MethodGet).
Name("accounts_get_code").
Name("GET /accounts/{address}/code").
HandlerFunc(utils.WrapHandlerFunc(a.handleGetCode))
sub.Path("/{address}/storage/{key}").
Methods("GET").
Name("accounts_get_storage").
Name("GET /accounts/{address}/storage").
HandlerFunc(utils.WrapHandlerFunc(a.handleGetStorage))
// These two methods are currently deprecated
sub.Path("").
Methods(http.MethodPost).
Name("accounts_call_contract").
Name("POST /accounts").
HandlerFunc(utils.WrapHandlerFunc(a.handleCallContract))
sub.Path("/{address}").
Methods(http.MethodPost).
Name("accounts_call_contract_address").
Name("POST /accounts/{address}").
HandlerFunc(utils.WrapHandlerFunc(a.handleCallContract))
}
2 changes: 1 addition & 1 deletion api/blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,6 @@ func (b *Blocks) Mount(root *mux.Router, pathPrefix string) {
sub := root.PathPrefix(pathPrefix).Subrouter()
sub.Path("/{revision}").
Methods(http.MethodGet).
Name("blocks_get_block").
Name("GET /blocks/{revision}").
HandlerFunc(utils.WrapHandlerFunc(b.handleGetBlock))
}
6 changes: 3 additions & 3 deletions api/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,14 @@ func (d *Debug) Mount(root *mux.Router, pathPrefix string) {

sub.Path("/tracers").
Methods(http.MethodPost).
Name("debug_trace_clause").
Name("POST /debug/tracers").
HandlerFunc(utils.WrapHandlerFunc(d.handleTraceClause))
sub.Path("/tracers/call").
Methods(http.MethodPost).
Name("debug_trace_call").
Name("POST /debug/tracers/call").
HandlerFunc(utils.WrapHandlerFunc(d.handleTraceCall))
sub.Path("/storage-range").
Methods(http.MethodPost).
Name("debug_trace_storage").
Name("POST /debug/storage-range").
HandlerFunc(utils.WrapHandlerFunc(d.handleDebugStorage))
}
4 changes: 2 additions & 2 deletions api/doc/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## 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
- 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
Expand All @@ -11,7 +11,7 @@ curl https://unpkg.com/[email protected]/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
- 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
Expand Down
2 changes: 1 addition & 1 deletion api/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ func (e *Events) Mount(root *mux.Router, pathPrefix string) {

sub.Path("").
Methods(http.MethodPost).
Name("logs_filter_event").
Name("POST /logs/event").
HandlerFunc(utils.WrapHandlerFunc(e.handleFilter))
}
28 changes: 17 additions & 11 deletions api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ import (
)

var (
websocketDurations = []int64{
0, 1, 2, 5, 10, 25, 50, 100, 250, 500, 1_000, 2_500, 5_000, 10_000, 25_000,
50_000, 100_000, 250_000, 500_000, 1000_000, 2_500_000, 5_000_000, 10_000_000,
}
metricHTTPReqCounter = metrics.LazyLoadCounterVec("api_request_count", []string{"name", "code", "method"})
metricHTTPReqDuration = metrics.LazyLoadHistogramVec("api_duration_ms", []string{"name", "code", "method"}, metrics.BucketHTTPReqs)
metricActiveWebsocketCount = metrics.LazyLoadGaugeVec("api_active_websocket_count", []string{"subject"})
metricWebsocketDuration = metrics.LazyLoadHistogramVec("api_websocket_duration", []string{"name", "code"}, websocketDurations)
metricActiveWebsocketGauge = metrics.LazyLoadGaugeVec("api_active_websocket_gauge", []string{"name"})
metricWebsocketCounter = metrics.LazyLoadCounterVec("api_websocket_counter", []string{"name"})
)

// metricsResponseWriter is a wrapper around http.ResponseWriter that captures the status code.
Expand Down Expand Up @@ -62,32 +68,32 @@ func metricsMiddleware(next http.Handler) http.Handler {
var (
enabled = false
name = ""
subscription = ""
subscription = false
)

// all named route will be recorded
if rt != nil && rt.GetName() != "" {
enabled = true
name = rt.GetName()
if strings.HasPrefix(name, "subscriptions") {
// example path: /subscriptions/txpool -> subject = txpool
paths := strings.Split(r.URL.Path, "/")
if len(paths) > 2 {
subscription = paths[2]
}
subscription = true
name = "WS " + r.URL.Path
}
}

now := time.Now()
mrw := newMetricsResponseWriter(w)
if subscription != "" {
metricActiveWebsocketCount().AddWithLabel(1, map[string]string{"subject": subscription})
if subscription {
metricActiveWebsocketGauge().AddWithLabel(1, map[string]string{"name": name})
metricWebsocketCounter().AddWithLabel(1, map[string]string{"name": name})
}

next.ServeHTTP(mrw, r)

if subscription != "" {
metricActiveWebsocketCount().AddWithLabel(-1, map[string]string{"subject": subscription})
if subscription {
metricActiveWebsocketGauge().AddWithLabel(-1, map[string]string{"name": name})
// record websocket duration in seconds, not MS
metricWebsocketDuration().ObserveWithLabels(time.Since(now).Milliseconds()/1000, map[string]string{"name": name, "code": strconv.Itoa(mrw.statusCode)})
} else if enabled {
metricHTTPReqCounter().AddWithLabel(1, map[string]string{"name": name, "code": strconv.Itoa(mrw.statusCode), "method": r.Method})
metricHTTPReqDuration().ObserveWithLabels(time.Since(now).Milliseconds(), map[string]string{"name": name, "code": strconv.Itoa(mrw.statusCode), "method": r.Method})
Expand Down
22 changes: 11 additions & 11 deletions api/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func TestMetricsMiddleware(t *testing.T) {
assert.Equal(t, "method", labels[1].GetName())
assert.Equal(t, "GET", labels[1].GetValue())
assert.Equal(t, "name", labels[2].GetName())
assert.Equal(t, "accounts_get_account", labels[2].GetValue())
assert.Equal(t, "GET /accounts/{address}", labels[2].GetValue())

labels = m[1].GetLabel()
assert.Equal(t, 3, len(labels))
Expand All @@ -86,7 +86,7 @@ func TestMetricsMiddleware(t *testing.T) {
assert.Equal(t, "method", labels[1].GetName())
assert.Equal(t, "GET", labels[1].GetValue())
assert.Equal(t, "name", labels[2].GetName())
assert.Equal(t, "accounts_get_account", labels[2].GetValue())
assert.Equal(t, "GET /accounts/{address}", labels[2].GetValue())

labels = m[2].GetLabel()
assert.Equal(t, 3, len(labels))
Expand All @@ -95,7 +95,7 @@ func TestMetricsMiddleware(t *testing.T) {
assert.Equal(t, "method", labels[1].GetName())
assert.Equal(t, "GET", labels[1].GetValue())
assert.Equal(t, "name", labels[2].GetName())
assert.Equal(t, "accounts_get_account", labels[2].GetValue())
assert.Equal(t, "GET /accounts/{address}", labels[2].GetValue())
}

func TestWebsocketMetrics(t *testing.T) {
Expand All @@ -120,13 +120,13 @@ func TestWebsocketMetrics(t *testing.T) {
metrics, err := parser.TextToMetricFamilies(bytes.NewReader(body))
assert.Nil(t, err)

m := metrics["thor_metrics_api_active_websocket_count"].GetMetric()
m := metrics["thor_metrics_api_active_websocket_gauge"].GetMetric()
assert.Equal(t, 1, len(m), "should be 1 metric entries")
assert.Equal(t, float64(1), m[0].GetGauge().GetValue())

labels := m[0].GetLabel()
assert.Equal(t, "subject", labels[0].GetName())
assert.Equal(t, "beat", labels[0].GetValue())
assert.Equal(t, "name", labels[0].GetName())
assert.Equal(t, "WS /subscriptions/beat", labels[0].GetValue())

// initiate 1 beat subscription, active websocket should be 2
conn2, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
Expand All @@ -137,7 +137,7 @@ func TestWebsocketMetrics(t *testing.T) {
metrics, err = parser.TextToMetricFamilies(bytes.NewReader(body))
assert.Nil(t, err)

m = metrics["thor_metrics_api_active_websocket_count"].GetMetric()
m = metrics["thor_metrics_api_active_websocket_gauge"].GetMetric()
assert.Equal(t, 1, len(m), "should be 1 metric entries")
assert.Equal(t, float64(2), m[0].GetGauge().GetValue())

Expand All @@ -151,16 +151,16 @@ func TestWebsocketMetrics(t *testing.T) {
metrics, err = parser.TextToMetricFamilies(bytes.NewReader(body))
assert.Nil(t, err)

m = metrics["thor_metrics_api_active_websocket_count"].GetMetric()
m = metrics["thor_metrics_api_active_websocket_gauge"].GetMetric()
assert.Equal(t, 2, len(m), "should be 2 metric entries")
// both m[0] and m[1] should have the value of 1
assert.Equal(t, float64(2), m[0].GetGauge().GetValue())
assert.Equal(t, float64(1), m[1].GetGauge().GetValue())

// m[1] should have the subject of block
// m[1] should have the name of block
labels = m[1].GetLabel()
assert.Equal(t, "subject", labels[0].GetName())
assert.Equal(t, "block", labels[0].GetValue())
assert.Equal(t, "name", labels[0].GetName())
assert.Equal(t, "WS /subscriptions/block", labels[0].GetValue())
}

func httpGet(t *testing.T, url string) ([]byte, int) {
Expand Down
2 changes: 1 addition & 1 deletion api/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ func (n *Node) Mount(root *mux.Router, pathPrefix string) {

sub.Path("/network/peers").
Methods(http.MethodGet).
Name("node_get_peers").
Name("GET /node/network/peers").
HandlerFunc(utils.WrapHandlerFunc(n.handleNetwork))
}
6 changes: 3 additions & 3 deletions api/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,14 @@ func (t *Transactions) Mount(root *mux.Router, pathPrefix string) {

sub.Path("").
Methods(http.MethodPost).
Name("transactions_send_tx").
Name("POST /transactions").
HandlerFunc(utils.WrapHandlerFunc(t.handleSendTransaction))
sub.Path("/{id}").
Methods(http.MethodGet).
Name("transactions_get_tx").
Name("GET /transactions/{id}").
HandlerFunc(utils.WrapHandlerFunc(t.handleGetTransactionByID))
sub.Path("/{id}/receipt").
Methods(http.MethodGet).
Name("transactions_get_receipt").
Name("GET /transactions/{id}/receipt").
HandlerFunc(utils.WrapHandlerFunc(t.handleGetTransactionReceiptByID))
}
Loading

0 comments on commit f1f7cd7

Please sign in to comment.