Skip to content

Commit

Permalink
merge branch 'feat/tx-log-seq' into feat/maindb-v4-candidate-pre-logdb
Browse files Browse the repository at this point in the history
  • Loading branch information
darrenvechain committed Nov 8, 2024
2 parents 67da142 + 10a4bc0 commit ca14f79
Show file tree
Hide file tree
Showing 11 changed files with 390 additions and 63 deletions.
86 changes: 85 additions & 1 deletion api/doc/thor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,8 @@ components:
enum:
- asc
- desc
optionalData:
$ref: '#/components/schemas/EventOptionalData'

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

TransferLogFilterRequest:
type: object
Expand Down Expand Up @@ -1325,6 +1327,68 @@ 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.
type: integer
nullable: true
example: 1

Block:
title: Block
Expand Down Expand Up @@ -1916,6 +1980,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
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)
fes[i] = convertEvent(e, ef.OptionalData)
}
return fes, nil
}
Expand Down
73 changes: 73 additions & 0 deletions api/events/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,79 @@ func TestEvents(t *testing.T) {
testEventWithBlocks(t, blocksToInsert)
}

func TestOptionalData(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: "empty optional data",
optData: &events.EventOptionalData{},
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),
},
},
}

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,
}

res, statusCode := httpPost(t, ts.URL+"/events", filter)
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.ExtendedLogMeta)
}
})
}
}

func TestOption(t *testing.T) {
thorChain := initEventServer(t, 5)
defer ts.Close()
Expand Down
77 changes: 63 additions & 14 deletions api/events/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,33 @@ 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"`
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)
}

type TopicSet struct {
Expand All @@ -42,8 +63,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, eventOptionalData *EventOptionalData) *FilteredEvent {
fe := &FilteredEvent{
Address: event.Address,
Data: hexutil.Encode(event.Data),
Meta: LogMeta{
Expand All @@ -55,13 +76,33 @@ func convertEvent(event *logdb.Event) *FilteredEvent {
ClauseIndex: event.ClauseIndex,
},
}
fe = addOptionalData(fe, event, eventOptionalData)

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
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 {
Expand All @@ -75,7 +116,8 @@ func (e *FilteredEvent) String() string {
blockTimestamp %v),
txID %v,
txOrigin %v,
clauseIndex %v)
clauseIndex %v,
optionalData (%v))
)`,
e.Address,
e.Topics,
Expand All @@ -86,6 +128,7 @@ func (e *FilteredEvent) String() string {
e.Meta.TxID,
e.Meta.TxOrigin,
e.Meta.ClauseIndex,
e.Meta.ExtendedLogMeta,
)
}

Expand All @@ -95,10 +138,16 @@ type EventCriteria struct {
}

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

type EventOptionalData struct {
LogIndex bool `json:"logIndex,omitempty"`
TxIndex bool `json:"txIndex,omitempty"`
}

func convertEventFilter(chain *chain.Chain, filter *EventFilter) (*logdb.EventFilter, error) {
Expand Down
Loading

0 comments on commit ca14f79

Please sign in to comment.