Skip to content

Commit

Permalink
Add LogIndex and TxIndex into logs/event response body (#862)
Browse files Browse the repository at this point in the history
* Thor client (#818)

* feat: add thorclient

* refactor: remove roundTripper

* refactor: change null check

* clean: remove commented code

* feat: add account revision and pending tx

* fix: add licence headers and fix linter issue

* refactor: rename package

* refactor: change revision type to string

* refactor: rename GetLogs and GetTransfers to FilterEvents and FilterTransfers

* refactor: change FilterEvents and FilterTransactions request type to EventFilter

* Adding common.EventWrapper to handle channel errors

* tweak

* update rawclient + update account tests

* tidy up names

* update tests

* pr comments

* adding raw tx

* Tidy up method names and calls

* options client

* tweaks

* pr comments

* Update thorclient/common/common.go

Co-authored-by: libotony <[email protected]>

* pr comments

* Adding Subscriptions

* Pr comments

* adjust func orders

* pr comments

* changing subscribe to use the channel close vs multiple channels

* adding go-doc

* no error after unsubscribe

* pr comments

* checking status code is 2xx

* fix: change FilterTransfers argument

---------

Co-authored-by: otherview <[email protected]>
Co-authored-by: libotony <[email protected]>

* Show all issues on lint (#869)

* Show all issues on lint

* fix lint

* fix(docker): using AWS docker repo for trivy (#872)

* fix(docker): using AWS docker repo for trivy

* fix(docker): using AWS docker repo for trivy

* Darren/feat/add subscription cache (#866)

* ehancement: create a cache for block based subscriptions

* minor: change function names for subscriptions

* test: add unit test for message cache

* chore: add license headers

* refactor: fix up error handling

* fix: remove bad test

* fix: PR comments

* fix: PR comments - remove block cache

* refactor(subscriptions): store structs in cache, not bytes

* fix(license): add license header

* chore(subscriptions): revert unit test changes

* enhancement: resolve pr comments to use simplelru

* enhancement: resolve pr comments - use id as key

* Add additional block tests (#863)

* enhancement(logging): leverage trace level (#873)

* Add testchain package (#844)

* Refactor thor node

* thorchain allows insertion of blocks

* remove thorNode, added testchain

* clean up + comments

* adding license headers

* adding templating tests for thorclient

* Remove test event hacks

* remove types

* removed chain_builder + added logdb to testchain

* pr comments

* Update test/testchain/chain.go

Co-authored-by: libotony <[email protected]>

---------

Co-authored-by: libotony <[email protected]>

* chore(docs): update spec for validator nodes (#875)

* chore(docs): update spec for validator nodes

* chore(docs): update cores

* chore(docs): remove public node stuff

* Darren/logdb remove leading zeros (#865)

* feat: add new txIndex column to event meta response

* test: add convert event test

* feat: make txLog and txIndex as optional return params

* chore: update swagger with new event optional data

* feat: save logIndex in sequence

* feat: tweaked bits in sequence

* refactor: rename optional log meta field

* refactor: comments, yaml and txIndex counts

* rebase to master

* fix: remove stale struct

* add txIndex to returned logdb query

* reset to 0 eventCount and transferCount each receipt and write blockId only once

* fix lint

* rephrase logIndex description in yaml file

* refactor: use filter.Option instead of eventFilter.Option

* move includeIndexes to api

---------

Co-authored-by: otherview <[email protected]>
Co-authored-by: libotony <[email protected]>
Co-authored-by: Darren Kelly <[email protected]>
Co-authored-by: Makis Christou <[email protected]>
  • Loading branch information
5 people committed Nov 12, 2024
1 parent a3c5815 commit f471d95
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 108 deletions.
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

0 comments on commit f471d95

Please sign in to comment.