Skip to content

Commit

Permalink
Merge branch 'feat/db' into miguel/object-write-read2
Browse files Browse the repository at this point in the history
  • Loading branch information
freemanzMrojo authored Jan 2, 2025
2 parents 9025c5f + 2425384 commit db0f469
Show file tree
Hide file tree
Showing 31 changed files with 521 additions and 265 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/publish-docker-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

- name: Update Docker Hub
# official documentation docker: https://docs.docker.com/build/ci/github-actions/update-dockerhub-desc/
if: ${{ inputs.environment == 'docker-publish' && github.event_name != 'pull_request' }}
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
repository: ${{ github.repository }}

- name: Scan for vulnerabilities
uses: crazy-max/ghaction-container-scan@v3
if: ${{ github.event_name == 'pull_request' || github.ref_name == 'master' }}
Expand Down
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/8b72bedff11c9e8873d88b6e2dba356d43b56779
ref: 8b72bedff11c9e8873d88b6e2dba356d43b56779
# https://github.com/vechain/thor-e2e-tests/tree/956b34bcf5b0b072cd7c8fd2e546a9beb66a866a
ref: 956b34bcf5b0b072cd7c8fd2e546a9beb66a866a

- name: Download artifact
uses: actions/download-artifact@v4
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jobs:
matrix:
go-version: [1.22.x]
os: [ubuntu-latest, macos-latest, windows-latest]
include:
- go-version: 1.23.x
os: ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ dep:| go_version_check

go_version_check:
@if test $(MAJOR) -lt 1; then \
echo "Go 1.19 or higher required"; \
echo "Go 1.22 or higher required"; \
exit 1; \
else \
if test $(MAJOR) -eq 1 -a $(MINOR) -lt 19; then \
echo "Go 1.19 or higher required"; \
if test $(MAJOR) -eq 1 -a $(MINOR) -lt 22; then \
echo "Go 1.22 or higher required"; \
exit 1; \
fi \
fi
Expand Down
12 changes: 7 additions & 5 deletions api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,6 @@ 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 Expand Up @@ -378,13 +375,18 @@ func (a *Accounts) Mount(root *mux.Router, pathPrefix string) {
Methods("GET").
Name("GET /accounts/{address}/storage").
HandlerFunc(utils.WrapHandlerFunc(a.handleGetStorage))

// These two methods are currently deprecated
callContractHandler := utils.HandleGone
if a.enabledDeprecated {
callContractHandler = a.handleCallContract
}
sub.Path("").
Methods(http.MethodPost).
Name("POST /accounts").
HandlerFunc(utils.WrapHandlerFunc(a.handleCallContract))
HandlerFunc(utils.WrapHandlerFunc(callContractHandler))
sub.Path("/{address}").
Methods(http.MethodPost).
Name("POST /accounts/{address}").
HandlerFunc(utils.WrapHandlerFunc(a.handleCallContract))
HandlerFunc(utils.WrapHandlerFunc(callContractHandler))
}
14 changes: 8 additions & 6 deletions api/doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

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
- See the latest version [here](https://unpkg.com/browse/[email protected]/)

```bash
curl https://unpkg.com/swagger-ui-dist@5.11.2/swagger-ui.css > swagger-ui/swagger-ui.css
curl https://unpkg.com/swagger-ui-dist@5.11.2/swagger-ui-bundle.js > swagger-ui/swagger-ui-bundle.js
curl https://unpkg.com/swagger-ui-dist@5.11.2/swagger-ui-standalone-preset.js > swagger-ui/swagger-ui-standalone-preset.js
curl https://unpkg.com/swagger-ui-dist@5.18.2/swagger-ui.css > swagger-ui/swagger-ui.css
curl https://unpkg.com/swagger-ui-dist@5.18.2/swagger-ui-bundle.js > swagger-ui/swagger-ui-bundle.js
curl https://unpkg.com/swagger-ui-dist@5.18.2/swagger-ui-standalone-preset.js > swagger-ui/swagger-ui-standalone-preset.js
```

## Stoplight
Spotlight UI from https://github.com/stoplightio/elements @v8.0.3
Spotlight UI from https://github.com/stoplightio/elements @v8.5.2
- Created [window-observer.js](./stoplight-ui/window-observer.js) to remove `Send API Request` functionality for subscription endpoints
- See the latest version [here](https://unpkg.com/browse/@stoplight/[email protected]/)

```bash
curl https://unpkg.com/@stoplight/elements@8.0.3/styles.min.css > stoplight-ui/styles.min.css
curl https://unpkg.com/@stoplight/elements@8.0.3/web-components.min.js > stoplight-ui/web-components.min.js
curl https://unpkg.com/@stoplight/elements@8.5.2/styles.min.css > stoplight-ui/styles.min.css
curl https://unpkg.com/@stoplight/elements@8.5.2/web-components.min.js > stoplight-ui/web-components.min.js
```
2 changes: 1 addition & 1 deletion api/doc/stoplight-ui/styles.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion api/doc/stoplight-ui/web-components.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions api/doc/swagger-ui/swagger-ui-bundle.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions api/doc/swagger-ui/swagger-ui-standalone-preset.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion api/doc/swagger-ui/swagger-ui.css

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ func metricsMiddleware(next http.Handler) http.Handler {
if rt != nil && rt.GetName() != "" {
enabled = true
name = rt.GetName()
if strings.HasPrefix(name, "subscriptions") {
if strings.HasPrefix(name, "WS") {
subscription = true
name = "WS " + r.URL.Path
}
}

Expand Down
88 changes: 86 additions & 2 deletions api/subscriptions/pending_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ package subscriptions

import (
"math/big"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/api/utils"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
"github.com/vechain/thor/v2/genesis"
"github.com/vechain/thor/v2/muxdb"
"github.com/vechain/thor/v2/packer"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/test/datagen"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/tx"
"github.com/vechain/thor/v2/txpool"
Expand Down Expand Up @@ -146,12 +152,90 @@ func createTx(repo *chain.Repository, addressNumber uint) *tx.Transaction {
new(tx.Builder).
ChainTag(repo.ChainTag()).
GasPriceCoef(1).
Expiration(10).
Expiration(1000).
Gas(21000).
Nonce(1).
Nonce(uint64(datagen.RandInt())).
Clause(cla).
BlockRef(tx.NewBlockRef(0)).
Build(),
genesis.DevAccounts()[addressNumber].PrivateKey,
)
}

func TestPendingTx_NoWriteAfterUnsubscribe(t *testing.T) {
// Arrange
thorChain := initChain(t)
txPool := txpool.New(thorChain.Repo(), thorChain.Stater(), txpool.Options{
Limit: 100,
LimitPerAccount: 16,
MaxLifetime: time.Hour,
})

p := newPendingTx(txPool)
txCh := make(chan *tx.Transaction, txQueueSize)

// Subscribe and then unsubscribe
p.Subscribe(txCh)
p.Unsubscribe(txCh)

done := make(chan struct{})
// Attempt to write a new transaction
trx := createTx(thorChain.Repo(), 0)
assert.NotPanics(t, func() {
p.dispatch(trx, done) // dispatch should not panic after unsubscribe
}, "Dispatching after unsubscribe should not panic")

select {
case <-txCh:
t.Fatal("Channel should not receive new transactions after unsubscribe")
default:
t.Log("No transactions sent to unsubscribed channel, as expected")
}
}

func TestPendingTx_UnsubscribeOnWebSocketClose(t *testing.T) {
// Arrange
thorChain := initChain(t)
txPool := txpool.New(thorChain.Repo(), thorChain.Stater(), txpool.Options{
Limit: 100,
LimitPerAccount: 16,
MaxLifetime: time.Hour,
})

// Subscriptions setup
sub := New(thorChain.Repo(), []string{"*"}, 100, txPool, false)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
utils.WrapHandlerFunc(sub.handlePendingTransactions)(w, r)
}))
defer server.Close()

require.Equal(t, len(sub.pendingTx.listeners), 0)

// Connect as WebSocket client
url := "ws" + server.URL[4:] + "/txpool"
ws, _, err := websocket.DefaultDialer.Dial(url, nil)
assert.NoError(t, err)
defer ws.Close()

// Add a transaction
trx := createTx(thorChain.Repo(), 0)
txPool.AddLocal(trx)

// Wait to receive transaction
time.Sleep(500 * time.Millisecond)
sub.pendingTx.mu.Lock()
require.Equal(t, len(sub.pendingTx.listeners), 1)
sub.pendingTx.mu.Unlock()

// Simulate WebSocket closure
ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
ws.Close()

// Wait for cleanup
time.Sleep(5 * time.Second)

// Assert cleanup
sub.pendingTx.mu.Lock()
require.Equal(t, len(sub.pendingTx.listeners), 0)
sub.pendingTx.mu.Unlock()
}
Loading

0 comments on commit db0f469

Please sign in to comment.