Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
3531c1d9
Commit
3531c1d9
authored
Aug 14, 2023
by
Hamdi Allam
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
contract event filter & rlp log clarify
parent
3af1cc0f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
67 additions
and
3 deletions
+67
-3
contract_events.go
indexer/database/contract_events.go
+67
-3
No files found.
indexer/database/contract_events.go
View file @
3531c1d9
...
@@ -2,6 +2,7 @@ package database
...
@@ -2,6 +2,7 @@ package database
import
(
import
(
"errors"
"errors"
"math/big"
"gorm.io/gorm"
"gorm.io/gorm"
...
@@ -16,15 +17,19 @@ import (
...
@@ -16,15 +17,19 @@ import (
*/
*/
type
ContractEvent
struct
{
type
ContractEvent
struct
{
GUID
uuid
.
UUID
`gorm:"primaryKey"`
GUID
uuid
.
UUID
`gorm:"primaryKey"`
// Some useful derived fields
BlockHash
common
.
Hash
`gorm:"serializer:json"`
BlockHash
common
.
Hash
`gorm:"serializer:json"`
ContractAddress
common
.
Address
`gorm:"serializer:json"`
ContractAddress
common
.
Address
`gorm:"serializer:json"`
TransactionHash
common
.
Hash
`gorm:"serializer:json"`
TransactionHash
common
.
Hash
`gorm:"serializer:json"`
LogIndex
uint64
EventSignature
common
.
Hash
`gorm:"serializer:json"`
EventSignature
common
.
Hash
`gorm:"serializer:json"`
LogIndex
uint64
Timestamp
uint64
Timestamp
uint64
// NOTE: NOT ALL THE DERIVED FIELDS ON `types.Log` ARE
// AVAILABLE. ONLY THE ONES LISTED ABOVE.
GethLog
*
types
.
Log
`gorm:"serializer:rlp;column:rlp_bytes"`
GethLog
*
types
.
Log
`gorm:"serializer:rlp;column:rlp_bytes"`
}
}
...
@@ -38,8 +43,8 @@ func ContractEventFromGethLog(log *types.Log, timestamp uint64) ContractEvent {
...
@@ -38,8 +43,8 @@ func ContractEventFromGethLog(log *types.Log, timestamp uint64) ContractEvent {
GUID
:
uuid
.
New
(),
GUID
:
uuid
.
New
(),
BlockHash
:
log
.
BlockHash
,
BlockHash
:
log
.
BlockHash
,
ContractAddress
:
log
.
Address
,
TransactionHash
:
log
.
TxHash
,
TransactionHash
:
log
.
TxHash
,
ContractAddress
:
log
.
Address
,
EventSignature
:
eventSig
,
EventSignature
:
eventSig
,
LogIndex
:
uint64
(
log
.
Index
),
LogIndex
:
uint64
(
log
.
Index
),
...
@@ -50,6 +55,15 @@ func ContractEventFromGethLog(log *types.Log, timestamp uint64) ContractEvent {
...
@@ -50,6 +55,15 @@ func ContractEventFromGethLog(log *types.Log, timestamp uint64) ContractEvent {
}
}
}
}
func
(
c
*
ContractEvent
)
AfterFind
(
tx
*
gorm
.
DB
)
error
{
// Fill in some of the derived fields that are not
// populated when decoding the GethLog from RLP
c
.
GethLog
.
BlockHash
=
c
.
BlockHash
c
.
GethLog
.
TxHash
=
c
.
TransactionHash
c
.
GethLog
.
Index
=
uint
(
c
.
LogIndex
)
return
nil
}
type
L1ContractEvent
struct
{
type
L1ContractEvent
struct
{
ContractEvent
`gorm:"embedded"`
ContractEvent
`gorm:"embedded"`
}
}
...
@@ -61,9 +75,11 @@ type L2ContractEvent struct {
...
@@ -61,9 +75,11 @@ type L2ContractEvent struct {
type
ContractEventsView
interface
{
type
ContractEventsView
interface
{
L1ContractEvent
(
uuid
.
UUID
)
(
*
L1ContractEvent
,
error
)
L1ContractEvent
(
uuid
.
UUID
)
(
*
L1ContractEvent
,
error
)
L1ContractEventByTxLogIndex
(
common
.
Hash
,
uint64
)
(
*
L1ContractEvent
,
error
)
L1ContractEventByTxLogIndex
(
common
.
Hash
,
uint64
)
(
*
L1ContractEvent
,
error
)
L1ContractEventsWithFilter
(
ContractEvent
,
*
big
.
Int
,
*
big
.
Int
)
([]
L1ContractEvent
,
error
)
L2ContractEvent
(
uuid
.
UUID
)
(
*
L2ContractEvent
,
error
)
L2ContractEvent
(
uuid
.
UUID
)
(
*
L2ContractEvent
,
error
)
L2ContractEventByTxLogIndex
(
common
.
Hash
,
uint64
)
(
*
L2ContractEvent
,
error
)
L2ContractEventByTxLogIndex
(
common
.
Hash
,
uint64
)
(
*
L2ContractEvent
,
error
)
L2ContractEventsWithFilter
(
ContractEvent
,
*
big
.
Int
,
*
big
.
Int
)
([]
L2ContractEvent
,
error
)
}
}
type
ContractEventsDB
interface
{
type
ContractEventsDB
interface
{
...
@@ -120,6 +136,30 @@ func (db *contractEventsDB) L1ContractEventByTxLogIndex(txHash common.Hash, logI
...
@@ -120,6 +136,30 @@ func (db *contractEventsDB) L1ContractEventByTxLogIndex(txHash common.Hash, logI
return
&
l1ContractEvent
,
nil
return
&
l1ContractEvent
,
nil
}
}
func
(
db
*
contractEventsDB
)
L1ContractEventsWithFilter
(
filter
ContractEvent
,
fromHeight
,
toHeight
*
big
.
Int
)
([]
L1ContractEvent
,
error
)
{
if
fromHeight
==
nil
{
fromHeight
=
big
.
NewInt
(
0
)
}
query
:=
db
.
gorm
.
Table
(
"l1_contract_events"
)
.
Where
(
&
filter
)
query
=
query
.
Joins
(
"INNER JOIN l1_block_headers ON l1_contract_events.block_hash = l1_block_headers.hash"
)
query
=
query
.
Where
(
"l1_block_headers.number >= ? AND l1_block_headers.number <= ?"
,
fromHeight
,
toHeight
)
query
=
query
.
Order
(
"l1_block_headers.number ASC"
)
.
Select
(
"l1_contract_events.*"
)
// NOTE: We use `Find` here instead of `Scan` since `Scan` doesn't not support
// model hooks like `ContractEvent#AfterFind`. Functionally they are the same
var
events
[]
L1ContractEvent
result
:=
query
.
Find
(
&
events
)
if
result
.
Error
!=
nil
{
if
errors
.
Is
(
result
.
Error
,
gorm
.
ErrRecordNotFound
)
{
return
nil
,
nil
}
return
nil
,
result
.
Error
}
return
events
,
nil
}
// L2
// L2
func
(
db
*
contractEventsDB
)
StoreL2ContractEvents
(
events
[]
*
L2ContractEvent
)
error
{
func
(
db
*
contractEventsDB
)
StoreL2ContractEvents
(
events
[]
*
L2ContractEvent
)
error
{
...
@@ -154,3 +194,27 @@ func (db *contractEventsDB) L2ContractEventByTxLogIndex(txHash common.Hash, logI
...
@@ -154,3 +194,27 @@ func (db *contractEventsDB) L2ContractEventByTxLogIndex(txHash common.Hash, logI
return
&
l2ContractEvent
,
nil
return
&
l2ContractEvent
,
nil
}
}
func
(
db
*
contractEventsDB
)
L2ContractEventsWithFilter
(
filter
ContractEvent
,
fromHeight
,
toHeight
*
big
.
Int
)
([]
L2ContractEvent
,
error
)
{
if
fromHeight
==
nil
{
fromHeight
=
big
.
NewInt
(
0
)
}
query
:=
db
.
gorm
.
Table
(
"l2_contract_events"
)
.
Where
(
&
filter
)
query
=
query
.
Joins
(
"INNER JOIN l2_block_headers ON l2_contract_events.block_hash = l2_block_headers.hash"
)
query
=
query
.
Where
(
"l2_block_headers.number >= ? AND l2_block_headers.number <= ?"
,
fromHeight
,
toHeight
)
query
=
query
.
Order
(
"l2_block_headers.number ASC"
)
.
Select
(
"l2_contract_events.*"
)
// NOTE: We use `Find` here instead of `Scan` since `Scan` doesn't not support
// model hooks like `ContractEvent#AfterFind`. Functionally they are the same
var
events
[]
L2ContractEvent
result
:=
query
.
Find
(
&
events
)
if
result
.
Error
!=
nil
{
if
errors
.
Is
(
result
.
Error
,
gorm
.
ErrRecordNotFound
)
{
return
nil
,
nil
}
return
nil
,
result
.
Error
}
return
events
,
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment