Skip to content

Commit

Permalink
feat: add indexes to transfer response too
Browse files Browse the repository at this point in the history
  • Loading branch information
paologalligit committed Nov 6, 2024
1 parent 10a4bc0 commit 6edc877
Show file tree
Hide file tree
Showing 9 changed files with 156 additions and 203 deletions.
66 changes: 9 additions & 57 deletions api/doc/thor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,6 @@ components:
enum:
- asc
- desc
optionalData:
$ref: '#/components/schemas/EventOptionalData'

EventLogsResponse:
type: array
Expand All @@ -1022,7 +1020,7 @@ components:
- $ref: '#/components/schemas/Event'
- properties:
meta:
$ref: '#/components/schemas/EventLogMeta'
$ref: '#/components/schemas/LogMeta'

TransferLogFilterRequest:
type: object
Expand Down Expand Up @@ -1326,65 +1324,13 @@ components:
description: The index of the clause in the transaction, from which the log was generated.
example: 0
nullable: false

EventLogMeta:
title: EventLogMeta
type: object
description: The event or transfer log metadata such as block number, block timestamp, etc.
properties:
blockID:
type: string
format: hex
description: The block identifier in which the log was included.
example: '0x0004f6cc88bb4626a92907718e82f255b8fa511453a78e8797eb8cea3393b215'
nullable: false
pattern: '^0x[0-9a-f]{64}$'
blockNumber:
type: integer
format: uint32
description: The block number (height) of the block in which the log was included.
example: 325324
nullable: false
blockTimestamp:
type: integer
format: uint64
description: The UNIX timestamp of the block in which the log was included.
example: 1533267900
nullable: false
txID:
type: string
format: hex
description: The transaction identifier, from which the log was generated.
example: '0x284bba50ef777889ff1a367ed0b38d5e5626714477c40de38d71cedd6f9fa477'
nullable: false
pattern: '^0x[0-9a-f]{64}$'
txOrigin:
type: string
description: The account from which the transaction was sent.
example: '0xdb4027477b2a8fe4c83c6dafe7f86678bb1b8a8d'
nullable: false
pattern: '^0x[0-9a-f]{40}$'
clauseIndex:
type: integer
format: uint32
description: The index of the clause in the transaction, from which the log was generated.
example: 0
nullable: false
extendedLogMeta:
$ref: '#/components/schemas/ExtendedLogMeta'

ExtendedLogMeta:
title: ExtendedLogMeta
type: object
nullable: true
properties:
txIndex:
description: The index of the transaction in the block, from which the log was generated.
type: integer
nullable: true
example: 1
logIndex:
descrption: The index of the log in the receipt's outputs.
description: The index of the log in the receipt's outputs.
type: integer
nullable: true
example: 1
Expand Down Expand Up @@ -1918,6 +1864,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 @@ -1928,7 +1879,8 @@ components:
{
"options": {
"offset": 0,
"limit": 10
"limit": 10,
"includeIndexes": true
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion 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, ef.OptionalData)
fes[i] = convertEvent(e, ef.Options.IncludeIndexes)
}
return fes, nil
}
Expand Down
56 changes: 16 additions & 40 deletions api/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,61 +59,36 @@ func TestEvents(t *testing.T) {
testEventWithBlocks(t, blocksToInsert)
}

func TestOptionalData(t *testing.T) {
func TestOptionalIndexes(t *testing.T) {
db := createDb(t)
initEventServer(t, db, defaultLogLimit)
defer ts.Close()
insertBlocks(t, db, 5)

testCases := []struct {
name string
optData *events.EventOptionalData
expected *events.ExtendedLogMeta
name string
includeIndexes bool
expected *uint32
}{
{
name: "empty optional data",
optData: &events.EventOptionalData{},
expected: nil,
name: "do not include indexes",
includeIndexes: false,
expected: nil,
},
{
name: "optional data with txIndex",
optData: &events.EventOptionalData{
TxIndex: true,
},
expected: &events.ExtendedLogMeta{
TxIndex: new(uint32),
},
},
{
name: "optional data with logIndex",
optData: &events.EventOptionalData{
LogIndex: true,
},
expected: &events.ExtendedLogMeta{
LogIndex: new(uint32),
},
},
{
name: "optional data with txIndex and logIndex",
optData: &events.EventOptionalData{
TxIndex: true,
LogIndex: true,
},
expected: &events.ExtendedLogMeta{
TxIndex: new(uint32),
LogIndex: new(uint32),
},
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: &logdb.Options{Limit: 6},
Order: logdb.DESC,
OptionalData: tc.optData,
CriteriaSet: make([]*events.EventCriteria, 0),
Range: nil,
Options: &logdb.Options{Limit: 6, IncludeIndexes: tc.includeIndexes},
Order: logdb.DESC,
}

res, statusCode := httpPost(t, ts.URL+"/events", filter)
Expand All @@ -126,7 +101,8 @@ func TestOptionalData(t *testing.T) {
assert.Equal(t, 5, len(tLogs))

for _, tLog := range tLogs {
assert.Equal(t, tc.expected, tLog.Meta.ExtendedLogMeta)
assert.Equal(t, tc.expected, tLog.Meta.TxIndex)
assert.Equal(t, tc.expected, tLog.Meta.LogIndex)
}
})
}
Expand Down
76 changes: 22 additions & 54 deletions api/events/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,14 @@ import (
)

type LogMeta struct {
BlockID thor.Bytes32 `json:"blockID"`
BlockNumber uint32 `json:"blockNumber"`
BlockTimestamp uint64 `json:"blockTimestamp"`
TxID thor.Bytes32 `json:"txID"`
TxOrigin thor.Address `json:"txOrigin"`
ClauseIndex uint32 `json:"clauseIndex"`
ExtendedLogMeta *ExtendedLogMeta `json:"extendedLogMeta,omitempty"`
}

type ExtendedLogMeta struct {
TxIndex *uint32 `json:"txIndex,omitempty"`
LogIndex *uint32 `json:"logIndex,omitempty"`
}

func (opt *ExtendedLogMeta) Empty() bool {
return opt == nil || (opt.TxIndex == nil && opt.LogIndex == nil)
}

func (opt *ExtendedLogMeta) String() string {
var parts []string
if opt.TxIndex != nil {
parts = append(parts, fmt.Sprintf("txIndex: %v", *opt.TxIndex))
}
if opt.LogIndex != nil {
parts = append(parts, fmt.Sprintf("logIndex: %v", *opt.LogIndex))
}
return fmt.Sprintf("%v", parts)
BlockID thor.Bytes32 `json:"blockID"`
BlockNumber uint32 `json:"blockNumber"`
BlockTimestamp uint64 `json:"blockTimestamp"`
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 @@ -63,7 +44,7 @@ type FilteredEvent struct {
}

// convert a logdb.Event into a json format Event
func convertEvent(event *logdb.Event, eventOptionalData *EventOptionalData) *FilteredEvent {
func convertEvent(event *logdb.Event, addIndexes bool) *FilteredEvent {
fe := &FilteredEvent{
Address: event.Address,
Data: hexutil.Encode(event.Data),
Expand All @@ -76,7 +57,11 @@ func convertEvent(event *logdb.Event, eventOptionalData *EventOptionalData) *Fil
ClauseIndex: event.ClauseIndex,
},
}
fe = addOptionalData(fe, event, eventOptionalData)

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

fe.Topics = make([]*thor.Bytes32, 0)
for i := 0; i < 5; i++ {
Expand All @@ -87,24 +72,6 @@ func convertEvent(event *logdb.Event, eventOptionalData *EventOptionalData) *Fil
return fe
}

func addOptionalData(fe *FilteredEvent, event *logdb.Event, eventOptionalData *EventOptionalData) *FilteredEvent {
if eventOptionalData != nil {
opt := &ExtendedLogMeta{}

if eventOptionalData.LogIndex {
opt.LogIndex = &event.Index
}
if eventOptionalData.TxIndex {
opt.TxIndex = &event.TxIndex
}

if !opt.Empty() {
fe.Meta.ExtendedLogMeta = opt
}
}
return fe
}

func (e *FilteredEvent) String() string {
return fmt.Sprintf(`
Event(
Expand All @@ -117,7 +84,8 @@ func (e *FilteredEvent) String() string {
txID %v,
txOrigin %v,
clauseIndex %v,
optionalData (%v))
txIndex: %v,
logIndex: %v)
)`,
e.Address,
e.Topics,
Expand All @@ -128,7 +96,8 @@ func (e *FilteredEvent) String() string {
e.Meta.TxID,
e.Meta.TxOrigin,
e.Meta.ClauseIndex,
e.Meta.ExtendedLogMeta,
e.Meta.TxIndex,
e.Meta.LogIndex,
)
}

Expand All @@ -138,11 +107,10 @@ type EventCriteria struct {
}

type EventFilter struct {
CriteriaSet []*EventCriteria `json:"criteriaSet"`
Range *Range `json:"range"`
Options *logdb.Options `json:"options"`
Order logdb.Order `json:"order"`
OptionalData *EventOptionalData `json:"optionalData,omitempty"`
CriteriaSet []*EventCriteria `json:"criteriaSet"`
Range *Range `json:"range"`
Options *logdb.Options `json:"options"`
Order logdb.Order `json:"order"`
}

type EventOptionalData struct {
Expand Down
41 changes: 3 additions & 38 deletions api/events/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,59 +145,24 @@ func TestConvertEvent(t *testing.T) {
nil,
},
}
eventOptData := &EventOptionalData{
LogIndex: true,
TxIndex: true,
}

expectedTopics := []*thor.Bytes32{
{0x0B},
{0x0C},
}
expectedData := hexutil.Encode(event.Data)

result := convertEvent(event, eventOptData)
result := convertEvent(event, true)

assert.Equal(t, event.Address, result.Address)
assert.Equal(t, expectedData, result.Data)
assert.Equal(t, event.BlockID, result.Meta.BlockID)
assert.Equal(t, event.BlockNumber, result.Meta.BlockNumber)
assert.Equal(t, event.BlockTime, result.Meta.BlockTimestamp)
assert.Equal(t, event.TxID, result.Meta.TxID)
assert.Equal(t, event.TxIndex, *result.Meta.ExtendedLogMeta.TxIndex)
assert.Equal(t, event.Index, *result.Meta.ExtendedLogMeta.LogIndex)
assert.Equal(t, event.TxIndex, *result.Meta.TxIndex)
assert.Equal(t, event.Index, *result.Meta.LogIndex)
assert.Equal(t, event.TxOrigin, result.Meta.TxOrigin)
assert.Equal(t, event.ClauseIndex, result.Meta.ClauseIndex)
assert.Equal(t, expectedTopics, result.Topics)
}

func TestIsEmpty(t *testing.T) {
// Empty cases
var nilCase *ExtendedLogMeta
assert.True(t, nilCase.Empty())

emptyCase := &ExtendedLogMeta{}
assert.True(t, emptyCase.Empty())

emptyCase = &ExtendedLogMeta{
LogIndex: nil,
}
assert.True(t, emptyCase.Empty())

emptyCase = &ExtendedLogMeta{
TxIndex: nil,
}
assert.True(t, emptyCase.Empty())

// Not empty cases
val := uint32(1)
notEmptyCase := &ExtendedLogMeta{
LogIndex: &val,
}
assert.False(t, notEmptyCase.Empty())

notEmptyCase = &ExtendedLogMeta{
TxIndex: &val,
}
assert.False(t, notEmptyCase.Empty())
}
2 changes: 1 addition & 1 deletion api/transfers/transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (t *Transfers) filter(ctx context.Context, filter *TransferFilter) ([]*Filt
}
tLogs := make([]*FilteredTransfer, len(transfers))
for i, trans := range transfers {
tLogs[i] = convertTransfer(trans)
tLogs[i] = convertTransfer(trans, filter.Options.IncludeIndexes)
}
return tLogs, nil
}
Expand Down
Loading

0 comments on commit 6edc877

Please sign in to comment.