Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/db #879

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion api/doc/thor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,16 @@ components:
description: The index of the clause in the transaction, from which the log was generated.
example: 0
nullable: false
txIndex:
description: The index of the transaction in the block, from which the log was generated.
type: integer
nullable: true
example: 1
logIndex:
description: The index of the log in the receipt's outputs. This is an overall index among all clauses.
type: integer
nullable: true
example: 1

Block:
title: Block
Expand Down Expand Up @@ -1855,6 +1865,11 @@ components:
The limit of records to be included in the output. Use this parameter for pagination.

Default's to all results.
includeIndexes:
type: boolean
example: true
nullable: true
description: Include both transaction and log index in the response.
description: |
Include these parameters to receive filtered results in a paged format.

Expand All @@ -1865,7 +1880,8 @@ components:
{
"options": {
"offset": 0,
"limit": 10
"limit": 10,
"includeIndexes": true
}
}
```
Expand Down Expand Up @@ -1916,6 +1932,26 @@ components:
}
```
This refers to the range from block 10 to block 1000.

EventOptionalData:
nullable: true
type: object
title: EventOptionalData
properties:
txIndex:
type: boolean
example: true
nullable: true
description: |
Specifies whether to include in the response the event transaction index.
loglIndex:
type: boolean
example: true
nullable: true
description: |
Specifies whether to include in the response the event log index.
description: |
Specifies all the optional data that can be included in the response.

EventCriteria:
type: object
Expand Down
9 changes: 5 additions & 4 deletions api/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (e *Events) filter(ctx context.Context, ef *EventFilter) ([]*FilteredEvent,
}
fes := make([]*FilteredEvent, len(events))
for i, e := range events {
fes[i] = convertEvent(e)
fes[i] = convertEvent(e, ef.Options.IncludeIndexes)
}
return fes, nil
}
Expand All @@ -60,9 +60,10 @@ func (e *Events) handleFilter(w http.ResponseWriter, req *http.Request) error {
if filter.Options == nil {
// if filter.Options is nil, set to the default limit +1
// to detect whether there are more logs than the default limit
filter.Options = &logdb.Options{
Offset: 0,
Limit: e.limit + 1,
filter.Options = &Options{
Offset: 0,
Limit: e.limit + 1,
IncludeIndexes: false,
}
}

Expand Down
52 changes: 51 additions & 1 deletion api/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,56 @@ func TestEvents(t *testing.T) {
testEventWithBlocks(t, blocksToInsert)
}

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

testCases := []struct {
name string
includeIndexes bool
expected *uint32
}{
{
name: "do not include indexes",
includeIndexes: false,
expected: nil,
},
{
name: "include indexes",
includeIndexes: true,
expected: new(uint32),
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
filter := events.EventFilter{
CriteriaSet: make([]*events.EventCriteria, 0),
Range: nil,
Options: &events.Options{Limit: 6, IncludeIndexes: tc.includeIndexes},
Order: logdb.DESC,
}

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

for _, tLog := range tLogs {
assert.Equal(t, tc.expected, tLog.Meta.TxIndex)
assert.Equal(t, tc.expected, tLog.Meta.LogIndex)
}
})
}
}

func TestOption(t *testing.T) {
thorChain := initEventServer(t, 5)
defer ts.Close()
Expand All @@ -65,7 +115,7 @@ func TestOption(t *testing.T) {
filter := events.EventFilter{
CriteriaSet: make([]*events.EventCriteria, 0),
Range: nil,
Options: &logdb.Options{Limit: 6},
Options: &events.Options{Limit: 6},
Order: logdb.DESC,
}

Expand Down
63 changes: 27 additions & 36 deletions api/events/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package events

import (
"fmt"
"math"

"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -23,6 +22,8 @@ type LogMeta struct {
TxID thor.Bytes32 `json:"txID"`
TxOrigin thor.Address `json:"txOrigin"`
ClauseIndex uint32 `json:"clauseIndex"`
TxIndex *uint32 `json:"txIndex,omitempty"`
LogIndex *uint32 `json:"logIndex,omitempty"`
}

type TopicSet struct {
Expand All @@ -42,8 +43,8 @@ type FilteredEvent struct {
}

// convert a logdb.Event into a json format Event
func convertEvent(event *logdb.Event) *FilteredEvent {
fe := FilteredEvent{
func convertEvent(event *logdb.Event, addIndexes bool) *FilteredEvent {
fe := &FilteredEvent{
Address: event.Address,
Data: hexutil.Encode(event.Data),
Meta: LogMeta{
Expand All @@ -55,50 +56,37 @@ func convertEvent(event *logdb.Event) *FilteredEvent {
ClauseIndex: event.ClauseIndex,
},
}

if addIndexes {
fe.Meta.TxIndex = &event.TxIndex
fe.Meta.LogIndex = &event.LogIndex
}

fe.Topics = make([]*thor.Bytes32, 0)
for i := 0; i < 5; i++ {
if event.Topics[i] != nil {
fe.Topics = append(fe.Topics, event.Topics[i])
}
}
return &fe
}

func (e *FilteredEvent) String() string {
return fmt.Sprintf(`
Event(
address: %v,
topics: %v,
data: %v,
meta: (blockID %v,
blockNumber %v,
blockTimestamp %v),
txID %v,
txOrigin %v,
clauseIndex %v)
)`,
e.Address,
e.Topics,
e.Data,
e.Meta.BlockID,
e.Meta.BlockNumber,
e.Meta.BlockTimestamp,
e.Meta.TxID,
e.Meta.TxOrigin,
e.Meta.ClauseIndex,
)
return fe
}

type EventCriteria struct {
Address *thor.Address `json:"address"`
TopicSet
}

type Options struct {
Offset uint64
Limit uint64
IncludeIndexes bool
}

type EventFilter struct {
CriteriaSet []*EventCriteria `json:"criteriaSet"`
Range *Range `json:"range"`
Options *logdb.Options `json:"options"`
Order logdb.Order `json:"order"`
CriteriaSet []*EventCriteria
Range *Range
Options *Options
Order logdb.Order // default asc
}

func convertEventFilter(chain *chain.Chain, filter *EventFilter) (*logdb.EventFilter, error) {
Expand All @@ -107,9 +95,12 @@ func convertEventFilter(chain *chain.Chain, filter *EventFilter) (*logdb.EventFi
return nil, err
}
f := &logdb.EventFilter{
Range: rng,
Options: filter.Options,
Order: filter.Order,
Range: rng,
Options: &logdb.Options{
Offset: filter.Options.Offset,
Limit: filter.Options.Limit,
},
Order: filter.Order,
}
if len(filter.CriteriaSet) > 0 {
f.CriteriaSet = make([]*logdb.EventCriteria, len(filter.CriteriaSet))
Expand Down
Loading
Loading