Skip to content

Commit

Permalink
Merge branch 'tony/maindb-v4' of https://github.com/vechain/thor into…
Browse files Browse the repository at this point in the history
… miguel/object-write-read
  • Loading branch information
freemanzMrojo committed Dec 4, 2024
2 parents 989c842 + f090234 commit d9fe95f
Show file tree
Hide file tree
Showing 34 changed files with 862 additions and 209 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))
}
105 changes: 57 additions & 48 deletions api/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package events_test

import (
"encoding/json"
"math/big"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -16,8 +17,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/api/events"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/builtin"
"github.com/vechain/thor/v2/genesis"
"github.com/vechain/thor/v2/logdb"
"github.com/vechain/thor/v2/test/datagen"
"github.com/vechain/thor/v2/test/testchain"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/thorclient"
Expand All @@ -28,8 +31,6 @@ const defaultLogLimit uint64 = 1000

var (
ts *httptest.Server
addr = thor.BytesToAddress([]byte("address"))
topic = thor.BytesToBytes32([]byte("topic"))
tclient *thorclient.Client
)

Expand All @@ -52,14 +53,14 @@ func TestEvents(t *testing.T) {

blocksToInsert := 5
tclient = thorclient.New(ts.URL)
insertBlocks(t, thorChain.LogDB(), blocksToInsert)
insertBlocks(t, thorChain, blocksToInsert)
testEventWithBlocks(t, blocksToInsert)
}

func TestOptionalIndexes(t *testing.T) {
thorChain := initEventServer(t, defaultLogLimit)
defer ts.Close()
insertBlocks(t, thorChain.LogDB(), 5)
insertBlocks(t, thorChain, 5)
tclient = thorclient.New(ts.URL)

testCases := []struct {
Expand Down Expand Up @@ -109,7 +110,7 @@ func TestOptionalIndexes(t *testing.T) {
func TestOption(t *testing.T) {
thorChain := initEventServer(t, 5)
defer ts.Close()
insertBlocks(t, thorChain.LogDB(), 5)
insertBlocks(t, thorChain, 5)

tclient = thorclient.New(ts.URL)
filter := events.EventFilter{
Expand Down Expand Up @@ -143,13 +144,47 @@ func TestOption(t *testing.T) {
assert.Equal(t, 5, len(tLogs))

// when the filtered events exceed the limit, should return the forbidden
insertBlocks(t, thorChain.LogDB(), 6)
insertBlocks(t, thorChain, 6)
res, statusCode, err = tclient.RawHTTPClient().RawHTTPPost("/logs/event", filter)
require.NoError(t, err)
assert.Equal(t, http.StatusForbidden, statusCode)
assert.Equal(t, "the number of filtered logs exceeds the maximum allowed value of 5, please use pagination", strings.Trim(string(res), "\n"))
}

func TestZeroFrom(t *testing.T) {
thorChain := initEventServer(t, 5)
defer ts.Close()
insertBlocks(t, thorChain, 5)

tclient = thorclient.New(ts.URL)
transferTopic := thor.MustParseBytes32("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")
criteria := []*events.EventCriteria{
{
TopicSet: events.TopicSet{
Topic0: &transferTopic,
},
},
}

from := uint64(0)
filter := events.EventFilter{
CriteriaSet: criteria,
Range: &events.Range{From: &from},
Options: nil,
Order: logdb.DESC,
}

res, statusCode, err := tclient.RawHTTPClient().RawHTTPPost("/logs/event", filter)
require.NoError(t, err)
var tLogs []*events.FilteredEvent
if err := json.Unmarshal(res, &tLogs); err != nil {
t.Fatal(err)
}

assert.Equal(t, http.StatusOK, statusCode)
assert.NotEmpty(t, tLogs)
}

// Test functions
func testEventsBadRequest(t *testing.T) {
badBody := []byte{0x00, 0x01, 0x02}
Expand Down Expand Up @@ -199,16 +234,14 @@ func testEventWithBlocks(t *testing.T, expectedBlocks int) {
assert.NotEmpty(t, tLog)
}

transferEvent := thor.MustParseBytes32("0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef")

// Test with matching filter
matchingFilter := events.EventFilter{
CriteriaSet: []*events.EventCriteria{{
Address: &addr,
Address: &builtin.Energy.Address,
TopicSet: events.TopicSet{
&topic,
&topic,
&topic,
&topic,
&topic,
Topic0: &transferEvent,
},
}},
}
Expand Down Expand Up @@ -239,41 +272,17 @@ func initEventServer(t *testing.T, limit uint64) *testchain.Chain {
}

// Utilities functions
func insertBlocks(t *testing.T, db *logdb.LogDB, n int) {
b := new(block.Builder).Build()
for i := 0; i < n; i++ {
b = new(block.Builder).
ParentID(b.Header().ID()).
Build()
receipts := tx.Receipts{newReceipt()}

w := db.NewWriter()
if err := w.Write(b, receipts); err != nil {
t.Fatal(err)
}

if err := w.Commit(); err != nil {
t.Fatal(err)
}
}
}
func insertBlocks(t *testing.T, chain *testchain.Chain, n int) {
transferABI, ok := builtin.Energy.ABI.MethodByName("transfer")
require.True(t, ok)

func newReceipt() *tx.Receipt {
return &tx.Receipt{
Outputs: []*tx.Output{
{
Events: tx.Events{{
Address: addr,
Topics: []thor.Bytes32{
topic,
topic,
topic,
topic,
topic,
},
Data: []byte("0x0"),
}},
},
},
encoded, err := transferABI.EncodeInput(genesis.DevAccounts()[2].Address, new(big.Int).SetUint64(datagen.RandUint64()))
require.NoError(t, err)

transferClause := tx.NewClause(&builtin.Energy.Address).WithData(encoded)

for i := 0; i < n; i++ {
err := chain.MintClauses(genesis.DevAccounts()[0], []*tx.Clause{transferClause})
require.NoError(t, err)
}
}
Loading

0 comments on commit d9fe95f

Please sign in to comment.